Skip to content

Commit

Permalink
Merge pull request #49 from briandela/master
Browse files Browse the repository at this point in the history
Return error during plugin registration for invalid options.
  • Loading branch information
stongo committed Feb 26, 2015
2 parents 30fd458 + 37e4e08 commit e372b1f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
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

0 comments on commit e372b1f

Please sign in to comment.