From 37e4e0894dba4d439d5f796dc62ee2d3bb35d165 Mon Sep 17 00:00:00 2001 From: Brian Delahunty Date: Wed, 25 Feb 2015 23:29:54 -0800 Subject: [PATCH] Return error during plugin registration for invalid options. --- lib/index.js | 6 +++++- test/index.js | 30 +++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index e273c62..24f663b 100755 --- a/lib/index.js +++ b/lib/index.js @@ -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 = { diff --git a/test/index.js b/test/index.js index 07b5c16..710c459 100755 --- a/test/index.js +++ b/test/index.js @@ -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(); @@ -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) {