Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error during plugin registration for invalid options. #49

Merged
merged 1 commit into from
Feb 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ internals.defaults = {

exports.register = function (server, options, next) {

Joi.assert(options, internals.schema, 'Invalid crumb options');
var validateOptions = internals.schema.validate(options);
if (validateOptions.error) {
return next(validateOptions.error);
}

var settings = Hoek.applyToDefaults(internals.defaults, options);

var routeDefaults = {
Expand Down
30 changes: 25 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ describe('Crumb', function () {
});
});

it('should fail to register with bad options', function (done) {

var server = new Hapi.Server();
server.connection();

server.register({
register: Crumb,
options: {
foo: 'bar'
}
}, function(err) {

expect(err).to.exist();
expect(err.name).to.equal('ValidationError');
expect(err.message).to.equal('foo is not allowed');
done();
});
});

it('route uses crumb when route.config.plugins.crumb set to true and autoGenerate set to false', function (done) {

var server = new Hapi.Server();
Expand Down Expand Up @@ -375,11 +394,12 @@ describe('Crumb', function () {
var server = new Hapi.Server();
server.connection();

expect(function () {

server.register({ register: Crumb, options: { allowOrigins: ['*'] } }, function (err) {});
}).to.throw(/Invalid crumb options/);
done();
server.register({ register: Crumb, options: { allowOrigins: ['*'] } }, function (err) {
expect(err).to.exist();
expect(err.name).to.equal('ValidationError');
expect(err.message).to.equal('allowOrigins position 0 contains an excluded value');
done();
});
});

it('does not set crumb cookie insecurely', function (done) {
Expand Down