From 88539856f30a6ade6173b129af9dc57c72db4d88 Mon Sep 17 00:00:00 2001 From: Jeffrey Lembeck Date: Fri, 18 Sep 2015 13:40:39 -0700 Subject: [PATCH 0001/1139] Detects and rejects malformed response headers This works around a byte truncation flaw in node core versions > 0.10 where the high bit is stripped from headers leading to a possible header injection. --- lib/response.js | 16 ++++++++++++++++ test/response.js | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/response.js b/lib/response.js index 8e839224f..71563e7a3 100755 --- a/lib/response.js +++ b/lib/response.js @@ -116,6 +116,22 @@ internals.Response.prototype._header = function (key, value, options) { options.separator = options.separator || ','; options.override = options.override !== false; + if ([].concat(value).some(function (val) { + + if (!val) { + return false; + } + var headerBytes = typeof val === 'string' ? new Buffer(val) : val; + for (var i = 0; i < headerBytes.length; i++) { + if ((headerBytes[i] & 0x7f) !== headerBytes[i]) { + + return true; + } + } + })) { + throw Boom.badImplementation('Header values must be ascii text'); + } + if ((!options.append && options.override) || !this.headers[key]) { diff --git a/test/response.js b/test/response.js index 3d1a92a8a..f9e240ac3 100755 --- a/test/response.js +++ b/test/response.js @@ -95,6 +95,30 @@ describe('Response', function () { done(); }); }); + + it('throws error on non-ascii value', function (done) { + + var thrown = false; + + var handler = function (request, reply) { + + try { + return reply('ok').header('set-cookie', decodeURIComponent('%E0%B4%8Aset-cookie:%20foo=bar')); + } catch (e) { + expect(e.message).to.equal('Header values must be ascii text'); + thrown = true; + } + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + + expect(thrown).to.equal(true); + done(); + }); + }); }); describe('created()', function () { From e2721e8f0e531dc2220826f74f2ec3ad3ecaaabf Mon Sep 17 00:00:00 2001 From: toby Date: Sun, 20 Sep 2015 19:41:23 +0100 Subject: [PATCH 0002/1139] Add next to docs example for server.after --- API.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 80301b537..863c3ee71 100755 --- a/API.md +++ b/API.md @@ -475,9 +475,11 @@ var Hapi = require('hapi'); var server = new Hapi.Server(); server.connection({ port: 80 }); -server.after(function () { +server.after(function (server, next) { // Perform some pre-start logic + + next(); }); server.start(function (err) { From 348f2bc776cf4a680a7ad6785224e3cf236cbeca Mon Sep 17 00:00:00 2001 From: Mathias Bogaert Date: Mon, 21 Sep 2015 00:29:33 +0100 Subject: [PATCH 0003/1139] Add preload flag to HSTS header. --- API.md | 3 +++ lib/route.js | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 80301b537..13a1d22c5 100755 --- a/API.md +++ b/API.md @@ -2163,6 +2163,9 @@ following options: - `maxAge` - the max-age portion of the header, as a number. Default is `15768000`. - `includeSubdomains` - a boolean specifying whether to add the `includeSubdomains` flag to the header. + - `preload` - a boolean specifying whether to add the 'preload' flag (used to submit + domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) + to the header. - `xframe` - controls the 'X-Frame-Options' header. When set to `true` the header will be set to `DENY`, you may also specify a string value of 'deny' or 'sameorigin'. To use the 'allow-from' rule, you must set this to an object with the following fields: diff --git a/lib/route.js b/lib/route.js index c918b7642..849d5dae4 100755 --- a/lib/route.js +++ b/lib/route.js @@ -204,7 +204,10 @@ exports = module.exports = internals.Route = function (options, connection, real else { security._hsts = 'max-age=' + (security.hsts.maxAge || 15768000); if (security.hsts.includeSubdomains) { - security._hsts += '; includeSubdomains'; + security._hsts += '; includeSubDomains'; + } + if (security.hsts.preload) { + security._hsts += '; preload'; } } } From 9d78a668838590b5989f67dd167a8d9be195a95e Mon Sep 17 00:00:00 2001 From: Mathias Bogaert Date: Mon, 21 Sep 2015 07:27:45 +0100 Subject: [PATCH 0004/1139] Fix tests and add support for includeSubDomains param too (backwards compatible). --- API.md | 2 +- lib/route.js | 2 +- lib/schema.js | 4 +++- test/transmit.js | 46 +++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 13a1d22c5..9184dbe52 100755 --- a/API.md +++ b/API.md @@ -2161,7 +2161,7 @@ following options: be set to that number. Defaults to `true`. You may also specify an object with the following fields: - `maxAge` - the max-age portion of the header, as a number. Default is `15768000`. - - `includeSubdomains` - a boolean specifying whether to add the `includeSubdomains` + - `includeSubDomains` - a boolean specifying whether to add the `includeSubDomains` flag to the header. - `preload` - a boolean specifying whether to add the 'preload' flag (used to submit domains inclusion in Chrome's HTTP Strict Transport Security (HSTS) preload list) diff --git a/lib/route.js b/lib/route.js index 849d5dae4..afa8b3768 100755 --- a/lib/route.js +++ b/lib/route.js @@ -203,7 +203,7 @@ exports = module.exports = internals.Route = function (options, connection, real } else { security._hsts = 'max-age=' + (security.hsts.maxAge || 15768000); - if (security.hsts.includeSubdomains) { + if (security.hsts.includeSubdomains || security.hsts.includeSubDomains) { security._hsts += '; includeSubDomains'; } if (security.hsts.preload) { diff --git a/lib/schema.js b/lib/schema.js index aacef8e29..6f5edaf2a 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -113,7 +113,9 @@ internals.routeBase = Joi.object({ hsts: [ Joi.object({ maxAge: Joi.number(), - includeSubdomains: Joi.boolean() + includeSubdomains: Joi.boolean(), + includeSubDomains: Joi.boolean(), + preload: Joi.boolean() }), Joi.boolean(), Joi.number() diff --git a/test/transmit.js b/test/transmit.js index e16c40b59..c1feaf0e7 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2843,14 +2843,14 @@ describe('transmission', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { security: { hsts: { maxAge: 123456789, includeSubdomains: true } } } }); + server.connection({ routes: { security: { hsts: { maxAge: 123456789, includeSubDomains: true } } } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ url: '/' }, function (res) { expect(res.result).to.exist(); expect(res.result).to.equal('Test'); - expect(res.headers['strict-transport-security']).to.equal('max-age=123456789; includeSubdomains'); + expect(res.headers['strict-transport-security']).to.equal('max-age=123456789; includeSubDomains'); done(); }); }); @@ -2890,7 +2890,47 @@ describe('transmission', function () { expect(res.result).to.exist(); expect(res.result).to.equal('Test'); - expect(res.headers['strict-transport-security']).to.equal('max-age=15768000; includeSubdomains'); + expect(res.headers['strict-transport-security']).to.equal('max-age=15768000; includeSubDomains'); + done(); + }); + }); + + it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains', function (done) { + + var handler = function (request, reply) { + + return reply('Test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { security: { hsts: { includeSubDomains: true } } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/' }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Test'); + expect(res.headers['strict-transport-security']).to.equal('max-age=15768000; includeSubDomains'); + done(); + }); + }); + + it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains and preload', function (done) { + + var handler = function (request, reply) { + + return reply('Test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { security: { hsts: { includeSubDomains: true, preload: true } } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/' }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Test'); + expect(res.headers['strict-transport-security']).to.equal('max-age=15768000; includeSubDomains; preload'); done(); }); }); From 183f685a71e3153343094ef6cec95067bddcdf12 Mon Sep 17 00:00:00 2001 From: Toby Date: Mon, 21 Sep 2015 09:09:25 +0100 Subject: [PATCH 0005/1139] Rename server to srv --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 863c3ee71..bd8f3069e 100755 --- a/API.md +++ b/API.md @@ -475,7 +475,7 @@ var Hapi = require('hapi'); var server = new Hapi.Server(); server.connection({ port: 80 }); -server.after(function (server, next) { +server.after(function (srv, next) { // Perform some pre-start logic From 52abd37b98f68077a352a02726f4c6743bdaa6fc Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Tue, 22 Sep 2015 18:21:46 -0700 Subject: [PATCH 0006/1139] Test incorrect timing. Closes #2779 --- test/request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/request.js b/test/request.js index 9dc4e7acb..20022444c 100755 --- a/test/request.js +++ b/test/request.js @@ -1433,7 +1433,7 @@ describe('Request', function () { setTimeout(function () { s.emit('end'); - }, 40); + }, 60); }; var timer = new Hoek.Bench(); From 9f1a2a1bd80c26bcffd9a2fea5683bd30e6a42eb Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Tue, 22 Sep 2015 18:22:21 -0700 Subject: [PATCH 0007/1139] 10.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6878a8e9e..935d63e28 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.0.0", + "version": "10.0.1", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From 7ed102b4bc0031c4da3d382ed45ac1f2785fc71a Mon Sep 17 00:00:00 2001 From: Scott Hulbert Date: Wed, 23 Sep 2015 17:24:37 -0700 Subject: [PATCH 0008/1139] update docs link to Joi docs because Joi moved docs into API.md --- API.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index bd8f3069e..9e6177a2f 100755 --- a/API.md +++ b/API.md @@ -2153,8 +2153,8 @@ following options: `false`. - `options` - options to pass to [Joi](http://github.com/hapijs/joi). Useful to set global options such as `stripUnknown` or `abortEarly` (the complete list is available - [here](https://github.com/hapijs/joi#validatevalue-schema-options-callback)). Defaults to - no options. + [here](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback)). + Defaults to no options. - `security` - sets common security headers (disabled by default). To enable set `security` to `true` or to an object with the following options: @@ -2264,8 +2264,8 @@ following options: - `options` - options to pass to [Joi](http://github.com/hapijs/joi). Useful to set global options such as `stripUnknown` or `abortEarly` (the complete list is available - [here](https://github.com/hapijs/joi#validatevalue-schema-options-callback)). Defaults to - no options. + [here](https://github.com/hapijs/joi/blob/master/API.md#validatevalue-schema-options-callback)). + Defaults to no options. - `timeout` - define timeouts for processing durations: - `server` - response timeout in milliseconds. Sets the maximum time allowed for the From aa22e6c767678f1261eae5e845ffdd0f23d234ac Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 25 Sep 2015 08:06:10 -0700 Subject: [PATCH 0009/1139] Fix test on windows. Closes #2762 --- test/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/connection.js b/test/connection.js index 13dfee34d..4a30e9b8c 100755 --- a/test/connection.js +++ b/test/connection.js @@ -1111,7 +1111,7 @@ describe('Connection', function () { server.inject('/', function (res) { - expect(res.result).to.equal('
\n

hola!

\n
\n'); + expect(res.result).to.match(/
\r?\n

hola!<\/h1>\r?\n<\/div>\r?\n/); done(); }); }); From 6f05a5138f2e23eb7e800c5bf458a57662eee6c7 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 25 Sep 2015 08:23:25 -0700 Subject: [PATCH 0010/1139] Upgrade dependencies. Closes #2781. Closes #2782. Closes #2783. Closes #2784. Closes #2785. Closes #2786. Closes #2787 --- npm-shrinkwrap.json | 16 ++++++++-------- package.json | 2 +- test/validation.js | 1 + 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 395469093..22be6a11d 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.0.0", + "version": "10.1.0", "dependencies": { "accept": { "version": "1.1.0" @@ -9,7 +9,7 @@ "version": "1.0.1" }, "boom": { - "version": "2.8.0" + "version": "2.9.0" }, "call": { "version": "2.0.2" @@ -21,13 +21,13 @@ "version": "1.1.2" }, "cryptiles": { - "version": "2.0.4" + "version": "2.0.5" }, "heavy": { "version": "3.0.0" }, "hoek": { - "version": "2.14.0" + "version": "2.16.3" }, "iron": { "version": "2.1.3" @@ -53,7 +53,7 @@ "version": "2.0.2", "dependencies": { "mime-db": { - "version": "1.18.0" + "version": "1.19.0" } } }, @@ -64,7 +64,7 @@ "version": "4.0.0" }, "shot": { - "version": "1.6.0" + "version": "1.6.1" }, "statehood": { "version": "2.1.1" @@ -79,7 +79,7 @@ "version": "1.0.0", "dependencies": { "b64": { - "version": "2.0.0" + "version": "2.0.1" }, "nigel": { "version": "1.0.1", @@ -92,7 +92,7 @@ } }, "wreck": { - "version": "6.1.0" + "version": "6.2.0" } } }, diff --git a/package.json b/package.json index 935d63e28..47f18e9f3 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.0.1", + "version": "10.1.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" diff --git a/test/validation.js b/test/validation.js index 94336694d..cc782a331 100755 --- a/test/validation.js +++ b/test/validation.js @@ -1451,6 +1451,7 @@ describe('validation', function () { config: { validate: { headers: { + host: 'localhost', accept: Joi.string().valid('application/json').required(), 'user-agent': Joi.string().optional() } From c2f67521561d63610b2ab3e031d895dd049c1dbb Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 25 Sep 2015 08:30:43 -0700 Subject: [PATCH 0011/1139] Fix null request.state. Closes #2505 --- lib/route.js | 2 +- test/transmit.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/route.js b/lib/route.js index afa8b3768..c19596560 100755 --- a/lib/route.js +++ b/lib/route.js @@ -337,7 +337,7 @@ internals.state = function (request, next) { request.connection.states.parse(cookies, function (err, state, failed) { - request.state = state; + request.state = state || {}; // Clear cookies diff --git a/test/transmit.js b/test/transmit.js index c1feaf0e7..f47ab99ae 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -547,10 +547,18 @@ describe('transmission', function () { server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); + var validState = false; + server.ext('onPreResponse', function (request, reply) { + + validState = request.state && typeof request.state === 'object'; + reply.continue(); + }); + server.inject({ method: 'GET', url: '/?callback=me', headers: { cookie: '+' } }, function (res) { expect(res.payload).to.equal('/**/me({"statusCode":400,"error":"Bad Request","message":"Invalid cookie header"});'); expect(res.headers['content-type']).to.equal('text/javascript; charset=utf-8'); + expect(validState).to.equal(true); done(); }); }); From 50033985a48fa8d58b9d1b3f0e4e745867ca1610 Mon Sep 17 00:00:00 2001 From: toboid Date: Sun, 27 Sep 2015 09:42:01 +0100 Subject: [PATCH 0012/1139] Add catbox cache stats to server method --- lib/methods.js | 3 ++- test/methods.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/methods.js b/lib/methods.js index 098d1472b..6fab08b17 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -164,7 +164,8 @@ internals.Methods.prototype._add = function (name, method, options, realm) { } return cache.drop(key, methodNext); - } + }, + stats: cache.stats }; this._assign(name, func, func); diff --git a/test/methods.js b/test/methods.js index 435ae48a0..b0ca5bf20 100755 --- a/test/methods.js +++ b/test/methods.js @@ -577,6 +577,31 @@ describe('Methods', function () { }); }); + it('reports cache stats', function (done) { + + var method = function (id, next) { + + return next(null, { id: id }); + }; + + var server = new Hapi.Server(); + server.connection(); + server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); + + server.initialize(function (err) { + + expect(err).to.not.exist(); + + server.methods.test(1, function (err) { + + expect(err).to.not.exist(); + expect(server.methods.test.cache.stats.gets).to.equal(1); + done(); + }); + }); + }); + + it('throws an error when name is not a string', function (done) { expect(function () { From 6f99b9ab4ea2686556d12aada05624c6e16e8899 Mon Sep 17 00:00:00 2001 From: toboid Date: Sun, 27 Sep 2015 09:48:59 +0100 Subject: [PATCH 0013/1139] Test to ensure that cache stats are per method --- test/methods.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/methods.js b/test/methods.js index b0ca5bf20..77288e1a2 100755 --- a/test/methods.js +++ b/test/methods.js @@ -577,7 +577,7 @@ describe('Methods', function () { }); }); - it('reports cache stats', function (done) { + it('reports cache stats for each method', function (done) { var method = function (id, next) { @@ -586,7 +586,8 @@ describe('Methods', function () { var server = new Hapi.Server(); server.connection(); - server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); + server.method('test', method, { cache: { generateTimeout: 10 } }); + server.method('test2', method, { cache: { generateTimeout: 10 } }); server.initialize(function (err) { @@ -596,12 +597,12 @@ describe('Methods', function () { expect(err).to.not.exist(); expect(server.methods.test.cache.stats.gets).to.equal(1); + expect(server.methods.test2.cache.stats.gets).to.equal(0); done(); }); }); }); - it('throws an error when name is not a string', function (done) { expect(function () { From f930abf7ea06e65fc217b7529a4ada805af1aa1c Mon Sep 17 00:00:00 2001 From: toboid Date: Sun, 27 Sep 2015 09:57:43 +0100 Subject: [PATCH 0014/1139] Update contrib docs with correct test coverage command --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 529d3165b..81a21030c 100755 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,6 +9,6 @@ Code changes are welcome and should follow the guidelines below. * Fork the repository on GitHub. * Fix the issue ensuring that your code follows the [style guide](https://github.com/hapijs/contrib/blob/master/Style.md). -* Add tests for your new code ensuring that you have 100% code coverage (we can help you reach 100% but will not merge without it). - * Run `make test-cov-html` to generate a report of test coverage +* Add tests for your new code ensuring that you have 100% code coverage (we can help you reach 100% but will not merge without it). + * Run `npm run test-cov-html` to generate a report of test coverage * [Pull requests](http://help.github.com/send-pull-requests/) should be made to the [master branch](https://github.com/hapijs/hapi/tree/master). From dee3ddade69f944296863c082ed5c4467d400d06 Mon Sep 17 00:00:00 2001 From: toboid Date: Sun, 27 Sep 2015 20:47:35 +0100 Subject: [PATCH 0015/1139] Add method cache stats to api docs --- API.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 48c4b729d..bd16df73d 100755 --- a/API.md +++ b/API.md @@ -1212,12 +1212,14 @@ to use the built-in cache and share across multiple request handlers without hav common module. Methods are registered via `server.method(name, method, [options])` where: -- `name` - a unique method name used to invoke the method via `server.methods[name]`. When - configured with caching enabled, `server.methods[name].cache.drop(arg1, arg2, ..., argn, callback)` - can be used to clear the cache for a given key. Supports using nested names such as - `utils.users.get` which will automatically create the missing path under - [`server.methods`](#servermethods) and can be accessed for the previous example via - `server.methods.utils.users.get`. +- `name` - a unique method name used to invoke the method via `server.methods[name]`. + Supports using nested names such as `utils.users.get` which will automatically + create the missing path under [`server.methods`](#servermethods) and can be accessed + for the previous example via `server.methods.utils.users.get`. + When configured with caching enabled, `server.methods[name].cache` will be an object + with the following properties and methods: + - `drop(arg1, arg2, ..., argn, callback)` - function that can be used to clear the cache for a given key. + - `stats` - an object with cache statistics, see stats documentation for **catbox**. - `method` - the method function with the signature is one of: - `function(arg1, arg2, ..., argn, next)` where: - `arg1`, `arg2`, etc. - the method function arguments. From dcb64d424fddfa9747ce344ed21fff484e4c74b0 Mon Sep 17 00:00:00 2001 From: Mike Wertman Date: Mon, 28 Sep 2015 20:49:30 -0400 Subject: [PATCH 0016/1139] Added documentation for the Request.generateResponse method --- API.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/API.md b/API.md index bd16df73d..7f9b81005 100755 --- a/API.md +++ b/API.md @@ -2600,6 +2600,28 @@ server.ext('onRequest', function (request, reply) { }); ``` +#### `request.generateResponse(source, [options])` + +_Always available._ + +Returns an [`response`](#response-object) which you can pass into the [reply interface](#reply-interface). Useful inside a promise to create a response object to return up to the [reply interface](#reply-interface). + +- `source` - the object to set as the source of the [reply interface](#reply-interface). +- `options` - options for the method, optional. + +```js +var handler = function (request, reply) { + var result = promiseMethod().then(function (thing) { + if (!thing) { + return request.generateResponse(Boom.notFound()); + } + + return thing; + }); + return reply(result); +}; +``` + #### `request.log(tags, [data, [timestamp]])` _Always available._ From 029bdaa781789d17999167b77a8f8587c6645d57 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 28 Sep 2015 19:28:54 -0700 Subject: [PATCH 0017/1139] lint --- package.json | 2 +- test/handler.js | 6 +++--- test/payload.js | 2 -- test/plugin.js | 13 ++++--------- test/request.js | 2 -- test/server.js | 1 - test/transmit.js | 1 - test/validation.js | 1 - 8 files changed, 8 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 47f18e9f3..1b8f82b8c 100755 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "code": "1.x.x", "handlebars": "2.x.x", "inert": "3.x.x", - "lab": "5.x.x", + "lab": "6.x.x", "vision": "2.x.x", "wreck": "6.x.x" }, diff --git a/test/handler.js b/test/handler.js index 118ef0ac3..9252d6c73 100755 --- a/test/handler.js +++ b/test/handler.js @@ -35,7 +35,7 @@ describe('handler', function () { var handler = function (request) { - var x = a.b.c; + a.b.c; }; server.route({ method: 'GET', path: '/domain', handler: handler }); @@ -53,7 +53,7 @@ describe('handler', function () { setImmediate(function () { - var x = not.here; + not.here; }); }; @@ -1166,7 +1166,7 @@ describe('handler', function () { server.connection(); server.ext('onRequest', function (request, next) { - var x = a.b.c; + a.b.c; }); var handler = function (request, reply) { diff --git a/test/payload.js b/test/payload.js index d9db4f9d9..60831211c 100755 --- a/test/payload.js +++ b/test/payload.js @@ -622,7 +622,6 @@ describe('payload', function () { req.on('error', function (err) { }); // Will error out, so don't allow error to escape test req.write('{}\n'); - var now = Date.now(); setTimeout(function () { req.end(); @@ -662,7 +661,6 @@ describe('payload', function () { req.on('error', function (err) { }); // Will error out, so don't allow error to escape test req.write('{}\n'); - var now = Date.now(); setTimeout(function () { req.end(); diff --git a/test/plugin.js b/test/plugin.js index be1697930..c196b083c 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1,6 +1,5 @@ // Load modules -var Os = require('os'); var Path = require('path'); var Boom = require('boom'); var CatboxMemory = require('catbox-memory'); @@ -140,10 +139,6 @@ describe('Plugin', function () { server.connection({ labels: ['b'] }); server.connection({ labels: ['c'] }); - var server1 = server.connections[0]; - var server2 = server.connections[1]; - var server3 = server.connections[2]; - var child = function (srv, options, next) { srv.expose('key2', srv.connections.length); @@ -1396,8 +1391,8 @@ describe('Plugin', function () { server.connection(); expect(function () { - var a1 = server.cache({ segment: 'a', expiresIn: 1000 }); - var a2 = server.cache({ segment: 'a', expiresIn: 1000 }); + server.cache({ segment: 'a', expiresIn: 1000 }); + server.cache({ segment: 'a', expiresIn: 1000 }); }).to.not.throw(); done(); }); @@ -1408,8 +1403,8 @@ describe('Plugin', function () { server.connection(); expect(function () { - var a1 = server.cache({ segment: 'a', expiresIn: 1000 }); - var a2 = server.cache({ segment: 'a', expiresIn: 1000, shared: true }); + server.cache({ segment: 'a', expiresIn: 1000 }); + server.cache({ segment: 'a', expiresIn: 1000, shared: true }); }).to.not.throw(); done(); }); diff --git a/test/request.js b/test/request.js index 20022444c..748d37ccb 100755 --- a/test/request.js +++ b/test/request.js @@ -694,8 +694,6 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var result = null; - server.once('tail', function () { done(); diff --git a/test/server.js b/test/server.js index 882a8ebf4..5a2d5393b 100755 --- a/test/server.js +++ b/test/server.js @@ -3,7 +3,6 @@ var Code = require('code'); var Hapi = require('..'); var Hoek = require('hoek'); -var Inert = require('inert'); var Lab = require('lab'); diff --git a/test/transmit.js b/test/transmit.js index f47ab99ae..5084a09fa 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -3,7 +3,6 @@ var ChildProcess = require('child_process'); var Fs = require('fs'); var Http = require('http'); -var Os = require('os'); var Path = require('path'); var Stream = require('stream'); var Zlib = require('zlib'); diff --git a/test/validation.js b/test/validation.js index cc782a331..7ef8d6640 100755 --- a/test/validation.js +++ b/test/validation.js @@ -856,7 +856,6 @@ describe('validation', function () { it('validates response with context', function (done) { - var i = 0; var handler = function (request, reply) { return reply({ some: 'thing', more: 'stuff' }); From 190198730c6f1bbb636e5fd2095cabc89cf9d499 Mon Sep 17 00:00:00 2001 From: Lois Desplat Date: Mon, 28 Sep 2015 18:07:03 -0700 Subject: [PATCH 0018/1139] response.vary() does not set the same value multiple times --- lib/response.js | 9 +++++++++ test/response.js | 20 ++++++++++++++++++++ test/transmit.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/lib/response.js b/lib/response.js index 8e839224f..d88cb4df2 100755 --- a/lib/response.js +++ b/lib/response.js @@ -143,6 +143,15 @@ internals.Response.prototype.vary = function (value) { this.headers.vary = value; } else if (this.headers.vary !== '*') { + + var values = this.headers.vary.split(','); + + for (var i = 0, il = values.length; i < il; ++i) { + if (values[i] === value) { + return this; + } + } + this._header('vary', value, { append: true }); } diff --git a/test/response.js b/test/response.js index 3d1a92a8a..e629254ae 100755 --- a/test/response.js +++ b/test/response.js @@ -286,6 +286,26 @@ describe('Response', function () { done(); }); }); + + it('sets Vary header with multiple similar and identical values', function (done) { + + var handler = function (request, reply) { + + return reply('ok').vary('x').vary('xyz').vary('xy').vary('x'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('ok'); + expect(res.statusCode).to.equal(200); + expect(res.headers.vary).to.equal('x,xyz,xy'); + done(); + }); + }); }); describe('etag()', function () { diff --git a/test/transmit.js b/test/transmit.js index f47ab99ae..4340148c6 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -1782,6 +1782,45 @@ describe('transmission', function () { }); }); + it('does not set accept-encoding multiple times', function (done) { + + var headersHandler = function (request, reply) { + + reply({ status: 'success' }) + .vary('X-Custom3'); + }; + + var upstream = new Hapi.Server(); + upstream.connection(); + upstream.route({ method: 'GET', path: '/headers', handler: headersHandler }); + upstream.start(function () { + + var proxyHandler = function (request, reply) { + + var options = {}; + options.headers = Hoek.clone(request.headers); + delete options.headers.host; + + Wreck.request(request.method, 'http://localhost:' + upstream.info.port + '/headers', options, function (err, res) { + + reply(res).code(res.statusCode); + }); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/headers', handler: proxyHandler }); + + server.inject({ url: '/headers', headers: { 'accept-encoding': 'gzip' } }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers.vary).to.equal('X-Custom3,accept-encoding'); + + upstream.stop(done); + }); + }); + }); + describe('response range', function () { var fileStreamHandler = function (request, reply) { From 71f2f4f88e967a346d5b8ad7b6fa01caa06c8c90 Mon Sep 17 00:00:00 2001 From: Mike Wertman Date: Tue, 29 Sep 2015 08:42:38 -0400 Subject: [PATCH 0019/1139] Update style / replace Useful --- API.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index 7f9b81005..f31b23d71 100755 --- a/API.md +++ b/API.md @@ -2604,21 +2604,22 @@ server.ext('onRequest', function (request, reply) { _Always available._ -Returns an [`response`](#response-object) which you can pass into the [reply interface](#reply-interface). Useful inside a promise to create a response object to return up to the [reply interface](#reply-interface). +Returns an [`response`](#response-object) which you can pass into the [reply interface](#reply-interface). For example inside a promise to create a response object to return up to the [reply interface](#reply-interface). - `source` - the object to set as the source of the [reply interface](#reply-interface). - `options` - options for the method, optional. ```js var handler = function (request, reply) { - var result = promiseMethod().then(function (thing) { - if (!thing) { - return request.generateResponse(Boom.notFound()); - } - return thing; - }); - return reply(result); + var result = promiseMethod().then(function (thing) { + + if (!thing) { + return request.generateResponse(Boom.notFound()); + } + return thing; + }); + return reply(result); }; ``` From b0548b7ab942ac29c85f2e2e70c57561bed50e50 Mon Sep 17 00:00:00 2001 From: Mike Wertman Date: Tue, 29 Sep 2015 11:54:48 -0400 Subject: [PATCH 0020/1139] Remove whitespace. Moved example --- API.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index f31b23d71..f9ff92524 100755 --- a/API.md +++ b/API.md @@ -2604,18 +2604,19 @@ server.ext('onRequest', function (request, reply) { _Always available._ -Returns an [`response`](#response-object) which you can pass into the [reply interface](#reply-interface). For example inside a promise to create a response object to return up to the [reply interface](#reply-interface). - +Returns a [`response`](#response-object) which you can pass into the [reply interface](#reply-interface) where: - `source` - the object to set as the source of the [reply interface](#reply-interface). - `options` - options for the method, optional. +For example it can be used inside a promise to create a response object which has a non-error code to resolve with the [reply interface](#reply-interface): + ```js var handler = function (request, reply) { var result = promiseMethod().then(function (thing) { if (!thing) { - return request.generateResponse(Boom.notFound()); + return request.generateResponse().code(214); } return thing; }); From a1635e328286b91f76c6c2131f0998ef3c50baa3 Mon Sep 17 00:00:00 2001 From: Adam Ulvi Date: Tue, 29 Sep 2015 11:20:06 -0700 Subject: [PATCH 0021/1139] Update API.md Updated plugin registration example and usage of 'server.views' to match the view tutorial example. --- API.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index bd16df73d..e121d75b0 100755 --- a/API.md +++ b/API.md @@ -3065,14 +3065,18 @@ a different response object. ```js var Hapi = require('hapi'); var Inert = require('inert'); +var vision = require('vision'); var server = new Hapi.Server(); -server.register(Inert); -server.connection({ port: 80 }); -server.views({ +server.register(Inert, function () {}); +server.register(vision, function (err) { + server.views({ engines: { html: require('handlebars') } + }); }); +server.connection({ port: 80 }); + server.ext('onPreResponse', function (request, reply) { From 1990c15194830794c21798695249410f93656471 Mon Sep 17 00:00:00 2001 From: Adam Ulvi Date: Tue, 29 Sep 2015 12:19:42 -0700 Subject: [PATCH 0022/1139] Corrected variable naming and indention --- API.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index e121d75b0..77f5b0953 100755 --- a/API.md +++ b/API.md @@ -3065,14 +3065,14 @@ a different response object. ```js var Hapi = require('hapi'); var Inert = require('inert'); -var vision = require('vision'); +var Vision = require('vision'); var server = new Hapi.Server(); server.register(Inert, function () {}); -server.register(vision, function (err) { - server.views({ - engines: { - html: require('handlebars') - } +server.register(Vision, function (err) { + server.views({ + engines: { + html: require('handlebars') + } }); }); server.connection({ port: 80 }); From 885692c72b9809656b0d39f842d9cbda5b96e63c Mon Sep 17 00:00:00 2001 From: Adam Ulvi Date: Tue, 29 Sep 2015 13:34:34 -0700 Subject: [PATCH 0023/1139] 'register' call updated to consume array --- API.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/API.md b/API.md index 77f5b0953..50503389e 100755 --- a/API.md +++ b/API.md @@ -3067,8 +3067,7 @@ var Hapi = require('hapi'); var Inert = require('inert'); var Vision = require('vision'); var server = new Hapi.Server(); -server.register(Inert, function () {}); -server.register(Vision, function (err) { +server.register([Inert, Vision], function (err) { server.views({ engines: { html: require('handlebars') From dde28e17cfa7f1d10c62b0cc19feba1b5b082d3e Mon Sep 17 00:00:00 2001 From: Adam Ulvi Date: Tue, 29 Sep 2015 18:23:05 -0700 Subject: [PATCH 0024/1139] Update API.md --- API.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/API.md b/API.md index 50503389e..4e7bb0817 100755 --- a/API.md +++ b/API.md @@ -3064,10 +3064,9 @@ a different response object. ```js var Hapi = require('hapi'); -var Inert = require('inert'); var Vision = require('vision'); var server = new Hapi.Server(); -server.register([Inert, Vision], function (err) { +server.register(Vision, function (err) { server.views({ engines: { html: require('handlebars') From 7b5b6934c4565c7589f94b0ce1c4945f7ffa2a14 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 1 Oct 2015 14:38:47 -0700 Subject: [PATCH 0025/1139] Apply arguments schema more consistently. Closes #2804 --- lib/auth.js | 2 +- lib/methods.js | 5 ++--- lib/plugin.js | 10 +++------- lib/route.js | 5 ++--- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 5c8fa84b4..435aeb6ff 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -66,7 +66,7 @@ internals.Auth.prototype.strategy = function (name, scheme /*, mode, options */) internals.Auth.prototype.default = function (options) { - Schema.assert('auth', options, 'default strategy'); + options = Schema.assert('auth', options, 'default strategy'); Hoek.assert(!this.settings.default, 'Cannot set default strategy more than once'); var settings = Hoek.clone(options); // options can be reused diff --git a/lib/methods.js b/lib/methods.js index 6fab08b17..56440aa22 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -29,7 +29,7 @@ internals.Methods.prototype.add = function (name, method, options, realm) { var items = [].concat(name); for (var i = 0, il = items.length; i < il; ++i) { var item = items[i]; - Schema.assert('methodObject', item); + item = Schema.assert('methodObject', item); this._add(item.name, item.method, item.options, realm); } }; @@ -45,8 +45,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { Hoek.assert(name.match(exports.methodNameRx), 'Invalid name:', name); Hoek.assert(!Hoek.reach(this.methods, name, { functions: false }), 'Server method function name already exists:', name); - options = options || {}; - Schema.assert('method', options, name); + options = Schema.assert('method', options || {}, name); var settings = Hoek.cloneWithShallow(options, ['bind']); settings.generateKey = settings.generateKey || internals.generateKey; diff --git a/lib/plugin.js b/lib/plugin.js index 239f84e98..d15ad1080 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -122,9 +122,6 @@ internals.Plugin.prototype._select = function (labels, plugin) { if (labels && labels.length) { // Captures both empty arrays and empty strings - Hoek.assert(typeof labels === 'string' || Array.isArray(labels), 'Bad labels object type (undefined or array required)'); - labels = [].concat(labels); - connections = []; for (var i = 0, il = this.connections.length; i < il; ++i) { var connection = this.connections[i]; @@ -171,7 +168,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback options.routes.vhost = this.realm.modifiers.route.vhost || options.routes.vhost; } - Schema.assert('register', options); + options = Schema.assert('register', options); /* var register = function (server, options, next) { return next(); }; @@ -226,8 +223,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback }; Hoek.assert(registration.name, 'Missing plugin name', hint); - Schema.assert('dependencies', registration.dependencies, 'must be a string or an array of strings'); - + registration.dependencies = Schema.assert('dependencies', registration.dependencies, 'must be a string or an array of strings'); registrations.push(registration); } @@ -278,7 +274,7 @@ internals.Plugin.prototype.bind = function (context) { internals.Plugin.prototype.cache = function (options, _segment) { - Schema.assert('cachePolicy', options); + options = Schema.assert('cachePolicy', options); var segment = options.segment || _segment || (this.realm.plugin ? '!' + this.realm.plugin : ''); Hoek.assert(segment, 'Missing cache segment name'); diff --git a/lib/route.js b/lib/route.js index c19596560..d2243c896 100755 --- a/lib/route.js +++ b/lib/route.js @@ -37,7 +37,7 @@ exports = module.exports = internals.Route = function (options, connection, real Hoek.assert(options.path === '/' || options.path[options.path.length - 1] !== '/' || !connection.settings.router.stripTrailingSlash, 'Path cannot end with a trailing slash when connection configured to strip:', options.method, options.path); Hoek.assert(/^[a-zA-Z0-9!#\$%&'\*\+\-\.^_`\|~]+$/.test(options.method), 'Invalid method name:', options.method, options.path); - Schema.assert('route', options, options.path); + options = Schema.assert('route', options, options.path); var handler = options.handler || options.config.handler; var method = options.method.toLowerCase(); @@ -50,8 +50,7 @@ exports = module.exports = internals.Route = function (options, connection, real base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind']); this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind']); this.settings.handler = handler; - - Schema.assert('routeConfig', this.settings, options.path); + this.settings = Schema.assert('routeConfig', this.settings, options.path); var socketTimeout = (this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket); Hoek.assert(!this.settings.timeout.server || !socketTimeout || this.settings.timeout.server < socketTimeout, 'Server timeout must be shorter than socket timeout:', options.path); From 733a54bd56c8032a9f67270a346a73357349fed6 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 1 Oct 2015 14:39:27 -0700 Subject: [PATCH 0026/1139] 10.2.0 --- API.md | 2 +- README.md | 2 +- npm-shrinkwrap.json | 2 +- package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index 4e7bb0817..96fa6efbc 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 10.0.x API Reference +# 10.2.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) diff --git a/README.md b/README.md index 404d254c0..eb7d8f03b 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse) authentication, and other essential facilities for building web and services applications. **hapi** enables developers to focus on writing reusable application logic in a highly modular and prescriptive approach. -Development version: **10.0.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) +Development version: **10.2.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) [![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi) For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 22be6a11d..8f8ded108 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.1.0", + "version": "10.2.0", "dependencies": { "accept": { "version": "1.1.0" diff --git a/package.json b/package.json index 1b8f82b8c..3b1733a14 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.1.0", + "version": "10.2.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From 8221ab1727c379d7ff1850855b4b79f2a910f36f Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 1 Oct 2015 14:55:12 -0700 Subject: [PATCH 0027/1139] Avoid setting undefined headers. Closes #2352 --- lib/transmit.js | 5 ++++- test/transmit.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/transmit.js b/lib/transmit.js index 387db58d4..b7e1b6a7c 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -253,7 +253,10 @@ internals.transmit = function (response, callback) { var headers = Object.keys(response.headers); for (var h = 0, hl = headers.length; h < hl; ++h) { var header = headers[h]; - request.raw.res.setHeader(header, response.headers[header]); + var value = response.headers[header]; + if (value !== undefined) { + request.raw.res.setHeader(header, value); + } } request.raw.res.writeHead(response.statusCode); diff --git a/test/transmit.js b/test/transmit.js index 5084a09fa..145b29b86 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2082,6 +2082,25 @@ describe('transmission', function () { }); }); }); + + it('skips undefined header values', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var handler = function (request, reply) { + + return reply('ok').header('x', undefined); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers.x).to.not.exist(); + done(); + }); + }); }); describe('cors()', function () { From 8145c1434d7f1c2529eb53f8a26b294f4ebcf554 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 1 Oct 2015 14:55:33 -0700 Subject: [PATCH 0028/1139] node versions --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2a2c73da5..7fa6ac7bf 100755 --- a/.travis.yml +++ b/.travis.yml @@ -2,5 +2,6 @@ language: node_js node_js: - 4.0 + - 4 sudo: false From 7f2bc715020c6457d75c44c3a58654a5b1b7d831 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 1 Oct 2015 16:14:11 -0700 Subject: [PATCH 0029/1139] Option to disable domains. Closes #2788 --- API.md | 12 ++++++++---- lib/auth.js | 6 +++--- lib/connection.js | 2 +- lib/defaults.js | 3 ++- lib/handler.js | 4 ++-- lib/protect.js | 49 +++++++++++++++++++++++++++++++++-------------- lib/schema.js | 3 ++- lib/validation.js | 6 +++--- test/protect.js | 28 +++++++++++++++++++++++++++ 9 files changed, 84 insertions(+), 29 deletions(-) diff --git a/API.md b/API.md index 96fa6efbc..06a73e04c 100755 --- a/API.md +++ b/API.md @@ -195,6 +195,9 @@ Creates a new `Server` object where: used to store static configuration values and [`server.plugins`](#serverplugins) which is meant for storing run-time state. Defaults to `{}`. + - `useDomains` - if `false`, will not use node domains to protect against exceptions thrown in + handlers and other external code. Defaults to `true`. + Note that the `options` object is deeply cloned and cannot contain any values that are unsafe to perform deep copy on. @@ -1982,9 +1985,9 @@ following options: string or an array of scope strings. The authenticated credentials object `scope` property must contain at least one of the scopes defined to access the route. You may also access properties on the request object (`query` and `params`} to populate a - dynamic scope by using `{}` characters around the property name, such as - `'user-{params.id}'`. Set to `false` to remove scope requirements. Defaults to no scope - required. + dynamic scope by using `{}` characters around the property name, such as + `'user-{params.id}'`. Set to `false` to remove scope requirements. Defaults to no scope + required. - `entity` - the required authenticated entity type. If set, must match the `entity` value of the authentication credentials. Available values: - `any` - the authentication can be on behalf of a user or application. This is the @@ -2508,7 +2511,8 @@ Each request object includes the following properties: [`'cookie'` authentication scheme](https://github.com/hapijs/hapi-auth-cookie). - `domain` - the node domain object used to protect against exceptions thrown in extensions, handlers and [route prerequisites](#route-prerequisites). Can be used to manually bind callback - functions otherwise bound to other domains. + functions otherwise bound to other domains. Set to `null` when the server `useDomains` options is + `false`. - `headers` - the raw request headers (references `request.raw.headers`). - `id` - a unique request identifier (using the format '{now}:{connection.info.id}:{5 digits counter}'). - `info` - request information: diff --git a/lib/auth.js b/lib/auth.js index 435aeb6ff..8af73e9dc 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -215,7 +215,7 @@ internals.Auth.prototype._authenticate = function (request, next) { var name = config.strategies[strategyPos]; ++strategyPos; - request._protect.run('auth:request:' + name, validate, function (exit) { + request._protect.run(validate, function (exit) { var transfer = function (response, data) { @@ -382,7 +382,7 @@ internals.Auth.payload = function (request, next) { return next(response); }; - request._protect.run('auth:payload:' + request.auth.strategy, finalize, function (exit) { + request._protect.run(finalize, function (exit) { var reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.payload(request, reply); @@ -406,7 +406,7 @@ internals.Auth.response = function (request, next) { return next(); } - request._protect.run('auth:response:' + request.auth.strategy, next, function (exit) { + request._protect.run(next, function (exit) { var reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.response(request, reply); diff --git a/lib/connection.js b/lib/connection.js index d8b0304ab..3e9dacfbe 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -245,7 +245,7 @@ internals.Connection.prototype._dispatch = function (options) { // Execute request lifecycle - request._protect.domain.run(function () { + request._protect.enter(function () { request._execute(); }); diff --git a/lib/defaults.js b/lib/defaults.js index 3dfd0a6da..b1fc5a82a 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -16,7 +16,8 @@ exports.server = { load: { sampleInterval: 0 }, - mime: null // Mimos options + mime: null, // Mimos options + useDomains: true }; diff --git a/lib/handler.js b/lib/handler.js index cb2e08e89..972738167 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -19,7 +19,7 @@ exports.execute = function (request, next) { return next(); // Must not include an argument }; - request._protect.run('handler', finalize, function (exit) { + request._protect.run(finalize, function (exit) { if (request._route._prerequisites) { internals.prerequisites(request, Hoek.once(exit)); @@ -301,7 +301,7 @@ exports.invoke = function (request, event, callback) { request._protect.reset(); } - request._protect.run('ext:' + event, callback, function (exit) { + request._protect.run(callback, function (exit) { Items.serial(exts.nodes, function (ext, next) { diff --git a/lib/protect.js b/lib/protect.js index c47b64417..03bfcf0f7 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -1,6 +1,6 @@ // Load modules -var Domain = require('domain'); +var Domain = null; // Loaded as needed var Boom = require('boom'); var Hoek = require('hoek'); @@ -15,39 +15,51 @@ exports = module.exports = internals.Protect = function (request) { var self = this; this._error = null; - this._at = ''; this.logger = request; // Replaced with server when request completes + if (!request.server.settings.useDomains) { + this.domain = null; + return; + } + + Domain = Domain || require('domain'); + this.domain = Domain.create(); this.domain.on('error', function (err) { - var handler = self._error; - if (handler) { - self._error = null; - return handler(err); - } - - self.logger._log(['internal', 'implementation', 'error'], err); + return self._onError(err); }); }; -internals.Protect.prototype.run = function (at, next, enter) { // enter: function (exit) +internals.Protect.prototype._onError = function (err) { + + var handler = this._error; + if (handler) { + this._error = null; + return handler(err); + } + + this.logger._log(['internal', 'implementation', 'error'], err); +}; + + +internals.Protect.prototype.run = function (next, enter) { // enter: function (exit) var self = this; - Hoek.assert(!this._error, 'Invalid nested use of protect.run() during: ' + this._at + ' while trying: ' + at); + if (!this.domain) { + return enter(finish); + } var finish = function (arg0, arg1, arg2) { self._error = null; - self._at = ''; return next(arg0, arg1, arg2); }; finish = Hoek.once(finish); - this._at = at; this._error = function (err) { return finish(Boom.badImplementation('Uncaught error', err)); @@ -60,5 +72,14 @@ internals.Protect.prototype.run = function (at, next, enter) { // enter internals.Protect.prototype.reset = function () { this._error = null; - this._at = ''; +}; + + +internals.Protect.prototype.enter = function (func) { + + if (!this.domain) { + return func(); + } + + this.domain.run(func); }; diff --git a/lib/schema.js b/lib/schema.js index 6f5edaf2a..0e1b96c1f 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -187,7 +187,8 @@ internals.server = Joi.object({ }).allow(false), load: Joi.object(), mime: Joi.object(), - plugins: Joi.object() + plugins: Joi.object(), + useDomains: Joi.boolean() }); diff --git a/lib/validation.js b/lib/validation.js index de9d55c2e..faaa7daba 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -101,7 +101,7 @@ internals.input = function (source, request, next) { // Custom handler - request._protect.run('validate:input:failAction', next, function (exit) { + request._protect.run(next, function (exit) { var reply = request.server._replier.interface(request, request.route.realm, exit); request.route.settings.validate.failAction(request, reply, source, error); @@ -129,7 +129,7 @@ internals.input = function (source, request, next) { return Joi.validate(request[source], schema, localOptions, postValidate); } - request._protect.run('validate:input', postValidate, function (exit) { + request._protect.run(postValidate, function (exit) { return schema(request[source], localOptions, exit); }); @@ -213,7 +213,7 @@ exports.response = function (request, next) { return Joi.validate(source, schema, localOptions, postValidate); } - request._protect.run('validate:response', postValidate, function (exit) { + request._protect.run(postValidate, function (exit) { return schema(source, localOptions, exit); }); diff --git a/test/protect.js b/test/protect.js index 6c7f507ec..14134e6e2 100755 --- a/test/protect.js +++ b/test/protect.js @@ -1,6 +1,7 @@ // Load modules var Events = require('events'); +var Domain = require('domain'); var Code = require('code'); var Hapi = require('..'); var Hoek = require('hoek'); @@ -22,6 +23,33 @@ var expect = Code.expect; describe('Protect', function () { + it('does not handle errors when useDomains is false', function (done) { + + var server = new Hapi.Server({ useDomains: false, debug: false }); + server.connection(); + + var handler = function (request, reply) { + + process.nextTick(function () { + + throw new Error('no domain'); + }); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + var domain = Domain.createDomain(); + domain.once('error', function (err) { + + expect(err.message).to.equal('no domain'); + done(); + }); + + domain.run(function () { + + server.inject('/', function (res) { }); + }); + }); + it('catches error when handler throws after reply() is called', function (done) { var server = new Hapi.Server({ debug: false }); From 900c9138997097c51a1b3be4856bfaf84b69f1a0 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 12:31:41 -0700 Subject: [PATCH 0030/1139] Support per plugin registration options. Closes #1850 --- API.md | 1 + lib/plugin.js | 49 +++++++++++++---------- lib/schema.js | 24 ++++++++++- npm-shrinkwrap.json | 4 +- package.json | 2 +- test/plugin.js | 98 ++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 146 insertions(+), 32 deletions(-) diff --git a/API.md b/API.md index 06a73e04c..5c0f70d40 100755 --- a/API.md +++ b/API.md @@ -1368,6 +1368,7 @@ Registers a plugin where: - an object with the following: - `register` - the plugin registration function. - `options` - optional options passed to the registration function when called. + - `select`, `routes` - optional plugin-specific registration options as defined below. - `options` - optional registration options (different from the options passed to the registration function): - `select` - a string or array of string labels used to pre-select connections for plugin diff --git a/lib/plugin.js b/lib/plugin.js index d15ad1080..ab840746c 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -176,7 +176,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback pkg: require('../package.json'), name: 'plugin', version: '1.1.1', - multiple: false + multiple: false, + dependencies: [] }; var item = { @@ -197,33 +198,39 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback plugins = [].concat(plugins); for (var i = 0, il = plugins.length; i < il; ++i) { var plugin = plugins[i]; - var hint = (plugins.length > 1 ? '(' + i + ')' : ''); - if (typeof plugin === 'function' && - !plugin.register) { - - plugin = { register: plugin }; + if (typeof plugin === 'function') { + if (!plugin.register) { // plugin is register() function + plugin = { register: plugin }; + } + else { + plugin = Hoek.shallow(plugin); // Convert function to object + } } if (plugin.register.register) { // Required plugin plugin.register = plugin.register.register; } - Hoek.assert(typeof plugin.register === 'function', 'Invalid plugin object - invalid or missing register function ', hint); - var attributes = plugin.register.attributes; - Hoek.assert(typeof plugin.register.attributes === 'object', 'Invalid plugin object - invalid or missing register function attributes property', hint); + plugin = Schema.assert('plugin', plugin); + var attributes = plugin.register.attributes; var registration = { register: plugin.register, - name: attributes.name || (attributes.pkg && attributes.pkg.name), - version: attributes.version || (attributes.pkg && attributes.pkg.version) || '0.0.0', - multiple: attributes.multiple || false, - options: plugin.options, - dependencies: attributes.dependencies + name: attributes.name || attributes.pkg.name, + version: attributes.version || attributes.pkg.version, + multiple: attributes.multiple, + pluginOptions: plugin.options, + dependencies: attributes.dependencies, + options: { + routes: { + prefix: plugin.routes.prefix || options.routes.prefix, + vhost: plugin.routes.vhost || options.routes.vhost + }, + select: plugin.select || options.select + } }; - Hoek.assert(registration.name, 'Missing plugin name', hint); - registration.dependencies = Schema.assert('dependencies', registration.dependencies, 'must be a string or an array of strings'); registrations.push(registration); } @@ -231,10 +238,10 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Items.serial(registrations, function (item, next) { - var selection = self._select(options.select, item.name); - selection.realm.modifiers.route.prefix = options.routes && options.routes.prefix; - selection.realm.modifiers.route.vhost = options.routes && options.routes.vhost; - selection.realm.pluginOptions = item.options || {}; + var selection = self._select(item.options.select, item.name); + selection.realm.modifiers.route.prefix = item.options.routes.prefix; + selection.realm.modifiers.route.vhost = item.options.routes.vhost; + selection.realm.pluginOptions = item.pluginOptions || {}; // Protect against multiple registrations @@ -250,7 +257,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback // Register - item.register(selection, item.options || {}, next); + item.register(selection, item.pluginOptions || {}, next); }, function (err) { self.root._registring = false; diff --git a/lib/schema.js b/lib/schema.js index 0e1b96c1f..0a9de44fe 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -287,8 +287,28 @@ internals.register = Joi.object({ routes: Joi.object({ prefix: Joi.string().regex(/^\/.+/), vhost: internals.vhost - }), + }) + .default({}), select: Joi.array().items(Joi.string()).single() }); -internals.dependencies = Joi.array().items(Joi.string()).single(); + +internals.plugin = internals.register.keys({ + register: Joi.func().keys({ + attributes: Joi.object({ + pkg: Joi.object({ + name: Joi.string(), + version: Joi.string().default('0.0.0') + }) + .unknown() + .default({}), + name: Joi.string() + .when('pkg.name', { is: Joi.exist(), otherwise: Joi.required() }), + version: Joi.string(), + multiple: Joi.boolean().default(false), + dependencies: Joi.array().items(Joi.string()).single() + }) + .required() + }).required(), + options: Joi.any() +}); diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8f8ded108..08a4d36f7 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -36,10 +36,10 @@ "version": "1.1.0" }, "joi": { - "version": "6.6.1", + "version": "6.8.1", "dependencies": { "isemail": { - "version": "1.1.1" + "version": "1.2.0" }, "moment": { "version": "2.10.6" diff --git a/package.json b/package.json index 3b1733a14..d4b981345 100755 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "hoek": "^2.14.x", "iron": "2.x.x", "items": "1.x.x", - "joi": "6.x.x", + "joi": "^6.8.1", "kilt": "^1.1.x", "mimos": "2.x.x", "peekaboo": "1.x.x", diff --git a/test/plugin.js b/test/plugin.js index c196b083c..262491fa5 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -230,7 +230,7 @@ describe('Plugin', function () { } }, function (err) { }); - }).to.throw('Invalid plugin object - invalid or missing register function attributes property'); + }).to.throw(); done(); }); @@ -248,7 +248,7 @@ describe('Plugin', function () { expect(function () { server.register(register, function (err) { }); - }).to.throw('Missing plugin name'); + }).to.throw(); done(); }); @@ -268,7 +268,7 @@ describe('Plugin', function () { expect(function () { server.register(register, function (err) { }); - }).to.throw('Missing plugin name'); + }).to.throw(); done(); }); @@ -484,6 +484,22 @@ describe('Plugin', function () { }); }); + it('registers a plugin with routes path prefix (plugin options)', function (done) { + + var server = new Hapi.Server(); + server.connection({ labels: 'test' }); + server.register({ register: internals.plugins.test1, routes: { prefix: '/abc' } }, { routes: { prefix: '/xyz' } }, function (err) { + + expect(server.plugins.test1.prefix).to.equal('/abc'); + expect(err).to.not.exist(); + server.inject('/abc/test1', function (res) { + + expect(res.result).to.equal('testing123'); + done(); + }); + }); + }); + it('registers a plugin with routes path prefix and plugin root route', function (done) { var test = function (srv, options, next) { @@ -627,6 +643,26 @@ describe('Plugin', function () { }); }); + it('registers a plugin with routes vhost (plugin options)', function (done) { + + var server = new Hapi.Server(); + server.connection({ labels: 'test' }); + server.register({ register: internals.plugins.test1, routes: { vhost: 'example.org' } }, { routes: { vhost: 'example.com' } }, function (err) { + + expect(err).to.not.exist(); + server.inject('/test1', function (res1) { + + expect(res1.statusCode).to.equal(404); + + server.inject({ url: '/test1', headers: { host: 'example.org' } }, function (res2) { + + expect(res2.result).to.equal('testing123'); + done(); + }); + }); + }); + }); + it('registers plugins with pre-selected label', function (done) { var server = new Hapi.Server(); @@ -718,6 +754,56 @@ describe('Plugin', function () { }); }); + it('registers plugins with pre-selected labels (plugin options)', function (done) { + + var server = new Hapi.Server(); + server.connection({ labels: ['a'] }); + server.connection({ labels: ['b'] }); + server.connection({ labels: ['c'] }); + + var server1 = server.connections[0]; + var server2 = server.connections[1]; + var server3 = server.connections[2]; + + var test = function (srv, options, next) { + + srv.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply('ok'); + } + }); + srv.expose('super', 'trooper'); + return next(); + }; + + test.attributes = { + name: 'test' + }; + + server.register({ register: test, select: ['a', 'c'] }, { select: ['b'] }, function (err) { + + expect(err).to.not.exist(); + expect(server.plugins.test.super).to.equal('trooper'); + + server1.inject('/', function (res1) { + + expect(res1.statusCode).to.equal(200); + server2.inject('/', function (res2) { + + expect(res2.statusCode).to.equal(404); + server3.inject('/', function (res3) { + + expect(res3.statusCode).to.equal(200); + done(); + }); + }); + }); + }); + }); + it('sets multiple dependencies in one statement', function (done) { var a = function (srv, options, next) { @@ -861,7 +947,7 @@ describe('Plugin', function () { a.attributes = { name: 'a', - dependecies: 'c' + dependencies: 'c' }; var b = function (srv, options, next) { @@ -913,7 +999,7 @@ describe('Plugin', function () { expect(function () { server.register(a, function () {}); - }).to.throw('Invalid dependencies options (must be a string or an array of strings) {\n \"b\": true,\n \u001b[41m\"0\"\u001b[0m\u001b[31m [1]: -- missing --\u001b[0m\n}\n\u001b[31m\n[1] "0" must be a string\u001b[0m'); + }).to.throw(); done(); }); @@ -934,7 +1020,7 @@ describe('Plugin', function () { expect(function () { server.register(a, function () {}); - }).to.throw('Invalid dependencies options (must be a string or an array of strings) [\n null\n]\n\u001b[31m\n[1] "0" must be a string\u001b[0m'); + }).to.throw(); done(); }); From 23760c7f0df3ad114e23c4e3fe24cea96d47103b Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 12:48:32 -0700 Subject: [PATCH 0031/1139] Cleanup for #2796 --- API.md | 3 +++ lib/response.js | 34 ++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/API.md b/API.md index 5c0f70d40..7d445dcda 100755 --- a/API.md +++ b/API.md @@ -2896,6 +2896,9 @@ The response object provides the following methods: `','`. - `override` - if `false`, the header value is not set if an existing value present. Defaults to `true`. + - `duplicate` - if `false`, the header value is not modified if the provided value is + already included. Does not apply when `append` is `false` or if the `name` is + `'set-cookie'`. Defaults to `true`. - `location(uri)` - sets the HTTP 'Location' header where: - `uri` - an absolute or relative URI used as the 'Location' header value. - `redirect(uri)` - sets an HTTP redirection response (302) and decorates the response with diff --git a/lib/response.js b/lib/response.js index d88cb4df2..49898f548 100755 --- a/lib/response.js +++ b/lib/response.js @@ -112,21 +112,32 @@ internals.Response.prototype.header = function (key, value, options) { internals.Response.prototype._header = function (key, value, options) { options = options || {}; - options.append = options.append || false; - options.separator = options.separator || ','; - options.override = options.override !== false; + var append = options.append || false; + var separator = options.separator || ','; + var override = options.override !== false; + var duplicate = options.duplicate !== false; - if ((!options.append && options.override) || + if ((!append && override) || !this.headers[key]) { this.headers[key] = value; } - else if (options.override) { + else if (override) { if (key === 'set-cookie') { this.headers[key] = [].concat(this.headers[key], value); } else { - this.headers[key] = this.headers[key] + options.separator + value; + var existing = this.headers[key]; + if (!duplicate) { + var values = existing.split(separator); + for (var i = 0, il = values.length; i < il; ++i) { + if (values[i] === value) { + return this; + } + } + } + + this.headers[key] = existing + separator + value; } } @@ -143,16 +154,7 @@ internals.Response.prototype.vary = function (value) { this.headers.vary = value; } else if (this.headers.vary !== '*') { - - var values = this.headers.vary.split(','); - - for (var i = 0, il = values.length; i < il; ++i) { - if (values[i] === value) { - return this; - } - } - - this._header('vary', value, { append: true }); + this._header('vary', value, { append: true, duplicate: false }); } return this; From 6120f4395e09c009f27f84ba4ba752d034252406 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 13:26:27 -0700 Subject: [PATCH 0032/1139] Move stop() error to callback. Closes #2736 --- lib/server.js | 5 ++++- test/server.js | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/server.js b/lib/server.js index 6b8802e52..f81e241a7 100755 --- a/lib/server.js +++ b/lib/server.js @@ -258,7 +258,10 @@ internals.Server.prototype.stop = function (/* [options], callback */) { options.timeout = options.timeout || 5000; // Default timeout to 5 seconds Hoek.assert(typeof callback === 'function', 'Missing required stop callback function'); - Hoek.assert(['stopped', 'initialized', 'started', 'invalid'].indexOf(this._state) !== -1, 'Cannot stop server while in', this._state, 'state'); + + if (['stopped', 'initialized', 'started', 'invalid'].indexOf(this._state) === -1) { + return Hoek.nextTick(callback)(new Error('Cannot stop server while in ' + this._state + ' state')); + } this._state = 'stopping'; diff --git a/test/server.js b/test/server.js index 5a2d5393b..99f3527a5 100755 --- a/test/server.js +++ b/test/server.js @@ -341,12 +341,12 @@ describe('Server', function () { server.connection(); server.stop(Hoek.ignore); - expect(function () { - - server.stop(Hoek.ignore); - }).to.throw('Cannot stop server while in stopping state'); + server.stop(function (err) { - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Cannot stop server while in stopping state'); + done(); + }); }); }); From 0ac67fc14c36654db5cd6df02d9b28156e58e584 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 13:41:13 -0700 Subject: [PATCH 0033/1139] Move start/initialize errors to callback. Closes #2808 --- lib/server.js | 20 +++++++++++++++---- test/server.js | 53 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/lib/server.js b/lib/server.js index f81e241a7..a87d0074c 100755 --- a/lib/server.js +++ b/lib/server.js @@ -142,7 +142,9 @@ internals.Server.prototype.start = function (callback) { return; } - Hoek.assert(this._state === 'stopped', 'Cannot start server while it is in', this._state, 'state'); + if (this._state !== 'stopped') { + return Hoek.nextTick(callback)(new Error('Cannot start server while it is in ' + this._state + ' state')); + } this.initialize(function (err) { @@ -160,9 +162,19 @@ internals.Server.prototype.initialize = function (callback) { var self = this; Hoek.assert(callback, 'Missing start callback function'); - Hoek.assert(this.connections.length, 'No connections to start'); - Hoek.assert(!this._registring, 'Cannot start server before plugins finished registration'); - Hoek.assert(this._state === 'stopped', 'Cannot initialize server while it is in', this._state, 'state'); + + var errorCallback = Hoek.nextTick(callback); + if (!this.connections.length) { + return errorCallback(new Error('No connections to start')); + } + + if (this._registring) { + return errorCallback(new Error('Cannot start server before plugins finished registration')); + } + + if (this._state !== 'stopped') { + return errorCallback(new Error('Cannot initialize server while it is in ' + this._state + ' state')); + } // Assert dependencies diff --git a/test/server.js b/test/server.js index 99f3527a5..1b90f39c0 100755 --- a/test/server.js +++ b/test/server.js @@ -210,11 +210,12 @@ describe('Server', function () { it('fails to start server without connections', function (done) { var server = new Hapi.Server(); - expect(function () { + server.start(function (err) { - server.start(Hoek.ignore); - }).to.throw('No connections to start'); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('No connections to start'); + done(); + }); }); it('fails to start server when registration incomplete', function (done) { @@ -225,12 +226,12 @@ describe('Server', function () { var server = new Hapi.Server(); server.connection(); server.register(plugin, Hoek.ignore); + server.start(function (err) { - expect(function () { - - server.start(Hoek.ignore); - }).to.throw('Cannot start server before plugins finished registration'); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Cannot start server before plugins finished registration'); + done(); + }); }); it('fails to start when no callback is passed', function (done) { @@ -243,6 +244,40 @@ describe('Server', function () { }).to.throw('Missing required start callback function'); done(); }); + + it('fails to initialize server when not stopped', function (done) { + + var plugin = function () { }; + plugin.attributes = { name: 'plugin' }; + + var server = new Hapi.Server(); + server.connection(); + server.start(function (err) { + + server.initialize(function (err) { + + expect(err).to.exist(); + expect(err.message).to.equal('Cannot initialize server while it is in started state'); + done(); + }); + }); + }); + + it('fails to start server when starting', function (done) { + + var plugin = function () { }; + plugin.attributes = { name: 'plugin' }; + + var server = new Hapi.Server(); + server.connection(); + server.start(Hoek.ignore); + server.start(function (err) { + + expect(err).to.exist(); + expect(err.message).to.equal('Cannot start server while it is in initializing state'); + done(); + }); + }); }); describe('stop()', function () { From fb78edf21cc1304c7161a9ba58895b1c93b30122 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 15:12:07 -0700 Subject: [PATCH 0034/1139] Plugin attributes.connections. Closes #2809. Closes #2811 --- API.md | 3 + lib/connection.js | 2 +- lib/plugin.js | 17 ++++- lib/schema.js | 3 +- lib/server.js | 11 ++- test/plugin.js | 186 ++++++++++++++++++++++++++++++++++++++++------ test/request.js | 2 +- 7 files changed, 193 insertions(+), 31 deletions(-) diff --git a/API.md b/API.md index f3897af3d..52f8861a0 100755 --- a/API.md +++ b/API.md @@ -1809,6 +1809,9 @@ The plugin function must include an `attributes` function property with the foll Defaults to `false`. - `dependencies` - optional string or array of string indicating a plugin dependency. Same as setting dependencies via [`server.dependency()`](#serverdependencydependencies-after). +- `connections` - if `false`, does not allow the plugin to call server APIs that modify the + connections such as adding a route or configuring state. This flag allows the plugin to be + registered before connections are added and to pass dependency requirements. Defaults to `true`. ```js var register = function (server, options, next) { diff --git a/lib/connection.js b/lib/connection.js index 3e9dacfbe..07ae10136 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -62,7 +62,7 @@ exports = module.exports = internals.Connection = function (server, options) { this._started = false; this._connections = {}; this._onConnection = null; // Used to remove event listener on stop - this._registrations = {}; // Tracks plugin for dependency validation + this.registrations = {}; // Tracks plugin for dependency validation { name -> { version } } this._extensions = { onRequest: null, // New request, before handing over to the router (allows changes to the request method, url, etc.) diff --git a/lib/plugin.js b/lib/plugin.js index ab840746c..0ad3bc94c 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -177,7 +177,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback name: 'plugin', version: '1.1.1', multiple: false, - dependencies: [] + dependencies: [], + connections: false }; var item = { @@ -222,6 +223,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback multiple: attributes.multiple, pluginOptions: plugin.options, dependencies: attributes.dependencies, + connections: attributes.connections, options: { routes: { prefix: plugin.routes.prefix || options.routes.prefix, @@ -245,10 +247,19 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback // Protect against multiple registrations + if (!item.connections) { + Hoek.assert(item.multiple || !self.root._registrations[item.name], 'Plugin', item.name, 'already registered'); + self.root._registrations[item.name] = { version: item.version }; + } + for (var j = 0, jl = selection.connections.length; j < jl; ++j) { var connection = selection.connections[j]; - Hoek.assert(item.multiple || !connection._registrations[item.name], 'Plugin', item.name, 'already registered in:', connection.info.uri); - connection._registrations[item.name] = item; + Hoek.assert(item.multiple || !connection.registrations[item.name], 'Plugin', item.name, 'already registered in:', connection.info.uri); + connection.registrations[item.name] = { version: item.version }; + } + + if (!item.connections) { + selection.connections = null; } if (item.dependencies) { diff --git a/lib/schema.js b/lib/schema.js index 0a9de44fe..dd6ca899b 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -306,7 +306,8 @@ internals.plugin = internals.register.keys({ .when('pkg.name', { is: Joi.exist(), otherwise: Joi.required() }), version: Joi.string(), multiple: Joi.boolean().default(false), - dependencies: Joi.array().items(Joi.string()).single() + dependencies: Joi.array().items(Joi.string()).single(), + connections: Joi.boolean().default(true) }) .required() }).required(), diff --git a/lib/server.js b/lib/server.js index a87d0074c..b959a6e67 100755 --- a/lib/server.js +++ b/lib/server.js @@ -38,6 +38,7 @@ exports = module.exports = internals.Server = function (options) { this._events = new Events.EventEmitter(); // Server-only events this._dependencies = []; // Plugin dependencies + this._registrations = {}; // Tracks plugins registered before connection added this._heavy = new Heavy(this._settings.load); this._mime = new Mimos(this._settings.mime); this._replier = new Reply(); @@ -119,6 +120,12 @@ internals.Server.prototype.connection = function (options) { this._single(); } + var registrations = Object.keys(this._registrations); + for (var i = 0, il = registrations.length; i < il; ++i) { + var name = registrations[i]; + connection.registrations[name] = this._registrations[name]; + } + return this._clone([connection]); }; @@ -184,7 +191,9 @@ internals.Server.prototype.initialize = function (callback) { var connection = dependency.connections[s]; for (var d = 0, dl = dependency.deps.length; d < dl; ++d) { var dep = dependency.deps[d]; - Hoek.assert(connection._registrations[dep], 'Plugin', dependency.plugin, 'missing dependency', dep, 'in connection:', connection.info.uri); + if (!connection.registrations[dep]) { + return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); + } } } } diff --git a/test/plugin.js b/test/plugin.js index 262491fa5..86431c87d 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -343,7 +343,7 @@ describe('Plugin', function () { server.register(test, function (err) { expect(err).to.not.exist(); - expect(server.connections[0]._registrations.steve.version).to.equal('0.0.0'); + expect(server.connections[0].registrations.steve.version).to.equal('0.0.0'); server.inject('/', function (res) { expect(res.result).to.equal(require('../package.json').version); @@ -842,7 +842,11 @@ describe('Plugin', function () { server.register(a, function (err) { - done(); + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); }); }); }); @@ -886,7 +890,11 @@ describe('Plugin', function () { server.register(a, function (err) { - done(); + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); }); }); }); @@ -931,7 +939,11 @@ describe('Plugin', function () { server.register(a, function (err) { - done(); + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); }); }); }); @@ -976,6 +988,127 @@ describe('Plugin', function () { server.register(a, function (err) { + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + }); + }); + }); + + it('errors when dependency loaded before connection was added', function (done) { + + var a = function (srv, options, next) { + + return next(); + }; + + a.attributes = { + name: 'a', + dependencies: 'b' + }; + + var b = function (srv, options, next) { + + return next(); + }; + + b.attributes = { + name: 'b' + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.exist(); + expect(err.message).to.equal('Plugin a missing dependency b in connection: ' + server.info.uri); + done(); + }); + }); + }); + }); + + it('set dependency on previously loaded connectionless plugin', function (done) { + + var a = function (srv, options, next) { + + return next(); + }; + + a.attributes = { + name: 'a', + dependencies: 'b' + }; + + var b = function (srv, options, next) { + + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + }); + }); + + it('allows multiple connectionless plugin', function (done) { + + var a = function (srv, options, next) { + + return next(); + }; + + a.attributes = { + name: 'a', + dependencies: 'b' + }; + + var b = function (srv, options, next) { + + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false, + multiple: true + }; + + var server = new Hapi.Server(); + server.connection(); + server.register([b, b], function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); done(); }); }); @@ -1744,11 +1877,12 @@ describe('Plugin', function () { server.connection(); server.register(test, function (err) { - expect(function () { + server.initialize(function (err) { - server.initialize(Hoek.ignore); - }).to.throw('Plugin test missing dependency none in connection: ' + server.info.uri); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Plugin test missing dependency none in connection: ' + server.info.uri); + done(); + }); }); }); @@ -1768,11 +1902,12 @@ describe('Plugin', function () { server.connection(); server.register(test, function (err) { - expect(function () { + server.initialize(function (err) { - server.initialize(Hoek.ignore); - }).to.throw('Plugin test missing dependency none in connection: ' + server.info.uri); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Plugin test missing dependency none in connection: ' + server.info.uri); + done(); + }); }); }); @@ -1782,11 +1917,12 @@ describe('Plugin', function () { server.connection({ port: 80, host: 'localhost' }); server.register([internals.plugins.deps1, internals.plugins.deps3], function (err) { - expect(function () { + server.initialize(function (err) { - server.initialize(Hoek.ignore); - }).to.throw('Plugin deps1 missing dependency deps2 in connection: http://localhost:80'); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Plugin deps1 missing dependency deps2 in connection: ' + server.info.uri); + done(); + }); }); }); @@ -1854,11 +1990,12 @@ describe('Plugin', function () { server.connection({ port: 80, host: 'localhost' }); server.register(a, function (err) { - expect(function () { + server.initialize(function (err) { - server.initialize(Hoek.ignore); - }).to.throw('Plugin b missing dependency c in connection: http://localhost:80'); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Plugin b missing dependency c in connection: ' + server.info.uri); + done(); + }); }); }); @@ -1887,11 +2024,12 @@ describe('Plugin', function () { server.connection({ port: 80, host: 'localhost' }); server.register(a, function (err) { - expect(function () { + server.initialize(function (err) { - server.initialize(Hoek.ignore); - }).to.throw('Plugin b missing dependency c in connection: http://localhost:80'); - done(); + expect(err).to.exist(); + expect(err.message).to.equal('Plugin b missing dependency c in connection: ' + server.info.uri); + done(); + }); }); }); }); diff --git a/test/request.js b/test/request.js index 748d37ccb..e4d167a86 100755 --- a/test/request.js +++ b/test/request.js @@ -1431,7 +1431,7 @@ describe('Request', function () { setTimeout(function () { s.emit('end'); - }, 60); + }, 65); }; var timer = new Hoek.Bench(); From c34a556216d7433c961028bd650cc6606a183ba8 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 15:45:58 -0700 Subject: [PATCH 0035/1139] CORS merge mode. Closes #2733 --- API.md | 3 ++- lib/route.js | 6 ++--- lib/schema.js | 2 +- lib/transmit.js | 62 ++++++++++++++++++++++++++++-------------------- test/response.js | 2 +- test/transmit.js | 22 ++++++++++++++++- 6 files changed, 64 insertions(+), 33 deletions(-) diff --git a/API.md b/API.md index 52f8861a0..cde0e1be8 100755 --- a/API.md +++ b/API.md @@ -2050,7 +2050,8 @@ following options: - `credentials` - if `true`, allows user credentials to be sent ('Access-Control-Allow-Credentials'). Defaults to `false`. - `override` - if `false`, preserves existing CORS headers set manually before the - response is sent. Defaults to `true`. + response is sent. If set to `'merge'`, appends the configured values to the manually set + headers. Defaults to `true`. - `files` - defines the behavior for accessing files: - `relativeTo` - determines the folder relative paths are resolved against. diff --git a/lib/route.js b/lib/route.js index d2243c896..e4e68102f 100755 --- a/lib/route.js +++ b/lib/route.js @@ -154,9 +154,9 @@ exports = module.exports = internals.Route = function (options, connection, real this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); var cors = this.settings.cors; - cors._headers = cors.headers.concat(cors.additionalHeaders).join(', '); - cors._methods = cors.methods.concat(cors.additionalMethods).join(', '); - cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(', '); + cors._headers = cors.headers.concat(cors.additionalHeaders).join(','); + cors._methods = cors.methods.concat(cors.additionalMethods).join(','); + cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(','); if (cors.origin.length) { cors._origin = { diff --git a/lib/schema.js b/lib/schema.js index dd6ca899b..2de940a6e 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -72,7 +72,7 @@ internals.routeBase = Joi.object({ exposedHeaders: Joi.array(), additionalExposedHeaders: Joi.array(), credentials: Joi.boolean(), - override: Joi.boolean() + override: Joi.boolean().allow('merge') }) .allow(null, false, true), files: Joi.object({ diff --git a/lib/transmit.js b/lib/transmit.js index b7e1b6a7c..03cc46e53 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -355,38 +355,48 @@ internals.cors = function (response) { var request = response.request; var cors = request.route.settings.cors; - if (cors) { - if (cors._origin && - (!response.headers['access-control-allow-origin'] || cors.override)) { - - if (cors.matchOrigin) { - response.vary('origin'); - if (internals.matchOrigin(request.headers.origin, cors)) { - response._header('access-control-allow-origin', request.headers.origin); - } - else if (cors.isOriginExposed) { - response._header('access-control-allow-origin', cors._origin.any ? '*' : cors._origin.qualifiedString); - } - } - else if (cors._origin.any) { - response._header('access-control-allow-origin', '*'); + if (!cors) { + return; + } + + if (cors._origin && + (!response.headers['access-control-allow-origin'] || cors.override)) { + + if (cors.matchOrigin) { + response.vary('origin'); + if (internals.matchOrigin(request.headers.origin, cors)) { + response._header('access-control-allow-origin', request.headers.origin); } - else { - response._header('access-control-allow-origin', cors._origin.qualifiedString); + else if (cors.isOriginExposed) { + response._header('access-control-allow-origin', cors._origin.any ? '*' : cors._origin.qualifiedString); } } + else if (cors._origin.any) { + response._header('access-control-allow-origin', '*'); + } + else { + response._header('access-control-allow-origin', cors._origin.qualifiedString); + } + } - response._header('access-control-max-age', cors.maxAge, { override: cors.override }); - response._header('access-control-allow-methods', cors._methods, { override: cors.override }); - response._header('access-control-allow-headers', cors._headers, { override: cors.override }); + var config = { override: !!cors.override }; // Value can be 'merge' + response._header('access-control-max-age', cors.maxAge, { override: cors.override }); - if (cors._exposedHeaders.length !== 0) { - response._header('access-control-expose-headers', cors._exposedHeaders, { override: cors.override }); - } + if (cors.credentials) { + response._header('access-control-allow-credentials', 'true', { override: cors.override }); + } - if (cors.credentials) { - response._header('access-control-allow-credentials', 'true', { override: cors.override }); - } + // Appended headers + + if (cors.override === 'merge') { + config.append = true; + } + + response._header('access-control-allow-methods', cors._methods, config); + response._header('access-control-allow-headers', cors._headers, config); + + if (cors._exposedHeaders.length !== 0) { + response._header('access-control-expose-headers', cors._exposedHeaders, config); } }; diff --git a/test/response.js b/test/response.js index e53580f64..29b600a7b 100755 --- a/test/response.js +++ b/test/response.js @@ -68,7 +68,7 @@ describe('Response', function () { expect(res.headers['content-type']).to.equal('text/plain; something=something, charset=ISO-8859-1'); expect(res.headers['access-control-allow-origin']).to.equal('*'); expect(res.headers['access-control-allow-credentials']).to.not.exist(); - expect(res.headers['access-control-allow-methods']).to.equal('GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS'); + expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); expect(res.headers['set-cookie']).to.deep.equal(['abc=123', 'sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'test=123', 'empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'always=present']); expect(res.headers.vary).to.equal('x-control,origin'); expect(res.headers.combo).to.equal('o-k'); diff --git a/test/transmit.js b/test/transmit.js index 4781ea43d..9f04846bf 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2220,7 +2220,7 @@ describe('transmission', function () { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers['access-control-allow-methods']).to.equal('GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS'); + expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); done(); }); }); @@ -2285,6 +2285,26 @@ describe('transmission', function () { }); }); + it('merges CORS origin header when override is merge', function (done) { + + var handler = function (request, reply) { + + return reply('ok').header('access-control-allow-methods', 'something'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { additionalMethods: ['xyz'], override: 'merge' } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject('/', function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-methods']).to.equal('something,GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,xyz'); + done(); + }); + }); + it('returns no CORS headers when route CORS disabled', function (done) { var handler = function (request, reply) { From e0d66e80de3f817675c682ff6efad80e4d252d3b Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 15:50:51 -0700 Subject: [PATCH 0036/1139] expose plugin registration. Closes #2777 --- API.md | 10 ++++++++++ lib/plugin.js | 1 + 2 files changed, 11 insertions(+) diff --git a/API.md b/API.md index cde0e1be8..63617b836 100755 --- a/API.md +++ b/API.md @@ -12,6 +12,7 @@ - [`server.mime`](#servermime) - [`server.plugins`](#serverplugins) - [`server.realm`](#serverrealm) + - [`server.registrations`](#serverregistrations) - [`server.root`](#serverroot) - [`server.settings`](#serversettings) - [`server.version`](#serverversion) @@ -427,6 +428,15 @@ exports.register = function (server, options, next) { }; ``` +#### `server.registrations` + +When the server contains exactly one connection, `registrations` is an object where each key is a +registered plugin name and value contains: +- `version` - the plugin version. + +When the server contains more than one connection, each [`server.connections`](#serverconnections) +array member provides its own `connection.registrations`. + #### `server.root` The root server object containing all the connections and the root server methods (e.g. `start()`, diff --git a/lib/plugin.js b/lib/plugin.js index 0ad3bc94c..866092530 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -100,6 +100,7 @@ internals.Plugin.prototype._single = function () { this.listener = this.connections[0].listener; this.lookup = internals.lookup; this.match = internals.match; + this.registrations = this.connections[0].registrations; }; From b9471359dc34265a81c0a6b32316ce53f21165e6 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 16:50:38 -0700 Subject: [PATCH 0037/1139] Assert when adding after() after init. Closes #2812 --- lib/plugin.js | 2 ++ test/plugin.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/plugin.js b/lib/plugin.js index 866092530..156266f82 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -406,6 +406,8 @@ internals.Plugin.prototype.ext = function (event, func, options) { // Server extensions + Hoek.assert(event !== 'onPreStart' || this.root._state === 'stopped', 'Cannot add onPreStart (after) extension after the server was initialized'); + this.root._extensions[event] = this.root._extensions[event] || new Topo(); this.root._extensions[event].add(nodes, settings); }; diff --git a/test/plugin.js b/test/plugin.js index 86431c87d..7500fa3cc 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1442,6 +1442,22 @@ describe('Plugin', function () { }); }); }); + + it('errors when added after initialization', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.initialize(function (err) { + + expect(function () { + + server.after(function () { }); + }).to.throw('Cannot add onPreStart (after) extension after the server was initialized'); + + done(); + }); + }); }); describe('auth', function () { From 79baeb0f10bae39899621741b1f32d7ab5028662 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 18:23:38 -0700 Subject: [PATCH 0038/1139] Load plugin once. Closes #2761. Closes #2797 --- API.md | 8 +- lib/plugin.js | 35 +++++++-- lib/schema.js | 7 +- test/plugin.js | 201 +++++++++++++++++++++++++++++++++++++++++++++++- test/request.js | 27 +++---- 5 files changed, 247 insertions(+), 31 deletions(-) diff --git a/API.md b/API.md index 63617b836..7dfeef4f8 100755 --- a/API.md +++ b/API.md @@ -1378,17 +1378,19 @@ Registers a plugin where: - an object with the following: - `register` - the plugin registration function. - `options` - optional options passed to the registration function when called. - - `select`, `routes` - optional plugin-specific registration options as defined below. + - `once`, `select`, `routes` - optional plugin-specific registration options as defined below. - `options` - optional registration options (different from the options passed to the registration function): - - `select` - a string or array of string labels used to pre-select connections for plugin - registration. + - `once` - if `true`, the registration is skipped for any connection already registered with. + Cannot be used with plugin options. Defaults to `false`. - `routes` - modifiers applied to each route added by the plugin: - `prefix` - string added as prefix to any route path (must begin with `'/'`). If a plugin registers a child plugin the `prefix` is passed on to the child or is added in front of the child-specific prefix. - `vhost` - virtual host string (or array of strings) applied to every route. The outer-most `vhost` overrides the any nested configuration. + - `select` - a string or array of string labels used to pre-select connections for plugin + registration. - `callback` - the callback function with signature `function(err)` where: - `err` - an error returned from the registration function. Note that exceptions thrown by the registration function are not handled by the framework. diff --git a/lib/plugin.js b/lib/plugin.js index 156266f82..04471c716 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -226,6 +226,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback dependencies: attributes.dependencies, connections: attributes.connections, options: { + once: plugin.once !== undefined ? plugin.once : options.once, routes: { prefix: plugin.routes.prefix || options.routes.prefix, vhost: plugin.routes.vhost || options.routes.vhost @@ -249,18 +250,40 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback // Protect against multiple registrations if (!item.connections) { - Hoek.assert(item.multiple || !self.root._registrations[item.name], 'Plugin', item.name, 'already registered'); - self.root._registrations[item.name] = { version: item.version }; + if (self.root._registrations[item.name]) { + if (item.options.once) { + return next(); + } + + Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered'); + } + else { + self.root._registrations[item.name] = { version: item.version }; + } } - for (var j = 0, jl = selection.connections.length; j < jl; ++j) { + var connections = []; + var originalCount = selection.connections.length; + for (var j = 0, jl = originalCount; j < jl; ++j) { var connection = selection.connections[j]; - Hoek.assert(item.multiple || !connection.registrations[item.name], 'Plugin', item.name, 'already registered in:', connection.info.uri); + if (connection.registrations[item.name]) { + if (item.options.once) { + continue; + } + + Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); + } + connection.registrations[item.name] = { version: item.version }; + connections.push(connection); } - if (!item.connections) { - selection.connections = null; + selection.connections = (item.connections ? connections : null); + if (item.options.once && + !selection.connections.length && + originalCount) { + + return next(); // All the connections already registered } if (item.dependencies) { diff --git a/lib/schema.js b/lib/schema.js index 2de940a6e..1cc6711c7 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -284,6 +284,7 @@ internals.methodObject = Joi.object({ internals.register = Joi.object({ + once: Joi.boolean(), routes: Joi.object({ prefix: Joi.string().regex(/^\/.+/), vhost: internals.vhost @@ -310,6 +311,8 @@ internals.plugin = internals.register.keys({ connections: Joi.boolean().default(true) }) .required() - }).required(), + }) + .required(), options: Joi.any() -}); +}) + .without('once', 'options'); diff --git a/test/plugin.js b/test/plugin.js index 7500fa3cc..1460bdbee 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1115,6 +1115,203 @@ describe('Plugin', function () { }); }); + it('register a plugin once per connection', function (done) { + + var a = function (srv, options, next) { + + server.register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + return next(); + }; + + b.attributes = { + name: 'b' + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(2); + done(); + }); + }); + }); + }); + + it('register a plugin once per connection (skip empty selection)', function (done) { + + var a = function (srv, options, next) { + + server.register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + return next(); + }; + + b.attributes = { + name: 'b' + }; + + var server = new Hapi.Server(); + server.connection(); + server.connection(); + server.register(b, function (err) { + + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + }); + }); + + it('register a connectionless plugin once', function (done) { + + var a = function (srv, options, next) { + + server.register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + }); + }); + + it('register a connectionless plugin once (plugin options)', function (done) { + + var a = function (srv, options, next) { + + server.register({ register: b, once: true }, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + }); + }); + + it('throws when once used with plugin options', function (done) { + + var a = function (srv, options, next) { + + return next(); + }; + + a.attributes = { + name: 'a' + }; + + var server = new Hapi.Server(); + server.connection(); + expect(function () { + + server.register({ register: a, options: {}, once: true }, function (err) { }); + }).to.throw(); + + done(); + }); + it('throws when dependencies is an object', function (done) { var a = function (srv, options, next) { @@ -1131,7 +1328,7 @@ describe('Plugin', function () { expect(function () { - server.register(a, function () {}); + server.register(a, function () { }); }).to.throw(); done(); }); @@ -1152,7 +1349,7 @@ describe('Plugin', function () { expect(function () { - server.register(a, function () {}); + server.register(a, function () { }); }).to.throw(); done(); }); diff --git a/test/request.js b/test/request.js index e4d167a86..67de49ab1 100755 --- a/test/request.js +++ b/test/request.js @@ -1419,45 +1419,36 @@ describe('Request', function () { it('does not return an error when server is responding when the timeout occurs', function (done) { + var ended = false; var respondingHandler = function (request, reply) { var s = new Stream.PassThrough(); reply(s); - for (var i = 10000; i > 0; --i) { - s.write(i.toString()); - } + s.write(new Buffer(10240)); setTimeout(function () { + ended = true; s.emit('end'); - }, 65); + }, 150); }; var timer = new Hoek.Bench(); var server = new Hapi.Server(); - server.connection({ routes: { timeout: { server: 50 } } }); - server.route({ method: 'GET', path: '/responding', config: { handler: respondingHandler } }); + server.connection({ routes: { timeout: { server: 100 } } }); + server.route({ method: 'GET', path: '/', config: { handler: respondingHandler } }); server.start(function (err) { expect(err).to.not.exist(); + Wreck.get(server.info.uri, {}, function (err, res, payload) { - var options = { - hostname: '127.0.0.1', - port: server.info.port, - path: '/responding', - method: 'GET' - }; - - var req = Http.request(options, function (res) { - - expect(timer.elapsed()).to.be.at.least(60); + expect(ended).to.be.true(); + expect(timer.elapsed()).to.be.at.least(150); expect(res.statusCode).to.equal(200); server.stop({ timeout: 1 }, done); }); - - req.write('\n'); }); }); From 15d2cc73de12b9e4fdf191564179997862a2a968 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 23:03:00 -0700 Subject: [PATCH 0039/1139] after() cleanup. Closes #2815 --- API.md | 11 ++++++----- lib/plugin.js | 12 ++++++++++-- test/plugin.js | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/API.md b/API.md index 7dfeef4f8..89631d867 100755 --- a/API.md +++ b/API.md @@ -16,7 +16,7 @@ - [`server.root`](#serverroot) - [`server.settings`](#serversettings) - [`server.version`](#serverversion) - - [`server.after(method, [dependencies])`](#serveraftermethod-dependencies) + - [`server.after(method, [options])`](#serveraftermethod-options) - [`server.auth.default(options)`](#serverauthdefaultoptions) - [`server.auth.scheme(name, scheme)`](#serverauthschemename-scheme) - [`server.auth.strategy(name, scheme, [mode], [options])`](#serverauthstrategyname-scheme-mode-options) @@ -467,7 +467,7 @@ var server = new Hapi.Server(); // server.version === '8.0.0' ``` -### `server.after(method, [dependencies])` +### `server.after(method, [options])` Adds a method to be called after all the plugin dependencies have been registered and before the server starts (only called if the server is started) where: @@ -477,9 +477,10 @@ server starts (only called if the server is started) where: and complete the registration process. The function signature is `function(err)` where: - `err` - internal error which is returned back via the [`server.start()`](#serverstartcallback) callback. -- `dependencies` - a string or array of string with the plugin names to call this method after - their `after()` methods. There is no requirement for the other [plugins](#plugins) to be - registered. Setting dependencies only arranges the after methods in the specified order. +- `options` - an optional object where: + - `after` - a string or array of string with the plugin names to call this method after + their `after()` methods. There is no requirement for the other [plugins](#plugins) to be + registered. Setting dependencies only arranges the after methods in the specified order. The `server.after()` method is identical to setting a server extension point on `'onPreStart'`. diff --git a/lib/plugin.js b/lib/plugin.js index 04471c716..be6d7b33e 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -301,9 +301,17 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback }; -internals.Plugin.prototype.after = function (method, dependencies) { +internals.Plugin.prototype.after = function (method, options) { - this.ext('onPreStart', method, { after: dependencies }); + options = options || {}; + + if (Array.isArray(options) || + typeof options === 'string') { // For backwards compatibility + + options = { after: options }; + } + + this.ext('onPreStart', method, options); }; diff --git a/test/plugin.js b/test/plugin.js index 1460bdbee..2776af179 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1546,6 +1546,43 @@ describe('Plugin', function () { expect(server.plugins.x).to.not.exist(); + var called = false; + server.after(function (srv, next) { + + expect(srv.plugins.x.a).to.equal('b'); + called = true; + return next(); + }, { after: 'x' }); + + server.register(x, function (err) { + + expect(err).to.not.exist(); + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(called).to.be.true(); + done(); + }); + }); + }); + + it('calls method after plugin (legacy)', function (done) { + + var x = function (srv, options, next) { + + srv.expose('a', 'b'); + return next(); + }; + + x.attributes = { + name: 'x' + }; + + var server = new Hapi.Server(); + server.connection(); + + expect(server.plugins.x).to.not.exist(); + var called = false; server.after(function (srv, next) { @@ -1596,7 +1633,7 @@ describe('Plugin', function () { called = true; return next(); - }, 'x'); + }, { after: 'x' }); server.initialize(function (err) { From 2f232985f163d64bb58bd37276692346b6bd502b Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 23:08:23 -0700 Subject: [PATCH 0040/1139] Clarify payload validation rules. Closes #2722 --- API.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index 89631d867..96cd7b6c0 100755 --- a/API.md +++ b/API.md @@ -2266,7 +2266,10 @@ following options: allowed: - `true` - any payload allowed (no validation performed). This is the default. - `false` - no payload allowed. - - a [Joi](http://github.com/hapijs/joi) validation object. + - a [Joi](http://github.com/hapijs/joi) validation object. Note that empty payloads + are represented by a `null` value. If a validation schema is provided and empty + payload are supported, it must be explicitly defined by setting the `payload` value + to a **joi** schema with `null` allowed (e.g. `Joi.object({ /* keys here */ }).allow('null')`). - a validation function using the signature `function(value, options, next)` where: - `value` - the object containing the payload object. - `options` - the server validation options. From a063abaa1ac5576a01c3a0b5de07a782a9d2712a Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 2 Oct 2015 23:19:34 -0700 Subject: [PATCH 0041/1139] Fix single connection bug. Closes #2813 --- lib/plugin.js | 33 +++++++++++++++++---------------- lib/server.js | 5 +---- test/plugin.js | 6 +++--- test/server.js | 18 ++++++++++++++++-- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index be6d7b33e..46a541eb6 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -70,16 +70,7 @@ exports = module.exports = internals.Plugin = function (server, connections, env } }; - if (this.connections.length === 1) { - this._single(); - } - else { - this.info = null; - this.inject = null; - this.listener = null; - this.lookup = null; - this.match = null; - } + this._single(); // Decorations @@ -95,12 +86,22 @@ Hoek.inherits(internals.Plugin, Kilt); internals.Plugin.prototype._single = function () { - this.info = this.connections[0].info; - this.inject = internals.inject; - this.listener = this.connections[0].listener; - this.lookup = internals.lookup; - this.match = internals.match; - this.registrations = this.connections[0].registrations; + if (this.connections.length === 1) { + this.info = this.connections[0].info; + this.inject = internals.inject; + this.listener = this.connections[0].listener; + this.lookup = internals.lookup; + this.match = internals.match; + this.registrations = this.connections[0].registrations; + } + else { + this.info = null; + this.inject = null; + this.listener = null; + this.lookup = null; + this.match = null; + this.registrations = null; + } }; diff --git a/lib/server.js b/lib/server.js index b959a6e67..2b789b5cb 100755 --- a/lib/server.js +++ b/lib/server.js @@ -115,10 +115,7 @@ internals.Server.prototype.connection = function (options) { var connection = new Connection(this, settings); this.connections.push(connection); this.addEmitter(connection); - - if (this.connections.length === 1) { - this._single(); - } + this._single(); var registrations = Object.keys(this._registrations); for (var i = 0, il = registrations.length; i < il; ++i) { diff --git a/test/plugin.js b/test/plugin.js index 2776af179..c2c71d414 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1029,7 +1029,7 @@ describe('Plugin', function () { server.initialize(function (err) { expect(err).to.exist(); - expect(err.message).to.equal('Plugin a missing dependency b in connection: ' + server.info.uri); + expect(err.message).to.equal('Plugin a missing dependency b in connection: ' + server.connections[1].info.uri); done(); }); }); @@ -1714,10 +1714,10 @@ describe('Plugin', function () { expect(err).to.not.exist(); - server.inject('/', function (res1) { + server.select('a').inject('/', function (res1) { expect(res1.statusCode).to.equal(401); - server.inject({ method: 'GET', url: '/', headers: { authorization: 'Basic ' + (new Buffer('john:12345', 'utf8')).toString('base64') } }, function (res2) { + server.select('a').inject({ method: 'GET', url: '/', headers: { authorization: 'Basic ' + (new Buffer('john:12345', 'utf8')).toString('base64') } }, function (res2) { expect(res2.statusCode).to.equal(200); expect(res2.result).to.equal('authenticated!'); diff --git a/test/server.js b/test/server.js index 1b90f39c0..2ccd14cba 100755 --- a/test/server.js +++ b/test/server.js @@ -153,8 +153,10 @@ describe('Server', function () { server.start(function (err) { expect(err).to.not.exist(); - server.connection({ port: server.info.port }); - server.connection({ port: server.info.port }); + var port = server.info.port; + + server.connection({ port: port }); + server.connection({ port: port }); server.stop(function (err) { expect(err).to.not.exist(); @@ -428,6 +430,18 @@ describe('Server', function () { expect(server.connections[0].settings.routes.security.xframe).to.equal('deny'); done(); }); + + it('decorates and clears single connection shortcuts', function (done) { + + var server = new Hapi.Server(); + expect(server.info).to.not.exist(); + server.connection(); + expect(server.info).to.exist(); + server.connection(); + expect(server.info).to.not.exist(); + + done(); + }); }); describe('load', { parallel: false }, function () { From 019732d5a2fb023ff988e6a051928bdddd90bc78 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 00:59:13 -0700 Subject: [PATCH 0042/1139] Small optimization --- lib/plugin.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/plugin.js b/lib/plugin.js index 46a541eb6..a04e4fa63 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -274,8 +274,10 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); } + else { + connection.registrations[item.name] = { version: item.version }; + } - connection.registrations[item.name] = { version: item.version }; connections.push(connection); } From 9ed674c1a16a4800431b532d08b52d1a04cdc046 Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Sat, 3 Oct 2015 11:10:17 -0400 Subject: [PATCH 0043/1139] Fix joi empty payload example in docs. --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 96cd7b6c0..07d8b48ec 100755 --- a/API.md +++ b/API.md @@ -2269,7 +2269,7 @@ following options: - a [Joi](http://github.com/hapijs/joi) validation object. Note that empty payloads are represented by a `null` value. If a validation schema is provided and empty payload are supported, it must be explicitly defined by setting the `payload` value - to a **joi** schema with `null` allowed (e.g. `Joi.object({ /* keys here */ }).allow('null')`). + to a **joi** schema with `null` allowed (e.g. `Joi.object({ /* keys here */ }).allow(null)`). - a validation function using the signature `function(value, options, next)` where: - `value` - the object containing the payload object. - `options` - the server validation options. From 0f7af21e92d0d105a9534d25aa6180df63eef9e1 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 09:08:58 -0700 Subject: [PATCH 0044/1139] Adjust test timing --- API.md | 4 ++-- lib/plugin.js | 3 +-- test/request.js | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 96cd7b6c0..b7de0e2f2 100755 --- a/API.md +++ b/API.md @@ -417,8 +417,8 @@ used by the framework when adding routes, extensions, and other properties. - `bind` The `server.realm` object should be considered read-only and must not be changed directly except -for the `plugins` property can be directly manipulated by the plugins (each setting its own under -`plugins[name]`). +for the `plugins` property which can be directly manipulated by each plugin, setting its properties +inside `plugins[name]`. ```js exports.register = function (server, options, next) { diff --git a/lib/plugin.js b/lib/plugin.js index a04e4fa63..1e7fa82d8 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -46,8 +46,7 @@ exports = module.exports = internals.Plugin = function (server, connections, env bind: undefined, files: { relativeTo: undefined - }, - plugin: null + } } }; diff --git a/test/request.js b/test/request.js index 67de49ab1..a508f3d44 100755 --- a/test/request.js +++ b/test/request.js @@ -489,11 +489,11 @@ describe('Request', function () { }; return reply(stream); - }, 10); + }, 100); }; var server = new Hapi.Server(); - server.connection({ routes: { timeout: { server: 5 } } }); + server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/', From 5c6cef2f08dfca013e689109a2bff8b0fd0138e5 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 12:09:20 -0700 Subject: [PATCH 0045/1139] Fix connectionless plugins. Closes #2817 --- API.md | 8 +- lib/plugin.js | 48 +++++--- lib/server.js | 22 +++- test/plugin.js | 292 ++++++++++++++++++++++++++++++++++++++++++++++++- test/state.js | 3 +- 5 files changed, 342 insertions(+), 31 deletions(-) diff --git a/API.md b/API.md index 52235649a..d27d524fb 100755 --- a/API.md +++ b/API.md @@ -866,7 +866,9 @@ Used within a plugin to declares a required dependency on other [plugins](#plugi - `err` - internal error condition, which is returned back via the [`server.start()`](#serverstartcallback) callback. -The `after` method is identical to setting a server extension point on `'onPreStart'`. +The `after` method is identical to setting a server extension point on `'onPreStart'`. Connectionless +plugins (those with `attributes.connections` set to `false`) can only depend on other connectionless +plugins (server initialization will fail even of the dependency is loaded but is not connectionless). ```js exports.register = function (server, options, next) { @@ -1383,7 +1385,9 @@ Registers a plugin where: - `options` - optional registration options (different from the options passed to the registration function): - `once` - if `true`, the registration is skipped for any connection already registered with. - Cannot be used with plugin options. Defaults to `false`. + Cannot be used with plugin options. If the plugin does not have a `connections` attribute set + to `false` and the registration selection is empty, registration will be skipped as no connections + are available to register once. Defaults to `false`. - `routes` - modifiers applied to each route added by the plugin: - `prefix` - string added as prefix to any route path (must begin with `'/'`). If a plugin registers a child plugin the `prefix` is passed on to the child or is added in front of diff --git a/lib/plugin.js b/lib/plugin.js index 1e7fa82d8..dda64923b 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -85,7 +85,9 @@ Hoek.inherits(internals.Plugin, Kilt); internals.Plugin.prototype._single = function () { - if (this.connections.length === 1) { + if (this.connections && + this.connections.length === 1) { + this.info = this.connections[0].info; this.inject = internals.inject; this.listener = this.connections[0].listener; @@ -123,6 +125,8 @@ internals.Plugin.prototype._select = function (labels, plugin) { if (labels && labels.length) { // Captures both empty arrays and empty strings + Hoek.assert(this.connections, 'Cannot select inside a connectionless plugin'); + connections = []; for (var i = 0, il = this.connections.length; i < il; ++i) { var connection = this.connections[i]; @@ -263,30 +267,33 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback } var connections = []; - var originalCount = selection.connections.length; - for (var j = 0, jl = originalCount; j < jl; ++j) { - var connection = selection.connections[j]; - if (connection.registrations[item.name]) { - if (item.options.once) { - continue; + if (selection.connections) { + for (var j = 0, jl = selection.connections.length; j < jl; ++j) { + var connection = selection.connections[j]; + if (connection.registrations[item.name]) { + if (item.options.once) { + continue; + } + + Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); + } + else { + connection.registrations[item.name] = { version: item.version }; } - Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); - } - else { - connection.registrations[item.name] = { version: item.version }; + connections.push(connection); } - connections.push(connection); + if (item.options.once && + item.connections && + !connections.length) { + + return next(); // All the connections already registered + } } selection.connections = (item.connections ? connections : null); - if (item.options.once && - !selection.connections.length && - originalCount) { - - return next(); // All the connections already registered - } + selection._single(); if (item.dependencies) { selection.dependency(item.dependencies); @@ -521,6 +528,7 @@ internals.Plugin.prototype.route = function (options) { Hoek.assert(arguments.length === 1, 'Method requires a single object argument or a single array of objects'); Hoek.assert(typeof options === 'object', 'Invalid route options'); + Hoek.assert(this.connections, 'Cannot add route from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add a route without any connections'); this._apply('route', Connection.prototype._route, [options, this.realm]); @@ -535,6 +543,8 @@ internals.Plugin.prototype.state = function (name, options) { internals.Plugin.prototype.table = function (host) { + Hoek.assert(this.connections, 'Cannot request routing table from a connectionless plugin'); + var table = []; for (var i = 0, il = this.connections.length; i < il; ++i) { var connection = this.connections[i]; @@ -547,6 +557,7 @@ internals.Plugin.prototype.table = function (host) { internals.Plugin.prototype._apply = function (type, func, args) { + Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); for (var i = 0, il = this.connections.length; i < il; ++i) { @@ -557,6 +568,7 @@ internals.Plugin.prototype._apply = function (type, func, args) { internals.Plugin.prototype._applyChild = function (type, child, func, args) { + Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); for (var i = 0, il = this.connections.length; i < il; ++i) { diff --git a/lib/server.js b/lib/server.js index 2b789b5cb..d6ab082ad 100755 --- a/lib/server.js +++ b/lib/server.js @@ -184,12 +184,22 @@ internals.Server.prototype.initialize = function (callback) { for (var i = 0, il = this._dependencies.length; i < il; ++i) { var dependency = this._dependencies[i]; - for (var s = 0, sl = dependency.connections.length; s < sl; ++s) { - var connection = dependency.connections[s]; - for (var d = 0, dl = dependency.deps.length; d < dl; ++d) { - var dep = dependency.deps[d]; - if (!connection.registrations[dep]) { - return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); + if (dependency.connections) { + for (var s = 0, sl = dependency.connections.length; s < sl; ++s) { + var connection = dependency.connections[s]; + for (var d = 0, dl = dependency.deps.length; d < dl; ++d) { + var dep = dependency.deps[d]; + if (!connection.registrations[dep]) { + return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); + } + } + } + } + else { + for (d = 0, dl = dependency.deps.length; d < dl; ++d) { + dep = dependency.deps[d]; + if (!this._registrations[dep]) { + return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep)); } } } diff --git a/test/plugin.js b/test/plugin.js index c2c71d414..c16c440b3 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1115,11 +1115,80 @@ describe('Plugin', function () { }); }); + it('register nested connectionless plugins', function (done) { + + var a = function (srv, options, next) { + + srv.register(b, function (err) { + + return next(); + }); + }; + + a.attributes = { + name: 'a', + connections: false + }; + + var b = function (srv, options, next) { + + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(a, function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + + it('throws when nested connectionless plugins select', function (done) { + + var a = function (srv, options, next) { + + expect(function () { + + srv.register(b, { select: 'none' }, function (err) { }); + }).to.throw('Cannot select inside a connectionless plugin'); + return next(); + }; + + a.attributes = { + name: 'a', + connections: false + }; + + var b = function (srv, options, next) { + + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(a, function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + it('register a plugin once per connection', function (done) { var a = function (srv, options, next) { - server.register(b, { once: true }, function (err) { + srv.register(b, { once: true }, function (err) { expect(err).to.not.exist(); return next(); @@ -1162,7 +1231,75 @@ describe('Plugin', function () { var a = function (srv, options, next) { - server.register(b, { once: true }, function (err) { + srv.select('none').register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + return next(); + }; + + b.attributes = { + name: 'b' + }; + + var server = new Hapi.Server(); + server.connection(); + server.connection(); + server.register(b, function (err) { + + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + }); + }); + + it('register a connectionless plugin once (empty selection)', function (done) { + + var count = 0; + var b = function (srv, options, next) { + + ++count; + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.connection(); + server.select('none').register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + + it('register a plugin once per connection (no selection left)', function (done) { + + var a = function (srv, options, next) { + + srv.register(b, { once: true }, function (err) { expect(err).to.not.exist(); return next(); @@ -1201,11 +1338,35 @@ describe('Plugin', function () { }); }); + it('register a plugin once (empty selection)', function (done) { + + var count = 0; + var b = function (srv, options, next) { + + ++count; + return next(); + }; + + b.attributes = { + name: 'b' + }; + + var server = new Hapi.Server(); + server.connection(); + server.connection(); + server.select('none').register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(0); + done(); + }); + }); + it('register a connectionless plugin once', function (done) { var a = function (srv, options, next) { - server.register(b, { once: true }, function (err) { + srv.register(b, { once: true }, function (err) { expect(err).to.not.exist(); return next(); @@ -1250,7 +1411,7 @@ describe('Plugin', function () { var a = function (srv, options, next) { - server.register({ register: b, once: true }, function (err) { + srv.register({ register: b, once: true }, function (err) { expect(err).to.not.exist(); return next(); @@ -1291,6 +1452,32 @@ describe('Plugin', function () { }); }); + it('register a connectionless plugin once (first time)', function (done) { + + var count = 0; + var b = function (srv, options, next) { + + ++count; + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.connection(); + server.register(b, { once: true }, function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + it('throws when once used with plugin options', function (done) { var a = function (srv, options, next) { @@ -2161,6 +2348,103 @@ describe('Plugin', function () { }); }); + it('fails to register single plugin with dependencies (connectionless)', function (done) { + + var test = function (srv, options, next) { + + srv.dependency('none'); + return next(); + }; + + test.attributes = { + name: 'test', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(test, function (err) { + + server.initialize(function (err) { + + expect(err).to.exist(); + expect(err.message).to.equal('Plugin test missing dependency none'); + done(); + }); + }); + }); + + it('fails to register plugin with multiple dependencies (connectionless)', function (done) { + + var test = function (srv, options, next) { + + srv.dependency(['b', 'none']); + return next(); + }; + + test.attributes = { + name: 'test', + connections: false + }; + + var b = function (srv, options, next) { + + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register([test, b], function (err) { + + server.initialize(function (err) { + + expect(err).to.exist(); + expect(err.message).to.equal('Plugin test missing dependency none'); + done(); + }); + }); + }); + + it('register plugin with multiple dependencies (connectionless)', function (done) { + + var test = function (srv, options, next) { + + srv.dependency(['b']); + return next(); + }; + + test.attributes = { + name: 'test', + connections: false + }; + + var b = function (srv, options, next) { + + return next(); + }; + + b.attributes = { + name: 'b', + connections: false + }; + + var server = new Hapi.Server(); + server.connection(); + server.register([test, b], function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + }); + it('fails to register multiple plugins with dependencies', function (done) { var server = new Hapi.Server(); diff --git a/test/state.js b/test/state.js index 0251ebf9f..013af8167 100755 --- a/test/state.js +++ b/test/state.js @@ -60,7 +60,8 @@ describe('state', function () { var server = new Hapi.Server(); server.connection(); - server.inject({ method: 'GET', url: '/', headers: { cookie: 'vab', clearInvalid: true } }, function (res) { + server.state('vab', { encoding: 'base64json', clearInvalid: true }); + server.inject({ method: 'GET', url: '/', headers: { cookie: 'vab' } }, function (res) { expect(res.statusCode).to.equal(400); expect(res.headers['set-cookie']).to.not.exists(); From e157ba6181d9088d49b6212c941683186366697f Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 12:32:05 -0700 Subject: [PATCH 0046/1139] Document adding connection from plugin. Closes #2754 --- API.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/API.md b/API.md index d27d524fb..eff1f7412 100755 --- a/API.md +++ b/API.md @@ -816,6 +816,51 @@ var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin // admin.connections.length === 1 ``` +Special care must be taken when adding connections inside a plugin `register()` method. Because +plugin connections selection happens before registration, any connection added inside the plugin +will not be included in the `server.connections` array. For this reason, the `server` object +provided to the `register()` method does not support the `connection()` method. Instead, the +`server.root.connection()` method must be called. + +However, connectionless plugins (plugins with `attributes.connections` set to `false`) provide +a powerful bridge and allow plugins to add connections. This is done by using the `register()` +`server` argument only for adding the new connection using `server.root.connection()` and then +using the return value from the `connection()` method (which is another `server` with the new +connection selected) to perform any other actions that should include the new connection (only). + +While this pattern can be accomplished without setting the plugin to connectionless mode, it +makes the code safer and easier to maintain because it will prevent trying to use the `server` +argument to manage the new connection and will throw an exception (instead of just failing +silently). + +For example: +```js +exports.register = function (server, options, next) { + + // Use the 'server' argument to add a new connection + + var srv = server.root.connection(); + + // Use the 'srv' return value to manage the new connection + + srv.route({ + path: '/', + method: 'GET', + handler: function (request, reply) { + + return reply('hello'); + } + }); + + return next(); +}; + +exports.register.attributes = { + name: 'example', + connections: false +}; +``` + ### `server.decorate(type, property, method)` Extends various framework interfaces with custom methods where: From fc2f49de5895963af494d5fa3a3f7010ffb294b5 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 12:32:56 -0700 Subject: [PATCH 0047/1139] 10.2.1 --- npm-shrinkwrap.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 08a4d36f7..9b162ad7c 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.2.0", + "version": "10.2.1", "dependencies": { "accept": { "version": "1.1.0" diff --git a/package.json b/package.json index d4b981345..967e0f76a 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.2.0", + "version": "10.2.1", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From 25f6d1bac3b0876c47af29a7c11b7cb1c349b762 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 12:36:06 -0700 Subject: [PATCH 0048/1139] Update API.md --- API.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index eff1f7412..9b6a0d9aa 100755 --- a/API.md +++ b/API.md @@ -835,15 +835,15 @@ silently). For example: ```js -exports.register = function (server, options, next) { +exports.register = function (app, options, next) { - // Use the 'server' argument to add a new connection + // Use the 'app' argument to add a new connection - var srv = server.root.connection(); + var server = app.root.connection(); - // Use the 'srv' return value to manage the new connection + // Use the 'server' return value to manage the new connection - srv.route({ + server.route({ path: '/', method: 'GET', handler: function (request, reply) { From 9a8ab527f25859296394e216a396657aea91db39 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 12:39:43 -0700 Subject: [PATCH 0049/1139] Update API.md --- API.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index 9b6a0d9aa..1102ff148 100755 --- a/API.md +++ b/API.md @@ -835,11 +835,11 @@ silently). For example: ```js -exports.register = function (app, options, next) { +exports.register = function (srv, options, next) { - // Use the 'app' argument to add a new connection + // Use the 'srv' argument to add a new connection - var server = app.root.connection(); + var server = srv.root.connection(); // Use the 'server' return value to manage the new connection From e7994395247c3cedb757203072baaf1cee34e8c2 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 17:04:40 -0700 Subject: [PATCH 0050/1139] add connection inside plugin. Closes #2754 --- API.md | 13 +++++++------ README.md | 2 +- lib/plugin.js | 4 ++++ lib/server.js | 22 ++++++++++++---------- npm-shrinkwrap.json | 2 +- package.json | 2 +- test/plugin.js | 38 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 64 insertions(+), 19 deletions(-) diff --git a/API.md b/API.md index 1102ff148..c895e6650 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 10.2.x API Reference +# 10.3.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) @@ -819,19 +819,20 @@ var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin Special care must be taken when adding connections inside a plugin `register()` method. Because plugin connections selection happens before registration, any connection added inside the plugin will not be included in the `server.connections` array. For this reason, the `server` object -provided to the `register()` method does not support the `connection()` method. Instead, the -`server.root.connection()` method must be called. +provided to the `register()` method does not support the `connection()` method. However, connectionless plugins (plugins with `attributes.connections` set to `false`) provide a powerful bridge and allow plugins to add connections. This is done by using the `register()` -`server` argument only for adding the new connection using `server.root.connection()` and then +`server` argument only for adding the new connection using `server.connection()` and then using the return value from the `connection()` method (which is another `server` with the new connection selected) to perform any other actions that should include the new connection (only). While this pattern can be accomplished without setting the plugin to connectionless mode, it makes the code safer and easier to maintain because it will prevent trying to use the `server` argument to manage the new connection and will throw an exception (instead of just failing -silently). +silently). Without setting the plugin to connectionless mode, you must use +`server.root.connection()` which will return a `server` object scoped for the root realm, not +the current plugin. For example: ```js @@ -839,7 +840,7 @@ exports.register = function (srv, options, next) { // Use the 'srv' argument to add a new connection - var server = srv.root.connection(); + var server = srv.connection(); // Use the 'server' return value to manage the new connection diff --git a/README.md b/README.md index eb7d8f03b..9ae268536 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse) authentication, and other essential facilities for building web and services applications. **hapi** enables developers to focus on writing reusable application logic in a highly modular and prescriptive approach. -Development version: **10.2.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) +Development version: **10.3.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) [![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi) For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the diff --git a/lib/plugin.js b/lib/plugin.js index dda64923b..ee60756a7 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -299,6 +299,10 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback selection.dependency(item.dependencies); } + if (!item.connections) { + selection.connection = self.connection; + } + // Register item.register(selection, item.pluginOptions || {}, next); diff --git a/lib/server.js b/lib/server.js index d6ab082ad..def902593 100755 --- a/lib/server.js +++ b/lib/server.js @@ -106,24 +106,26 @@ internals.Server.prototype._createCache = function (options) { internals.Server.prototype.connection = function (options) { - var settings = Hoek.applyToDefaultsWithShallow(this._settings.connections, options || {}, ['listener', 'routes.bind']); - settings.routes.cors = Hoek.applyToDefaults(this._settings.connections.routes.cors || Defaults.cors, settings.routes.cors); - settings.routes.security = Hoek.applyToDefaults(this._settings.connections.routes.security || Defaults.security, settings.routes.security); + var root = this.root; // Explicitly use the root reference (for plugin invocation) + + var settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']); + settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors); + settings.routes.security = Hoek.applyToDefaults(root._settings.connections.routes.security || Defaults.security, settings.routes.security); settings = Schema.assert('connection', settings); // Applies validation changes (type cast) - var connection = new Connection(this, settings); - this.connections.push(connection); - this.addEmitter(connection); - this._single(); + var connection = new Connection(root, settings); + root.connections.push(connection); + root.addEmitter(connection); + root._single(); - var registrations = Object.keys(this._registrations); + var registrations = Object.keys(root._registrations); for (var i = 0, il = registrations.length; i < il; ++i) { var name = registrations[i]; - connection.registrations[name] = this._registrations[name]; + connection.registrations[name] = root._registrations[name]; } - return this._clone([connection]); + return this._clone([connection]); // Use this for active realm }; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 9b162ad7c..2d7de7419 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.2.1", + "version": "10.3.0", "dependencies": { "accept": { "version": "1.1.0" diff --git a/package.json b/package.json index 967e0f76a..c776502a1 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.2.1", + "version": "10.3.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" diff --git a/test/plugin.js b/test/plugin.js index c16c440b3..b51ba377d 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -2123,6 +2123,44 @@ describe('Plugin', function () { }); }); + describe('connection()', function () { + + it('returns a selection object within the same realm', function (done) { + + var plugin = function (srv, options, next) { + + srv.bind({ some: 'context' }); + var con = srv.connection(); + con.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(this.some); + } + }); + + return next(); + }; + + plugin.attributes = { + name: 'test', + connections: false + }; + + var server = new Hapi.Server(); + server.register(plugin, function (err) { + + expect(err).to.not.exist(); + server.connections[0].inject('/', function (res) { + + expect(res.result).to.equal('context'); + done(); + }); + }); + }); + }); + describe('decorate()', function () { it('decorates request', function (done) { From 7afc730bfde2281dea98bc9e0458a0479a7c89e4 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 17:24:11 -0700 Subject: [PATCH 0051/1139] Plugin once attribute. Closes #2818 --- API.md | 5 ++++- README.md | 2 +- lib/plugin.js | 5 +++-- lib/schema.js | 3 ++- npm-shrinkwrap.json | 2 +- package.json | 2 +- test/plugin.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index c895e6650..a0d8dd37b 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 10.3.x API Reference +# 10.4.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) @@ -1875,6 +1875,9 @@ The plugin function must include an `attributes` function property with the foll - `connections` - if `false`, does not allow the plugin to call server APIs that modify the connections such as adding a route or configuring state. This flag allows the plugin to be registered before connections are added and to pass dependency requirements. Defaults to `true`. +- `once` - if `true`, will only register the plugin once per connection (or once per server for a + connectionless plugin). If set, overrides the `once` option passed to `server.register()`. + Defaults to `undefined` (registration will be based on the `server.register()` option `once`). ```js var register = function (server, options, next) { diff --git a/README.md b/README.md index 9ae268536..19ea711bb 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse) authentication, and other essential facilities for building web and services applications. **hapi** enables developers to focus on writing reusable application logic in a highly modular and prescriptive approach. -Development version: **10.3.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) +Development version: **10.4.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) [![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi) For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the diff --git a/lib/plugin.js b/lib/plugin.js index ee60756a7..b63fca426 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -183,7 +183,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback version: '1.1.1', multiple: false, dependencies: [], - connections: false + connections: false, + once: true }; var item = { @@ -230,7 +231,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback dependencies: attributes.dependencies, connections: attributes.connections, options: { - once: plugin.once !== undefined ? plugin.once : options.once, + once: attributes.once || (plugin.once !== undefined ? plugin.once : options.once), routes: { prefix: plugin.routes.prefix || options.routes.prefix, vhost: plugin.routes.vhost || options.routes.vhost diff --git a/lib/schema.js b/lib/schema.js index 1cc6711c7..ae5da1a42 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -308,7 +308,8 @@ internals.plugin = internals.register.keys({ version: Joi.string(), multiple: Joi.boolean().default(false), dependencies: Joi.array().items(Joi.string()).single(), - connections: Joi.boolean().default(true) + connections: Joi.boolean().default(true), + once: Joi.boolean().valid(true) }) .required() }) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 2d7de7419..866cf10f3 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.3.0", + "version": "10.4.0", "dependencies": { "accept": { "version": "1.1.0" diff --git a/package.json b/package.json index c776502a1..33a0eef36 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.3.0", + "version": "10.4.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" diff --git a/test/plugin.js b/test/plugin.js index b51ba377d..82364cbfa 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1407,6 +1407,52 @@ describe('Plugin', function () { }); }); + it('register a connectionless plugin once (plugin attributes)', function (done) { + + var a = function (srv, options, next) { + + srv.register(b, function (err) { + + expect(err).to.not.exist(); + return next(); + }); + }; + + a.attributes = { + name: 'a' + }; + + var count = 0; + var b = function (srv, options, next) { + + ++count; + expect(srv.connections).to.be.null(); + return next(); + }; + + b.attributes = { + name: 'b', + connections: false, + once: true + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(b, function (err) { + + server.connection(); + server.register(a, function (err) { + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(count).to.equal(1); + done(); + }); + }); + }); + }); + it('register a connectionless plugin once (plugin options)', function (done) { var a = function (srv, options, next) { From cab99e086c4b7ec52167200f1a526303f05bf664 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 21:14:49 -0700 Subject: [PATCH 0052/1139] Skip empty extention points in lifecycle. Closes #2823 --- lib/connection.js | 28 ++++++++++++++++++----- lib/handler.js | 14 +++++------- lib/request.js | 18 ++++++++++----- lib/route.js | 30 +++++++++++++++++-------- test/connection.js | 46 ++++++++++++++++++++++++++++++++++++++ test/request.js | 55 ++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 156 insertions(+), 35 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 07ae10136..70f8c18c1 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -78,6 +78,7 @@ exports = module.exports = internals.Connection = function (server, options) { this.states = new Statehood.Definitions(this.settings.state); this.auth = new Auth(this); this._router = new Call.Router(this.settings.router); + this._routes = []; // An array of all route objects this._defaultRoutes(); this.plugins = {}; // Registered plugin APIs by plugin name @@ -331,6 +332,10 @@ internals.Connection.prototype._ext = function (event, nodes, options) { this._extensions[event] = this._extensions[event] || new Topo(); this._extensions[event].add(nodes, options); + + for (var i = 0, il = this._routes.length; i < il; ++i) { + this._routes[i].rebuild(); + } }; @@ -367,12 +372,14 @@ internals.Connection.prototype._addRoute = function (config, realm) { route.fingerprint = record.fingerprint; route.params = record.params; } + + this._routes.push(route); }; internals.Connection.prototype._defaultRoutes = function () { - this._router.special('notFound', new Route({ + var notFound = new Route({ method: 'notFound', path: '/{p*}', config: { @@ -382,9 +389,12 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(Boom.notFound()); } } - }, this, this.server.realm)); + }, this, this.server.realm); - this._router.special('badRequest', new Route({ + this._router.special('notFound', notFound); + this._routes.push(notFound); + + var badRequest = new Route({ method: 'badRequest', path: '/{p*}', config: { @@ -394,10 +404,13 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(Boom.badRequest()); } } - }, this, this.server.realm)); + }, this, this.server.realm); + + this._router.special('badRequest', badRequest); + this._routes.push(badRequest); if (this.settings.routes.cors) { - this._router.special('options', new Route({ + var optionsRoute = new Route({ path: '/{p*}', method: 'options', config: { @@ -408,6 +421,9 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(); } } - }, this, this.server.realm)); + }, this, this.server.realm); + + this._router.special('options', optionsRoute); + this._routes.push(optionsRoute); } }; diff --git a/lib/handler.js b/lib/handler.js index 972738167..1ce2fd714 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -292,18 +292,16 @@ internals.pre = function (pre) { exports.invoke = function (request, event, callback) { - var exts = request.connection._extensions[event]; - if (!exts) { - return Hoek.nextTick(callback)(); - } - - if (event === 'onPreResponse') { - request._protect.reset(); + if (typeof event === 'string') { + event = request.connection._extensions[event]; + if (!event) { + return Hoek.nextTick(callback)(); + } } request._protect.run(callback, function (exit) { - Items.serial(exts.nodes, function (ext, next) { + Items.serial(event.nodes, function (ext, next) { var reply = request.server._replier.interface(request, ext.realm, next); var bind = (ext.bind || ext.realm.settings.bind); diff --git a/lib/request.js b/lib/request.js index 5bf9bbc6a..cf6cdb7f4 100755 --- a/lib/request.js +++ b/lib/request.js @@ -370,15 +370,15 @@ internals.Request.prototype._execute = function () { return next(Boom.internal('Already closed')); // Error is not used } - if (typeof func === 'string') { // Extension point + if (typeof func !== 'function') { // Extension point return Handler.invoke(self, func, next); } - func(self, next); + return func(self, next); }, function (err) { - self._reply(err); + return self._reply(err); }); }); }; @@ -414,7 +414,9 @@ internals.Request.prototype._reply = function (exit) { this._setResponse(Response.wrap(exit, this)); } - Handler.invoke(this, 'onPreResponse', function (err) { + this._protect.reset(); + + var transmit = function (err) { if (err) { // err can be valid response or error self._setResponse(Response.wrap(err, self)); @@ -424,7 +426,13 @@ internals.Request.prototype._reply = function (exit) { return self._finalize(); }); - }); + }; + + if (!this._route._onPreResponse) { + return transmit(); + } + + Handler.invoke(this, this._route._onPreResponse, transmit); }; diff --git a/lib/route.js b/lib/route.js index e4e68102f..83a1878ae 100755 --- a/lib/route.js +++ b/lib/route.js @@ -239,7 +239,10 @@ exports = module.exports = internals.Route = function (options, connection, real // Route lifecycle - this._cycle = this.lifecycle(); + this._cycle = null; + this._onPreResponse = null; + + this.rebuild(); }; @@ -256,7 +259,7 @@ internals.compileRule = function (rule) { }; -internals.Route.prototype.lifecycle = function () { +internals.Route.prototype.rebuild = function () { var cycle = []; @@ -270,7 +273,9 @@ internals.Route.prototype.lifecycle = function () { cycle.push(internals.state); } - cycle.push('onPreAuth'); + if (this.connection._extensions.onPreAuth) { + cycle.push(this.connection._extensions.onPreAuth); + } var authenticate = (this.settings.auth !== false); // Anything other than 'false' can still require authentication if (authenticate) { @@ -286,7 +291,9 @@ internals.Route.prototype.lifecycle = function () { } } - cycle.push('onPostAuth'); + if (this.connection._extensions.onPostAuth) { + cycle.push(this.connection._extensions.onPostAuth); + } if (this.settings.validate.headers) { cycle.push(Validation.headers); @@ -308,9 +315,15 @@ internals.Route.prototype.lifecycle = function () { cycle.push(Validation.payload); } - cycle.push('onPreHandler'); + if (this.connection._extensions.onPreHandler) { + cycle.push(this.connection._extensions.onPreHandler); + } + cycle.push(Handler.execute); // Must not call next() with an Error - cycle.push('onPostHandler'); // An error from here on will override any result set in handler() + + if (this.connection._extensions.onPostHandler) { + cycle.push(this.connection._extensions.onPostHandler); // An error from here on will override any result set in handler() + } if (this.settings.response && this.settings.response.sample !== 0) { @@ -318,9 +331,8 @@ internals.Route.prototype.lifecycle = function () { cycle.push(Validation.response); } - // 'onPreResponse' - - return cycle; + this._onPreResponse = this.connection._extensions.onPreResponse; + this._cycle = cycle; }; diff --git a/test/connection.js b/test/connection.js index 4a30e9b8c..da9b833b0 100755 --- a/test/connection.js +++ b/test/connection.js @@ -913,6 +913,52 @@ describe('Connection', function () { describe('ext()', function () { + it('executes along the request lifecycle', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.app.x += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + return reply(request.app.x + '6'); + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + it('supports adding an array of methods', function (done) { var server = new Hapi.Server(); diff --git a/test/request.js b/test/request.js index a508f3d44..a0f1d3a7f 100755 --- a/test/request.js +++ b/test/request.js @@ -326,7 +326,43 @@ describe('Request', function () { method: 'GET' }); - clientRequest.on('error', function () { /* NOP */ }); + clientRequest.on('error', Hoek.ignore); + clientRequest.end(); + }); + }); + + it('does not fail on abort (onPreHandler)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', handler: Hoek.ignore }); + + var clientRequest; + + server.ext('onPreHandler', function (request, reply) { + + clientRequest.abort(); + setTimeout(function () { + + reply.continue(); + setTimeout(function () { + + server.stop(done); + }, 10); + }, 10); + }); + + server.start(function (err) { + + expect(err).to.not.exist(); + + clientRequest = Http.request({ + hostname: 'localhost', + port: server.info.port, + method: 'GET' + }); + + clientRequest.on('error', Hoek.ignore); clientRequest.end(); }); }); @@ -368,7 +404,7 @@ describe('Request', function () { method: 'GET' }); - clientRequest.on('error', function () { /* NOP */ }); + clientRequest.on('error', Hoek.ignore); clientRequest.end(); }); }); @@ -1333,17 +1369,22 @@ describe('Request', function () { it('returns server error message when server timeout happens during request execution (and handler yields)', function (done) { - var slowHandler = function (request, reply) { + var handler = function (request, reply) { setTimeout(function () { - return reply('Slow'); - }, 30); + return reply(); + }, 20); }; var server = new Hapi.Server(); - server.connection({ routes: { timeout: { server: 2 } } }); - server.route({ method: 'GET', path: '/', config: { handler: slowHandler } }); + server.connection({ routes: { timeout: { server: 10 } } }); + server.route({ method: 'GET', path: '/', config: { handler: handler } }); + + server.ext('onPostHandler', function (request, reply) { + + return reply.continue(); + }); server.inject('/', function (res) { From e5d9cda0de303fe71cc3307eb731a272d0eb4fc9 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 06:48:45 -0700 Subject: [PATCH 0053/1139] Relax plugin schema. Closes #2822 --- lib/schema.js | 3 ++- test/plugin.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/schema.js b/lib/schema.js index ae5da1a42..e6a2f4226 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -316,4 +316,5 @@ internals.plugin = internals.register.keys({ .required(), options: Joi.any() }) - .without('once', 'options'); + .without('once', 'options') + .unknown(); diff --git a/test/plugin.js b/test/plugin.js index 82364cbfa..f1c17a066 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -563,6 +563,64 @@ describe('Plugin', function () { }); }); + it('ignores unknown plugin properties', function (done) { + + var a = { + register: function (srv, options, next) { + + srv.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply('ok'); + } + }); + return next(); + }, + other: {} + }; + + a.register.attributes = { name: 'a' }; + + var server = new Hapi.Server(); + server.connection(); + server.register(a, function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + + it('ignores unknown plugin properties (with options)', function (done) { + + var a = { + register: function (srv, options, next) { + + srv.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply('ok'); + } + }); + return next(); + }, + other: {} + }; + + a.register.attributes = { name: 'a' }; + + var server = new Hapi.Server(); + server.connection(); + server.register({ register: a }, function (err) { + + expect(err).to.not.exist(); + done(); + }); + }); + it('registers a child plugin with parent routes path prefix', function (done) { var server = new Hapi.Server(); From dc2c9a95973942655010d93020269212c9462363 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 3 Oct 2015 10:35:59 -0700 Subject: [PATCH 0054/1139] Refactor extensions --- lib/connection.js | 6 ++- lib/handler.js | 22 -------- lib/request.js | 128 ++++++++++++++++++++++++++------------------ lib/route.js | 38 ++++++++----- test/connection.js | 129 +++++++++++++++++++++++++++++++++++++++++++-- test/request.js | 16 +++--- 6 files changed, 242 insertions(+), 97 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 70f8c18c1..87c338dd7 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -333,8 +333,12 @@ internals.Connection.prototype._ext = function (event, nodes, options) { this._extensions[event] = this._extensions[event] || new Topo(); this._extensions[event].add(nodes, options); + if (event === 'onRequest') { + return; + } + for (var i = 0, il = this._routes.length; i < il; ++i) { - this._routes[i].rebuild(); + this._routes[i].rebuild(event, nodes, options); } }; diff --git a/lib/handler.js b/lib/handler.js index 1ce2fd714..56642bc38 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -288,25 +288,3 @@ internals.pre = function (pre) { pre.method.call(bind, request, reply); }; }; - - -exports.invoke = function (request, event, callback) { - - if (typeof event === 'string') { - event = request.connection._extensions[event]; - if (!event) { - return Hoek.nextTick(callback)(); - } - } - - request._protect.run(callback, function (exit) { - - Items.serial(event.nodes, function (ext, next) { - - var reply = request.server._replier.interface(request, ext.realm, next); - var bind = (ext.bind || ext.realm.settings.bind); - - ext.func.call(bind, request, reply); - }, exit); - }); -}; diff --git a/lib/request.js b/lib/request.js index cf6cdb7f4..bfa5da082 100755 --- a/lib/request.js +++ b/lib/request.js @@ -8,7 +8,6 @@ var Hoek = require('hoek'); var Items = require('items'); var Peekaboo = require('peekaboo'); var Qs = require('qs'); -var Handler = require('./handler'); var Protect = require('./protect'); var Response = require('./response'); var Transmit = require('./transmit'); @@ -310,76 +309,105 @@ internals.Request.prototype._execute = function () { // Execute onRequest extensions (can change request method and url) - Handler.invoke(this, 'onRequest', function (err) { + if (!this.connection._extensions.onRequest) { + return this._lifecycle(); + } - // Undecorate request + this._invoke(this.connection._extensions.onRequest, function (err) { - self.setUrl = undefined; - self.setMethod = undefined; + return self._lifecycle(err); + }); +}; - if (err) { - return self._reply(err); - } - if (!self.path || self.path[0] !== '/') { - return self._reply(Boom.badRequest('Invalid path')); - } +internals.Request.prototype._lifecycle = function (err) { + + var self = this; - // Lookup route + // Undecorate request - var match = self.connection._router.route(self.method, self.path, self.info.hostname); - if (!match.route.settings.isInternal || - self._allowInternals) { + this.setUrl = undefined; + this.setMethod = undefined; - self._route = match.route; - self.route = self._route.public; - } + if (err) { + return this._reply(err); + } + + if (!this.path || this.path[0] !== '/') { + return this._reply(Boom.badRequest('Invalid path')); + } + + // Lookup route + + var match = this.connection._router.route(this.method, this.path, this.info.hostname); + if (!match.route.settings.isInternal || + this._allowInternals) { + + this._route = match.route; + this.route = this._route.public; + } + + this.params = match.params; + this.paramsArray = match.paramsArray; - self.params = match.params; - self.paramsArray = match.paramsArray; + // Setup timeout - // Setup timeout + if (this.raw.req.socket && + this.route.settings.timeout.socket !== undefined) { - if (self.raw.req.socket && - self.route.settings.timeout.socket !== undefined) { + this.raw.req.socket.setTimeout(this.route.settings.timeout.socket || 0); // Value can be false or positive + } + + var serverTimeout = this.route.settings.timeout.server; + if (serverTimeout) { + serverTimeout = Math.floor(serverTimeout - this._bench.elapsed()); // Calculate the timeout from when the request was constructed + var timeoutReply = function () { - self.raw.req.socket.setTimeout(self.route.settings.timeout.socket || 0); // Value can be false or positive + self._log(['request', 'server', 'timeout', 'error'], { timeout: serverTimeout, elapsed: self._bench.elapsed() }); + self._reply(Boom.serverTimeout()); + }; + + if (serverTimeout <= 0) { + return timeoutReply(); } - var serverTimeout = self.route.settings.timeout.server; - if (serverTimeout) { - serverTimeout = Math.floor(serverTimeout - self._bench.elapsed()); // Calculate the timeout from when the request was constructed - var timeoutReply = function () { + this._serverTimeoutId = setTimeout(timeoutReply, serverTimeout); + } - self._log(['request', 'server', 'timeout', 'error'], { timeout: serverTimeout, elapsed: self._bench.elapsed() }); - self._reply(Boom.serverTimeout()); - }; + Items.serial(this._route._cycle, function (func, next) { - if (serverTimeout <= 0) { - return timeoutReply(); - } + if (self._isReplied || + self._isBailed) { - self._serverTimeoutId = setTimeout(timeoutReply, serverTimeout); + return next(Boom.internal('Already closed')); // Error is not used } - Items.serial(self._route._cycle, function (func, next) { + if (typeof func !== 'function') { // Extension point + return self._invoke(func, next); + } - if (self._isReplied || - self._isBailed) { + return func(self, next); + }, + function (err) { - return next(Boom.internal('Already closed')); // Error is not used - } + return self._reply(err); + }); +}; - if (typeof func !== 'function') { // Extension point - return Handler.invoke(self, func, next); - } - return func(self, next); - }, - function (err) { +internals.Request.prototype._invoke = function (event, callback) { - return self._reply(err); - }); + var self = this; + + this._protect.run(callback, function (exit) { + + Items.serial(event.nodes, function (ext, next) { + + var reply = self.server._replier.interface(self, ext.realm, next); + var bind = (ext.bind || ext.realm.settings.bind); + + ext.func.call(bind, self, reply); + }, exit); }); }; @@ -428,11 +456,11 @@ internals.Request.prototype._reply = function (exit) { }); }; - if (!this._route._onPreResponse) { + if (!this._route._extensions.onPreResponse) { return transmit(); } - Handler.invoke(this, this._route._onPreResponse, transmit); + this._invoke(this._route._extensions.onPreResponse, transmit); }; diff --git a/lib/route.js b/lib/route.js index 83a1878ae..f3c8955a4 100755 --- a/lib/route.js +++ b/lib/route.js @@ -5,6 +5,7 @@ var Catbox = require('catbox'); var Hoek = require('hoek'); var Joi = require('joi'); var Subtext = require('subtext'); +var Topo = require('topo'); var Auth = require('./auth'); var Defaults = require('./defaults'); var Handler = require('./handler'); @@ -239,9 +240,15 @@ exports = module.exports = internals.Route = function (options, connection, real // Route lifecycle - this._cycle = null; - this._onPreResponse = null; + this._extensions = { + onPreAuth: Hoek.clone(this.connection._extensions.onPreAuth), + onPostAuth: Hoek.clone(this.connection._extensions.onPostAuth), + onPreHandler: Hoek.clone(this.connection._extensions.onPreHandler), + onPostHandler: Hoek.clone(this.connection._extensions.onPostHandler), + onPreResponse: Hoek.clone(this.connection._extensions.onPreResponse) + }; + this._cycle = null; this.rebuild(); }; @@ -259,7 +266,14 @@ internals.compileRule = function (rule) { }; -internals.Route.prototype.rebuild = function () { +internals.Route.prototype.rebuild = function (event, nodes, options) { + + if (event) { + this._extensions[event] = this._extensions[event] || new Topo(); + this._extensions[event].add(nodes, options); + } + + // Builde lifecycle array var cycle = []; @@ -273,8 +287,8 @@ internals.Route.prototype.rebuild = function () { cycle.push(internals.state); } - if (this.connection._extensions.onPreAuth) { - cycle.push(this.connection._extensions.onPreAuth); + if (this._extensions.onPreAuth) { + cycle.push(this._extensions.onPreAuth); } var authenticate = (this.settings.auth !== false); // Anything other than 'false' can still require authentication @@ -283,7 +297,6 @@ internals.Route.prototype.rebuild = function () { } if (this.method !== 'get') { - cycle.push(internals.payload); if (authenticate) { @@ -291,8 +304,8 @@ internals.Route.prototype.rebuild = function () { } } - if (this.connection._extensions.onPostAuth) { - cycle.push(this.connection._extensions.onPostAuth); + if (this._extensions.onPostAuth) { + cycle.push(this._extensions.onPostAuth); } if (this.settings.validate.headers) { @@ -315,14 +328,14 @@ internals.Route.prototype.rebuild = function () { cycle.push(Validation.payload); } - if (this.connection._extensions.onPreHandler) { - cycle.push(this.connection._extensions.onPreHandler); + if (this._extensions.onPreHandler) { + cycle.push(this._extensions.onPreHandler); } cycle.push(Handler.execute); // Must not call next() with an Error - if (this.connection._extensions.onPostHandler) { - cycle.push(this.connection._extensions.onPostHandler); // An error from here on will override any result set in handler() + if (this._extensions.onPostHandler) { + cycle.push(this._extensions.onPostHandler); // An error from here on will override any result set in handler() } if (this.settings.response && @@ -331,7 +344,6 @@ internals.Route.prototype.rebuild = function () { cycle.push(Validation.response); } - this._onPreResponse = this.connection._extensions.onPreResponse; this._cycle = cycle; }; diff --git a/test/connection.js b/test/connection.js index da9b833b0..7b9923b97 100755 --- a/test/connection.js +++ b/test/connection.js @@ -913,7 +913,7 @@ describe('Connection', function () { describe('ext()', function () { - it('executes along the request lifecycle', function (done) { + it('executes along the request lifecycle (route last)', function (done) { var server = new Hapi.Server(); server.connection(); @@ -943,13 +943,136 @@ describe('Connection', function () { server.ext('onPostHandler', function (request, reply) { - request.app.x += '5'; + request.response.source += '5'; return reply.continue(); }); server.ext('onPreResponse', function (request, reply) { - return reply(request.app.x + '6'); + request.response.source += '6'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + + it('executes along the request lifecycle (route first)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.response.source += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + request.response.source += '6'; + return reply.continue(); + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + + it('executes along the request lifecycle (route middle)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.response.source += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + request.response.source += '6'; + return reply.continue(); }); server.inject('/', function (res) { diff --git a/test/request.js b/test/request.js index a0f1d3a7f..0715eab2e 100755 --- a/test/request.js +++ b/test/request.js @@ -618,7 +618,7 @@ describe('Request', function () { var req = null; server.on('request-error', function (request, err) { - errs++; + ++errs; expect(err).to.exist(); expect(err.message).to.equal('Uncaught error: boom'); req = request; @@ -631,19 +631,19 @@ describe('Request', function () { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { - - expect(res.statusCode).to.equal(500); - expect(res.result).to.exist(); - expect(res.result.message).to.equal('An internal server error occurred'); - }); - server.once('response', function () { expect(errs).to.equal(1); expect(req.getLog('error')[0].tags).to.deep.equal(['internal', 'implementation', 'error']); done(); }); + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(500); + expect(res.result).to.exist(); + expect(res.result.message).to.equal('An internal server error occurred'); + }); }); it('does not emit request-error when error is replaced with valid response', function (done) { From 7ee7cc1ba1abcae95bffc198323c1e140e0e617f Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sun, 4 Oct 2015 10:18:42 -0700 Subject: [PATCH 0055/1139] Allow unknown attributes --- lib/schema.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/schema.js b/lib/schema.js index e6a2f4226..986eca200 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -312,6 +312,7 @@ internals.plugin = internals.register.keys({ once: Joi.boolean().valid(true) }) .required() + .unknown() }) .required(), options: Joi.any() From b37dc3e2ed4e08621db5972c1013394741deb5ce Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sun, 4 Oct 2015 22:35:31 -0700 Subject: [PATCH 0056/1139] update topo. Closes #2826 --- npm-shrinkwrap.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 866cf10f3..454edf7ee 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -97,7 +97,7 @@ } }, "topo": { - "version": "1.0.3" + "version": "1.1.0" } } } diff --git a/package.json b/package.json index 33a0eef36..db3c22232 100755 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "shot": "1.x.x", "statehood": "2.x.x", "subtext": "2.x.x", - "topo": "1.x.x" + "topo": "^1.1.x" }, "devDependencies": { "bluebird": "2.x.x", From 6f8374f0ec8c4c2a4460e0c1acd662baa9c27c6a Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 5 Oct 2015 10:40:43 -0700 Subject: [PATCH 0057/1139] Support server.ext(events). Closes #2827 --- API.md | 91 +++++++++++++++++++++++++++++++++----------------- lib/plugin.js | 47 ++++++++++++++++++-------- lib/protect.js | 8 ++--- lib/route.js | 26 +++++++++++---- test/plugin.js | 55 ++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 54 deletions(-) diff --git a/API.md b/API.md index a0d8dd37b..2e260cea3 100755 --- a/API.md +++ b/API.md @@ -28,6 +28,7 @@ - [`server.dependency(dependencies, [after])`](#serverdependencydependencies-after) - [`server.expose(key, value)`](#serverexposekey-value) - [`server.expose(obj)`](#serverexposeobj) + - [`server.ext(events)`](#serverextevents) - [`server.ext(event, method, [options])`](#serverextevent-method-options) - [`server.handler(name, method)`](#serverhandlername-method) - [`server.initialize(callback)`](#serverinitializecallback) @@ -973,38 +974,68 @@ exports.register = function (server, options, next) { }; ``` -### `server.ext(event, method, [options])` +### `server.ext(events)` Registers an extension function in one of the available extension points where: -- `event` - the event name. -- `method` - a function or an array of functions to be executed at a specified point during request - processing. The required extension function signature is: - - server extension points: `function(server, next)` where: - - `server` - the server object. - - `next` - the continuation method with signature `function(err)`. - - `this` - the object provided via `options.bind` or the current active context set with - [`server.bind()`](#serverbindcontext). - - request extension points: `function(request, reply)` where: - - `request` - the [request object](#request-object). - - `reply` - the [reply interface](#reply-interface) which is used to return control back to the - framework. To continue normal execution of the [request lifecycle](#request-lifecycle), - `reply.continue()` must be called. To abort processing and return a response to the client, - call `reply(value)` where value is an error or any other valid response. - - `this` - the object provided via `options.bind` or the current active context set with - [`server.bind()`](#serverbindcontext). -- `options` - an optional object with the following: - - `before` - a string or array of strings of plugin names this method must execute before (on - the same event). Otherwise, extension methods are executed in the order added. - - `after` - a string or array of strings of plugin names this method must execute after (on the - same event). Otherwise, extension methods are executed in the order added. - - `bind` - a context object passed back to the provided method (via `this`) when called. - -The available extension points include the [request extension points](#request-lifecycle) as well -as the following server extension points: -- `'onPreStart'` - called before the connection listeners are started. -- `'onPostStart'` - called after the connection listeners are started. -- `'onPreStop'` - called before the connection listeners are stopped. -- `'onPostStop'` - called after the connection listeners are stopped. +- `events` - an object or array of objects with the following: + - `type` - the extension point event name. The available extension points include the + [request extension points](#request-lifecycle) as well as the following server extension points: + - `'onPreStart'` - called before the connection listeners are started. + - `'onPostStart'` - called after the connection listeners are started. + - `'onPreStop'` - called before the connection listeners are stopped. + - `'onPostStop'` - called after the connection listeners are stopped. + - `method` - a function or an array of functions to be executed at a specified point during request + processing. The required extension function signature is: + - server extension points: `function(server, next)` where: + - `server` - the server object. + - `next` - the continuation method with signature `function(err)`. + - `this` - the object provided via `options.bind` or the current active context set with + [`server.bind()`](#serverbindcontext). + - request extension points: `function(request, reply)` where: + - `request` - the [request object](#request-object). + - `reply` - the [reply interface](#reply-interface) which is used to return control back to the + framework. To continue normal execution of the [request lifecycle](#request-lifecycle), + `reply.continue()` must be called. To abort processing and return a response to the client, + call `reply(value)` where value is an error or any other valid response. + - `this` - the object provided via `options.bind` or the current active context set with + [`server.bind()`](#serverbindcontext). + - `options` - an optional object with the following: + - `before` - a string or array of strings of plugin names this method must execute before (on + the same event). Otherwise, extension methods are executed in the order added. + - `after` - a string or array of strings of plugin names this method must execute after (on the + same event). Otherwise, extension methods are executed in the order added. + - `bind` - a context object passed back to the provided method (via `this`) when called. + +```js +var Hapi = require('hapi'); +var server = new Hapi.Server(); +server.connection({ port: 80 }); + +server.ext({ + type: 'onRequest', + method: function (request, reply) { + + // Change all requests to '/test' + request.setUrl('/test'); + return reply.continue(); + } +}); + +var handler = function (request, reply) { + + return reply({ status: 'ok' }); +}; + +server.route({ method: 'GET', path: '/test', handler: handler }); +server.start(function (err) { }); + +// All requests will get routed to '/test' +``` + +### `server.ext(event, method, [options])` + +Registers a single extension event using the same properties as used in +[`server.ext(events)`](#serverextevents), but passed as arguments. ```js var Hapi = require('hapi'); diff --git a/lib/plugin.js b/lib/plugin.js index b63fca426..11d1046e0 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -36,6 +36,13 @@ exports = module.exports = internals.Plugin = function (server, connections, env this.version = Package.version; this.realm = typeof env !== 'string' ? env : { + _extensions: { + onPreAuth: null, + onPostAuth: null, + onPreHandler: null, + onPostHandler: null, + onPreResponse: null + }, modifiers: { route: {} }, @@ -418,11 +425,24 @@ internals.Plugin.prototype.expose = function (key, value) { }; -internals.Plugin.prototype.ext = function (event, func, options) { +internals.Plugin.prototype.ext = function (events) { // (event, method, options) -OR- (events) - var self = this; + if (typeof events === 'string') { + events = { type: arguments[0], method: arguments[1], options: arguments[2] }; + } + + events = [].concat(events); + for (var i = 0, il = events.length; i < il; ++i) { + this._ext(events[i]); + } +}; - options = options || {}; + +internals.Plugin.prototype._ext = function (event) { + + var type = event.type; + var methods = [].concat(event.method); + var options = event.options || {}; var settings = { before: options.before, @@ -431,30 +451,29 @@ internals.Plugin.prototype.ext = function (event, func, options) { }; var nodes = []; - ([].concat(func)).forEach(function (fn, i) { - + for (var i = 0, il = methods.length; i < il; ++i) { var node = { - func: fn, // Connection: function (request, next), Server: function (server, next) - realm: self.realm, + func: methods[i], // Connection: function (request, next), Server: function (server, next) + realm: this.realm, bind: options.bind, - plugin: self + plugin: this }; nodes.push(node); - }); + } // Connection extensions - if (this.root._extensions[event] === undefined) { - return this._apply('ext', Connection.prototype._ext, [event, nodes, settings]); + if (this.root._extensions[type] === undefined) { + return this._apply('ext', Connection.prototype._ext, [type, nodes, settings]); } // Server extensions - Hoek.assert(event !== 'onPreStart' || this.root._state === 'stopped', 'Cannot add onPreStart (after) extension after the server was initialized'); + Hoek.assert(type !== 'onPreStart' || this.root._state === 'stopped', 'Cannot add onPreStart (after) extension after the server was initialized'); - this.root._extensions[event] = this.root._extensions[event] || new Topo(); - this.root._extensions[event].add(nodes, settings); + this.root._extensions[type] = this.root._extensions[type] || new Topo(); + this.root._extensions[type].add(nodes, settings); }; diff --git a/lib/protect.js b/lib/protect.js index 03bfcf0f7..348b58a5d 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -48,10 +48,6 @@ internals.Protect.prototype.run = function (next, enter) { // enter var self = this; - if (!this.domain) { - return enter(finish); - } - var finish = function (arg0, arg1, arg2) { self._error = null; @@ -60,6 +56,10 @@ internals.Protect.prototype.run = function (next, enter) { // enter finish = Hoek.once(finish); + if (!this.domain) { + return enter(finish); + } + this._error = function (err) { return finish(Boom.badImplementation('Uncaught error', err)); diff --git a/lib/route.js b/lib/route.js index f3c8955a4..706b09978 100755 --- a/lib/route.js +++ b/lib/route.js @@ -61,12 +61,13 @@ exports = module.exports = internals.Route = function (options, connection, real this.server = connection.server; this.path = options.path; this.method = method; + this.realm = realm; this.public = { method: this.method, path: this.path, vhost: this.vhost, - realm: realm, + realm: this.realm, settings: this.settings }; @@ -241,11 +242,11 @@ exports = module.exports = internals.Route = function (options, connection, real // Route lifecycle this._extensions = { - onPreAuth: Hoek.clone(this.connection._extensions.onPreAuth), - onPostAuth: Hoek.clone(this.connection._extensions.onPostAuth), - onPreHandler: Hoek.clone(this.connection._extensions.onPreHandler), - onPostHandler: Hoek.clone(this.connection._extensions.onPostHandler), - onPreResponse: Hoek.clone(this.connection._extensions.onPreResponse) + onPreAuth: this._combineExtensions('onPreAuth'), + onPostAuth: this._combineExtensions('onPostAuth'), + onPreHandler: this._combineExtensions('onPreHandler'), + onPostHandler: this._combineExtensions('onPostHandler'), + onPreResponse: this._combineExtensions('onPreResponse') }; this._cycle = null; @@ -253,6 +254,19 @@ exports = module.exports = internals.Route = function (options, connection, real }; +internals.Route.prototype._combineExtensions = function (event) { + + var sources = [ + this.connection._extensions[event], + this.realm._extensions[event] + ]; + + var ext = new Topo(); + ext.merge(sources); + return (ext.nodes.length ? ext : null); +}; + + internals.compileRule = function (rule) { // null, undefined, true - anything allowed diff --git a/test/plugin.js b/test/plugin.js index f1c17a066..d2ebdff50 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -3062,6 +3062,61 @@ describe('Plugin', function () { }); }); }); + + it('extends server actions (single call)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var result = ''; + server.ext([ + { + type: 'onPreStart', + method: function (srv, next) { + + result += '1'; + return next(); + } + }, + { + type: 'onPostStart', + method: function (srv, next) { + + result += '2'; + return next(); + } + }, + { + type: 'onPreStop', + method: function (srv, next) { + + result += '3'; + return next(); + } + }, + { + type: 'onPreStop', + method: function (srv, next) { + + result += '4'; + return next(); + } + } + ]); + + server.start(function (err) { + + expect(err).to.not.exist(); + expect(result).to.equal('12'); + + server.stop(function (err) { + + expect(err).to.not.exist(); + expect(result).to.equal('1234'); + done(); + }); + }); + }); }); describe('handler()', function () { From ae2c280f48aa5ccd8ba60537521f5f4e98af11d2 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 5 Oct 2015 14:49:23 -0700 Subject: [PATCH 0058/1139] Route level and plugin level extensions. Closes #2566 --- API.md | 10 ++ lib/auth.js | 2 +- lib/connection.js | 47 ++++----- lib/ext.js | 68 +++++++++++++ lib/methods.js | 4 +- lib/plugin.js | 58 +++++------- lib/request.js | 8 +- lib/route.js | 59 ++++++++---- lib/schema.js | 25 ++++- lib/server.js | 30 +++--- test/connection.js | 169 --------------------------------- test/plugin.js | 74 +++++++++++++++ test/route.js | 232 +++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 509 insertions(+), 277 deletions(-) create mode 100755 lib/ext.js diff --git a/API.md b/API.md index 2e260cea3..da7e2b72d 100755 --- a/API.md +++ b/API.md @@ -1005,6 +1005,11 @@ Registers an extension function in one of the available extension points where: - `after` - a string or array of strings of plugin names this method must execute after (on the same event). Otherwise, extension methods are executed in the order added. - `bind` - a context object passed back to the provided method (via `this`) when called. + - `sandbox` - if set to `'plugin'` when adding a [request extension points](#request-lifecycle) + the extension is only added to routes defined by the current plugin. Not allowed when + configuring route-level extensions, or when adding server extensions. Defaults to + `'connection'` which applies to any route added to the connection the extension is added + to. ```js var Hapi = require('hapi'); @@ -2150,6 +2155,11 @@ following options: response is sent. If set to `'merge'`, appends the configured values to the manually set headers. Defaults to `true`. +- `ext` - defined a route-level [request extension points](#request-lifecycle) by setting + the option to an object with a key for each of the desired extension points (`'onRequest'` + is not allowed), and the value is the same as the [`server.ext(events)`](#serverextevents) + `event` argument. + - `files` - defines the behavior for accessing files: - `relativeTo` - determines the folder relative paths are resolved against. diff --git a/lib/auth.js b/lib/auth.js index 8af73e9dc..3c7d387b3 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -66,7 +66,7 @@ internals.Auth.prototype.strategy = function (name, scheme /*, mode, options */) internals.Auth.prototype.default = function (options) { - options = Schema.assert('auth', options, 'default strategy'); + options = Schema.apply('auth', options, 'default strategy'); Hoek.assert(!this.settings.default, 'Cannot set default strategy more than once'); var settings = Hoek.clone(options); // options can be reused diff --git a/lib/connection.js b/lib/connection.js index 87c338dd7..3cf0401d8 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -10,8 +10,8 @@ var Call = require('call'); var Hoek = require('hoek'); var Shot = require('shot'); var Statehood = require('statehood'); -var Topo = require('topo'); var Auth = require('./auth'); +var Ext = require('./ext'); var Route = require('./route'); @@ -65,12 +65,12 @@ exports = module.exports = internals.Connection = function (server, options) { this.registrations = {}; // Tracks plugin for dependency validation { name -> { version } } this._extensions = { - onRequest: null, // New request, before handing over to the router (allows changes to the request method, url, etc.) - onPreAuth: null, // After cookie parse and before authentication (skipped if state error) - onPostAuth: null, // After authentication (and payload processing) and before validation (skipped if auth or payload error) - onPreHandler: null, // After validation and body parsing, before route handler (skipped if auth or validation error) - onPostHandler: null, // After route handler returns, before sending response (skipped if onPreHandler not called) - onPreResponse: null // Before response is sent (always called) + onRequest: new Ext(this.server), + onPreAuth: new Ext(this.server), + onPostAuth: new Ext(this.server), + onPreHandler: new Ext(this.server), + onPostHandler: new Ext(this.server), + onPreResponse: new Ext(this.server) }; this._requestCounter = { value: internals.counter.min, min: internals.counter.min, max: internals.counter.max }; @@ -78,7 +78,6 @@ exports = module.exports = internals.Connection = function (server, options) { this.states = new Statehood.Definitions(this.settings.state); this.auth = new Auth(this); this._router = new Call.Router(this.settings.router); - this._routes = []; // An array of all route objects this._defaultRoutes(); this.plugins = {}; // Registered plugin APIs by plugin name @@ -326,20 +325,11 @@ internals.Connection.prototype.match = function (method, path, host) { }; -internals.Connection.prototype._ext = function (event, nodes, options) { +internals.Connection.prototype._ext = function (event) { - Hoek.assert(this._extensions[event] !== undefined, 'Unknown event type', event); - - this._extensions[event] = this._extensions[event] || new Topo(); - this._extensions[event].add(nodes, options); - - if (event === 'onRequest') { - return; - } - - for (var i = 0, il = this._routes.length; i < il; ++i) { - this._routes[i].rebuild(event, nodes, options); - } + var type = event.type; + Hoek.assert(this._extensions[type], 'Unknown event type', type); + this._extensions[type].add(event); }; @@ -365,9 +355,9 @@ internals.Connection.prototype._route = function (configs, realm) { }; -internals.Connection.prototype._addRoute = function (config, realm) { +internals.Connection.prototype._addRoute = function (config, plugin) { - var route = new Route(config, this, realm); // Do no use config beyond this point, use route members + var route = new Route(config, this, plugin); // Do no use config beyond this point, use route members var vhosts = [].concat(route.settings.vhost || '*'); for (var i = 0, il = vhosts.length; i < il; ++i) { @@ -376,8 +366,6 @@ internals.Connection.prototype._addRoute = function (config, realm) { route.fingerprint = record.fingerprint; route.params = record.params; } - - this._routes.push(route); }; @@ -393,10 +381,9 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(Boom.notFound()); } } - }, this, this.server.realm); + }, this, this.server); this._router.special('notFound', notFound); - this._routes.push(notFound); var badRequest = new Route({ method: 'badRequest', @@ -408,10 +395,9 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(Boom.badRequest()); } } - }, this, this.server.realm); + }, this, this.server); this._router.special('badRequest', badRequest); - this._routes.push(badRequest); if (this.settings.routes.cors) { var optionsRoute = new Route({ @@ -425,9 +411,8 @@ internals.Connection.prototype._defaultRoutes = function () { return reply(); } } - }, this, this.server.realm); + }, this, this.server); this._router.special('options', optionsRoute); - this._routes.push(optionsRoute); } }; diff --git a/lib/ext.js b/lib/ext.js new file mode 100755 index 000000000..03213865b --- /dev/null +++ b/lib/ext.js @@ -0,0 +1,68 @@ +// Load modules + +var Topo = require('topo'); + + +// Declare internals + +var internals = {}; + + +exports = module.exports = internals.Ext = function (server) { + + this._topo = new Topo(); + this._server = server; + this._routes = []; + + this.nodes = null; +}; + + +internals.Ext.prototype.add = function (event) { + + var methods = [].concat(event.method); + var options = event.options; + + for (var i = 0, il = methods.length; i < il; ++i) { + var settings = { + before: options.before, + after: options.after, + group: event.plugin.realm.plugin, + sort: this._server._extensionsSeq++ + }; + + var node = { + func: methods[i], // Connection: function (request, next), Server: function (server, next) + bind: options.bind, + plugin: event.plugin + }; + + this._topo.add(node, settings); + } + + this.nodes = this._topo.nodes; + + // Notify routes + + for (i = 0, il = this._routes.length; i < il; ++i) { + this._routes[i].rebuild(event); + } +}; + + +internals.Ext.prototype.merge = function (others) { + + var merge = []; + for (var i = 0, il = others.length; i < il; ++i) { + merge.push(others[i]._topo); + } + + this._topo.merge(merge); + this.nodes = (this._topo.nodes.length ? this._topo.nodes : null); +}; + + +internals.Ext.prototype.subscribe = function (route) { + + this._routes.push(route); +}; diff --git a/lib/methods.js b/lib/methods.js index 56440aa22..a1299a2ce 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -29,7 +29,7 @@ internals.Methods.prototype.add = function (name, method, options, realm) { var items = [].concat(name); for (var i = 0, il = items.length; i < il; ++i) { var item = items[i]; - item = Schema.assert('methodObject', item); + item = Schema.apply('methodObject', item); this._add(item.name, item.method, item.options, realm); } }; @@ -45,7 +45,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { Hoek.assert(name.match(exports.methodNameRx), 'Invalid name:', name); Hoek.assert(!Hoek.reach(this.methods, name, { functions: false }), 'Server method function name already exists:', name); - options = Schema.assert('method', options || {}, name); + options = Schema.apply('method', options || {}, name); var settings = Hoek.cloneWithShallow(options, ['bind']); settings.generateKey = settings.generateKey || internals.generateKey; diff --git a/lib/plugin.js b/lib/plugin.js index 11d1046e0..07f7356f7 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -4,8 +4,8 @@ var Catbox = require('catbox'); var Hoek = require('hoek'); var Items = require('items'); var Kilt = require('kilt'); -var Topo = require('topo'); var Connection = require('./connection'); +var Ext = require('./ext'); var Package = require('../package.json'); var Schema = require('./schema'); @@ -37,11 +37,11 @@ exports = module.exports = internals.Plugin = function (server, connections, env this.realm = typeof env !== 'string' ? env : { _extensions: { - onPreAuth: null, - onPostAuth: null, - onPreHandler: null, - onPostHandler: null, - onPreResponse: null + onPreAuth: new Ext(this.root), + onPostAuth: new Ext(this.root), + onPreHandler: new Ext(this.root), + onPostHandler: new Ext(this.root), + onPreResponse: new Ext(this.root) }, modifiers: { route: {} @@ -180,7 +180,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback options.routes.vhost = this.realm.modifiers.route.vhost || options.routes.vhost; } - options = Schema.assert('register', options); + options = Schema.apply('register', options); /* var register = function (server, options, next) { return next(); }; @@ -226,7 +226,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback plugin.register = plugin.register.register; } - plugin = Schema.assert('plugin', plugin); + plugin = Schema.apply('plugin', plugin); var attributes = plugin.register.attributes; var registration = { @@ -345,7 +345,7 @@ internals.Plugin.prototype.bind = function (context) { internals.Plugin.prototype.cache = function (options, _segment) { - options = Schema.assert('cachePolicy', options); + options = Schema.apply('cachePolicy', options); var segment = options.segment || _segment || (this.realm.plugin ? '!' + this.realm.plugin : ''); Hoek.assert(segment, 'Missing cache segment name'); @@ -431,7 +431,8 @@ internals.Plugin.prototype.ext = function (events) { // (event, method, o events = { type: arguments[0], method: arguments[1], options: arguments[2] }; } - events = [].concat(events); + events = Schema.apply('exts', events); + for (var i = 0, il = events.length; i < il; ++i) { this._ext(events[i]); } @@ -440,40 +441,29 @@ internals.Plugin.prototype.ext = function (events) { // (event, method, o internals.Plugin.prototype._ext = function (event) { + event = Hoek.shallow(event); + event.plugin = this; var type = event.type; - var methods = [].concat(event.method); - var options = event.options || {}; - var settings = { - before: options.before, - after: options.after, - group: this.realm.plugin - }; + if (!this.root._extensions[type]) { - var nodes = []; - for (var i = 0, il = methods.length; i < il; ++i) { - var node = { - func: methods[i], // Connection: function (request, next), Server: function (server, next) - realm: this.realm, - bind: options.bind, - plugin: this - }; + // Realm route extensions - nodes.push(node); - } + if (event.options.sandbox === 'plugin') { + Hoek.assert(this.realm._extensions[type], 'Unknown event type', type); + return this.realm._extensions[type].add(event); + } - // Connection extensions + // Connection route extensions - if (this.root._extensions[type] === undefined) { - return this._apply('ext', Connection.prototype._ext, [type, nodes, settings]); + return this._apply('ext', Connection.prototype._ext, [event]); } // Server extensions + Hoek.assert(!event.options.sandbox, 'Cannot specify sandbox option for server extension'); Hoek.assert(type !== 'onPreStart' || this.root._state === 'stopped', 'Cannot add onPreStart (after) extension after the server was initialized'); - - this.root._extensions[type] = this.root._extensions[type] || new Topo(); - this.root._extensions[type].add(nodes, settings); + this.root._extensions[type].add(event); }; @@ -555,7 +545,7 @@ internals.Plugin.prototype.route = function (options) { Hoek.assert(this.connections, 'Cannot add route from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add a route without any connections'); - this._apply('route', Connection.prototype._route, [options, this.realm]); + this._apply('route', Connection.prototype._route, [options, this]); }; diff --git a/lib/request.js b/lib/request.js index bfa5da082..c10de9c39 100755 --- a/lib/request.js +++ b/lib/request.js @@ -309,7 +309,7 @@ internals.Request.prototype._execute = function () { // Execute onRequest extensions (can change request method and url) - if (!this.connection._extensions.onRequest) { + if (!this.connection._extensions.onRequest.nodes) { return this._lifecycle(); } @@ -403,8 +403,8 @@ internals.Request.prototype._invoke = function (event, callback) { Items.serial(event.nodes, function (ext, next) { - var reply = self.server._replier.interface(self, ext.realm, next); - var bind = (ext.bind || ext.realm.settings.bind); + var reply = self.server._replier.interface(self, ext.plugin.realm, next); + var bind = (ext.bind || ext.plugin.realm.settings.bind); ext.func.call(bind, self, reply); }, exit); @@ -456,7 +456,7 @@ internals.Request.prototype._reply = function (exit) { }); }; - if (!this._route._extensions.onPreResponse) { + if (!this._route._extensions.onPreResponse.nodes) { return transmit(); } diff --git a/lib/route.js b/lib/route.js index 706b09978..3d02e929a 100755 --- a/lib/route.js +++ b/lib/route.js @@ -5,9 +5,9 @@ var Catbox = require('catbox'); var Hoek = require('hoek'); var Joi = require('joi'); var Subtext = require('subtext'); -var Topo = require('topo'); var Auth = require('./auth'); var Defaults = require('./defaults'); +var Ext = require('./ext'); var Handler = require('./handler'); var Validation = require('./validation'); var Schema = require('./schema'); @@ -18,10 +18,11 @@ var Schema = require('./schema'); var internals = {}; -exports = module.exports = internals.Route = function (options, connection, realm) { +exports = module.exports = internals.Route = function (options, connection, plugin) { // Apply plugin environment (before schema validation) + var realm = plugin.realm; if (realm.modifiers.route.vhost || realm.modifiers.route.prefix) { @@ -38,7 +39,7 @@ exports = module.exports = internals.Route = function (options, connection, real Hoek.assert(options.path === '/' || options.path[options.path.length - 1] !== '/' || !connection.settings.router.stripTrailingSlash, 'Path cannot end with a trailing slash when connection configured to strip:', options.method, options.path); Hoek.assert(/^[a-zA-Z0-9!#\$%&'\*\+\-\.^_`\|~]+$/.test(options.method), 'Invalid method name:', options.method, options.path); - options = Schema.assert('route', options, options.path); + options = Schema.apply('route', options, options.path); var handler = options.handler || options.config.handler; var method = options.method.toLowerCase(); @@ -51,7 +52,7 @@ exports = module.exports = internals.Route = function (options, connection, real base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind']); this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind']); this.settings.handler = handler; - this.settings = Schema.assert('routeConfig', this.settings, options.path); + this.settings = Schema.apply('routeConfig', this.settings, options.path); var socketTimeout = (this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket); Hoek.assert(!this.settings.timeout.server || !socketTimeout || this.settings.timeout.server < socketTimeout, 'Server timeout must be shorter than socket timeout:', options.path); @@ -61,13 +62,13 @@ exports = module.exports = internals.Route = function (options, connection, real this.server = connection.server; this.path = options.path; this.method = method; - this.realm = realm; + this.plugin = plugin; this.public = { method: this.method, path: this.path, vhost: this.vhost, - realm: this.realm, + realm: this.plugin.realm, settings: this.settings }; @@ -254,16 +255,30 @@ exports = module.exports = internals.Route = function (options, connection, real }; -internals.Route.prototype._combineExtensions = function (event) { +internals.Route.prototype._combineExtensions = function (type, subscribe) { - var sources = [ - this.connection._extensions[event], - this.realm._extensions[event] - ]; + var ext = new Ext(this.server); - var ext = new Topo(); - ext.merge(sources); - return (ext.nodes.length ? ext : null); + var events = this.settings.ext[type]; + if (events) { + for (var i = 0, il = events.length; i < il; ++i) { + var event = events[i]; + Hoek.assert(!event.options.sandbox, 'Cannot specify sandbox option for route extension'); + event = Hoek.shallow(event); + event.plugin = this.plugin; + ext.add(event); + } + } + + var connection = this.connection._extensions[type]; + var realm = this.plugin.realm._extensions[type]; + + ext.merge([connection, realm]); + + connection.subscribe(this); + realm.subscribe(this); + + return ext; }; @@ -280,11 +295,13 @@ internals.compileRule = function (rule) { }; -internals.Route.prototype.rebuild = function (event, nodes, options) { +internals.Route.prototype.rebuild = function (event) { if (event) { - this._extensions[event] = this._extensions[event] || new Topo(); - this._extensions[event].add(nodes, options); + this._extensions[event.type].add(event); + if (event.type === 'onPreResponse') { + return; + } } // Builde lifecycle array @@ -301,7 +318,7 @@ internals.Route.prototype.rebuild = function (event, nodes, options) { cycle.push(internals.state); } - if (this._extensions.onPreAuth) { + if (this._extensions.onPreAuth.nodes) { cycle.push(this._extensions.onPreAuth); } @@ -318,7 +335,7 @@ internals.Route.prototype.rebuild = function (event, nodes, options) { } } - if (this._extensions.onPostAuth) { + if (this._extensions.onPostAuth.nodes) { cycle.push(this._extensions.onPostAuth); } @@ -342,13 +359,13 @@ internals.Route.prototype.rebuild = function (event, nodes, options) { cycle.push(Validation.payload); } - if (this._extensions.onPreHandler) { + if (this._extensions.onPreHandler.nodes) { cycle.push(this._extensions.onPreHandler); } cycle.push(Handler.execute); // Must not call next() with an Error - if (this._extensions.onPostHandler) { + if (this._extensions.onPostHandler.nodes) { cycle.push(this._extensions.onPostHandler); // An error from here on will override any result set in handler() } diff --git a/lib/schema.js b/lib/schema.js index 986eca200..56563abb2 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -9,7 +9,7 @@ var Hoek = require('hoek'); var internals = {}; -exports.assert = function (type, options, message) { +exports.apply = function (type, options, message) { var result = Joi.validate(options, internals[type]); Hoek.assert(!result.error, 'Invalid', type, 'options', message ? '(' + message + ')' : '', result.error && result.error.annotate()); @@ -50,6 +50,21 @@ internals.auth = Joi.alternatives([ ]); +internals.event = Joi.object({ + method: Joi.array().items(Joi.func()).single(), + options: Joi.object({ + before: Joi.array().items(Joi.string()).single(), + after: Joi.array().items(Joi.string()).single(), + bind: Joi.any(), + sandbox: Joi.string().valid('connection', 'plugin') + }) + .default({}) +}); + + +internals.exts = Joi.array().items(internals.event.keys({ type: Joi.string().required() })).single(); + + internals.routeBase = Joi.object({ app: Joi.object().allow(null), auth: internals.auth.allow(false), @@ -75,6 +90,14 @@ internals.routeBase = Joi.object({ override: Joi.boolean().allow('merge') }) .allow(null, false, true), + ext: Joi.object({ + onPreAuth: Joi.array().items(internals.event).single(), + onPostAuth: Joi.array().items(internals.event).single(), + onPreHandler: Joi.array().items(internals.event).single(), + onPostHandler: Joi.array().items(internals.event).single(), + onPreResponse: Joi.array().items(internals.event).single() + }) + .default({}), files: Joi.object({ relativeTo: Joi.string().regex(/^([\/\.])|([A-Za-z]:\\)|(\\\\)/).required() }), diff --git a/lib/server.js b/lib/server.js index def902593..201ebdb91 100755 --- a/lib/server.js +++ b/lib/server.js @@ -9,6 +9,7 @@ var Items = require('items'); var Mimos = require('mimos'); var Connection = require('./connection'); var Defaults = require('./defaults'); +var Ext = require('./ext'); var Methods = require('./methods'); var Plugin = require('./plugin'); var Reply = require('./reply'); @@ -25,7 +26,7 @@ exports = module.exports = internals.Server = function (options) { Hoek.assert(this instanceof internals.Server, 'Server must be instantiated using new'); - options = Schema.assert('server', options || {}); + options = Schema.apply('server', options || {}); this._settings = Hoek.applyToDefaultsWithShallow(Defaults.server, options, ['connections.routes.bind']); this._settings.connections = Hoek.applyToDefaultsWithShallow(Defaults.connection, this._settings.connections || {}, ['routes.bind']); @@ -49,11 +50,12 @@ exports = module.exports = internals.Server = function (options) { this._registring = false; // true while register() is waiting for plugin callbacks this._state = 'stopped'; // 'stopped', 'initializing', 'initialized', 'starting', 'started', 'stopping', 'invalid' + this._extensionsSeq = 0; // Used to keep absolute order of extensions based on the order added across locations this._extensions = { - onPreStart: null, - onPostStart: null, - onPreStop: null, - onPostStop: null + onPreStart: new Ext(this), + onPostStart: new Ext(this), + onPreStop: new Ext(this), + onPostStop: new Ext(this) }; if (options.cache) { @@ -112,7 +114,7 @@ internals.Server.prototype.connection = function (options) { settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors); settings.routes.security = Hoek.applyToDefaults(root._settings.connections.routes.security || Defaults.security, settings.routes.security); - settings = Schema.assert('connection', settings); // Applies validation changes (type cast) + settings = Schema.apply('connection', settings); // Applies validation changes (type cast) var connection = new Connection(root, settings); root.connections.push(connection); @@ -335,16 +337,16 @@ internals.Server.prototype.stop = function (/* [options], callback */) { }; -internals.Server.prototype._invoke = function (event, callback) { +internals.Server.prototype._invoke = function (type, next) { - var exts = this._extensions[event]; - if (!exts) { - return Hoek.nextTick(callback)(); + var exts = this._extensions[type]; + if (!exts.nodes) { + return next(); } - Items.serial(exts.nodes, function (ext, next) { + Items.serial(exts.nodes, function (ext, nextExt) { - var bind = (ext.bind || ext.realm.settings.bind); - ext.func.call(bind, ext.plugin._select(), next); - }, callback); + var bind = (ext.bind || ext.plugin.realm.settings.bind); + ext.func.call(bind, ext.plugin._select(), nextExt); + }, next); }; diff --git a/test/connection.js b/test/connection.js index 7b9923b97..4a30e9b8c 100755 --- a/test/connection.js +++ b/test/connection.js @@ -913,175 +913,6 @@ describe('Connection', function () { describe('ext()', function () { - it('executes along the request lifecycle (route last)', function (done) { - - var server = new Hapi.Server(); - server.connection(); - server.ext('onRequest', function (request, reply) { - - request.app.x = '1'; - return reply.continue(); - }); - - server.ext('onPreAuth', function (request, reply) { - - request.app.x += '2'; - return reply.continue(); - }); - - server.ext('onPostAuth', function (request, reply) { - - request.app.x += '3'; - return reply.continue(); - }); - - server.ext('onPreHandler', function (request, reply) { - - request.app.x += '4'; - return reply.continue(); - }); - - server.ext('onPostHandler', function (request, reply) { - - request.response.source += '5'; - return reply.continue(); - }); - - server.ext('onPreResponse', function (request, reply) { - - request.response.source += '6'; - return reply.continue(); - }); - - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - - return reply(request.app.x); - } - }); - - server.inject('/', function (res) { - - expect(res.result).to.equal('123456'); - done(); - }); - }); - - it('executes along the request lifecycle (route first)', function (done) { - - var server = new Hapi.Server(); - server.connection(); - - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - - return reply(request.app.x); - } - }); - - server.ext('onRequest', function (request, reply) { - - request.app.x = '1'; - return reply.continue(); - }); - - server.ext('onPreAuth', function (request, reply) { - - request.app.x += '2'; - return reply.continue(); - }); - - server.ext('onPostAuth', function (request, reply) { - - request.app.x += '3'; - return reply.continue(); - }); - - server.ext('onPreHandler', function (request, reply) { - - request.app.x += '4'; - return reply.continue(); - }); - - server.ext('onPostHandler', function (request, reply) { - - request.response.source += '5'; - return reply.continue(); - }); - - server.ext('onPreResponse', function (request, reply) { - - request.response.source += '6'; - return reply.continue(); - }); - - server.inject('/', function (res) { - - expect(res.result).to.equal('123456'); - done(); - }); - }); - - it('executes along the request lifecycle (route middle)', function (done) { - - var server = new Hapi.Server(); - server.connection(); - server.ext('onRequest', function (request, reply) { - - request.app.x = '1'; - return reply.continue(); - }); - - server.ext('onPreAuth', function (request, reply) { - - request.app.x += '2'; - return reply.continue(); - }); - - server.ext('onPostAuth', function (request, reply) { - - request.app.x += '3'; - return reply.continue(); - }); - - server.route({ - method: 'GET', - path: '/', - handler: function (request, reply) { - - return reply(request.app.x); - } - }); - - server.ext('onPreHandler', function (request, reply) { - - request.app.x += '4'; - return reply.continue(); - }); - - server.ext('onPostHandler', function (request, reply) { - - request.response.source += '5'; - return reply.continue(); - }); - - server.ext('onPreResponse', function (request, reply) { - - request.response.source += '6'; - return reply.continue(); - }); - - server.inject('/', function (res) { - - expect(res.result).to.equal('123456'); - done(); - }); - }); - it('supports adding an array of methods', function (done) { var server = new Hapi.Server(); diff --git a/test/plugin.js b/test/plugin.js index d2ebdff50..16ec2b775 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -3117,6 +3117,80 @@ describe('Plugin', function () { }); }); }); + + it('combine route extensions', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + var plugin = function (srv, options, next) { + + srv.route({ + method: 'GET', + path: '/', + config: { + ext: { + onPreAuth: { + method: function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + } + } + }, + handler: function (request, reply) { + + return reply(request.app.x); + } + } + }); + + srv.ext('onPreAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }, { sandbox: 'plugin' }); + + return next(); + }; + + plugin.attributes = { + name: 'test' + }; + + server.register(plugin, function (err) { + + expect(err).to.not.exist(); + + server.route({ + method: 'GET', + path: '/a', + config: { + handler: function (request, reply) { + + return reply(request.app.x); + } + } + }); + + server.inject('/', function (res1) { + + expect(res1.result).to.equal('123'); + + server.inject('/a', function (res2) { + + expect(res2.result).to.equal('1'); + done(); + }); + }); + }); + }); }); describe('handler()', function () { diff --git a/test/route.js b/test/route.js index 9b03a5fe3..42dcf36e1 100755 --- a/test/route.js +++ b/test/route.js @@ -401,4 +401,236 @@ describe('Route', function () { done(); }); }); + + describe('extensions', function () { + + it('combine connection extensions (route last)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.response.source += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + request.response.source += '6'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + + it('combine connection extensions (route first)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.response.source += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + request.response.source += '6'; + return reply.continue(); + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + + it('combine connection extensions (route middle)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.ext('onRequest', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + }); + + server.ext('onPostAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(request.app.x); + } + }); + + server.ext('onPreHandler', function (request, reply) { + + request.app.x += '4'; + return reply.continue(); + }); + + server.ext('onPostHandler', function (request, reply) { + + request.response.source += '5'; + return reply.continue(); + }); + + server.ext('onPreResponse', function (request, reply) { + + request.response.source += '6'; + return reply.continue(); + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('123456'); + done(); + }); + }); + + it('combine connection extensions (mixed sources)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x = '1'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/', + config: { + ext: { + onPreAuth: { + method: function (request, reply) { + + request.app.x += '2'; + return reply.continue(); + } + } + }, + handler: function (request, reply) { + + return reply(request.app.x); + } + } + }); + + server.ext('onPreAuth', function (request, reply) { + + request.app.x += '3'; + return reply.continue(); + }); + + server.route({ + method: 'GET', + path: '/a', + config: { + handler: function (request, reply) { + + return reply(request.app.x); + } + } + }); + + server.inject('/', function (res1) { + + expect(res1.result).to.equal('123'); + + server.inject('/a', function (res2) { + + expect(res2.result).to.equal('13'); + done(); + }); + }); + }); + }); }); From 8ae70053712ad41f9140edb5a130d4a48b5c75bc Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 5 Oct 2015 15:00:19 -0700 Subject: [PATCH 0059/1139] Throw when calling single connection methods. Closes #2819 --- lib/plugin.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 07f7356f7..ce03aaaed 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -96,18 +96,12 @@ internals.Plugin.prototype._single = function () { this.connections.length === 1) { this.info = this.connections[0].info; - this.inject = internals.inject; this.listener = this.connections[0].listener; - this.lookup = internals.lookup; - this.match = internals.match; this.registrations = this.connections[0].registrations; } else { this.info = null; - this.inject = null; this.listener = null; - this.lookup = null; - this.match = null; this.registrations = null; } }; @@ -477,8 +471,9 @@ internals.Plugin.prototype.handler = function (name, method) { }; -internals.inject = function (options, callback) { +internals.Plugin.prototype.inject = function (options, callback) { + Hoek.assert(this.connections.length === 1, 'Method not available when the selection has more than one connection or none'); return this.connections[0].inject(options, callback); }; @@ -513,14 +508,16 @@ internals.Plugin.prototype._log = function (tags, data) { }; -internals.lookup = function (id) { +internals.Plugin.prototype.lookup = function (id) { + Hoek.assert(this.connections.length === 1, 'Method not available when the selection has more than one connection or none'); return this.connections[0].lookup(id); }; -internals.match = function (method, path, host) { +internals.Plugin.prototype.match = function (method, path, host) { + Hoek.assert(this.connections.length === 1, 'Method not available when the selection has more than one connection or none'); return this.connections[0].match(method, path, host); }; From 21983e082780968736936c0e5f50e362d631f36a Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 5 Oct 2015 15:38:49 -0700 Subject: [PATCH 0060/1139] Fix default host header. Closes #2824. Closes #2828 --- lib/connection.js | 11 ++++- npm-shrinkwrap.json | 2 +- package.json | 2 +- test/connection.js | 97 +++++++++++++++++++++++++++++++++++++++++++++ test/validation.js | 2 +- 5 files changed, 109 insertions(+), 5 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 3cf0401d8..a627d694d 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -257,13 +257,20 @@ internals.Connection.prototype._dispatch = function (options) { internals.Connection.prototype.inject = function (options, callback) { var settings = options; - if (settings.credentials || + if (typeof settings === 'string') { + settings = { url: settings }; + } + + if (!settings.authority || + settings.credentials || settings.allowInternals !== undefined) { // Can be false - settings = Hoek.shallow(options); // options can be reused + settings = Hoek.shallow(settings); // options can be reused delete settings.credentials; delete settings.artifacts; // Cannot appear without credentials delete settings.allowInternals; + + settings.authority = settings.authority || (this.info.host + ':' + this.info.port); } var needle = this._dispatch({ diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 454edf7ee..d18502a1b 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -64,7 +64,7 @@ "version": "4.0.0" }, "shot": { - "version": "1.6.1" + "version": "1.7.0" }, "statehood": { "version": "2.1.1" diff --git a/package.json b/package.json index db3c22232..6f56de4e0 100755 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "mimos": "2.x.x", "peekaboo": "1.x.x", "qs": "4.x.x", - "shot": "1.x.x", + "shot": "^1.7.x", "statehood": "2.x.x", "subtext": "2.x.x", "topo": "^1.1.x" diff --git a/test/connection.js b/test/connection.js index 4a30e9b8c..6cc515ab1 100755 --- a/test/connection.js +++ b/test/connection.js @@ -715,6 +715,83 @@ describe('Connection', function () { }); }); + it('sets credentials (with host header)', function (done) { + + var handler = function (request, reply) { + + return reply(); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', config: { handler: handler } }); + + var options = { + url: '/', + credentials: { foo: 'bar' }, + headers: { + host: 'something' + } + }; + + server.inject(options, function (res) { + + expect(res.statusCode).to.equal(200); + expect(options.credentials).to.exist(); + done(); + }); + }); + + it('sets credentials (with authority)', function (done) { + + var handler = function (request, reply) { + + return reply(request.headers.host); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', config: { handler: handler } }); + + var options = { + url: '/', + credentials: { foo: 'bar' }, + authority: 'something' + }; + + server.inject(options, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('something'); + expect(options.credentials).to.exist(); + done(); + }); + }); + + it('sets authority', function (done) { + + var handler = function (request, reply) { + + return reply(request.headers.host); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', config: { handler: handler } }); + + var options = { + url: '/', + authority: 'something' + }; + + server.inject(options, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('something'); + done(); + }); + }); + it('passes the options.artifacts object', function (done) { var handler = function (request, reply) { @@ -798,6 +875,26 @@ describe('Connection', function () { done(); }); }); + + it('sets correct host header', function (done) { + + var server = new Hapi.Server(); + server.connection({ host: 'example.com', port: 2080 }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + reply(request.headers.host); + } + }); + + server.inject('/', function (res) { + + expect(res.result).to.equal('example.com:2080'); + done(); + }); + }); }); describe('table()', function () { diff --git a/test/validation.js b/test/validation.js index 7ef8d6640..6385d3022 100755 --- a/test/validation.js +++ b/test/validation.js @@ -1450,7 +1450,7 @@ describe('validation', function () { config: { validate: { headers: { - host: 'localhost', + host: server.info.host + ':' + server.info.port, accept: Joi.string().valid('application/json').required(), 'user-agent': Joi.string().optional() } From d9d0867cd89cc6cd17da34f6d1e83b052b6a4d3b Mon Sep 17 00:00:00 2001 From: Adam Bretz Date: Mon, 5 Oct 2015 20:06:31 -0400 Subject: [PATCH 0061/1139] Expanded `registrations` API. Added test. Updated docs. --- API.md | 8 ++++++-- lib/plugin.js | 10 ++++++++-- test/plugin.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index da7e2b72d..1286eae94 100755 --- a/API.md +++ b/API.md @@ -434,6 +434,10 @@ exports.register = function (server, options, next) { When the server contains exactly one connection, `registrations` is an object where each key is a registered plugin name and value contains: - `version` - the plugin version. +- `name` - the plugin name. +- `multiple` - the plugin multiple status. +- `options` - options used to register the plugin. +- `attributes` - plugin registration attributes. When the server contains more than one connection, each [`server.connections`](#serverconnections) array member provides its own `connection.registrations`. @@ -1469,7 +1473,7 @@ Registers a plugin where: - `once` - if `true`, the registration is skipped for any connection already registered with. Cannot be used with plugin options. If the plugin does not have a `connections` attribute set to `false` and the registration selection is empty, registration will be skipped as no connections - are available to register once. Defaults to `false`. + are available to register once. Defaults to `false`. - `routes` - modifiers applied to each route added by the plugin: - `prefix` - string added as prefix to any route path (must begin with `'/'`). If a plugin registers a child plugin the `prefix` is passed on to the child or is added in front of @@ -2733,7 +2737,7 @@ For example it can be used inside a promise to create a response object which ha var handler = function (request, reply) { var result = promiseMethod().then(function (thing) { - + if (!thing) { return request.generateResponse().code(214); } diff --git a/lib/plugin.js b/lib/plugin.js index ce03aaaed..f30abd2b8 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -226,7 +226,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback var registration = { register: plugin.register, name: attributes.name || attributes.pkg.name, - version: attributes.version || attributes.pkg.version, + version: attributes.version || attributes.pkg.version || '0.0.0', multiple: attributes.multiple, pluginOptions: plugin.options, dependencies: attributes.dependencies, @@ -280,7 +280,13 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); } else { - connection.registrations[item.name] = { version: item.version }; + connection.registrations[item.name] = { + version: item.version, + name: item.name, + multiple: item.multiple, + options: item.pluginOptions, + attributes: item.register.attributes + }; } connections.push(connection); diff --git a/test/plugin.js b/test/plugin.js index 16ec2b775..eb815d0eb 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -352,6 +352,52 @@ describe('Plugin', function () { }); }); + it('exposes plugin registration information', function (done) { + + var test = function (srv, options, next) { + + srv.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(srv.version); + } + }); + return next(); + }; + + test.attributes = { + multiple: true, + pkg: { + name: 'bob', + version: '1.2.3' + } + }; + + var server = new Hapi.Server(); + server.connection(); + + server.register({ + register: test, + options: { foo: 'bar' } + }, function (err) { + + expect(err).to.not.exist(); + var bob = server.connections[0].registrations.bob; + expect(bob).to.exist(); + expect(bob).to.be.an.object(); + expect(bob.version).to.equal('1.2.3'); + expect(bob.multiple).to.be.true(); + expect(bob.options.foo).to.equal('bar'); + server.inject('/', function (res) { + + expect(res.result).to.equal(require('../package.json').version); + done(); + }); + }); + }); + it('prevents plugin from multiple registrations', function (done) { var test = function (srv, options, next) { From 14be3164422c46467cbecec34e96849c653bd10a Mon Sep 17 00:00:00 2001 From: Adam Bretz Date: Mon, 5 Oct 2015 21:26:36 -0700 Subject: [PATCH 0062/1139] PR updates 1. --- lib/plugin.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index f30abd2b8..9adc3c866 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -253,6 +253,14 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback selection.realm.modifiers.route.vhost = item.options.routes.vhost; selection.realm.pluginOptions = item.pluginOptions || {}; + var registrationData = { + version: item.version, + name: item.name, + multiple: item.multiple, + options: item.pluginOptions, + attributes: item.register.attributes + }; + // Protect against multiple registrations if (!item.connections) { @@ -264,7 +272,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered'); } else { - self.root._registrations[item.name] = { version: item.version }; + self.root._registrations[item.name] = registrationData; } } @@ -280,13 +288,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered in:', connection.info.uri); } else { - connection.registrations[item.name] = { - version: item.version, - name: item.name, - multiple: item.multiple, - options: item.pluginOptions, - attributes: item.register.attributes - }; + connection.registrations[item.name] = registrationData; } connections.push(connection); From 2267ded554806c17a0c1620e501c6f81a7a5c918 Mon Sep 17 00:00:00 2001 From: Adam Bretz Date: Tue, 6 Oct 2015 15:53:11 -0700 Subject: [PATCH 0063/1139] Updated plugin schema. --- lib/plugin.js | 2 +- lib/schema.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 9adc3c866..11deb9948 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -226,7 +226,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback var registration = { register: plugin.register, name: attributes.name || attributes.pkg.name, - version: attributes.version || attributes.pkg.version || '0.0.0', + version: attributes.version || attributes.pkg.version, multiple: attributes.multiple, pluginOptions: plugin.options, dependencies: attributes.dependencies, diff --git a/lib/schema.js b/lib/schema.js index 56563abb2..cd4648179 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -325,7 +325,9 @@ internals.plugin = internals.register.keys({ version: Joi.string().default('0.0.0') }) .unknown() - .default({}), + .default({ + version: '0.0.0' + }), name: Joi.string() .when('pkg.name', { is: Joi.exist(), otherwise: Joi.required() }), version: Joi.string(), From 799613e9fec07bf308c4344ccccf71bb8a952490 Mon Sep 17 00:00:00 2001 From: Adam Bretz Date: Wed, 7 Oct 2015 13:40:05 -0700 Subject: [PATCH 0064/1139] Remove multplie from data payload. --- API.md | 1 - lib/plugin.js | 1 - test/plugin.js | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/API.md b/API.md index 1286eae94..222fb2b87 100755 --- a/API.md +++ b/API.md @@ -435,7 +435,6 @@ When the server contains exactly one connection, `registrations` is an object wh registered plugin name and value contains: - `version` - the plugin version. - `name` - the plugin name. -- `multiple` - the plugin multiple status. - `options` - options used to register the plugin. - `attributes` - plugin registration attributes. diff --git a/lib/plugin.js b/lib/plugin.js index 11deb9948..507181b7e 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -256,7 +256,6 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback var registrationData = { version: item.version, name: item.name, - multiple: item.multiple, options: item.pluginOptions, attributes: item.register.attributes }; diff --git a/test/plugin.js b/test/plugin.js index eb815d0eb..a5ae338db 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -388,7 +388,7 @@ describe('Plugin', function () { expect(bob).to.exist(); expect(bob).to.be.an.object(); expect(bob.version).to.equal('1.2.3'); - expect(bob.multiple).to.be.true(); + expect(bob.attributes.multiple).to.be.true(); expect(bob.options.foo).to.equal('bar'); server.inject('/', function (res) { From 6e61eedf98d2c6d796021ac84c463553cb237128 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 8 Oct 2015 09:05:38 -0700 Subject: [PATCH 0065/1139] heavy deps. Closes #2836 --- npm-shrinkwrap.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index d18502a1b..ded92704a 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.4.0", + "version": "10.4.1", "dependencies": { "accept": { "version": "1.1.0" @@ -24,7 +24,7 @@ "version": "2.0.5" }, "heavy": { - "version": "3.0.0" + "version": "3.0.1" }, "hoek": { "version": "2.16.3" diff --git a/package.json b/package.json index 6f56de4e0..d439dfdd9 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.4.0", + "version": "10.4.1", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From 22ce70961989e42c87d960d1d67ba907672a9df7 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 12 Oct 2015 15:17:01 -0700 Subject: [PATCH 0066/1139] Add test for nested method in string pre --- test/handler.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/handler.js b/test/handler.js index 9252d6c73..5f3143c33 100755 --- a/test/handler.js +++ b/test/handler.js @@ -504,6 +504,37 @@ describe('handler', function () { }); }); + it('returns a user record using server method (nested method name)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.method('user.get', function (id, next) { + + return next(null, { id: id, name: 'Bob' }); + }); + + server.route({ + method: 'GET', + path: '/user/{id}', + config: { + pre: [ + 'user.get(params.id)' + ], + handler: function (request, reply) { + + return reply(request.pre['user.get']); + } + } + }); + + server.inject('/user/5', function (res) { + + expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); + done(); + }); + }); + it('returns a user record using server method in object', function (done) { var server = new Hapi.Server(); From edb2d9afa5eaa2196fb18a32635332392b3c8b94 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Wed, 14 Oct 2015 01:35:44 -0700 Subject: [PATCH 0067/1139] Fix route-specific cors. Closes #2491 --- lib/connection.js | 37 +++++++++- lib/route.js | 2 + test/connection.js | 163 +++++++++++++++++++++++++++++++++++++++++++++ test/transmit.js | 16 +++-- 4 files changed, 210 insertions(+), 8 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index a627d694d..d279200f5 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -79,6 +79,7 @@ exports = module.exports = internals.Connection = function (server, options) { this.auth = new Auth(this); this._router = new Call.Router(this.settings.router); this._defaultRoutes(); + this._corsPaths = {}; this.plugins = {}; // Registered plugin APIs by plugin name this.app = {}; // Place for application-specific state without conflicts with hapi, should not be used by plugins @@ -216,6 +217,36 @@ internals.Connection.prototype._stop = function (options, callback) { }; +internals.Connection.prototype._cors = function (method, path, plugin, options) { + + if (method === 'options' || + Hoek.deepEqual(this.settings.routes.cors, options)) { + + return; + } + + if (this._corsPaths[path]) { + Hoek.assert(Hoek.deepEqual(this._corsPaths[path], options), 'Cannot add multiple routes with different CORS options on different methods:', method.toUpperCase(), path); + return; + } + + this._corsPaths[path] = Hoek.clone(options); + + this._route({ + path: path, + method: 'options', + config: { + auth: false, // Override any defaults + cors: options, + handler: function (request, reply) { + + return reply(); + } + } + }, plugin); +}; + + internals.Connection.prototype._dispatch = function (options) { var self = this; @@ -340,7 +371,7 @@ internals.Connection.prototype._ext = function (event) { }; -internals.Connection.prototype._route = function (configs, realm) { +internals.Connection.prototype._route = function (configs, plugin) { configs = [].concat(configs); for (var i = 0, il = configs.length; i < il; ++i) { @@ -352,11 +383,11 @@ internals.Connection.prototype._route = function (configs, realm) { var settings = Hoek.shallow(config); settings.method = method; - this._addRoute(settings, realm); + this._addRoute(settings, plugin); } } else { - this._addRoute(config, realm); + this._addRoute(config, plugin); } } }; diff --git a/lib/route.js b/lib/route.js index 3d02e929a..a8c14c351 100755 --- a/lib/route.js +++ b/lib/route.js @@ -157,6 +157,8 @@ exports = module.exports = internals.Route = function (options, connection, plug this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); var cors = this.settings.cors; + this.connection._cors(this.method, this.path, this.plugin, cors); + cors._headers = cors.headers.concat(cors.additionalHeaders).join(','); cors._methods = cors.methods.concat(cors.additionalMethods).join(','); cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(','); diff --git a/test/connection.js b/test/connection.js index 6cc515ab1..7fc51bc26 100755 --- a/test/connection.js +++ b/test/connection.js @@ -646,6 +646,169 @@ describe('Connection', function () { }); }); + describe('_cors()', function () { + + it('returns CORS headers on single route', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/b', handler: handler }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(404); + expect(res2.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); + }); + + it('allows CORS headers on multiple routes but not all', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/c', handler: handler }); + + expect(server.table()[0].table).to.have.length(5); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/c' }, function (res3) { + + expect(res3.statusCode).to.equal(404); + expect(res3.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); + }); + }); + + it('allows same CORS headers on multiple routes with same path', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.be.null(); + expect(res.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + + it('errors on different CORS headers on multiple routes with same path', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); + expect(function () { + + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: { origin: ['b'] } } }); + }).to.throw('Cannot add multiple routes with different CORS options on different methods: POST /a'); + + done(); + }); + + it('reuses connections CORS route when route has same settings', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); + + expect(server.table()[0].table).to.have.length(2); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.be.null(); + expect(res.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + + it('returns CORS headers on single route (overrides defaults)', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['b'] } } }); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); + server.route({ method: 'GET', path: '/b', handler: handler }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('a'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('b'); + done(); + }); + }); + }); + }); + describe('_dispatch()', function () { it('rejects request due to high rss load', { parallel: false }, function (done) { diff --git a/test/transmit.js b/test/transmit.js index 9f04846bf..7d175ff58 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2155,12 +2155,18 @@ describe('transmission', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); - server.inject('/', function (res) { + server.inject('/', function (res1) { - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - done(); + expect(res1.result).to.exist(); + expect(res1.result).to.equal('ok'); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/' }, function (res2) { + + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); }); }); From 737edb1c87e9541f64ebc423f1c5395ad21e9a5f Mon Sep 17 00:00:00 2001 From: Olivier Vaillancourt Date: Wed, 14 Oct 2015 11:18:12 -0400 Subject: [PATCH 0068/1139] Fix typo in api doc. --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 222fb2b87..b4ba70915 100755 --- a/API.md +++ b/API.md @@ -2534,7 +2534,7 @@ are called in parallel. `pre` can be assigned a mixed array of: - arrays containing the elements listed below, which are executed in parallel. - objects with: - `method` - the function to call (or short-hand method string as described below). the - function signature is identical to a route handler as describer in + function signature is identical to a route handler as described in [Route handler](#route-handler). - `assign` - key name to assign the result of the function to within `request.pre`. - `failAction` - determines how to handle errors returned by the method. Allowed values are: From f36ff763869647a9b5f498ff9955c56f5f7a4a8b Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Wed, 14 Oct 2015 12:45:20 -0700 Subject: [PATCH 0069/1139] Prep for #2840 --- API.md | 4 ++-- lib/connection.js | 8 ++++++-- lib/route.js | 9 ++++----- lib/schema.js | 2 +- lib/server.js | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/API.md b/API.md index b4ba70915..f83986a89 100755 --- a/API.md +++ b/API.md @@ -2123,8 +2123,8 @@ following options: - `cors` - the [Cross-Origin Resource Sharing](http://www.w3.org/TR/cors/) protocol allows browsers to make cross-origin API calls. CORS is required by web applications running inside a browser which are loaded from a different domain than the API server. CORS - headers are disabled by default. To enable, set `cors` to `true`, or to an object with - the following options: + headers are disabled by default (`false`). To enable, set `cors` to `true`, or to an object + with the following options: - `origin` - a strings array of allowed origin servers ('Access-Control-Allow-Origin'). The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '*' character, or a single `'*'` origin string. Defaults diff --git a/lib/connection.js b/lib/connection.js index d279200f5..8b19b1584 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -78,8 +78,8 @@ exports = module.exports = internals.Connection = function (server, options) { this.states = new Statehood.Definitions(this.settings.state); this.auth = new Auth(this); this._router = new Call.Router(this.settings.router); - this._defaultRoutes(); this._corsPaths = {}; + this._defaultRoutes(); this.plugins = {}; // Registered plugin APIs by plugin name this.app = {}; // Place for application-specific state without conflicts with hapi, should not be used by plugins @@ -219,7 +219,7 @@ internals.Connection.prototype._stop = function (options, callback) { internals.Connection.prototype._cors = function (method, path, plugin, options) { - if (method === 'options' || + if (['options', 'notfound', 'badrequest'].indexOf(method) !== -1 || Hoek.deepEqual(this.settings.routes.cors, options)) { return; @@ -230,6 +230,10 @@ internals.Connection.prototype._cors = function (method, path, plugin, options) return; } + if (!options) { + return; + } + this._corsPaths[path] = Hoek.clone(options); this._route({ diff --git a/lib/route.js b/lib/route.js index a8c14c351..19e425a12 100755 --- a/lib/route.js +++ b/lib/route.js @@ -153,12 +153,11 @@ exports = module.exports = internals.Route = function (options, connection, plug // CORS - if (this.settings.cors) { - this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); - - var cors = this.settings.cors; - this.connection._cors(this.method, this.path, this.plugin, cors); + this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); + var cors = this.settings.cors; + this.connection._cors(this.method, this.path, this.plugin, cors); + if (cors) { cors._headers = cors.headers.concat(cors.additionalHeaders).join(','); cors._methods = cors.methods.concat(cors.additionalMethods).join(','); cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(','); diff --git a/lib/schema.js b/lib/schema.js index cd4648179..ab529eeb1 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -89,7 +89,7 @@ internals.routeBase = Joi.object({ credentials: Joi.boolean(), override: Joi.boolean().allow('merge') }) - .allow(null, false, true), + .allow(false, true), ext: Joi.object({ onPreAuth: Joi.array().items(internals.event).single(), onPostAuth: Joi.array().items(internals.event).single(), diff --git a/lib/server.js b/lib/server.js index 201ebdb91..7e00c7e3b 100755 --- a/lib/server.js +++ b/lib/server.js @@ -111,7 +111,7 @@ internals.Server.prototype.connection = function (options) { var root = this.root; // Explicitly use the root reference (for plugin invocation) var settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']); - settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors); + settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors) || false; settings.routes.security = Hoek.applyToDefaults(root._settings.connections.routes.security || Defaults.security, settings.routes.security); settings = Schema.apply('connection', settings); // Applies validation changes (type cast) From c841ffd7d7c07d942b05b5670cc79566d1b88629 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 08:53:29 -0700 Subject: [PATCH 0070/1139] Server route event. Closes #2844 --- API.md | 16 +++++++++++++++- README.md | 2 +- lib/connection.js | 20 +++++++++++++------- lib/route.js | 29 ++++++++++++++++------------- lib/transmit.js | 28 +++++++++++++++------------- npm-shrinkwrap.json | 2 +- package.json | 2 +- test/connection.js | 22 ++++++++++++++++++++++ 8 files changed, 84 insertions(+), 37 deletions(-) diff --git a/API.md b/API.md index f83986a89..2babc1223 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 10.4.x API Reference +# 10.5.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) @@ -1757,6 +1757,9 @@ The server object inherits from `Events.EventEmitter` and emits the following ev per request. - `'tail'` - emitted when a request finished processing, including any registered tails. Single event per request. +- `'route'` - emitted when a route is added to a connection. Note that if a route is added to + multiple connections at the same time, each will emit a separate event. Note that the `route` + object must not be modified. Note that the server object should not be used to emit application events as its internal implementation is designed to fan events out to the various plugin selections and not for @@ -1814,6 +1817,17 @@ server.on('response', function (request) { }); ``` +The `'route'` event includes the [route public interface](#route-public-interface), the connection, +and the server object used to add the route (e.g. the result of a plugin select operation): + +```js +server.on('route', function (route, connection, server) { + + console.log('New route added: ' + route.path); +}); +``` + + #### Internal events The following logs are generated automatically by the framework. Each event can be identified by diff --git a/README.md b/README.md index 19ea711bb..0b85157c6 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse) authentication, and other essential facilities for building web and services applications. **hapi** enables developers to focus on writing reusable application logic in a highly modular and prescriptive approach. -Development version: **10.4.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) +Development version: **10.5.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) [![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi) For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the diff --git a/lib/connection.js b/lib/connection.js index 8b19b1584..6c0539014 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -107,6 +107,8 @@ exports = module.exports = internals.Connection = function (server, options) { }; this.info.uri = (this.settings.uri || (this.info.protocol + ':' + (this.type === 'tcp' ? '//' + this.info.host + (this.info.port ? ':' + this.info.port : '') : this.info.port))); + + this.on('route', internals.cors); }; Hoek.inherits(internals.Connection, Events.EventEmitter); @@ -217,31 +219,33 @@ internals.Connection.prototype._stop = function (options, callback) { }; -internals.Connection.prototype._cors = function (method, path, plugin, options) { +internals.cors = function (route, connection, plugin) { - if (['options', 'notfound', 'badrequest'].indexOf(method) !== -1 || - Hoek.deepEqual(this.settings.routes.cors, options)) { + var settings = route.settings.cors; + if (route.method === 'options' || + Hoek.deepEqual(this.settings.routes.cors, settings)) { return; } + var path = route.path; if (this._corsPaths[path]) { - Hoek.assert(Hoek.deepEqual(this._corsPaths[path], options), 'Cannot add multiple routes with different CORS options on different methods:', method.toUpperCase(), path); + Hoek.assert(Hoek.deepEqual(this._corsPaths[path], settings), 'Cannot add multiple routes with different CORS options on different methods:', route.method.toUpperCase(), path); return; } - if (!options) { + if (!settings) { return; } - this._corsPaths[path] = Hoek.clone(options); + this._corsPaths[path] = Hoek.clone(settings); this._route({ path: path, method: 'options', config: { auth: false, // Override any defaults - cors: options, + cors: settings, handler: function (request, reply) { return reply(); @@ -408,6 +412,8 @@ internals.Connection.prototype._addRoute = function (config, plugin) { route.fingerprint = record.fingerprint; route.params = record.params; } + + this.emit('route', route.public, this, plugin); }; diff --git a/lib/route.js b/lib/route.js index 19e425a12..4fdca9b0c 100755 --- a/lib/route.js +++ b/lib/route.js @@ -153,17 +153,20 @@ exports = module.exports = internals.Route = function (options, connection, plug // CORS - this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); - var cors = this.settings.cors; - this.connection._cors(this.method, this.path, this.plugin, cors); + if (this.settings.cors) { + this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); + + var cors = this.settings.cors; + this.settings._cors = { + headers: cors.headers.concat(cors.additionalHeaders).join(','), + methods: cors.methods.concat(cors.additionalMethods).join(','), + exposedHeaders: cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(',') + }; - if (cors) { - cors._headers = cors.headers.concat(cors.additionalHeaders).join(','); - cors._methods = cors.methods.concat(cors.additionalMethods).join(','); - cors._exposedHeaders = cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(','); + var _cors = this.settings._cors; if (cors.origin.length) { - cors._origin = { + _cors.origin = { any: false, qualified: [], qualifiedString: '', @@ -172,21 +175,21 @@ exports = module.exports = internals.Route = function (options, connection, plug if (cors.origin.indexOf('*') !== -1) { Hoek.assert(cors.origin.length === 1, 'Cannot specify cors.origin * together with other values'); - cors._origin.any = true; + _cors.origin.any = true; } else { for (var c = 0, cl = cors.origin.length; c < cl; ++c) { var origin = cors.origin[c]; if (origin.indexOf('*') !== -1) { - cors._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); + _cors.origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); } else { - cors._origin.qualified.push(origin); + _cors.origin.qualified.push(origin); } } - Hoek.assert(cors.matchOrigin || !cors._origin.wildcards.length, 'Cannot include wildcard origin values with matchOrigin disabled'); - cors._origin.qualifiedString = cors._origin.qualified.join(' '); + Hoek.assert(cors.matchOrigin || !_cors.origin.wildcards.length, 'Cannot include wildcard origin values with matchOrigin disabled'); + _cors.origin.qualifiedString = _cors.origin.qualified.join(' '); } } } diff --git a/lib/transmit.js b/lib/transmit.js index 03cc46e53..fb5329375 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -359,23 +359,25 @@ internals.cors = function (response) { return; } - if (cors._origin && + var _cors = request.route.settings._cors; + + if (_cors.origin && (!response.headers['access-control-allow-origin'] || cors.override)) { if (cors.matchOrigin) { response.vary('origin'); - if (internals.matchOrigin(request.headers.origin, cors)) { + if (internals.matchOrigin(request.headers.origin, _cors)) { response._header('access-control-allow-origin', request.headers.origin); } else if (cors.isOriginExposed) { - response._header('access-control-allow-origin', cors._origin.any ? '*' : cors._origin.qualifiedString); + response._header('access-control-allow-origin', _cors.origin.any ? '*' : _cors.origin.qualifiedString); } } - else if (cors._origin.any) { + else if (_cors.origin.any) { response._header('access-control-allow-origin', '*'); } else { - response._header('access-control-allow-origin', cors._origin.qualifiedString); + response._header('access-control-allow-origin', _cors.origin.qualifiedString); } } @@ -392,11 +394,11 @@ internals.cors = function (response) { config.append = true; } - response._header('access-control-allow-methods', cors._methods, config); - response._header('access-control-allow-headers', cors._headers, config); + response._header('access-control-allow-methods', _cors.methods, config); + response._header('access-control-allow-headers', _cors.headers, config); - if (cors._exposedHeaders.length !== 0) { - response._header('access-control-expose-headers', cors._exposedHeaders, config); + if (_cors.exposedHeaders.length !== 0) { + response._header('access-control-expose-headers', _cors.exposedHeaders, config); } }; @@ -407,16 +409,16 @@ internals.matchOrigin = function (origin, cors) { return false; } - if (cors._origin.any) { + if (cors.origin.any) { return true; } - if (cors._origin.qualified.indexOf(origin) !== -1) { + if (cors.origin.qualified.indexOf(origin) !== -1) { return true; } - for (var i = 0, il = cors._origin.wildcards.length; i < il; ++i) { - if (origin.match(cors._origin.wildcards[i])) { + for (var i = 0, il = cors.origin.wildcards.length; i < il; ++i) { + if (origin.match(cors.origin.wildcards[i])) { return true; } } diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index ded92704a..72dce0a94 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.4.1", + "version": "10.5.0", "dependencies": { "accept": { "version": "1.1.0" diff --git a/package.json b/package.json index d439dfdd9..4169795e2 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.4.1", + "version": "10.5.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" diff --git a/test/connection.js b/test/connection.js index 7fc51bc26..af5ac3d87 100755 --- a/test/connection.js +++ b/test/connection.js @@ -1557,6 +1557,28 @@ describe('Connection', function () { describe('route()', function () { + it('emits route event', function (done) { + + var server = new Hapi.Server(); + server.connection({ labels: 'a' }); + server.on('route', function (route, connection, srv) { + + expect(route.path).to.equal('/'); + expect(connection.settings.labels).to.deep.equal(['a']); + expect(srv).to.equal(server); + done(); + }); + + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(); + } + }); + }); + it('overrides the default notFound handler', function (done) { var handler = function (request, reply) { From cb933e12ab1cc3fe12069dfc084580b064c1842d Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 11:13:56 -0700 Subject: [PATCH 0071/1139] Refactor cors. For #2840 --- lib/connection.js | 39 +---------- lib/cors.js | 170 ++++++++++++++++++++++++++++++++++++++++++++++ lib/route.js | 42 +----------- lib/transmit.js | 79 +-------------------- 4 files changed, 176 insertions(+), 154 deletions(-) create mode 100755 lib/cors.js diff --git a/lib/connection.js b/lib/connection.js index 6c0539014..6f87fa7ba 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -11,6 +11,7 @@ var Hoek = require('hoek'); var Shot = require('shot'); var Statehood = require('statehood'); var Auth = require('./auth'); +var Cors = require('./cors'); var Ext = require('./ext'); var Route = require('./route'); @@ -108,7 +109,7 @@ exports = module.exports = internals.Connection = function (server, options) { this.info.uri = (this.settings.uri || (this.info.protocol + ':' + (this.type === 'tcp' ? '//' + this.info.host + (this.info.port ? ':' + this.info.port : '') : this.info.port))); - this.on('route', internals.cors); + this.on('route', Cors.options); }; Hoek.inherits(internals.Connection, Events.EventEmitter); @@ -219,42 +220,6 @@ internals.Connection.prototype._stop = function (options, callback) { }; -internals.cors = function (route, connection, plugin) { - - var settings = route.settings.cors; - if (route.method === 'options' || - Hoek.deepEqual(this.settings.routes.cors, settings)) { - - return; - } - - var path = route.path; - if (this._corsPaths[path]) { - Hoek.assert(Hoek.deepEqual(this._corsPaths[path], settings), 'Cannot add multiple routes with different CORS options on different methods:', route.method.toUpperCase(), path); - return; - } - - if (!settings) { - return; - } - - this._corsPaths[path] = Hoek.clone(settings); - - this._route({ - path: path, - method: 'options', - config: { - auth: false, // Override any defaults - cors: settings, - handler: function (request, reply) { - - return reply(); - } - } - }, plugin); -}; - - internals.Connection.prototype._dispatch = function (options) { var self = this; diff --git a/lib/cors.js b/lib/cors.js new file mode 100755 index 000000000..0fc6ee229 --- /dev/null +++ b/lib/cors.js @@ -0,0 +1,170 @@ +// Load modules + +var Hoek = require('hoek'); +var Defaults = require('./defaults'); + + +// Declare internals + +var internals = {}; + + +exports.route = function (options) { + + var settings = Hoek.applyToDefaults(Defaults.cors, options); + if (!settings) { + return false; + } + + settings._headers = settings.headers.concat(settings.additionalHeaders).join(','); + settings._methods = settings.methods.concat(settings.additionalMethods).join(','); + settings._exposedHeaders = settings.exposedHeaders.concat(settings.additionalExposedHeaders).join(','); + + if (settings.origin.length) { + settings._origin = { + any: false, + qualified: [], + qualifiedString: '', + wildcards: [] + }; + + if (settings.origin.indexOf('*') !== -1) { + Hoek.assert(settings.origin.length === 1, 'Cannot specify cors.origin * together with other values'); + settings._origin.any = true; + } + else { + for (var c = 0, cl = settings.origin.length; c < cl; ++c) { + var origin = settings.origin[c]; + if (origin.indexOf('*') !== -1) { + settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); + } + else { + settings._origin.qualified.push(origin); + } + } + + Hoek.assert(settings.matchOrigin || !settings._origin.wildcards.length, 'Cannot include wildcard origin values with matchOrigin disabled'); + settings._origin.qualifiedString = settings._origin.qualified.join(' '); + } + } + + return settings; +}; + + +exports.headers = function (response) { + + var request = response.request; + var settings = request.route.settings.cors; + if (!settings) { + return; + } + + if (settings._origin && + (!response.headers['access-control-allow-origin'] || settings.override)) { + + if (settings.matchOrigin) { + response.vary('origin'); + if (internals.matchOrigin(request.headers.origin, settings)) { + response._header('access-control-allow-origin', request.headers.origin); + } + else if (settings.isOriginExposed) { + response._header('access-control-allow-origin', settings._origin.any ? '*' : settings._origin.qualifiedString); + } + } + else if (settings._origin.any) { + response._header('access-control-allow-origin', '*'); + } + else { + response._header('access-control-allow-origin', settings._origin.qualifiedString); + } + } + + var config = { override: !!settings.override }; // Value can be 'merge' + response._header('access-control-max-age', settings.maxAge, { override: settings.override }); + + if (settings.credentials) { + response._header('access-control-allow-credentials', 'true', { override: settings.override }); + } + + // Appended headers + + if (settings.override === 'merge') { + config.append = true; + } + + response._header('access-control-allow-methods', settings._methods, config); + response._header('access-control-allow-headers', settings._headers, config); + + if (settings._exposedHeaders.length !== 0) { + response._header('access-control-expose-headers', settings._exposedHeaders, config); + } +}; + + +internals.matchOrigin = function (origin, settings) { + + if (!origin) { + return false; + } + + if (settings._origin.any) { + return true; + } + + if (settings._origin.qualified.indexOf(origin) !== -1) { + return true; + } + + for (var i = 0, il = settings._origin.wildcards.length; i < il; ++i) { + if (origin.match(settings._origin.wildcards[i])) { + return true; + } + } + + return false; +}; + + +exports.options = function (route, connection, plugin) { + + var settings = Hoek.clone(route.settings.cors); + + if (settings) { + delete settings._origin; + delete settings._exposedHeaders; + delete settings._headers; + delete settings._methods; + } + + if (route.method === 'options' || + Hoek.deepEqual(connection.settings.routes.cors, settings)) { + + return; + } + + if (!settings) { + return; + } + + var path = route.path; + if (connection._corsPaths[path]) { + Hoek.assert(Hoek.deepEqual(connection._corsPaths[path], settings), 'Cannot add multiple routes with different CORS options on different methods:', route.method.toUpperCase(), path); + return; + } + + connection._corsPaths[path] = settings; + + connection._route({ + path: path, + method: 'options', + config: { + auth: false, // Override any defaults + cors: settings, + handler: function (request, reply) { + + return reply(); + } + } + }, plugin); +}; diff --git a/lib/route.js b/lib/route.js index 4fdca9b0c..c854b0239 100755 --- a/lib/route.js +++ b/lib/route.js @@ -6,6 +6,7 @@ var Hoek = require('hoek'); var Joi = require('joi'); var Subtext = require('subtext'); var Auth = require('./auth'); +var Cors = require('./cors'); var Defaults = require('./defaults'); var Ext = require('./ext'); var Handler = require('./handler'); @@ -153,46 +154,7 @@ exports = module.exports = internals.Route = function (options, connection, plug // CORS - if (this.settings.cors) { - this.settings.cors = Hoek.applyToDefaults(Defaults.cors, this.settings.cors); - - var cors = this.settings.cors; - this.settings._cors = { - headers: cors.headers.concat(cors.additionalHeaders).join(','), - methods: cors.methods.concat(cors.additionalMethods).join(','), - exposedHeaders: cors.exposedHeaders.concat(cors.additionalExposedHeaders).join(',') - }; - - var _cors = this.settings._cors; - - if (cors.origin.length) { - _cors.origin = { - any: false, - qualified: [], - qualifiedString: '', - wildcards: [] - }; - - if (cors.origin.indexOf('*') !== -1) { - Hoek.assert(cors.origin.length === 1, 'Cannot specify cors.origin * together with other values'); - _cors.origin.any = true; - } - else { - for (var c = 0, cl = cors.origin.length; c < cl; ++c) { - var origin = cors.origin[c]; - if (origin.indexOf('*') !== -1) { - _cors.origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); - } - else { - _cors.origin.qualified.push(origin); - } - } - - Hoek.assert(cors.matchOrigin || !_cors.origin.wildcards.length, 'Cannot include wildcard origin values with matchOrigin disabled'); - _cors.origin.qualifiedString = _cors.origin.qualified.join(' '); - } - } - } + this.settings.cors = Cors.route(this.settings.cors); // Security diff --git a/lib/transmit.js b/lib/transmit.js index fb5329375..2b51af3e5 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -9,6 +9,7 @@ var Hoek = require('hoek'); var Items = require('items'); var Shot = require('shot'); var Auth = require('./auth'); +var Cors = require('./cors'); var Response = require('./response'); @@ -40,7 +41,7 @@ internals.marshal = function (request, next) { var response = request.response; - internals.cors(response); + Cors.headers(response); internals.content(response); internals.security(response); @@ -351,82 +352,6 @@ internals.Empty.prototype._read = function (/* size */) { }; -internals.cors = function (response) { - - var request = response.request; - var cors = request.route.settings.cors; - if (!cors) { - return; - } - - var _cors = request.route.settings._cors; - - if (_cors.origin && - (!response.headers['access-control-allow-origin'] || cors.override)) { - - if (cors.matchOrigin) { - response.vary('origin'); - if (internals.matchOrigin(request.headers.origin, _cors)) { - response._header('access-control-allow-origin', request.headers.origin); - } - else if (cors.isOriginExposed) { - response._header('access-control-allow-origin', _cors.origin.any ? '*' : _cors.origin.qualifiedString); - } - } - else if (_cors.origin.any) { - response._header('access-control-allow-origin', '*'); - } - else { - response._header('access-control-allow-origin', _cors.origin.qualifiedString); - } - } - - var config = { override: !!cors.override }; // Value can be 'merge' - response._header('access-control-max-age', cors.maxAge, { override: cors.override }); - - if (cors.credentials) { - response._header('access-control-allow-credentials', 'true', { override: cors.override }); - } - - // Appended headers - - if (cors.override === 'merge') { - config.append = true; - } - - response._header('access-control-allow-methods', _cors.methods, config); - response._header('access-control-allow-headers', _cors.headers, config); - - if (_cors.exposedHeaders.length !== 0) { - response._header('access-control-expose-headers', _cors.exposedHeaders, config); - } -}; - - -internals.matchOrigin = function (origin, cors) { - - if (!origin) { - return false; - } - - if (cors.origin.any) { - return true; - } - - if (cors.origin.qualified.indexOf(origin) !== -1) { - return true; - } - - for (var i = 0, il = cors.origin.wildcards.length; i < il; ++i) { - if (origin.match(cors.origin.wildcards[i])) { - return true; - } - } - - return false; -}; - - internals.cache = function (response) { if (response.headers['cache-control']) { From 8358da17378b4d2164eeb6783f309a40546b5d94 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 11:23:04 -0700 Subject: [PATCH 0072/1139] Move cors tests. For #2840 --- test/connection.js | 199 -------------- test/cors.js | 638 +++++++++++++++++++++++++++++++++++++++++++++ test/reply.js | 11 +- test/transmit.js | 400 ---------------------------- 4 files changed, 641 insertions(+), 607 deletions(-) create mode 100755 test/cors.js diff --git a/test/connection.js b/test/connection.js index af5ac3d87..9c6211a77 100755 --- a/test/connection.js +++ b/test/connection.js @@ -646,169 +646,6 @@ describe('Connection', function () { }); }); - describe('_cors()', function () { - - it('returns CORS headers on single route', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); - server.route({ method: 'GET', path: '/b', handler: handler }); - - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { - - expect(res1.statusCode).to.equal(200); - expect(res1.result).to.be.null(); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); - - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { - - expect(res2.statusCode).to.equal(404); - expect(res2.headers['access-control-allow-origin']).to.not.exist(); - done(); - }); - }); - }); - - it('allows CORS headers on multiple routes but not all', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); - server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); - server.route({ method: 'GET', path: '/c', handler: handler }); - - expect(server.table()[0].table).to.have.length(5); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { - - expect(res1.statusCode).to.equal(200); - expect(res1.result).to.be.null(); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); - - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { - - expect(res2.statusCode).to.equal(200); - expect(res2.result).to.be.null(); - expect(res2.headers['access-control-allow-origin']).to.equal('*'); - - server.inject({ method: 'OPTIONS', url: '/c' }, function (res3) { - - expect(res3.statusCode).to.equal(404); - expect(res3.headers['access-control-allow-origin']).to.not.exist(); - done(); - }); - }); - }); - }); - - it('allows same CORS headers on multiple routes with same path', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); - server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); - - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { - - expect(res.statusCode).to.equal(200); - expect(res.result).to.be.null(); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - done(); - }); - }); - - it('errors on different CORS headers on multiple routes with same path', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); - expect(function () { - - server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: { origin: ['b'] } } }); - }).to.throw('Cannot add multiple routes with different CORS options on different methods: POST /a'); - - done(); - }); - - it('reuses connections CORS route when route has same settings', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: true } }); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); - server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); - - expect(server.table()[0].table).to.have.length(2); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { - - expect(res.statusCode).to.equal(200); - expect(res.result).to.be.null(); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - done(); - }); - }); - - it('returns CORS headers on single route (overrides defaults)', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['b'] } } }); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); - server.route({ method: 'GET', path: '/b', handler: handler }); - - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { - - expect(res1.statusCode).to.equal(200); - expect(res1.result).to.be.null(); - expect(res1.headers['access-control-allow-origin']).to.equal('a'); - - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { - - expect(res2.statusCode).to.equal(200); - expect(res2.result).to.be.null(); - expect(res2.headers['access-control-allow-origin']).to.equal('b'); - done(); - }); - }); - }); - }); - describe('_dispatch()', function () { it('rejects request due to high rss load', { parallel: false }, function (done) { @@ -1781,24 +1618,6 @@ describe('Connection', function () { }); }); - it('returns 404 on OPTIONS when cors disabled', function (done) { - - var handler = function (request, reply) { - - return reply(); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: false } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ method: 'OPTIONS', url: '/' }, function (res) { - - expect(res.statusCode).to.equal(404); - done(); - }); - }); - it('returns 400 on bad request', function (done) { var handler = function (request, reply) { @@ -1815,23 +1634,5 @@ describe('Connection', function () { done(); }); }); - - it('returns OPTIONS response', function (done) { - - var handler = function (request, reply) { - - return reply(Boom.badRequest()); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: true } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ method: 'OPTIONS', url: '/' }, function (res) { - - expect(res.headers['access-control-allow-origin']).to.equal('*'); - done(); - }); - }); }); }); diff --git a/test/cors.js b/test/cors.js new file mode 100755 index 000000000..9678bc9a2 --- /dev/null +++ b/test/cors.js @@ -0,0 +1,638 @@ +// Load modules + +var Boom = require('boom'); +var Code = require('code'); +var Hapi = require('..'); +var Lab = require('lab'); + + +// Declare internals + +var internals = {}; + + +// Test shortcuts + +var lab = exports.lab = Lab.script(); +var describe = lab.describe; +var it = lab.it; +var expect = Code.expect; + + +describe('CORS', function () { + + it('returns 404 on OPTIONS when cors disabled', function (done) { + + var handler = function (request, reply) { + + return reply(); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: false } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ method: 'OPTIONS', url: '/' }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + + it('returns OPTIONS response', function (done) { + + var handler = function (request, reply) { + + return reply(Boom.badRequest()); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ method: 'OPTIONS', url: '/' }, function (res) { + + expect(res.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + + it('returns headers on single route', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/b', handler: handler }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(404); + expect(res2.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); + }); + + it('allows headers on multiple routes but not all', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); + server.route({ method: 'GET', path: '/c', handler: handler }); + + expect(server.table()[0].table).to.have.length(5); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/c' }, function (res3) { + + expect(res3.statusCode).to.equal(404); + expect(res3.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); + }); + }); + + it('allows same headers on multiple routes with same path', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.be.null(); + expect(res.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + + it('errors on different headers on multiple routes with same path', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); + expect(function () { + + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: { origin: ['b'] } } }); + }).to.throw('Cannot add multiple routes with different CORS options on different methods: POST /a'); + + done(); + }); + + it('reuses connections CORS route when route has same settings', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); + server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); + + expect(server.table()[0].table).to.have.length(2); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.be.null(); + expect(res.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + + it('returns headers on single route (overrides defaults)', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['b'] } } }); + server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); + server.route({ method: 'GET', path: '/b', handler: handler }); + + expect(server.table()[0].table).to.have.length(3); + + server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + + expect(res1.statusCode).to.equal(200); + expect(res1.result).to.be.null(); + expect(res1.headers['access-control-allow-origin']).to.equal('a'); + + server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('b'); + done(); + }); + }); + }); + + it('sets access-control-allow-credentials header', function (done) { + + var handler = function (request, reply) { + + return reply(); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { credentials: true } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject('/', function (res) { + + expect(res.result).to.equal(null); + expect(res.headers['access-control-allow-credentials']).to.equal('true'); + done(); + }); + }); + + describe('headers()', function () { + + it('returns CORS origin (route level)', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection(); + server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); + + server.inject('/', function (res1) { + + expect(res1.result).to.exist(); + expect(res1.result).to.equal('ok'); + expect(res1.headers['access-control-allow-origin']).to.equal('*'); + + server.inject({ method: 'OPTIONS', url: '/' }, function (res2) { + + expect(res2.result).to.be.null(); + expect(res2.headers['access-control-allow-origin']).to.equal('*'); + done(); + }); + }); + }); + + it('returns CORS origin (GET)', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); + done(); + }); + }); + + it('returns CORS origin (OPTIONS)', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ method: 'options', url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.payload.length).to.equal(0); + expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); + done(); + }); + }); + + it('returns CORS without origin', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: [] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.not.exist(); + expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); + done(); + }); + }); + + it('override CORS origin', function (done) { + + var handler = function (request, reply) { + + return reply('ok').header('access-control-allow-origin', 'something'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); + done(); + }); + }); + + it('preserves CORS origin header when not locally configured', function (done) { + + var handler = function (request, reply) { + + return reply('ok').header('access-control-allow-origin', 'something'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: [] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.equal('something'); + done(); + }); + }); + + it('preserves CORS origin header when override disabled', function (done) { + + var handler = function (request, reply) { + + return reply('ok').header('access-control-allow-origin', 'something'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { override: false } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.equal('something'); + done(); + }); + }); + + it('merges CORS origin header when override is merge', function (done) { + + var handler = function (request, reply) { + + return reply('ok').header('access-control-allow-methods', 'something'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { additionalMethods: ['xyz'], override: 'merge' } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject('/', function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-methods']).to.equal('something,GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,xyz'); + done(); + }); + }); + + it('returns no CORS headers when route CORS disabled', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler, config: { cors: false } }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); + + it('does not return CORS for no origin without isOriginExposed', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/' }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.not.exist(); + expect(res.headers.vary).to.equal('origin'); + done(); + }); + }); + + it('hides CORS origin if no match found', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-origin']).to.not.exist(); + expect(res.headers.vary).to.equal('origin'); + done(); + }); + }); + + it('returns matching CORS origin', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); + expect(res.headers.vary).to.equal('x-test,origin'); + done(); + }); + }); + + it('returns origin header when matching against *', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['*'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); + expect(res.headers.vary).to.equal('x-test,origin'); + done(); + }); + }); + + it('returns * when matching is disabled', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['*'], matchOrigin: false } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('*'); + expect(res.headers.vary).to.equal('x-test'); + done(); + }); + }); + + it('returns matching CORS origin without exposing full list', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test', true); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); + expect(res.headers.vary).to.equal('x-test,origin'); + done(); + }); + }); + + it('returns matching CORS origin wildcard', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://www.a.com'); + expect(res.headers.vary).to.equal('x-test,origin'); + done(); + }); + }); + + it('returns matching CORS origin wildcard when more than one wildcard', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test', true); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.b.com', 'http://*.a.com'] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://www.a.com'); + expect(res.headers.vary).to.equal('x-test,origin'); + done(); + }); + }); + + it('returns all CORS origins when match is disabled', function (done) { + + var handler = function (request, reply) { + + return reply('Tada').header('vary', 'x-test'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'], matchOrigin: false } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('Tada'); + expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); + expect(res.headers.vary).to.equal('x-test'); + done(); + }); + }); + + it('does not set empty CORS expose headers', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { exposedHeaders: [] } } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ url: '/' }, function (res) { + + expect(res.result).to.exist(); + expect(res.result).to.equal('ok'); + expect(res.headers['access-control-allow-methods']).to.exist(); + expect(res.headers['access-control-expose-headers']).to.not.exist(); + done(); + }); + }); + }); +}); diff --git a/test/reply.js b/test/reply.js index 92188e3c1..004ec1260 100755 --- a/test/reply.js +++ b/test/reply.js @@ -221,14 +221,13 @@ describe('Reply', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { cors: { credentials: true } } }); + server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { expect(res.statusCode).to.equal(299); expect(res.result).to.equal(null); - expect(res.headers['access-control-allow-credentials']).to.equal('true'); done(); }); }); @@ -260,7 +259,7 @@ describe('Reply', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['test.example.com'] } } }); + server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); server.inject('/stream', function (res1) { @@ -268,14 +267,12 @@ describe('Reply', function () { expect(res1.result).to.equal('xy'); expect(res1.statusCode).to.equal(200); expect(res1.headers['cache-control']).to.equal('max-age=2, must-revalidate'); - expect(res1.headers['access-control-allow-origin']).to.equal('test.example.com'); server.inject({ method: 'HEAD', url: '/stream' }, function (res2) { expect(res2.result).to.equal(''); expect(res2.statusCode).to.equal(200); expect(res2.headers['cache-control']).to.equal('max-age=2, must-revalidate'); - expect(res2.headers['access-control-allow-origin']).to.equal('test.example.com'); done(); }); }); @@ -429,15 +426,13 @@ describe('Reply', function () { }; var server = new Hapi.Server({ debug: false }); - server.connection({ routes: { cors: { origin: ['test.example.com'] } } }); + server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); server.inject('/stream', function (res) { expect(res.result).to.equal('xy'); expect(res.statusCode).to.equal(299); - expect(res.headers['cache-control']).to.equal('max-age=2, must-revalidate'); - expect(res.headers['access-control-allow-origin']).to.equal('test.example.com'); done(); }); }); diff --git a/test/transmit.js b/test/transmit.js index 7d175ff58..35e10f3e5 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2142,406 +2142,6 @@ describe('transmission', function () { }); }); - describe('cors()', function () { - - it('returns CORS origin (route level)', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); - - server.inject('/', function (res1) { - - expect(res1.result).to.exist(); - expect(res1.result).to.equal('ok'); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); - - server.inject({ method: 'OPTIONS', url: '/' }, function (res2) { - - expect(res2.result).to.be.null(); - expect(res2.headers['access-control-allow-origin']).to.equal('*'); - done(); - }); - }); - }); - - it('returns CORS origin (GET)', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - done(); - }); - }); - - it('returns CORS origin (OPTIONS)', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ method: 'options', url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.statusCode).to.equal(200); - expect(res.payload.length).to.equal(0); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - done(); - }); - }); - - it('returns CORS without origin', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: [] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); - done(); - }); - }); - - it('override CORS origin', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - done(); - }); - }); - - it('preserves CORS origin header when not locally configured', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: [] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('something'); - done(); - }); - }); - - it('preserves CORS origin header when override disabled', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { override: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('something'); - done(); - }); - }); - - it('merges CORS origin header when override is merge', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-methods', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { additionalMethods: ['xyz'], override: 'merge' } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject('/', function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-methods']).to.equal('something,GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,xyz'); - done(); - }); - }); - - it('returns no CORS headers when route CORS disabled', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler, config: { cors: false } }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - done(); - }); - }); - - it('does not return CORS for no origin without isOriginExposed', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/' }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers.vary).to.equal('origin'); - done(); - }); - }); - - it('hides CORS origin if no match found', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers.vary).to.equal('origin'); - done(); - }); - }); - - it('returns matching CORS origin', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - - it('returns origin header when matching against *', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['*'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - - it('returns * when matching is disabled', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['*'], matchOrigin: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - expect(res.headers.vary).to.equal('x-test'); - done(); - }); - }); - - it('returns matching CORS origin without exposing full list', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test', true); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - - it('returns matching CORS origin wildcard', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.a.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - - it('returns matching CORS origin wildcard when more than one wildcard', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test', true); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.b.com', 'http://*.a.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.a.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - - it('returns all CORS origins when match is disabled', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'], matchOrigin: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - expect(res.headers.vary).to.equal('x-test'); - done(); - }); - }); - - it('does not set empty CORS expose headers', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { exposedHeaders: [] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/' }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-methods']).to.exist(); - expect(res.headers['access-control-expose-headers']).to.not.exist(); - done(); - }); - }); - }); - describe('cache()', function () { it('sets max-age value (method and route)', function (done) { From c93454c7852f76aca03e04a5dca07ee689ce86e2 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 19:41:35 -0700 Subject: [PATCH 0073/1139] Partial implementation for #2840 --- API.md | 6 +- lib/connection.js | 20 +----- lib/cors.js | 102 +++++++++++++++++++---------- lib/defaults.js | 10 --- lib/route.js | 23 +++---- lib/schema.js | 2 - test/cors.js | 159 +++++++++++++++++++++++++--------------------- test/reply.js | 1 + test/response.js | 1 - 9 files changed, 172 insertions(+), 152 deletions(-) diff --git a/API.md b/API.md index 2babc1223..c2a462dd7 100755 --- a/API.md +++ b/API.md @@ -2157,10 +2157,6 @@ following options: Defaults to `['Authorization', 'Content-Type', 'If-None-Match']`. - `additionalHeaders` - a strings array of additional headers to `headers`. Use this to keep the default headers in place. - - `methods` - a strings array of allowed HTTP methods ('Access-Control-Allow-Methods'). - Defaults to `['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS']`. - - `additionalMethods` - a strings array of additional methods to `methods`. Use this to - keep the default methods in place. - `exposedHeaders` - a strings array of exposed headers ('Access-Control-Expose-Headers'). Defaults to `['WWW-Authenticate', 'Server-Authorization']`. @@ -2170,7 +2166,7 @@ following options: ('Access-Control-Allow-Credentials'). Defaults to `false`. - `override` - if `false`, preserves existing CORS headers set manually before the response is sent. If set to `'merge'`, appends the configured values to the manually set - headers. Defaults to `true`. + headers (applies only to Access-Control-Expose-Headers). Defaults to `true`. - `ext` - defined a route-level [request extension points](#request-lifecycle) by setting the option to an object with a key for each of the desired extension points (`'onRequest'` diff --git a/lib/connection.js b/lib/connection.js index 6f87fa7ba..b50cac596 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -79,7 +79,6 @@ exports = module.exports = internals.Connection = function (server, options) { this.states = new Statehood.Definitions(this.settings.state); this.auth = new Auth(this); this._router = new Call.Router(this.settings.router); - this._corsPaths = {}; this._defaultRoutes(); this.plugins = {}; // Registered plugin APIs by plugin name @@ -388,7 +387,7 @@ internals.Connection.prototype._defaultRoutes = function () { method: 'notFound', path: '/{p*}', config: { - auth: false, // Override any defaults + auth: false, // Override any defaults handler: function (request, reply) { return reply(Boom.notFound()); @@ -402,7 +401,7 @@ internals.Connection.prototype._defaultRoutes = function () { method: 'badRequest', path: '/{p*}', config: { - auth: false, // Override any defaults + auth: false, // Override any defaults handler: function (request, reply) { return reply(Boom.badRequest()); @@ -413,19 +412,6 @@ internals.Connection.prototype._defaultRoutes = function () { this._router.special('badRequest', badRequest); if (this.settings.routes.cors) { - var optionsRoute = new Route({ - path: '/{p*}', - method: 'options', - config: { - auth: false, // Override any defaults - cors: this.settings.routes.cors, - handler: function (request, reply) { - - return reply(); - } - } - }, this, this.server); - - this._router.special('options', optionsRoute); + Cors.handler(this); } }; diff --git a/lib/cors.js b/lib/cors.js index 0fc6ee229..ac42af832 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -1,7 +1,9 @@ // Load modules +var Boom = require('boom'); var Hoek = require('hoek'); var Defaults = require('./defaults'); +var Route = null; // Delayed load due to circular dependency // Declare internals @@ -16,8 +18,8 @@ exports.route = function (options) { return false; } - settings._headers = settings.headers.concat(settings.additionalHeaders).join(','); - settings._methods = settings.methods.concat(settings.additionalMethods).join(','); + settings._headers = settings.headers.concat(settings.additionalHeaders); + settings._headersString = settings._headers.join(','); settings._exposedHeaders = settings.exposedHeaders.concat(settings.additionalExposedHeaders).join(','); if (settings.origin.length) { @@ -52,10 +54,10 @@ exports.route = function (options) { }; -exports.headers = function (response) { +exports.headers = function (response, options) { var request = response.request; - var settings = request.route.settings.cors; + var settings = options || request.route.settings.cors; if (!settings) { return; } @@ -81,7 +83,6 @@ exports.headers = function (response) { } var config = { override: !!settings.override }; // Value can be 'merge' - response._header('access-control-max-age', settings.maxAge, { override: settings.override }); if (settings.credentials) { response._header('access-control-allow-credentials', 'true', { override: settings.override }); @@ -93,9 +94,6 @@ exports.headers = function (response) { config.append = true; } - response._header('access-control-allow-methods', settings._methods, config); - response._header('access-control-allow-headers', settings._headers, config); - if (settings._exposedHeaders.length !== 0) { response._header('access-control-expose-headers', settings._exposedHeaders, config); } @@ -126,45 +124,81 @@ internals.matchOrigin = function (origin, settings) { }; -exports.options = function (route, connection, plugin) { +exports.options = function (route, connection, server) { - var settings = Hoek.clone(route.settings.cors); + if (route.method === 'options' || + !route.settings.cors) { - if (settings) { - delete settings._origin; - delete settings._exposedHeaders; - delete settings._headers; - delete settings._methods; + return; } - if (route.method === 'options' || - Hoek.deepEqual(connection.settings.routes.cors, settings)) { + exports.handler(connection); +}; + + +exports.handler = function (connection) { + + Route = Route || require('./route'); + if (connection._router.specials.options) { return; } - if (!settings) { - return; + var optionsRoute = new Route({ + path: '/{p*}', + method: 'options', + config: { + auth: false, // Override any defaults + cors: false, // CORS headers are set in handler() + handler: internals.handler + } + }, connection, connection.server); + + connection._router.special('options', optionsRoute); +}; + + +internals.handler = function (request, reply) { + + // Validate CORS preflight request + + var origin = request.headers.origin; + if (!origin) { + return reply(Boom.notFound()); } - var path = route.path; - if (connection._corsPaths[path]) { - Hoek.assert(Hoek.deepEqual(connection._corsPaths[path], settings), 'Cannot add multiple routes with different CORS options on different methods:', route.method.toUpperCase(), path); - return; + var method = request.headers['access-control-request-method']; + if (!method) { + return reply(Boom.notFound()); } - connection._corsPaths[path] = settings; + // Lookup route - connection._route({ - path: path, - method: 'options', - config: { - auth: false, // Override any defaults - cors: settings, - handler: function (request, reply) { + var route = request.connection.match(method, request.path, request.headers.host); + if (!route) { + return reply(Boom.notFound()); + } - return reply(); - } + var settings = route.settings.cors; + if (!settings) { + return reply(Boom.notFound()); + } + + // Validate allowed headers + + var headers = request.headers['access-control-request-headers']; + if (headers) { + headers = headers.split(/\s*,\s*/); + if (Hoek.intersect(headers, settings._headers).length !== headers.length) { + return reply(Boom.notFound()); } - }, plugin); + } + + // Reply with the route CORS headers + + var response = reply(); + exports.headers(response, settings); + response._header('access-control-allow-methods', method); + response._header('access-control-allow-headers', settings._headersString); + response._header('access-control-max-age', settings.maxAge); }; diff --git a/lib/defaults.js b/lib/defaults.js index b1fc5a82a..c2166fd9a 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -88,16 +88,6 @@ exports.cors = { 'If-None-Match' ], additionalHeaders: [], - methods: [ - 'GET', - 'HEAD', - 'POST', - 'PUT', - 'PATCH', - 'DELETE', - 'OPTIONS' - ], - additionalMethods: [], exposedHeaders: [ 'WWW-Authenticate', 'Server-Authorization' diff --git a/lib/route.js b/lib/route.js index c854b0239..d356a0a3a 100755 --- a/lib/route.js +++ b/lib/route.js @@ -49,9 +49,9 @@ exports = module.exports = internals.Route = function (options, connection, plug // Apply settings in order: {connection} <- {handler} <- {realm} <- {route} var handlerDefaults = Handler.defaults(method, handler, connection.server); - var base = Hoek.applyToDefaultsWithShallow(connection.settings.routes, handlerDefaults, ['bind']); - base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind']); - this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind']); + var base = Hoek.applyToDefaultsWithShallow(connection.settings.routes, handlerDefaults, ['bind', 'cors']); + base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind', 'cors']); + this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind', 'cors']); this.settings.handler = handler; this.settings = Schema.apply('routeConfig', this.settings, options.path); @@ -65,14 +65,6 @@ exports = module.exports = internals.Route = function (options, connection, plug this.method = method; this.plugin = plugin; - this.public = { - method: this.method, - path: this.path, - vhost: this.vhost, - realm: this.plugin.realm, - settings: this.settings - }; - this.settings.vhost = options.vhost; this.settings.plugins = this.settings.plugins || {}; // Route-specific plugins settings, namespaced using plugin name this.settings.app = this.settings.app || {}; // Route-specific application settings @@ -83,6 +75,15 @@ exports = module.exports = internals.Route = function (options, connection, plug this.params = this._analysis.params; this.fingerprint = this._analysis.fingerprint; + this.public = { + method: this.method, + path: this.path, + vhost: this.vhost, + realm: this.plugin.realm, + settings: this.settings, + fingerprint: this.fingerprint + }; + // Validation var validation = this.settings.validate; diff --git a/lib/schema.js b/lib/schema.js index ab529eeb1..58ea7a8d9 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -82,8 +82,6 @@ internals.routeBase = Joi.object({ maxAge: Joi.number(), headers: Joi.array(), additionalHeaders: Joi.array(), - methods: Joi.array(), - additionalMethods: Joi.array(), exposedHeaders: Joi.array(), additionalExposedHeaders: Joi.array(), credentials: Joi.boolean(), diff --git a/test/cors.js b/test/cors.js index 9678bc9a2..4289d32d5 100755 --- a/test/cors.js +++ b/test/cors.js @@ -32,7 +32,7 @@ describe('CORS', function () { server.connection({ routes: { cors: false } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/' }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); done(); @@ -50,9 +50,9 @@ describe('CORS', function () { server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/' }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { - expect(res.headers['access-control-allow-origin']).to.equal('*'); + expect(res.headers['access-control-allow-origin']).to.equal('http://example.com/'); done(); }); }); @@ -69,15 +69,13 @@ describe('CORS', function () { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/b', handler: handler }); - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); + expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.statusCode).to.equal(404); expect(res2.headers['access-control-allow-origin']).to.not.exist(); @@ -99,21 +97,19 @@ describe('CORS', function () { server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/c', handler: handler }); - expect(server.table()[0].table).to.have.length(5); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); + expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.statusCode).to.equal(200); expect(res2.result).to.be.null(); - expect(res2.headers['access-control-allow-origin']).to.equal('*'); + expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/c' }, function (res3) { + server.inject({ method: 'OPTIONS', url: '/c', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res3) { expect(res3.statusCode).to.equal(404); expect(res3.headers['access-control-allow-origin']).to.not.exist(); @@ -135,54 +131,11 @@ describe('CORS', function () { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(200); expect(res.result).to.be.null(); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - done(); - }); - }); - - it('errors on different headers on multiple routes with same path', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection(); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); - expect(function () { - - server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: { origin: ['b'] } } }); - }).to.throw('Cannot add multiple routes with different CORS options on different methods: POST /a'); - - done(); - }); - - it('reuses connections CORS route when route has same settings', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: true } }); - server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); - server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); - - expect(server.table()[0].table).to.have.length(2); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res) { - - expect(res.statusCode).to.equal(200); - expect(res.result).to.be.null(); - expect(res.headers['access-control-allow-origin']).to.equal('*'); + expect(res.headers['access-control-allow-origin']).to.equal('http://example.com/'); done(); }); }); @@ -199,15 +152,13 @@ describe('CORS', function () { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); server.route({ method: 'GET', path: '/b', handler: handler }); - expect(server.table()[0].table).to.have.length(3); - - server.inject({ method: 'OPTIONS', url: '/a' }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); expect(res1.headers['access-control-allow-origin']).to.equal('a'); - server.inject({ method: 'OPTIONS', url: '/b' }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.statusCode).to.equal(200); expect(res2.result).to.be.null(); @@ -255,10 +206,10 @@ describe('CORS', function () { expect(res1.result).to.equal('ok'); expect(res1.headers['access-control-allow-origin']).to.equal('*'); - server.inject({ method: 'OPTIONS', url: '/' }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.result).to.be.null(); - expect(res2.headers['access-control-allow-origin']).to.equal('*'); + expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); done(); }); }); @@ -295,7 +246,7 @@ describe('CORS', function () { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'options', url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://x.example.com', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(200); expect(res.payload.length).to.equal(0); @@ -320,7 +271,6 @@ describe('CORS', function () { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); done(); }); }); @@ -389,18 +339,18 @@ describe('CORS', function () { var handler = function (request, reply) { - return reply('ok').header('access-control-allow-methods', 'something'); + return reply('ok').header('access-control-expose-headers', 'something'); }; var server = new Hapi.Server(); - server.connection({ routes: { cors: { additionalMethods: ['xyz'], override: 'merge' } } }); + server.connection({ routes: { cors: { additionalExposedHeaders: ['xyz'], override: 'merge' } } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-methods']).to.equal('something,GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,xyz'); + expect(res.headers['access-control-expose-headers']).to.equal('something,WWW-Authenticate,Server-Authorization,xyz'); done(); }); }); @@ -629,10 +579,75 @@ describe('CORS', function () { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-methods']).to.exist(); expect(res.headers['access-control-expose-headers']).to.not.exist(); done(); }); }); }); + + describe('options()', function () { + + it('ignores OPTIONS route', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'OPTIONS', + path: '/', + handler: function (request, reply) { } + }); + + expect(server.connections[0]._router.special.options).to.not.exist(); + done(); + }); + }); + + describe('handler()', function () { + + it('errors on missing origin header', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { } + }); + + server.inject({ method: 'OPTIONS', url: '/', headers: { 'access-control-request-method': 'GET' } }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + + it('errors on missing access-control-request-method header', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { } + }); + + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/' } }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + + it('errors on missing route', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + }); }); diff --git a/test/reply.js b/test/reply.js index 004ec1260..661deae97 100755 --- a/test/reply.js +++ b/test/reply.js @@ -227,6 +227,7 @@ describe('Reply', function () { server.inject('/', function (res) { expect(res.statusCode).to.equal(299); + expect(res.headers['content-length']).to.equal(0); expect(res.result).to.equal(null); done(); }); diff --git a/test/response.js b/test/response.js index 29b600a7b..83026a236 100755 --- a/test/response.js +++ b/test/response.js @@ -68,7 +68,6 @@ describe('Response', function () { expect(res.headers['content-type']).to.equal('text/plain; something=something, charset=ISO-8859-1'); expect(res.headers['access-control-allow-origin']).to.equal('*'); expect(res.headers['access-control-allow-credentials']).to.not.exist(); - expect(res.headers['access-control-allow-methods']).to.equal('GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS'); expect(res.headers['set-cookie']).to.deep.equal(['abc=123', 'sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'test=123', 'empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'always=present']); expect(res.headers.vary).to.equal('x-control,origin'); expect(res.headers.combo).to.equal('o-k'); From 7283931abc4a167a9493d38fa7cbbbab91a5f1f2 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 20:02:02 -0700 Subject: [PATCH 0074/1139] Fix test --- test/request.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/request.js b/test/request.js index 0715eab2e..7f8f49408 100755 --- a/test/request.js +++ b/test/request.js @@ -1461,7 +1461,7 @@ describe('Request', function () { it('does not return an error when server is responding when the timeout occurs', function (done) { var ended = false; - var respondingHandler = function (request, reply) { + var handler = function (request, reply) { var s = new Stream.PassThrough(); reply(s); @@ -1471,7 +1471,7 @@ describe('Request', function () { setTimeout(function () { ended = true; - s.emit('end'); + s.end(); }, 150); }; @@ -1479,7 +1479,7 @@ describe('Request', function () { var server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 100 } } }); - server.route({ method: 'GET', path: '/', config: { handler: respondingHandler } }); + server.route({ method: 'GET', path: '/', config: { handler: handler } }); server.start(function (err) { expect(err).to.not.exist(); From cece789c1a4e9bc012d07e5634f573bc743b2554 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 15 Oct 2015 23:13:51 -0700 Subject: [PATCH 0075/1139] Rework CORS. Closes #2840 --- API.md | 10 -- lib/cors.js | 165 +++++++++++------------ lib/defaults.js | 5 +- lib/schema.js | 7 +- test/cors.js | 336 +++++++++++++++++++---------------------------- test/request.js | 30 ++++- test/response.js | 6 +- 7 files changed, 237 insertions(+), 322 deletions(-) diff --git a/API.md b/API.md index c2a462dd7..358afd580 100755 --- a/API.md +++ b/API.md @@ -2143,13 +2143,6 @@ following options: The array can contain any combination of fully qualified origins along with origin strings containing a wildcard '*' character, or a single `'*'` origin string. Defaults to any origin `['*']`. - - `matchOrigin` - if `true`, matches the value of the incoming 'Origin' header to the - list of `origin` values ('*' matches anything) and if a match is found, uses that as - the value of the 'Access-Control-Allow-Origin' response header. When false, the - `origin` config is returned as-is. Defaults to `true`. - - `isOriginExposed` - if `false`, prevents the connection from returning the full list - of non-wildcard `origin` values if the incoming origin header does not match any of - the values. Has no impact if `matchOrigin` is set to `false`. Defaults to `true`. - `maxAge` - number of seconds the browser should cache the CORS response ('Access-Control-Max-Age'). The greater the value, the longer it will take before the browser checks for changes in policy. Defaults to `86400` (one day). @@ -2164,9 +2157,6 @@ following options: `exposedHeaders`. Use this to keep the default headers in place. - `credentials` - if `true`, allows user credentials to be sent ('Access-Control-Allow-Credentials'). Defaults to `false`. - - `override` - if `false`, preserves existing CORS headers set manually before the - response is sent. If set to `'merge'`, appends the configured values to the manually set - headers (applies only to Access-Control-Expose-Headers). Defaults to `true`. - `ext` - defined a route-level [request extension points](#request-lifecycle) by setting the option to an object with a key for each of the desired extension points (`'onRequest'` diff --git a/lib/cors.js b/lib/cors.js index ac42af832..01f33ce00 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -22,105 +22,28 @@ exports.route = function (options) { settings._headersString = settings._headers.join(','); settings._exposedHeaders = settings.exposedHeaders.concat(settings.additionalExposedHeaders).join(','); - if (settings.origin.length) { + if (settings.origin.indexOf('*') !== -1) { + Hoek.assert(settings.origin.length === 1, 'Cannot specify cors.origin * together with other values'); + settings._origin = true; + } + else { settings._origin = { - any: false, qualified: [], - qualifiedString: '', wildcards: [] }; - if (settings.origin.indexOf('*') !== -1) { - Hoek.assert(settings.origin.length === 1, 'Cannot specify cors.origin * together with other values'); - settings._origin.any = true; - } - else { - for (var c = 0, cl = settings.origin.length; c < cl; ++c) { - var origin = settings.origin[c]; - if (origin.indexOf('*') !== -1) { - settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); - } - else { - settings._origin.qualified.push(origin); - } + for (var c = 0, cl = settings.origin.length; c < cl; ++c) { + var origin = settings.origin[c]; + if (origin.indexOf('*') !== -1) { + settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); } - - Hoek.assert(settings.matchOrigin || !settings._origin.wildcards.length, 'Cannot include wildcard origin values with matchOrigin disabled'); - settings._origin.qualifiedString = settings._origin.qualified.join(' '); - } - } - - return settings; -}; - - -exports.headers = function (response, options) { - - var request = response.request; - var settings = options || request.route.settings.cors; - if (!settings) { - return; - } - - if (settings._origin && - (!response.headers['access-control-allow-origin'] || settings.override)) { - - if (settings.matchOrigin) { - response.vary('origin'); - if (internals.matchOrigin(request.headers.origin, settings)) { - response._header('access-control-allow-origin', request.headers.origin); - } - else if (settings.isOriginExposed) { - response._header('access-control-allow-origin', settings._origin.any ? '*' : settings._origin.qualifiedString); + else { + settings._origin.qualified.push(origin); } } - else if (settings._origin.any) { - response._header('access-control-allow-origin', '*'); - } - else { - response._header('access-control-allow-origin', settings._origin.qualifiedString); - } - } - - var config = { override: !!settings.override }; // Value can be 'merge' - - if (settings.credentials) { - response._header('access-control-allow-credentials', 'true', { override: settings.override }); - } - - // Appended headers - - if (settings.override === 'merge') { - config.append = true; - } - - if (settings._exposedHeaders.length !== 0) { - response._header('access-control-expose-headers', settings._exposedHeaders, config); - } -}; - - -internals.matchOrigin = function (origin, settings) { - - if (!origin) { - return false; - } - - if (settings._origin.any) { - return true; - } - - if (settings._origin.qualified.indexOf(origin) !== -1) { - return true; - } - - for (var i = 0, il = settings._origin.wildcards.length; i < il; ++i) { - if (origin.match(settings._origin.wildcards[i])) { - return true; - } } - return false; + return settings; }; @@ -184,6 +107,12 @@ internals.handler = function (request, reply) { return reply(Boom.notFound()); } + // Validate Origin header + + if (!internals.matchOrigin(origin, settings)) { + return reply(Boom.notFound()); + } + // Validate allowed headers var headers = request.headers['access-control-request-headers']; @@ -197,8 +126,64 @@ internals.handler = function (request, reply) { // Reply with the route CORS headers var response = reply(); - exports.headers(response, settings); + response._header('access-control-allow-origin', request.headers.origin); response._header('access-control-allow-methods', method); response._header('access-control-allow-headers', settings._headersString); response._header('access-control-max-age', settings.maxAge); + + if (settings.credentials) { + response._header('access-control-allow-credentials', 'true'); + } + + if (settings._exposedHeaders) { + response._header('access-control-expose-headers', settings._exposedHeaders); + } +}; + + +exports.headers = function (response) { + + var request = response.request; + var settings = request.route.settings.cors; + if (!settings) { + return; + } + + response.vary('origin'); + + if (!request.headers.origin || + !internals.matchOrigin(request.headers.origin, settings)) { + + return; + } + + response._header('access-control-allow-origin', request.headers.origin); + + if (settings.credentials) { + response._header('access-control-allow-credentials', 'true'); + } + + if (settings._exposedHeaders) { + response._header('access-control-expose-headers', settings._exposedHeaders, { append: true }); + } +}; + + +internals.matchOrigin = function (origin, settings) { + + if (settings._origin === true) { + return true; + } + + if (settings._origin.qualified.indexOf(origin) !== -1) { + return true; + } + + for (var i = 0, il = settings._origin.wildcards.length; i < il; ++i) { + if (origin.match(settings._origin.wildcards[i])) { + return true; + } + } + + return false; }; diff --git a/lib/defaults.js b/lib/defaults.js index c2166fd9a..e4d3584de 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -79,8 +79,6 @@ exports.security = { exports.cors = { origin: ['*'], - isOriginExposed: true, // Return the list of supported origins if incoming origin does not match - matchOrigin: true, // Attempt to match incoming origin against allowed values and return narrow response maxAge: 86400, // One day headers: [ 'Authorization', @@ -93,6 +91,5 @@ exports.cors = { 'Server-Authorization' ], additionalExposedHeaders: [], - credentials: false, - override: true + credentials: false }; diff --git a/lib/schema.js b/lib/schema.js index 58ea7a8d9..d31349586 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -76,16 +76,13 @@ internals.routeBase = Joi.object({ statuses: Joi.array().items(Joi.number().integer().min(200)).min(1) }), cors: Joi.object({ - origin: Joi.array(), - matchOrigin: Joi.boolean(), - isOriginExposed: Joi.boolean(), + origin: Joi.array().min(1), maxAge: Joi.number(), headers: Joi.array(), additionalHeaders: Joi.array(), exposedHeaders: Joi.array(), additionalExposedHeaders: Joi.array(), - credentials: Joi.boolean(), - override: Joi.boolean().allow('merge') + credentials: Joi.boolean() }) .allow(false, true), ext: Joi.object({ diff --git a/test/cors.js b/test/cors.js index 4289d32d5..d9d8663f3 100755 --- a/test/cors.js +++ b/test/cors.js @@ -152,13 +152,13 @@ describe('CORS', function () { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); server.route({ method: 'GET', path: '/b', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'a', 'access-control-request-method': 'GET' } }, function (res1) { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); expect(res1.headers['access-control-allow-origin']).to.equal('a'); - server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'b', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.statusCode).to.equal(200); expect(res2.result).to.be.null(); @@ -179,7 +179,7 @@ describe('CORS', function () { server.connection({ routes: { cors: { credentials: true } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res) { expect(res.result).to.equal(null); expect(res.headers['access-control-allow-credentials']).to.equal('true'); @@ -200,11 +200,11 @@ describe('CORS', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); - server.inject('/', function (res1) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res1) { expect(res1.result).to.exist(); expect(res1.result).to.equal('ok'); - expect(res1.headers['access-control-allow-origin']).to.equal('*'); + expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { @@ -223,14 +223,14 @@ describe('CORS', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); + server.connection({ routes: { cors: { origin: ['http://x.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); + expect(res.headers['access-control-allow-origin']).to.equal('http://x.example.com'); done(); }); }); @@ -246,96 +246,16 @@ describe('CORS', function () { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://x.example.com', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://test.example.com', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(200); expect(res.payload.length).to.equal(0); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - done(); - }); - }); - - it('returns CORS without origin', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: [] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - done(); - }); - }); - - it('override CORS origin', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - done(); - }); - }); - - it('preserves CORS origin header when not locally configured', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: [] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('something'); - done(); - }); - }); - - it('preserves CORS origin header when override disabled', function (done) { - - var handler = function (request, reply) { - - return reply('ok').header('access-control-allow-origin', 'something'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { override: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.equal('something'); + expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com'); done(); }); }); - it('merges CORS origin header when override is merge', function (done) { + it('merges CORS access-control-expose-headers header', function (done) { var handler = function (request, reply) { @@ -343,10 +263,10 @@ describe('CORS', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { cors: { additionalExposedHeaders: ['xyz'], override: 'merge' } } }); + server.connection({ routes: { cors: { additionalExposedHeaders: ['xyz'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res) { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); @@ -375,48 +295,6 @@ describe('CORS', function () { }); }); - it('does not return CORS for no origin without isOriginExposed', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/' }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers.vary).to.equal('origin'); - done(); - }); - }); - - it('hides CORS origin if no match found', function (done) { - - var handler = function (request, reply) { - - return reply('ok'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-allow-origin']).to.not.exist(); - expect(res.headers.vary).to.equal('origin'); - done(); - }); - }); - it('returns matching CORS origin', function (done) { var handler = function (request, reply) { @@ -459,48 +337,6 @@ describe('CORS', function () { }); }); - it('returns * when matching is disabled', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['*'], matchOrigin: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - expect(res.headers.vary).to.equal('x-test'); - done(); - }); - }); - - it('returns matching CORS origin without exposing full list', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test', true); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { isOriginExposed: false, origin: ['http://test.example.com', 'http://www.example.com'] } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://www.example.com'); - expect(res.headers.vary).to.equal('x-test,origin'); - done(); - }); - }); - it('returns matching CORS origin wildcard', function (done) { var handler = function (request, reply) { @@ -543,27 +379,6 @@ describe('CORS', function () { }); }); - it('returns all CORS origins when match is disabled', function (done) { - - var handler = function (request, reply) { - - return reply('Tada').header('vary', 'x-test'); - }; - - var server = new Hapi.Server(); - server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'], matchOrigin: false } } }); - server.route({ method: 'GET', path: '/', handler: handler }); - - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { - - expect(res.result).to.exist(); - expect(res.result).to.equal('Tada'); - expect(res.headers['access-control-allow-origin']).to.equal('http://test.example.com http://www.example.com'); - expect(res.headers.vary).to.equal('x-test'); - done(); - }); - }); - it('does not set empty CORS expose headers', function (done) { var handler = function (request, reply) { @@ -575,12 +390,17 @@ describe('CORS', function () { server.connection({ routes: { cors: { exposedHeaders: [] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/' }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { - expect(res.result).to.exist(); - expect(res.result).to.equal('ok'); - expect(res.headers['access-control-expose-headers']).to.not.exist(); - done(); + expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); + expect(res1.headers['access-control-expose-headers']).to.not.exist(); + + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + + expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); + expect(res2.headers['access-control-expose-headers']).to.not.exist(); + done(); + }); }); }); }); @@ -649,5 +469,117 @@ describe('CORS', function () { done(); }); }); + + it('errors on mismatching origin header', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { origin: ['a'] } } }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { } + }); + + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + + it('matches allowed headers', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ + method: 'OPTIONS', + url: '/', + headers: { + origin: 'http://test.example.com', + 'access-control-request-method': 'GET', + 'access-control-request-headers': 'Authorization' + } + }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['access-control-allow-headers']).to.equal('Authorization,Content-Type,If-None-Match'); + done(); + }); + }); + + it('errors on disallowed headers', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ + method: 'OPTIONS', + url: '/', + headers: { + origin: 'http://test.example.com', + 'access-control-request-method': 'GET', + 'access-control-request-headers': 'X' + } + }, function (res) { + + expect(res.statusCode).to.equal(404); + done(); + }); + }); + + it('allows credentials', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: { credentials: true } } }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { } + }); + + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['access-control-allow-credentials']).to.equal('true'); + done(); + }); + }); + }); + + describe('headers()', function () { + + it('skips CORS when missing origin header', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply('ok'); + } + }); + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['access-control-allow-origin']).to.not.exist(); + done(); + }); + }); }); }); diff --git a/test/request.js b/test/request.js index 7f8f49408..e5962b03a 100755 --- a/test/request.js +++ b/test/request.js @@ -1463,16 +1463,32 @@ describe('Request', function () { var ended = false; var handler = function (request, reply) { - var s = new Stream.PassThrough(); - reply(s); + var TestStream = function () { - s.write(new Buffer(10240)); + Stream.Readable.call(this); + }; - setTimeout(function () { + Hoek.inherits(TestStream, Stream.Readable); - ended = true; - s.end(); - }, 150); + TestStream.prototype._read = function (size) { + + var self = this; + + if (this.isDone) { + return; + } + this.isDone = true; + + self.push('Hello'); + + setTimeout(function () { + + self.push(null); + ended = true; + }, 150); + }; + + return reply(new TestStream()); }; var timer = new Hoek.Bench(); diff --git a/test/response.js b/test/response.js index 83026a236..0f5d80647 100755 --- a/test/response.js +++ b/test/response.js @@ -48,7 +48,7 @@ describe('Response', function () { }; var server = new Hapi.Server(); - server.connection({ routes: { cors: true } }); + server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { expiresIn: 9999 } } }); server.state('sid', { encoding: 'base64' }); server.state('always', { autoValue: 'present' }); @@ -66,10 +66,8 @@ describe('Response', function () { expect(res.result).to.equal('text'); expect(res.headers['cache-control']).to.equal('max-age=1, must-revalidate, private'); expect(res.headers['content-type']).to.equal('text/plain; something=something, charset=ISO-8859-1'); - expect(res.headers['access-control-allow-origin']).to.equal('*'); - expect(res.headers['access-control-allow-credentials']).to.not.exist(); expect(res.headers['set-cookie']).to.deep.equal(['abc=123', 'sid=YWJjZGVmZzEyMzQ1Ng==', 'other=something; Secure', 'x=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'test=123', 'empty=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT', 'always=present']); - expect(res.headers.vary).to.equal('x-control,origin'); + expect(res.headers.vary).to.equal('x-control'); expect(res.headers.combo).to.equal('o-k'); done(); }); From a48ca47aaf1c1ef3c35477c56fd79f282ad585a7 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 00:01:45 -0700 Subject: [PATCH 0076/1139] Update qs. Closes #2847 --- API.md | 2 +- README.md | 2 +- npm-shrinkwrap.json | 4 ++-- package.json | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/API.md b/API.md index 358afd580..a35eb1938 100755 --- a/API.md +++ b/API.md @@ -1,4 +1,4 @@ -# 10.5.x API Reference +# 11.0.x API Reference - [Server](#server) - [`new Server([options])`](#new-serveroptions) diff --git a/README.md b/README.md index 0b85157c6..c11f1fba4 100755 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Lead Maintainer: [Eran Hammer](https://github.com/hueniverse) authentication, and other essential facilities for building web and services applications. **hapi** enables developers to focus on writing reusable application logic in a highly modular and prescriptive approach. -Development version: **10.5.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) +Development version: **11.0.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed)) [![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg)](http://travis-ci.org/hapijs/hapi) For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 72dce0a94..8dcef55b2 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "10.5.0", + "version": "11.0.0", "dependencies": { "accept": { "version": "1.1.0" @@ -61,7 +61,7 @@ "version": "1.0.0" }, "qs": { - "version": "4.0.0" + "version": "5.2.0" }, "shot": { "version": "1.7.0" diff --git a/package.json b/package.json index 4169795e2..d1bde7f9d 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "10.5.0", + "version": "11.0.0", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" @@ -33,7 +33,7 @@ "kilt": "^1.1.x", "mimos": "2.x.x", "peekaboo": "1.x.x", - "qs": "4.x.x", + "qs": "5.x.x", "shot": "^1.7.x", "statehood": "2.x.x", "subtext": "2.x.x", From 8e462b5e4fc1c599c3c05fe5186fbeaafe751a05 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 00:06:10 -0700 Subject: [PATCH 0077/1139] Remove duplicate request id. Closes #2807 --- lib/request.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/request.js b/lib/request.js index c10de9c39..8e83cf649 100755 --- a/lib/request.js +++ b/lib/request.js @@ -191,7 +191,6 @@ internals.Request = function (connection, req, res, options) { // Log request var about = { - id: this.id, method: this.method, url: this.url.href, agent: this.raw.req.headers['user-agent'] From 76b0a58509d88952c0910d89076ea23ceb551939 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 00:16:46 -0700 Subject: [PATCH 0078/1139] Remove server.after(). Closes #2814 --- API.md | 37 ------ lib/plugin.js | 16 +-- test/plugin.js | 294 +++++++++++++++++++++--------------------------- test/protect.js | 2 +- 4 files changed, 129 insertions(+), 220 deletions(-) diff --git a/API.md b/API.md index a35eb1938..0804dd942 100755 --- a/API.md +++ b/API.md @@ -16,7 +16,6 @@ - [`server.root`](#serverroot) - [`server.settings`](#serversettings) - [`server.version`](#serverversion) - - [`server.after(method, [options])`](#serveraftermethod-options) - [`server.auth.default(options)`](#serverauthdefaultoptions) - [`server.auth.scheme(name, scheme)`](#serverauthschemename-scheme) - [`server.auth.strategy(name, scheme, [mode], [options])`](#serverauthstrategyname-scheme-mode-options) @@ -471,41 +470,6 @@ var server = new Hapi.Server(); // server.version === '8.0.0' ``` -### `server.after(method, [options])` - -Adds a method to be called after all the plugin dependencies have been registered and before the -server starts (only called if the server is started) where: -- `method` - the method with signature `function(server, next)` where: - - `server` - server object the `after()` method was called on. - - `next` - the callback function the method must call to return control over to the application - and complete the registration process. The function signature is `function(err)` where: - - `err` - internal error which is returned back via the - [`server.start()`](#serverstartcallback) callback. -- `options` - an optional object where: - - `after` - a string or array of string with the plugin names to call this method after - their `after()` methods. There is no requirement for the other [plugins](#plugins) to be - registered. Setting dependencies only arranges the after methods in the specified order. - -The `server.after()` method is identical to setting a server extension point on `'onPreStart'`. - -```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); -server.connection({ port: 80 }); - -server.after(function (srv, next) { - - // Perform some pre-start logic - - next(); -}); - -server.start(function (err) { - - // After method already executed -}); -``` - ### `server.auth.default(options)` Sets a default strategy which is applied to every route where: @@ -1827,7 +1791,6 @@ server.on('route', function (route, connection, server) { }); ``` - #### Internal events The following logs are generated automatically by the framework. Each event can be identified by diff --git a/lib/plugin.js b/lib/plugin.js index 507181b7e..160a5a89f 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -323,20 +323,6 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback }; -internals.Plugin.prototype.after = function (method, options) { - - options = options || {}; - - if (Array.isArray(options) || - typeof options === 'string') { // For backwards compatibility - - options = { after: options }; - } - - this.ext('onPreStart', method, options); -}; - - internals.Plugin.prototype.bind = function (context) { Hoek.assert(typeof context === 'object', 'bind must be an object'); @@ -405,7 +391,7 @@ internals.Plugin.prototype.dependency = function (dependencies, after) { this.root._dependencies.push({ plugin: this.realm.plugin, connections: this.connections, deps: dependencies }); if (after) { - this.after(after, dependencies); + this.ext('onPreStart', after, { after: dependencies }); } }; diff --git a/test/plugin.js b/test/plugin.js index a5ae338db..7d9452371 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1864,173 +1864,6 @@ describe('Plugin', function () { }); }); - describe('after()', function () { - - it('calls method after plugin', function (done) { - - var x = function (srv, options, next) { - - srv.expose('a', 'b'); - return next(); - }; - - x.attributes = { - name: 'x' - }; - - var server = new Hapi.Server(); - server.connection(); - - expect(server.plugins.x).to.not.exist(); - - var called = false; - server.after(function (srv, next) { - - expect(srv.plugins.x.a).to.equal('b'); - called = true; - return next(); - }, { after: 'x' }); - - server.register(x, function (err) { - - expect(err).to.not.exist(); - server.initialize(function (err) { - - expect(err).to.not.exist(); - expect(called).to.be.true(); - done(); - }); - }); - }); - - it('calls method after plugin (legacy)', function (done) { - - var x = function (srv, options, next) { - - srv.expose('a', 'b'); - return next(); - }; - - x.attributes = { - name: 'x' - }; - - var server = new Hapi.Server(); - server.connection(); - - expect(server.plugins.x).to.not.exist(); - - var called = false; - server.after(function (srv, next) { - - expect(srv.plugins.x.a).to.equal('b'); - called = true; - return next(); - }, 'x'); - - server.register(x, function (err) { - - expect(err).to.not.exist(); - server.initialize(function (err) { - - expect(err).to.not.exist(); - expect(called).to.be.true(); - done(); - }); - }); - }); - - it('calls method before start', function (done) { - - var server = new Hapi.Server(); - server.connection(); - - var called = false; - server.after(function (srv, next) { - - called = true; - return next(); - }); - - server.initialize(function (err) { - - expect(err).to.not.exist(); - expect(called).to.be.true(); - done(); - }); - }); - - it('calls method before start even if plugin not registered', function (done) { - - var server = new Hapi.Server(); - server.connection(); - - var called = false; - server.after(function (srv, next) { - - called = true; - return next(); - }, { after: 'x' }); - - server.initialize(function (err) { - - expect(err).to.not.exist(); - expect(called).to.be.true(); - done(); - }); - }); - - it('fails to start server when after method fails', function (done) { - - var test = function (srv, options, next) { - - srv.after(function (inner, finish) { - - return finish(); - }); - - srv.after(function (inner, finish) { - - return finish(new Error('Not in the mood')); - }); - - return next(); - }; - - test.attributes = { - name: 'test' - }; - - var server = new Hapi.Server(); - server.connection(); - server.register(test, function (err) { - - expect(err).to.not.exist(); - server.initialize(function (err) { - - expect(err).to.exist(); - done(); - }); - }); - }); - - it('errors when added after initialization', function (done) { - - var server = new Hapi.Server(); - server.connection(); - - server.initialize(function (err) { - - expect(function () { - - server.after(function () { }); - }).to.throw('Cannot add onPreStart (after) extension after the server was initialized'); - - done(); - }); - }); - }); - describe('auth', function () { it('adds auth strategy via plugin', function (done) { @@ -3237,6 +3070,133 @@ describe('Plugin', function () { }); }); }); + + it('calls method after plugin', function (done) { + + var x = function (srv, options, next) { + + srv.expose('a', 'b'); + return next(); + }; + + x.attributes = { + name: 'x' + }; + + var server = new Hapi.Server(); + server.connection(); + + expect(server.plugins.x).to.not.exist(); + + var called = false; + server.ext('onPreStart', function (srv, next) { + + expect(srv.plugins.x.a).to.equal('b'); + called = true; + return next(); + }, { after: 'x' }); + + server.register(x, function (err) { + + expect(err).to.not.exist(); + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(called).to.be.true(); + done(); + }); + }); + }); + + it('calls method before start', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var called = false; + server.ext('onPreStart', function (srv, next) { + + called = true; + return next(); + }); + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(called).to.be.true(); + done(); + }); + }); + + it('calls method before start even if plugin not registered', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var called = false; + server.ext('onPreStart', function (srv, next) { + + called = true; + return next(); + }, { after: 'x' }); + + server.initialize(function (err) { + + expect(err).to.not.exist(); + expect(called).to.be.true(); + done(); + }); + }); + + it('fails to start server when after method fails', function (done) { + + var test = function (srv, options, next) { + + srv.ext('onPreStart', function (inner, finish) { + + return finish(); + }); + + srv.ext('onPreStart', function (inner, finish) { + + return finish(new Error('Not in the mood')); + }); + + return next(); + }; + + test.attributes = { + name: 'test' + }; + + var server = new Hapi.Server(); + server.connection(); + server.register(test, function (err) { + + expect(err).to.not.exist(); + server.initialize(function (err) { + + expect(err).to.exist(); + done(); + }); + }); + }); + + it('errors when added after initialization', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + server.initialize(function (err) { + + expect(function () { + + server.ext('onPreStart', function () { }); + }).to.throw('Cannot add onPreStart (after) extension after the server was initialized'); + + done(); + }); + }); }); describe('handler()', function () { diff --git a/test/protect.js b/test/protect.js index 14134e6e2..634924fae 100755 --- a/test/protect.js +++ b/test/protect.js @@ -111,7 +111,7 @@ describe('Protect', function () { var test = function (srv, options, next) { - srv.after(function (plugin, afterNext) { + srv.ext('onPreStart', function (plugin, afterNext) { var client = new Client(); // Created in the global domain plugin.bind({ client: client }); From c81dae655d9fe4631f140a1ddaf25f263a294374 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 10:50:28 -0700 Subject: [PATCH 0079/1139] Validate non-objects. Closes #2848 --- API.md | 15 +++++----- lib/validation.js | 6 ++-- test/response.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++ test/validation.js | 71 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 11 deletions(-) diff --git a/API.md b/API.md index 0804dd942..afd872ccb 100755 --- a/API.md +++ b/API.md @@ -2209,9 +2209,8 @@ following options: - `pre` - an array with [route prerequisites](#route-prerequisites) methods which are executed in serial or in parallel before the handler is called. -- `response` - validation rules for the outgoing response payload (response body). Can only - validate object response: - - `schema` - the default response object validation rules (for all non-error responses) +- `response` - processing rules for the outgoing response: + - `schema` - the default response payload validation rules (for all non-error responses) expressed as one of: - `true` - any payload allowed (no validation performed). This is the default. - `false` - no payload allowed. @@ -2222,17 +2221,17 @@ following options: - `options` - the server validation options, merged with an object containing the request's headers, params, payload, and auth credentials object and isAuthenticated flag. - `next(err)` - the callback function called when validation is completed. - - `status` - HTTP status-code-specific validation rules. The `status` key is set to an + - `status` - HTTP status-code-specific payload validation rules. The `status` key is set to an object where each key is a 3 digit HTTP status code and the value has the same definition as `schema`. If a response status code is not present in the `status` object, the `schema` definition is used, expect for errors which are not validated by default. - - `sample` - the percent of responses validated (0 - 100). Set to `0` to disable all - validation. Defaults to `100` (all responses). - - `failAction` - defines what to do when a response fails validation. Options are: + - `sample` - the percent of response payloads validated (0 - 100). Set to `0` to disable all + validation. Defaults to `100` (all response payloads). + - `failAction` - defines what to do when a response fails payload validation. Options are: - `error` - return an Internal Server Error (500) error response. This is the default value. - `log` - log the error but send the response. - - `modify` - if `true`, applies the validation rule changes to the response. Defaults to + - `modify` - if `true`, applies the validation rule changes to the response payload. Defaults to `false`. - `options` - options to pass to [Joi](http://github.com/hapijs/joi). Useful to set global options such as `stripUnknown` or `abortEarly` (the complete list is available diff --git a/lib/validation.js b/lib/validation.js index faaa7daba..530deb0b1 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -147,7 +147,6 @@ exports.response = function (request, next) { var response = request.response; var statusCode = response.isBoom ? response.output.statusCode : response.statusCode; - var source = response.isBoom ? response.output.payload : response.source; var statusSchema = request.route.settings.response.status[statusCode]; if (statusCode >= 400 && @@ -161,8 +160,8 @@ exports.response = function (request, next) { return next(); // No rules } - if ((!response.isBoom && request.response.variety !== 'plain') || - typeof source !== 'object') { + if (!response.isBoom && + request.response.variety !== 'plain') { return next(Boom.badImplementation('Cannot validate non-object response')); } @@ -207,6 +206,7 @@ exports.response = function (request, next) { } }; + var source = response.isBoom ? response.output.payload : response.source; Hoek.merge(localOptions, request.route.settings.response.options); if (typeof schema !== 'function') { diff --git a/test/response.js b/test/response.js index 0f5d80647..efb0c7319 100755 --- a/test/response.js +++ b/test/response.js @@ -73,6 +73,79 @@ describe('Response', function () { }); }); + describe('_setSource()', function () { + + it('returns an empty string reply', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(''); + } + }); + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['content-length']).to.equal(0); + expect(res.headers['content-type']).to.not.exist(); + expect(res.result).to.equal(null); + expect(res.payload).to.equal(''); + done(); + }); + }); + + it('returns a null reply', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(null); + } + }); + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['content-length']).to.equal(0); + expect(res.result).to.equal(null); + expect(res.payload).to.equal(''); + done(); + }); + }); + + it('returns an undefined reply', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + return reply(); + } + }); + + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['content-length']).to.equal(0); + expect(res.result).to.equal(null); + expect(res.payload).to.equal(''); + done(); + }); + }); + }); + describe('header()', function () { it('appends to set-cookie header', function (done) { diff --git a/test/validation.js b/test/validation.js index 6385d3022..68f76530b 100755 --- a/test/validation.js +++ b/test/validation.js @@ -1436,6 +1436,77 @@ describe('validation', function () { }); }); + it('validates string response', function (done) { + + var value = 'abcd'; + var handler = function (request, reply) { + + return reply(value); + }; + + var server = new Hapi.Server({ debug: false }); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + response: { + schema: Joi.string().min(5) + } + }, + handler: handler + }); + + server.inject('/', function (res1) { + + expect(res1.statusCode).to.equal(500); + value += 'e'; + + server.inject('/', function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.payload).to.equal('abcde'); + done(); + }); + }); + }); + + it('validates boolean response', function (done) { + + var value = 'abcd'; + var handler = function (request, reply) { + + return reply(value); + }; + + var server = new Hapi.Server({ debug: false }); + server.connection(); + server.route({ + method: 'GET', + path: '/', + config: { + response: { + schema: Joi.boolean(), + modify: true + } + }, + handler: handler + }); + + server.inject('/', function (res1) { + + expect(res1.statusCode).to.equal(500); + value = 'on'; + + server.inject('/', function (res2) { + + expect(res2.statusCode).to.equal(200); + expect(res2.payload).to.equal('true'); + done(); + }); + }); + }); + it('validates valid header', function (done) { var server = new Hapi.Server(); From d34f892d0b58ce13b05476b52ab4cad6964116e4 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 11:20:23 -0700 Subject: [PATCH 0080/1139] Default empty status code. Closes #2845 --- API.md | 4 ++++ lib/defaults.js | 3 ++- lib/route.js | 12 ++++++------ lib/schema.js | 1 + lib/transmit.js | 10 ++++++++++ test/transmit.js | 19 +++++++++++++++++++ 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/API.md b/API.md index afd872ccb..77c702463 100755 --- a/API.md +++ b/API.md @@ -2210,6 +2210,10 @@ following options: in serial or in parallel before the handler is called. - `response` - processing rules for the outgoing response: + - `emptyStatusCode` - the default HTTP status code when the payload is empty. Value can + be `200` or `204`. Note that a `200` status code is converted to a `204` only at the time + or response transmission (the response status code will remain `200` throughout the + request lifecycle unless manually set). Defaults to `200`. - `schema` - the default response payload validation rules (for all non-error responses) expressed as one of: - `true` - any payload allowed (no validation performed). This is the default. diff --git a/lib/defaults.js b/lib/defaults.js index e4d3584de..0ba02b479 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -29,7 +29,7 @@ exports.connection = { }, routes: { cache: { - statuses: [200] // Array of HTTP status codes for which cache-control header is set + statuses: [200, 204] // Array of HTTP status codes for which cache-control header is set }, cors: false, // CORS headers files: { @@ -50,6 +50,7 @@ exports.connection = { defaultContentType: 'application/json' }, response: { + emptyStatusCode: 200, // HTTP status code when payload is empty (200, 204) options: {} // Joi validation options }, security: false, // Security headers on responses: false -> null, true -> defaults, {} -> override defaults diff --git a/lib/route.js b/lib/route.js index d356a0a3a..a222700a2 100755 --- a/lib/route.js +++ b/lib/route.js @@ -105,13 +105,16 @@ exports = module.exports = internals.Route = function (options, connection, plug if (this.settings.response.schema !== undefined || this.settings.response.status) { + this.settings.response._validate = true; + var rule = this.settings.response.schema; this.settings.response.status = this.settings.response.status || {}; var statuses = Object.keys(this.settings.response.status); - if (rule === true && !statuses.length) { + if (rule === true && + !statuses.length) { - this.settings.response = null; + this.settings.response._validate = false; } else { this.settings.response.schema = internals.compileRule(rule); @@ -121,9 +124,6 @@ exports = module.exports = internals.Route = function (options, connection, plug } } } - else { - this.settings.response = null; - } // Payload parsing @@ -336,7 +336,7 @@ internals.Route.prototype.rebuild = function (event) { cycle.push(this._extensions.onPostHandler); // An error from here on will override any result set in handler() } - if (this.settings.response && + if (this.settings.response._validate && this.settings.response.sample !== 0) { cycle.push(Validation.response); diff --git a/lib/schema.js b/lib/schema.js index d31349586..7fba017cf 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -119,6 +119,7 @@ internals.routeBase = Joi.object({ }), plugins: Joi.object(), response: Joi.object({ + emptyStatusCode: Joi.number().valid(200, 204), schema: Joi.alternatives(Joi.object(), Joi.func()).allow(true, false), status: Joi.object().pattern(/\d\d\d/, Joi.alternatives(Joi.object(), Joi.func()).allow(true, false)), sample: Joi.number().min(0).max(100), diff --git a/lib/transmit.js b/lib/transmit.js index 2b51af3e5..37a4e2462 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -182,6 +182,16 @@ internals.transmit = function (response, callback) { var source = response._payload; var length = response.headers['content-length'] ? parseInt(response.headers['content-length'], 10) : 0; // In case value is a string + // Empty response + + if (!length && + response.statusCode === 200 && + request.route.settings.response.emptyStatusCode === 204) { + + response.code(204); + delete response.headers['content-length']; + } + // Compression var mime = request.server.mime.type(response.headers['content-type'] || 'application/octet-stream'); diff --git a/test/transmit.js b/test/transmit.js index 35e10f3e5..8428e9fe7 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -599,6 +599,25 @@ describe('transmission', function () { }); }); + it('sends 204 on empty payload', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { response: { emptyStatusCode: 204 } } }); + + var handler = function (request, reply) { + + return reply(); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(204); + expect(res.result).to.equal(null); + done(); + }); + }); + it('skips compression on empty', function (done) { var server = new Hapi.Server(); From 6b59790f25d62ca7084a4c24aaf6895637eac609 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 16 Oct 2015 11:44:27 -0700 Subject: [PATCH 0081/1139] Add tests. Closes #2578 --- lib/transmit.js | 40 +++++++++++++++++++++------------------- test/request.js | 14 +++++++++++++- test/response.js | 35 +++++++++++++++++++++++++++++++++++ test/server.js | 2 +- 4 files changed, 70 insertions(+), 21 deletions(-) diff --git a/lib/transmit.js b/lib/transmit.js index 37a4e2462..3de784d5d 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -281,33 +281,35 @@ internals.transmit = function (response, callback) { var hasEnded = false; var end = function (err, event) { - if (!hasEnded) { - hasEnded = true; + if (hasEnded) { + return; + } - if (event !== 'aborted') { - request.raw.res.end(); - } + hasEnded = true; - source.removeListener('error', end); + if (event !== 'aborted') { + request.raw.res.end(); + } - request.raw.req.removeListener('aborted', onAborted); - request.raw.req.removeListener('close', onClose); + source.removeListener('error', end); - request.raw.res.removeListener('close', onClose); - request.raw.res.removeListener('error', end); - request.raw.res.removeListener('finish', end); + request.raw.req.removeListener('aborted', onAborted); + request.raw.req.removeListener('close', onClose); - var tags = (err ? ['response', 'error'] - : (event ? ['response', 'error', event] - : ['response'])); + request.raw.res.removeListener('close', onClose); + request.raw.res.removeListener('error', end); + request.raw.res.removeListener('finish', end); - if (event || err) { - request.emit('disconnect'); - } + var tags = (err ? ['response', 'error'] + : (event ? ['response', 'error', event] + : ['response'])); - request._log(tags, err); - callback(); + if (event || err) { + request.emit('disconnect'); } + + request._log(tags, err); + callback(); }; source.once('error', end); diff --git a/test/request.js b/test/request.js index e5962b03a..f0651cb74 100755 --- a/test/request.js +++ b/test/request.js @@ -241,6 +241,17 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); + var disconnected = 0; + server.ext('onRequest', function (request, reply) { + + request.once('disconnect', function () { + + ++disconnected; + }); + + return reply.continue(); + }); + server.start(function (err) { expect(err).to.not.exist(); @@ -256,7 +267,7 @@ describe('Request', function () { client.on('data', function () { - total--; + --total; client.destroy(); }); }; @@ -268,6 +279,7 @@ describe('Request', function () { setTimeout(check, 10); } else { + expect(disconnected).to.equal(4); // Each connection sents two HTTP requests server.stop(done); } }; diff --git a/test/response.js b/test/response.js index efb0c7319..86ecd5d4d 100755 --- a/test/response.js +++ b/test/response.js @@ -1216,6 +1216,41 @@ describe('Response', function () { }); }); + describe('_tap()', function () { + + it('peeks into the response stream', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var output = ''; + server.route({ + method: 'GET', + path: '/', + handler: function (request, reply) { + + var response = reply('1234567890'); + + response.on('peek', function (chunk) { + + output += chunk.toString(); + }); + + response.once('finish', function () { + + output += '!'; + }); + } + }); + + server.inject('/', function (res) { + + expect(output).to.equal('1234567890!'); + done(); + }); + }); + }); + describe('_close()', function () { it('calls custom close processor', function (done) { diff --git a/test/server.js b/test/server.js index 2ccd14cba..552a4be61 100755 --- a/test/server.js +++ b/test/server.js @@ -465,7 +465,7 @@ describe('Server', function () { server.inject('/', function (res1) { - expect(server.load.eventLoopDelay).to.equal(0); + expect(server.load.eventLoopDelay).to.be.below(5); setImmediate(function () { From 4b4e9b59c7041a5c0ad339e2fc28b250c9e3cea9 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sat, 17 Oct 2015 01:08:27 -0700 Subject: [PATCH 0082/1139] style --- lib/request.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/request.js b/lib/request.js index 8e83cf649..35cb1b7b9 100755 --- a/lib/request.js +++ b/lib/request.js @@ -332,7 +332,9 @@ internals.Request.prototype._lifecycle = function (err) { return this._reply(err); } - if (!this.path || this.path[0] !== '/') { + if (!this.path || + this.path[0] !== '/') { + return this._reply(Boom.badRequest('Invalid path')); } From ab0761926904561829b3e25df7371b1035717c77 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 19 Oct 2015 02:07:56 -0700 Subject: [PATCH 0083/1139] Test for #2855 --- test/cors.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/cors.js b/test/cors.js index d9d8663f3..2c4bf2199 100755 --- a/test/cors.js +++ b/test/cors.js @@ -57,6 +57,24 @@ describe('CORS', function () { }); }); + it('returns OPTIONS response (server config)', function (done) { + + var handler = function (request, reply) { + + return reply(Boom.badRequest()); + }; + + var server = new Hapi.Server({ connections: { routes: { cors: true } } }); + server.connection(); + server.route({ method: 'GET', path: '/x', handler: handler }); + + server.inject({ method: 'OPTIONS', url: '/x', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + + expect(res.headers['access-control-allow-origin']).to.equal('http://example.com/'); + done(); + }); + }); + it('returns headers on single route', function (done) { var handler = function (request, reply) { From d03464da6632479690d124956340680d153a854d Mon Sep 17 00:00:00 2001 From: Andreas Lappe Date: Mon, 19 Oct 2015 17:37:12 +0200 Subject: [PATCH 0084/1139] =?UTF-8?q?Fix=20typo=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 77c702463..8727931ea 100755 --- a/API.md +++ b/API.md @@ -2169,7 +2169,7 @@ following options: while files are saved. Note that it is the sole responsibility of the application to clean up the files generated by the framework. This can be done by keeping track of which files are used (e.g. using the `request.app` object), and listening to - the server `'response'` event to perform any needed cleaup. + the server `'response'` event to perform any needed cleanup. - `parse` - can be `true`, `false`, or `gunzip`; determines if the incoming payload is processed or presented raw. `true` and `gunzip` includes gunzipping when the appropriate 'Content-Encoding' is specified on the received request. If parsing is enabled and the From 7d202c13af124fbae97e049d254975fbaf2a1291 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 19 Oct 2015 21:44:29 +0200 Subject: [PATCH 0085/1139] Add 'Accept' to default header per #2855 --- API.md | 2 +- lib/defaults.js | 3 ++- test/cors.js | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index 8727931ea..4778503b9 100755 --- a/API.md +++ b/API.md @@ -2110,7 +2110,7 @@ following options: ('Access-Control-Max-Age'). The greater the value, the longer it will take before the browser checks for changes in policy. Defaults to `86400` (one day). - `headers` - a strings array of allowed headers ('Access-Control-Allow-Headers'). - Defaults to `['Authorization', 'Content-Type', 'If-None-Match']`. + Defaults to `['Authorization', 'Content-Type', 'If-None-Match', 'Accept']`. - `additionalHeaders` - a strings array of additional headers to `headers`. Use this to keep the default headers in place. - `exposedHeaders` - a strings array of exposed headers diff --git a/lib/defaults.js b/lib/defaults.js index 0ba02b479..6764fb510 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -84,7 +84,8 @@ exports.cors = { headers: [ 'Authorization', 'Content-Type', - 'If-None-Match' + 'If-None-Match', + 'Accept' ], additionalHeaders: [], exposedHeaders: [ diff --git a/test/cors.js b/test/cors.js index 2c4bf2199..25982eff8 100755 --- a/test/cors.js +++ b/test/cors.js @@ -527,7 +527,7 @@ describe('CORS', function () { }, function (res) { expect(res.statusCode).to.equal(200); - expect(res.headers['access-control-allow-headers']).to.equal('Authorization,Content-Type,If-None-Match'); + expect(res.headers['access-control-allow-headers']).to.equal('Authorization,Content-Type,If-None-Match,Accept'); done(); }); }); From af5b39a4aaa99d748b755b12873444cac21a0ce5 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 19 Oct 2015 22:05:30 +0200 Subject: [PATCH 0086/1139] Add error messages to 404's caused by cors closes #2857 --- lib/cors.js | 12 ++++++------ test/cors.js | 7 +++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/cors.js b/lib/cors.js index 01f33ce00..0622ac3af 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -87,30 +87,30 @@ internals.handler = function (request, reply) { var origin = request.headers.origin; if (!origin) { - return reply(Boom.notFound()); + return reply(Boom.notFound('Missing origin header')); } var method = request.headers['access-control-request-method']; if (!method) { - return reply(Boom.notFound()); + return reply(Boom.notFound('Missing access-control-request-method header')); } // Lookup route var route = request.connection.match(method, request.path, request.headers.host); if (!route) { - return reply(Boom.notFound()); + return reply(Boom.notFound('Missing route')); } var settings = route.settings.cors; if (!settings) { - return reply(Boom.notFound()); + return reply(Boom.notFound('CORS is disabled for this route')); } // Validate Origin header if (!internals.matchOrigin(origin, settings)) { - return reply(Boom.notFound()); + return reply(Boom.notFound('Origin header mismatch')); } // Validate allowed headers @@ -119,7 +119,7 @@ internals.handler = function (request, reply) { if (headers) { headers = headers.split(/\s*,\s*/); if (Hoek.intersect(headers, settings._headers).length !== headers.length) { - return reply(Boom.notFound()); + return reply(Boom.notFound('Some headers are not allowed')); } } diff --git a/test/cors.js b/test/cors.js index 2c4bf2199..d4f8dde82 100755 --- a/test/cors.js +++ b/test/cors.js @@ -96,6 +96,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { expect(res2.statusCode).to.equal(404); + expect(res2.result.message).to.equal('CORS is disabled for this route'); expect(res2.headers['access-control-allow-origin']).to.not.exist(); done(); }); @@ -130,6 +131,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/c', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res3) { expect(res3.statusCode).to.equal(404); + expect(res3.result.message).to.equal('CORS is disabled for this route'); expect(res3.headers['access-control-allow-origin']).to.not.exist(); done(); }); @@ -455,6 +457,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); + expect(res.result.message).to.equal('Missing origin header'); done(); }); }); @@ -472,6 +475,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/' } }, function (res) { expect(res.statusCode).to.equal(404); + expect(res.result.message).to.equal('Missing access-control-request-method header'); done(); }); }); @@ -484,6 +488,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); + expect(res.result.message).to.equal('Missing route'); done(); }); }); @@ -501,6 +506,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); + expect(res.result.message).to.equal('Origin header mismatch'); done(); }); }); @@ -554,6 +560,7 @@ describe('CORS', function () { }, function (res) { expect(res.statusCode).to.equal(404); + expect(res.result.message).to.equal('Some headers are not allowed'); done(); }); }); From 0f06182f8756e7b9d31ca02b29a07d614f2646af Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 19 Oct 2015 22:19:44 +0200 Subject: [PATCH 0087/1139] updated messages --- lib/cors.js | 8 ++++---- test/cors.js | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/cors.js b/lib/cors.js index 0622ac3af..752dd8dc3 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -87,19 +87,19 @@ internals.handler = function (request, reply) { var origin = request.headers.origin; if (!origin) { - return reply(Boom.notFound('Missing origin header')); + return reply(Boom.notFound('Missing Origin header')); } var method = request.headers['access-control-request-method']; if (!method) { - return reply(Boom.notFound('Missing access-control-request-method header')); + return reply(Boom.notFound('Missing Access-Control-Request-Method header')); } // Lookup route var route = request.connection.match(method, request.path, request.headers.host); if (!route) { - return reply(Boom.notFound('Missing route')); + return reply(Boom.notFound()); } var settings = route.settings.cors; @@ -110,7 +110,7 @@ internals.handler = function (request, reply) { // Validate Origin header if (!internals.matchOrigin(origin, settings)) { - return reply(Boom.notFound('Origin header mismatch')); + return reply(Boom.notFound('Origin not allowed')); } // Validate allowed headers diff --git a/test/cors.js b/test/cors.js index d4f8dde82..d3742aafe 100755 --- a/test/cors.js +++ b/test/cors.js @@ -457,7 +457,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); - expect(res.result.message).to.equal('Missing origin header'); + expect(res.result.message).to.equal('Missing Origin header'); done(); }); }); @@ -475,7 +475,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/' } }, function (res) { expect(res.statusCode).to.equal(404); - expect(res.result.message).to.equal('Missing access-control-request-method header'); + expect(res.result.message).to.equal('Missing Access-Control-Request-Method header'); done(); }); }); @@ -488,7 +488,6 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); - expect(res.result.message).to.equal('Missing route'); done(); }); }); @@ -506,7 +505,7 @@ describe('CORS', function () { server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { expect(res.statusCode).to.equal(404); - expect(res.result.message).to.equal('Origin header mismatch'); + expect(res.result.message).to.equal('Origin not allowed'); done(); }); }); From 2d711613afd3846604068e5248455bb0c9245ea1 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 19 Oct 2015 22:22:03 +0200 Subject: [PATCH 0088/1139] alphabetical --- lib/defaults.js | 4 ++-- test/cors.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/defaults.js b/lib/defaults.js index 6764fb510..54baba43c 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -82,10 +82,10 @@ exports.cors = { origin: ['*'], maxAge: 86400, // One day headers: [ + 'Accept', 'Authorization', 'Content-Type', - 'If-None-Match', - 'Accept' + 'If-None-Match' ], additionalHeaders: [], exposedHeaders: [ diff --git a/test/cors.js b/test/cors.js index 25982eff8..7e226f3cb 100755 --- a/test/cors.js +++ b/test/cors.js @@ -527,7 +527,7 @@ describe('CORS', function () { }, function (res) { expect(res.statusCode).to.equal(200); - expect(res.headers['access-control-allow-headers']).to.equal('Authorization,Content-Type,If-None-Match,Accept'); + expect(res.headers['access-control-allow-headers']).to.equal('Accept,Authorization,Content-Type,If-None-Match'); done(); }); }); From 014e8ed89b14d9dcc25e410f2a5c1d4d48b00fac Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 19 Oct 2015 22:23:13 +0200 Subject: [PATCH 0089/1139] docs --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 4778503b9..e41a7e34d 100755 --- a/API.md +++ b/API.md @@ -2110,7 +2110,7 @@ following options: ('Access-Control-Max-Age'). The greater the value, the longer it will take before the browser checks for changes in policy. Defaults to `86400` (one day). - `headers` - a strings array of allowed headers ('Access-Control-Allow-Headers'). - Defaults to `['Authorization', 'Content-Type', 'If-None-Match', 'Accept']`. + Defaults to `['Accept', 'Authorization', 'Content-Type', 'If-None-Match']`. - `additionalHeaders` - a strings array of additional headers to `headers`. Use this to keep the default headers in place. - `exposedHeaders` - a strings array of exposed headers From bea84c4ac521fc809c3834f211445438ad5914e3 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 19 Oct 2015 21:46:26 -0700 Subject: [PATCH 0090/1139] 11.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1bde7f9d..2a51e4174 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "11.0.0", + "version": "11.0.1", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From fa224130f779b8a04e166f20ca427f4ee4fda447 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Mon, 19 Oct 2015 21:46:44 -0700 Subject: [PATCH 0091/1139] 11.0.1 --- npm-shrinkwrap.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8dcef55b2..0e9705f3a 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "11.0.0", + "version": "11.0.1", "dependencies": { "accept": { "version": "1.1.0" From 6b62d76b7ed368de4352c9b4ae5416e8e0489f04 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Wed, 21 Oct 2015 08:35:08 -0700 Subject: [PATCH 0092/1139] Fix CORS case sensitive header comparison. Closes #2866 --- lib/cors.js | 6 +++++- package.json | 2 +- test/cors.js | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/cors.js b/lib/cors.js index 752dd8dc3..5d4bd9ec3 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -20,6 +20,10 @@ exports.route = function (options) { settings._headers = settings.headers.concat(settings.additionalHeaders); settings._headersString = settings._headers.join(','); + for (var i = 0, il = settings._headers.length; i < il; ++i) { + settings._headers[i] = settings._headers[i].toLowerCase(); + } + settings._exposedHeaders = settings.exposedHeaders.concat(settings.additionalExposedHeaders).join(','); if (settings.origin.indexOf('*') !== -1) { @@ -117,7 +121,7 @@ internals.handler = function (request, reply) { var headers = request.headers['access-control-request-headers']; if (headers) { - headers = headers.split(/\s*,\s*/); + headers = headers.toLowerCase().split(/\s*,\s*/); if (Hoek.intersect(headers, settings._headers).length !== headers.length) { return reply(Boom.notFound('Some headers are not allowed')); } diff --git a/package.json b/package.json index 2a51e4174..604c30cf5 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "11.0.1", + "version": "11.0.2", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" diff --git a/test/cors.js b/test/cors.js index 22ce39d8a..f6a2d0854 100755 --- a/test/cors.js +++ b/test/cors.js @@ -537,6 +537,33 @@ describe('CORS', function () { }); }); + it('matches allowed headers (case insensitive', function (done) { + + var handler = function (request, reply) { + + return reply('ok'); + }; + + var server = new Hapi.Server(); + server.connection({ routes: { cors: true } }); + server.route({ method: 'GET', path: '/', handler: handler }); + + server.inject({ + method: 'OPTIONS', + url: '/', + headers: { + origin: 'http://test.example.com', + 'access-control-request-method': 'GET', + 'access-control-request-headers': 'authorization' + } + }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['access-control-allow-headers']).to.equal('Accept,Authorization,Content-Type,If-None-Match'); + done(); + }); + }); + it('errors on disallowed headers', function (done) { var handler = function (request, reply) { From 99c1c4cfccafc36b596313b89e73e44ddeb115be Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Wed, 21 Oct 2015 08:41:51 -0700 Subject: [PATCH 0093/1139] Missing params when not found. Closes #2852 --- lib/request.js | 4 ++-- test/request.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/request.js b/lib/request.js index 35cb1b7b9..e9c880b83 100755 --- a/lib/request.js +++ b/lib/request.js @@ -348,8 +348,8 @@ internals.Request.prototype._lifecycle = function (err) { this.route = this._route.public; } - this.params = match.params; - this.paramsArray = match.paramsArray; + this.params = match.params || {}; + this.paramsArray = match.paramsArray || []; // Setup timeout diff --git a/test/request.js b/test/request.js index f0651cb74..70c60251b 100755 --- a/test/request.js +++ b/test/request.js @@ -306,6 +306,22 @@ describe('Request', function () { }); }); + it('returns empty params array when none present (not found)', function (done) { + + var server = new Hapi.Server(); + server.connection(); + server.ext('onPreResponse', function (request, reply) { + + return reply(request.params); + }); + + server.inject('/', function (res) { + + expect(res.result).to.deep.equal({}); + done(); + }); + }); + it('does not fail on abort', function (done) { var clientRequest; From 51f855db92360ba513d40b5960c166b5fe0c9ee0 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Fri, 23 Oct 2015 10:01:55 +0200 Subject: [PATCH 0094/1139] Fix lab style warning --- lib/response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 0f25ec108..bca5bd846 100755 --- a/lib/response.js +++ b/lib/response.js @@ -129,7 +129,7 @@ internals.Response.prototype._header = function (key, value, options) { for (var b = 0, bl = buffer.length; b < bl; ++b) { Hoek.assert((buffer[b] & 0x7f) === buffer[b], 'Header value cannot contain or convert into non-ascii characters:', key); } - }; + } } if ((!append && override) || From dcd65d63bd0428804c50dd422ce2e19eec7ceebb Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Fri, 23 Oct 2015 10:55:27 +0200 Subject: [PATCH 0095/1139] Fix empty content-length handling for gzip and 204 responses. Closes #2869 --- lib/transmit.js | 8 +++--- test/transmit.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/lib/transmit.js b/lib/transmit.js index 3de784d5d..80dd0f2d7 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -180,11 +180,11 @@ internals.transmit = function (response, callback) { var request = response.request; var source = response._payload; - var length = response.headers['content-length'] ? parseInt(response.headers['content-length'], 10) : 0; // In case value is a string + var length = parseInt(response.headers['content-length'], 10); // In case value is a string // Empty response - if (!length && + if (length === 0 && response.statusCode === 200 && request.route.settings.response.emptyStatusCode === 204) { @@ -202,7 +202,7 @@ internals.transmit = function (response, callback) { if (request.method === 'get' && response.statusCode === 200 && - length && + length > 0 && !encoding) { if (request.headers.range) { @@ -243,7 +243,7 @@ internals.transmit = function (response, callback) { } if (encoding && - length && + length !== 0 && response._isPayloadSupported()) { delete response.headers['content-length']; diff --git a/test/transmit.js b/test/transmit.js index 8428e9fe7..d85f997cb 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -618,6 +618,39 @@ describe('transmission', function () { }); }); + it('does not send 204 for chunked transfer payloads', function (done) { + + var server = new Hapi.Server(); + server.connection({ routes: { response: { emptyStatusCode: 204 } } }); + + var handler = function (request, reply) { + + var TestStream = function () { + + Stream.Readable.call(this); + }; + + Hoek.inherits(TestStream, Stream.Readable); + + TestStream.prototype._read = function () { + + this.push('success'); + this.push(null); + }; + + var stream = new TestStream(); + return reply(stream); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject('/', function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('success'); + done(); + }); + }); + it('skips compression on empty', function (done) { var server = new Hapi.Server(); @@ -625,7 +658,7 @@ describe('transmission', function () { var handler = function (request, reply) { - return reply(); + return reply().type('text/html'); }; server.route({ method: 'GET', path: '/', handler: handler }); @@ -638,6 +671,39 @@ describe('transmission', function () { }); }); + it('does not skip compression for chunked transfer payloads', function (done) { + + var server = new Hapi.Server(); + server.connection(); + + var handler = function (request, reply) { + + var TestStream = function () { + + Stream.Readable.call(this); + }; + + Hoek.inherits(TestStream, Stream.Readable); + + TestStream.prototype._read = function () { + + this.push('success'); + this.push(null); + }; + + var stream = new TestStream(); + return reply(stream).type('text/html'); + }; + + server.route({ method: 'GET', path: '/', handler: handler }); + server.inject({ url: '/', headers: { 'accept-encoding': 'gzip' } }, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.headers['content-encoding']).to.equal('gzip'); + done(); + }); + }); + it('sets vary header when accept-encoding is present but does not match', function (done) { var server = new Hapi.Server(); From c880142f60600ae1870f9a79e54944684bc2aafc Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 08:42:56 -0700 Subject: [PATCH 0096/1139] Update subtext. Closes #2862 --- npm-shrinkwrap.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 0e9705f3a..551973b79 100755 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "hapi", - "version": "11.0.1", + "version": "11.0.3", "dependencies": { "accept": { "version": "1.1.0" @@ -70,7 +70,7 @@ "version": "2.1.1" }, "subtext": { - "version": "2.0.0", + "version": "2.0.1", "dependencies": { "content": { "version": "1.0.2" diff --git a/package.json b/package.json index 604c30cf5..d203af578 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "hapi", "description": "HTTP Server framework", "homepage": "http://hapijs.com", - "version": "11.0.2", + "version": "11.0.3", "repository": { "type": "git", "url": "git://github.com/hapijs/hapi" From 61f0822de5100b85214e66734f3be5024623d895 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 09:54:13 -0700 Subject: [PATCH 0097/1139] Add strict mode. Closes #2872 --- lib/auth.js | 2 ++ lib/connection.js | 2 ++ lib/cors.js | 2 ++ lib/defaults.js | 2 ++ lib/ext.js | 2 ++ lib/handler.js | 2 ++ lib/index.js | 2 ++ lib/methods.js | 2 ++ lib/plugin.js | 2 ++ lib/protect.js | 2 ++ lib/reply.js | 2 ++ lib/request.js | 2 ++ lib/response.js | 2 ++ lib/route.js | 2 ++ lib/schema.js | 2 ++ lib/server.js | 2 ++ lib/transmit.js | 2 ++ lib/validation.js | 2 ++ test/auth.js | 2 ++ test/connection.js | 2 ++ test/cors.js | 2 ++ test/handler.js | 2 ++ test/methods.js | 2 ++ test/payload.js | 2 ++ test/plugin.js | 2 ++ test/protect.js | 2 ++ test/reply.js | 2 ++ test/request.js | 2 ++ test/response.js | 2 ++ test/route.js | 2 ++ test/security.js | 2 ++ test/server.js | 2 ++ test/state.js | 2 ++ test/transmit.js | 2 ++ test/validation.js | 2 ++ 35 files changed, 70 insertions(+) diff --git a/lib/auth.js b/lib/auth.js index 3c7d387b3..0f9954b99 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/lib/connection.js b/lib/connection.js index b50cac596..ec40a771b 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Events = require('events'); diff --git a/lib/cors.js b/lib/cors.js index 5d4bd9ec3..79d720b95 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/lib/defaults.js b/lib/defaults.js index 54baba43c..5ebcf3748 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Os = require('os'); diff --git a/lib/ext.js b/lib/ext.js index 03213865b..8a0522591 100755 --- a/lib/ext.js +++ b/lib/ext.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Topo = require('topo'); diff --git a/lib/handler.js b/lib/handler.js index 56642bc38..959e11367 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Hoek = require('hoek'); diff --git a/lib/index.js b/lib/index.js index f6825e795..f1ed5f65a 100755 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Server = require('./server'); diff --git a/lib/methods.js b/lib/methods.js index a1299a2ce..96c50cefa 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/lib/plugin.js b/lib/plugin.js index 160a5a89f..274dc0a5c 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Catbox = require('catbox'); diff --git a/lib/protect.js b/lib/protect.js index 348b58a5d..3cdc72cfd 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Domain = null; // Loaded as needed diff --git a/lib/reply.js b/lib/reply.js index b13b8d672..d123eaf3e 100755 --- a/lib/reply.js +++ b/lib/reply.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Hoek = require('hoek'); diff --git a/lib/request.js b/lib/request.js index e9c880b83..51f161486 100755 --- a/lib/request.js +++ b/lib/request.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Events = require('events'); diff --git a/lib/response.js b/lib/response.js index bca5bd846..51a0e5904 100755 --- a/lib/response.js +++ b/lib/response.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Stream = require('stream'); diff --git a/lib/route.js b/lib/route.js index a222700a2..058606f5a 100755 --- a/lib/route.js +++ b/lib/route.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/lib/schema.js b/lib/schema.js index 7fba017cf..9c017e827 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Joi = require('joi'); diff --git a/lib/server.js b/lib/server.js index 7e00c7e3b..b83890deb 100755 --- a/lib/server.js +++ b/lib/server.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Events = require('events'); diff --git a/lib/transmit.js b/lib/transmit.js index 80dd0f2d7..d57e70f75 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Http = require('http'); diff --git a/lib/validation.js b/lib/validation.js index 530deb0b1..9294a728b 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/test/auth.js b/test/auth.js index 3d5e8dd87..6335d17b7 100755 --- a/test/auth.js +++ b/test/auth.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Path = require('path'); diff --git a/test/connection.js b/test/connection.js index 9c6211a77..844a44c1e 100755 --- a/test/connection.js +++ b/test/connection.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var ChildProcess = require('child_process'); diff --git a/test/cors.js b/test/cors.js index f6a2d0854..214ff5f4b 100755 --- a/test/cors.js +++ b/test/cors.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); diff --git a/test/handler.js b/test/handler.js index 5f3143c33..c315be580 100755 --- a/test/handler.js +++ b/test/handler.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Path = require('path'); diff --git a/test/methods.js b/test/methods.js index 77288e1a2..565b6fcc0 100755 --- a/test/methods.js +++ b/test/methods.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Bluebird = require('bluebird'); diff --git a/test/payload.js b/test/payload.js index 60831211c..0b8342e9c 100755 --- a/test/payload.js +++ b/test/payload.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Fs = require('fs'); diff --git a/test/plugin.js b/test/plugin.js index 7d9452371..e032fe200 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Path = require('path'); diff --git a/test/protect.js b/test/protect.js index 634924fae..269dd8ec7 100755 --- a/test/protect.js +++ b/test/protect.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Events = require('events'); diff --git a/test/reply.js b/test/reply.js index 661deae97..46143e49d 100755 --- a/test/reply.js +++ b/test/reply.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Http = require('http'); diff --git a/test/request.js b/test/request.js index 70c60251b..e9291e6cd 100755 --- a/test/request.js +++ b/test/request.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Http = require('http'); diff --git a/test/response.js b/test/response.js index 86ecd5d4d..c4e1ffa95 100755 --- a/test/response.js +++ b/test/response.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Stream = require('stream'); diff --git a/test/route.js b/test/route.js index 42dcf36e1..19fa7a35b 100755 --- a/test/route.js +++ b/test/route.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Code = require('code'); diff --git a/test/security.js b/test/security.js index b910b38ad..ac5a27eca 100755 --- a/test/security.js +++ b/test/security.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Code = require('code'); diff --git a/test/server.js b/test/server.js index 552a4be61..0d15e3599 100755 --- a/test/server.js +++ b/test/server.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Code = require('code'); diff --git a/test/state.js b/test/state.js index 013af8167..def9cca42 100755 --- a/test/state.js +++ b/test/state.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Code = require('code'); diff --git a/test/transmit.js b/test/transmit.js index d85f997cb..16eede88a 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var ChildProcess = require('child_process'); diff --git a/test/validation.js b/test/validation.js index 68f76530b..193e7cb3e 100755 --- a/test/validation.js +++ b/test/validation.js @@ -1,3 +1,5 @@ +'use strict'; + // Load modules var Boom = require('boom'); From dcc1065eed4b12c50dd8378b96f9e8f85fc53ced Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 10:33:24 -0700 Subject: [PATCH 0098/1139] Repalce var with const where possible. Closes #2873 --- lib/auth.js | 76 +++--- lib/connection.js | 84 +++---- lib/cors.js | 28 +-- lib/defaults.js | 4 +- lib/ext.js | 14 +- lib/handler.js | 66 ++--- lib/index.js | 4 +- lib/methods.js | 49 ++-- lib/plugin.js | 78 +++--- lib/protect.js | 18 +- lib/reply.js | 24 +- lib/request.js | 78 +++--- lib/response.js | 56 ++--- lib/route.js | 76 +++--- lib/schema.js | 8 +- lib/server.js | 76 +++--- lib/transmit.js | 114 ++++----- lib/validation.js | 38 +-- test/auth.js | 232 ++++++++--------- test/connection.js | 318 +++++++++++------------ test/cors.js | 116 ++++----- test/handler.js | 178 ++++++------- test/methods.js | 250 +++++++++---------- test/payload.js | 186 +++++++------- test/plugin.js | 610 ++++++++++++++++++++++----------------------- test/protect.js | 48 ++-- test/reply.js | 140 +++++------ test/request.js | 306 +++++++++++------------ test/response.js | 258 +++++++++---------- test/route.js | 102 ++++---- test/security.js | 40 +-- test/server.js | 86 +++---- test/state.js | 72 +++--- test/transmit.js | 592 +++++++++++++++++++++---------------------- test/validation.js | 164 ++++++------ 35 files changed, 2293 insertions(+), 2296 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 0f9954b99..66b2e3c80 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -2,14 +2,14 @@ // Load modules -var Boom = require('boom'); -var Hoek = require('hoek'); -var Schema = require('./schema'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Schema = require('./schema'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Auth = function (connection) { @@ -35,9 +35,9 @@ internals.Auth.prototype.scheme = function (name, scheme) { internals.Auth.prototype.strategy = function (name, scheme /*, mode, options */) { - var hasMode = (typeof arguments[2] === 'string' || typeof arguments[2] === 'boolean'); - var mode = (hasMode ? arguments[2] : false); - var options = (hasMode ? arguments[3] : arguments[2]) || null; + const hasMode = (typeof arguments[2] === 'string' || typeof arguments[2] === 'boolean'); + const mode = (hasMode ? arguments[2] : false); + const options = (hasMode ? arguments[3] : arguments[2]) || null; Hoek.assert(name, 'Authentication strategy must have a name'); Hoek.assert(name !== 'bypass', 'Cannot use reserved strategy name: bypass'); @@ -45,8 +45,8 @@ internals.Auth.prototype.strategy = function (name, scheme /*, mode, options */) Hoek.assert(scheme, 'Authentication strategy', name, 'missing scheme'); Hoek.assert(this._schemes[scheme], 'Authentication strategy', name, 'uses unknown scheme:', scheme); - var server = this.connection.server._clone([this.connection], ''); - var strategy = this._schemes[scheme](server, options); + const server = this.connection.server._clone([this.connection], ''); + const strategy = this._schemes[scheme](server, options); Hoek.assert(strategy.authenticate, 'Invalid scheme:', name, 'missing authenticate() method'); Hoek.assert(typeof strategy.authenticate === 'function', 'Invalid scheme:', name, 'invalid authenticate() method'); @@ -93,22 +93,22 @@ internals.Auth.prototype.default = function (options) { internals.Auth.prototype.test = function (name, request, next) { Hoek.assert(name, 'Missing authentication strategy name'); - var strategy = this._strategies[name]; + const strategy = this._strategies[name]; Hoek.assert(strategy, 'Unknown authentication strategy:', name); - var transfer = function (response, data) { + const transfer = function (response, data) { return next(response, data && data.credentials); }; - var reply = request.server._replier.interface(request, strategy.realm, transfer); + const reply = request.server._replier.interface(request, strategy.realm, transfer); strategy.methods.authenticate(request, reply); }; internals.Auth.prototype._setupRoute = function (options, path) { - var self = this; + const self = this; if (!options) { return options; // Preseve the difference between undefined and false @@ -150,7 +150,7 @@ internals.Auth.prototype._setupRoute = function (options, path) { var hasAuthenticatePayload = false; options.strategies.forEach(function (name) { - var strategy = self._strategies[name]; + const strategy = self._strategies[name]; Hoek.assert(strategy, 'Unknown authentication strategy:', name, 'in path:', path); Hoek.assert(strategy.methods.payload || options.payload !== 'required', 'Payload validation can only be required when all strategies support it in path:', path); hasAuthenticatePayload = hasAuthenticatePayload || strategy.methods.payload; @@ -175,31 +175,31 @@ internals.Auth.prototype.lookup = function (route) { internals.Auth.authenticate = function (request, next) { - var auth = request.connection.auth; + const auth = request.connection.auth; return auth._authenticate(request, next); }; internals.Auth.prototype._authenticate = function (request, next) { - var self = this; + const self = this; - var config = this.lookup(request.route); + const config = this.lookup(request.route); if (!config) { return next(); } request.auth.mode = config.mode; - var authErrors = []; + const authErrors = []; var strategyPos = 0; - var authenticate = function () { + const authenticate = function () { // Find next strategy if (strategyPos >= config.strategies.length) { - var err = Boom.unauthorized('Missing authentication', authErrors); + const err = Boom.unauthorized('Missing authentication', authErrors); if (config.mode === 'optional' || config.mode === 'try') { @@ -214,23 +214,23 @@ internals.Auth.prototype._authenticate = function (request, next) { return next(err); } - var name = config.strategies[strategyPos]; + const name = config.strategies[strategyPos]; ++strategyPos; request._protect.run(validate, function (exit) { - var transfer = function (response, data) { + const transfer = function (response, data) { exit(response, name, data); }; - var strategy = self._strategies[name]; - var reply = request.server._replier.interface(request, strategy.realm, transfer); + const strategy = self._strategies[name]; + const reply = request.server._replier.interface(request, strategy.realm, transfer); strategy.methods.authenticate(request, reply); }); }; - var validate = function (err, name, result) { // err can be Boom, Error, or a valid response object + const validate = function (err, name, result) { // err can be Boom, Error, or a valid response object if (!name) { return next(err); @@ -277,7 +277,7 @@ internals.Auth.prototype._authenticate = function (request, next) { // Authenticated - var credentials = result.credentials; + const credentials = result.credentials; request.auth.strategy = name; request.auth.credentials = credentials; request.auth.artifacts = result.artifacts; @@ -288,7 +288,7 @@ internals.Auth.prototype._authenticate = function (request, next) { var scopes = config.scope; if (config.hasScopeParameters) { scopes = []; - var context = { params: request.params, query: request.query }; + const context = { params: request.params, query: request.query }; for (var i = 0, il = config.scope.length; i < il; ++i) { scopes[i] = Hoek.reachTemplate(context, config.scope[i]); } @@ -304,7 +304,7 @@ internals.Auth.prototype._authenticate = function (request, next) { // Check entity - var entity = config.entity || 'any'; + const entity = config.entity || 'any'; // Entity: 'any' @@ -359,20 +359,20 @@ internals.Auth.payload = function (request, next) { return next(); } - var auth = request.connection.auth; - var strategy = auth._strategies[request.auth.strategy]; + const auth = request.connection.auth; + const strategy = auth._strategies[request.auth.strategy]; if (!strategy.methods.payload) { return next(); } - var config = auth.lookup(request.route); - var setting = config.payload || (strategy.methods.options.payload ? 'required' : false); + const config = auth.lookup(request.route); + const setting = config.payload || (strategy.methods.options.payload ? 'required' : false); if (!setting) { return next(); } - var finalize = function (response) { + const finalize = function (response) { if (response && response.isBoom && @@ -386,7 +386,7 @@ internals.Auth.payload = function (request, next) { request._protect.run(finalize, function (exit) { - var reply = request.server._replier.interface(request, strategy.realm, exit); + const reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.payload(request, reply); }); }; @@ -394,8 +394,8 @@ internals.Auth.payload = function (request, next) { internals.Auth.response = function (request, next) { - var auth = request.connection.auth; - var config = auth.lookup(request.route); + const auth = request.connection.auth; + const config = auth.lookup(request.route); if (!config || !request.auth.isAuthenticated || request.auth.strategy === 'bypass') { @@ -403,14 +403,14 @@ internals.Auth.response = function (request, next) { return next(); } - var strategy = auth._strategies[request.auth.strategy]; + const strategy = auth._strategies[request.auth.strategy]; if (!strategy.methods.response) { return next(); } request._protect.run(next, function (exit) { - var reply = request.server._replier.interface(request, strategy.realm, exit); + const reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.response(request, reply); }); }; diff --git a/lib/connection.js b/lib/connection.js index ec40a771b..6369db089 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -2,25 +2,25 @@ // Load modules -var Events = require('events'); -var Http = require('http'); -var Https = require('https'); -var Os = require('os'); -var Path = require('path'); -var Boom = require('boom'); -var Call = require('call'); -var Hoek = require('hoek'); -var Shot = require('shot'); -var Statehood = require('statehood'); -var Auth = require('./auth'); -var Cors = require('./cors'); -var Ext = require('./ext'); -var Route = require('./route'); +const Events = require('events'); +const Http = require('http'); +const Https = require('https'); +const Os = require('os'); +const Path = require('path'); +const Boom = require('boom'); +const Call = require('call'); +const Hoek = require('hoek'); +const Shot = require('shot'); +const Statehood = require('statehood'); +const Auth = require('./auth'); +const Cors = require('./cors'); +const Ext = require('./ext'); +const Route = require('./route'); // Declare internals -var internals = { +const internals = { counter: { min: 10000, max: 99999 @@ -30,9 +30,9 @@ var internals = { exports = module.exports = internals.Connection = function (server, options) { - var self = this; + const self = this; - var now = Date.now(); + const now = Date.now(); Events.EventEmitter.call(this); @@ -118,7 +118,7 @@ Hoek.inherits(internals.Connection, Events.EventEmitter); internals.Connection.prototype._init = function () { - var self = this; + const self = this; // Setup listener @@ -127,7 +127,7 @@ internals.Connection.prototype._init = function () { // Update the address, port, and uri with active values if (self.type === 'tcp') { - var address = self.listener.address(); + const address = self.listener.address(); self.info.address = address.address; self.info.port = address.port; self.info.uri = (self.settings.uri || (self.info.protocol + '://' + self.info.host + ':' + self.info.port)); @@ -135,7 +135,7 @@ internals.Connection.prototype._init = function () { self._onConnection = function (connection) { - var key = connection.remoteAddress + ':' + connection.remotePort; + const key = connection.remoteAddress + ':' + connection.remotePort; self._connections[key] = connection; connection.once('close', function () { @@ -151,7 +151,7 @@ internals.Connection.prototype._init = function () { internals.Connection.prototype._start = function (callback) { - var self = this; + const self = this; if (this._started) { return process.nextTick(callback); @@ -164,7 +164,7 @@ internals.Connection.prototype._start = function (callback) { return process.nextTick(callback); } - var onError = function (err) { + const onError = function (err) { self._started = false; return callback(err); @@ -172,7 +172,7 @@ internals.Connection.prototype._start = function (callback) { this.listener.once('error', onError); - var finalize = function () { + const finalize = function () { self.listener.removeListener('error', onError); callback(); @@ -182,7 +182,7 @@ internals.Connection.prototype._start = function (callback) { this.listener.listen(this.settings.port, finalize); } else { - var address = this.settings.address || this.settings.host || '0.0.0.0'; + const address = this.settings.address || this.settings.host || '0.0.0.0'; this.listener.listen(this.settings.port, address, finalize); } }; @@ -190,7 +190,7 @@ internals.Connection.prototype._start = function (callback) { internals.Connection.prototype._stop = function (options, callback) { - var self = this; + const self = this; if (!this._started) { return process.nextTick(callback); @@ -199,7 +199,7 @@ internals.Connection.prototype._stop = function (options, callback) { this._started = false; this.info.started = 0; - var timeoutId = setTimeout(function () { + const timeoutId = setTimeout(function () { Object.keys(self._connections).forEach(function (key) { @@ -223,7 +223,7 @@ internals.Connection.prototype._stop = function (options, callback) { internals.Connection.prototype._dispatch = function (options) { - var self = this; + const self = this; options = options || {}; @@ -237,11 +237,11 @@ internals.Connection.prototype._dispatch = function (options) { // Create request - var request = self.server._requestor.request(self, req, res, options); + const request = self.server._requestor.request(self, req, res, options); // Check load - var overload = self._load.check(); + const overload = self._load.check(); if (overload) { self.server._log(['load'], self.server.load); request._reply(overload); @@ -278,7 +278,7 @@ internals.Connection.prototype.inject = function (options, callback) { settings.authority = settings.authority || (this.info.host + ':' + this.info.port); } - var needle = this._dispatch({ + const needle = this._dispatch({ credentials: options.credentials, artifacts: options.artifacts, allowInternals: options.allowInternals @@ -311,7 +311,7 @@ internals.Connection.prototype.lookup = function (id) { Hoek.assert(id && typeof id === 'string', 'Invalid route id:', id); - var record = this._router.ids[id]; + const record = this._router.ids[id]; if (!record) { return null; } @@ -326,7 +326,7 @@ internals.Connection.prototype.match = function (method, path, host) { Hoek.assert(path && typeof path === 'string' && path[0] === '/', 'Invalid path:', path); Hoek.assert(!host || typeof host === 'string', 'Invalid host:', host); - var match = this._router.route(method.toLowerCase(), path, host); + const match = this._router.route(method.toLowerCase(), path, host); if (match.route.method === 'notfound') { return null; } @@ -339,7 +339,7 @@ internals.Connection.prototype.match = function (method, path, host) { internals.Connection.prototype._ext = function (event) { - var type = event.type; + const type = event.type; Hoek.assert(this._extensions[type], 'Unknown event type', type); this._extensions[type].add(event); }; @@ -349,13 +349,13 @@ internals.Connection.prototype._route = function (configs, plugin) { configs = [].concat(configs); for (var i = 0, il = configs.length; i < il; ++i) { - var config = configs[i]; + const config = configs[i]; if (Array.isArray(config.method)) { for (var m = 0, ml = config.method.length; m < ml; ++m) { - var method = config.method[m]; + const method = config.method[m]; - var settings = Hoek.shallow(config); + const settings = Hoek.shallow(config); settings.method = method; this._addRoute(settings, plugin); } @@ -369,12 +369,12 @@ internals.Connection.prototype._route = function (configs, plugin) { internals.Connection.prototype._addRoute = function (config, plugin) { - var route = new Route(config, this, plugin); // Do no use config beyond this point, use route members - var vhosts = [].concat(route.settings.vhost || '*'); + const route = new Route(config, this, plugin); // Do no use config beyond this point, use route members + const vhosts = [].concat(route.settings.vhost || '*'); for (var i = 0, il = vhosts.length; i < il; ++i) { - var vhost = vhosts[i]; - var record = this._router.add({ method: route.method, path: route.path, vhost: vhost, analysis: route._analysis, id: route.settings.id }, route); + const vhost = vhosts[i]; + const record = this._router.add({ method: route.method, path: route.path, vhost: vhost, analysis: route._analysis, id: route.settings.id }, route); route.fingerprint = record.fingerprint; route.params = record.params; } @@ -385,7 +385,7 @@ internals.Connection.prototype._addRoute = function (config, plugin) { internals.Connection.prototype._defaultRoutes = function () { - var notFound = new Route({ + const notFound = new Route({ method: 'notFound', path: '/{p*}', config: { @@ -399,7 +399,7 @@ internals.Connection.prototype._defaultRoutes = function () { this._router.special('notFound', notFound); - var badRequest = new Route({ + const badRequest = new Route({ method: 'badRequest', path: '/{p*}', config: { diff --git a/lib/cors.js b/lib/cors.js index 79d720b95..e1f4c7ce4 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -2,20 +2,20 @@ // Load modules -var Boom = require('boom'); -var Hoek = require('hoek'); -var Defaults = require('./defaults'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Defaults = require('./defaults'); var Route = null; // Delayed load due to circular dependency // Declare internals -var internals = {}; +const internals = {}; exports.route = function (options) { - var settings = Hoek.applyToDefaults(Defaults.cors, options); + const settings = Hoek.applyToDefaults(Defaults.cors, options); if (!settings) { return false; } @@ -39,7 +39,7 @@ exports.route = function (options) { }; for (var c = 0, cl = settings.origin.length; c < cl; ++c) { - var origin = settings.origin[c]; + const origin = settings.origin[c]; if (origin.indexOf('*') !== -1) { settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); } @@ -73,7 +73,7 @@ exports.handler = function (connection) { return; } - var optionsRoute = new Route({ + const optionsRoute = new Route({ path: '/{p*}', method: 'options', config: { @@ -91,24 +91,24 @@ internals.handler = function (request, reply) { // Validate CORS preflight request - var origin = request.headers.origin; + const origin = request.headers.origin; if (!origin) { return reply(Boom.notFound('Missing Origin header')); } - var method = request.headers['access-control-request-method']; + const method = request.headers['access-control-request-method']; if (!method) { return reply(Boom.notFound('Missing Access-Control-Request-Method header')); } // Lookup route - var route = request.connection.match(method, request.path, request.headers.host); + const route = request.connection.match(method, request.path, request.headers.host); if (!route) { return reply(Boom.notFound()); } - var settings = route.settings.cors; + const settings = route.settings.cors; if (!settings) { return reply(Boom.notFound('CORS is disabled for this route')); } @@ -131,7 +131,7 @@ internals.handler = function (request, reply) { // Reply with the route CORS headers - var response = reply(); + const response = reply(); response._header('access-control-allow-origin', request.headers.origin); response._header('access-control-allow-methods', method); response._header('access-control-allow-headers', settings._headersString); @@ -149,8 +149,8 @@ internals.handler = function (request, reply) { exports.headers = function (response) { - var request = response.request; - var settings = request.route.settings.cors; + const request = response.request; + const settings = request.route.settings.cors; if (!settings) { return; } diff --git a/lib/defaults.js b/lib/defaults.js index 5ebcf3748..f479d060a 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -2,12 +2,12 @@ // Load modules -var Os = require('os'); +const Os = require('os'); // Declare internals -var internals = {}; +const internals = {}; exports.server = { diff --git a/lib/ext.js b/lib/ext.js index 8a0522591..3398fc5e5 100755 --- a/lib/ext.js +++ b/lib/ext.js @@ -2,12 +2,12 @@ // Load modules -var Topo = require('topo'); +const Topo = require('topo'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Ext = function (server) { @@ -22,18 +22,18 @@ exports = module.exports = internals.Ext = function (server) { internals.Ext.prototype.add = function (event) { - var methods = [].concat(event.method); - var options = event.options; + const methods = [].concat(event.method); + const options = event.options; for (var i = 0, il = methods.length; i < il; ++i) { - var settings = { + const settings = { before: options.before, after: options.after, group: event.plugin.realm.plugin, sort: this._server._extensionsSeq++ }; - var node = { + const node = { func: methods[i], // Connection: function (request, next), Server: function (server, next) bind: options.bind, plugin: event.plugin @@ -54,7 +54,7 @@ internals.Ext.prototype.add = function (event) { internals.Ext.prototype.merge = function (others) { - var merge = []; + const merge = []; for (var i = 0, il = others.length; i < il; ++i) { merge.push(others[i]._topo); } diff --git a/lib/handler.js b/lib/handler.js index 959e11367..073f9aede 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -2,20 +2,20 @@ // Load modules -var Hoek = require('hoek'); -var Items = require('items'); -var Methods = require('./methods'); -var Response = require('./response'); +const Hoek = require('hoek'); +const Items = require('items'); +const Methods = require('./methods'); +const Response = require('./response'); // Declare internals -var internals = {}; +const internals = {}; exports.execute = function (request, next) { - var finalize = function (err, result) { + const finalize = function (err, result) { request._setResponse(err || result); return next(); // Must not include an argument @@ -66,8 +66,8 @@ internals.prerequisites = function (request, callback) { internals.handler = function (request, callback) { - var timer = new Hoek.Bench(); - var finalize = function (response, data) { + const timer = new Hoek.Bench(); + const finalize = function (response, data) { if (response === null) { // reply.continue() response = Response.wrap(null, request); @@ -87,8 +87,8 @@ internals.handler = function (request, callback) { // Decorate request - var reply = request.server._replier.interface(request, request.route.realm, finalize); - var bind = request.route.settings.bind; + const reply = request.server._replier.interface(request, request.route.realm, finalize); + const bind = request.route.settings.bind; // Execute handler @@ -101,8 +101,8 @@ exports.defaults = function (method, handler, server) { var defaults = null; if (typeof handler === 'object') { - var type = Object.keys(handler)[0]; - var serverHandler = server._handlers[type]; + const type = Object.keys(handler)[0]; + const serverHandler = server._handlers[type]; Hoek.assert(serverHandler, 'Unknown handler:', type); @@ -118,8 +118,8 @@ exports.defaults = function (method, handler, server) { exports.configure = function (handler, route) { if (typeof handler === 'object') { - var type = Object.keys(handler)[0]; - var serverHandler = route.server._handlers[type]; + const type = Object.keys(handler)[0]; + const serverHandler = route.server._handlers[type]; Hoek.assert(serverHandler, 'Unknown handler:', type); @@ -127,7 +127,7 @@ exports.configure = function (handler, route) { } if (typeof handler === 'string') { - var parsed = internals.fromString('handler', handler, route.server); + const parsed = internals.fromString('handler', handler, route.server); return parsed.method; } @@ -158,26 +158,26 @@ exports.prerequisites = function (config, server) { ] */ - var prerequisites = []; + const prerequisites = []; for (var i = 0, il = config.length; i < il; ++i) { - var pres = [].concat(config[i]); + const pres = [].concat(config[i]); - var set = []; + const set = []; for (var p = 0, pl = pres.length; p < pl; ++p) { var pre = pres[p]; if (typeof pre !== 'object') { pre = { method: pre }; } - var item = { + const item = { method: pre.method, assign: pre.assign, failAction: pre.failAction || 'error' }; if (typeof item.method === 'string') { - var parsed = internals.fromString('pre', item.method, server); + const parsed = internals.fromString('pre', item.method, server); item.method = parsed.method; item.assign = item.assign || parsed.name; } @@ -195,18 +195,18 @@ exports.prerequisites = function (config, server) { internals.fromString = function (type, notation, server) { // 1:name 2:( 3:arguments - var methodParts = notation.match(/^([\w\.]+)(?:\s*)(?:(\()(?:\s*)(\w+(?:\.\w+)*(?:\s*\,\s*\w+(?:\.\w+)*)*)?(?:\s*)\))?$/); + const methodParts = notation.match(/^([\w\.]+)(?:\s*)(?:(\()(?:\s*)(\w+(?:\.\w+)*(?:\s*\,\s*\w+(?:\.\w+)*)*)?(?:\s*)\))?$/); Hoek.assert(methodParts, 'Invalid server method string notation:', notation); - var name = methodParts[1]; + const name = methodParts[1]; Hoek.assert(name.match(Methods.methodNameRx), 'Invalid server method name:', name); - var method = server._methods._normalized[name]; + const method = server._methods._normalized[name]; Hoek.assert(method, 'Unknown server method in string notation:', notation); - var result = { name: name }; - var argsNotation = !!methodParts[2]; - var methodArgs = (argsNotation ? (methodParts[3] || '').split(/\s*\,\s*/) : null); + const result = { name: name }; + const argsNotation = !!methodParts[2]; + const methodArgs = (argsNotation ? (methodParts[3] || '').split(/\s*\,\s*/) : null); result.method = function (request, reply) { @@ -214,7 +214,7 @@ internals.fromString = function (type, notation, server) { return method(request, reply); // Method is already bound to context } - var finalize = function (err, value, cached, report) { + const finalize = function (err, value, cached, report) { if (report) { request._log([type, 'method', name], report); @@ -223,9 +223,9 @@ internals.fromString = function (type, notation, server) { return reply(err, value); }; - var args = []; + const args = []; for (var i = 0, il = methodArgs.length; i < il; ++i) { - var arg = methodArgs[i]; + const arg = methodArgs[i]; if (arg) { args.push(Hoek.reach(request, arg)); } @@ -251,8 +251,8 @@ internals.pre = function (pre) { return function (request, next) { - var timer = new Hoek.Bench(); - var finalize = function (response, data) { + const timer = new Hoek.Bench(); + const finalize = function (response, data) { if (response === null) { // reply.continue() response = Response.wrap(null, request); @@ -282,8 +282,8 @@ internals.pre = function (pre) { // Setup environment - var reply = request.server._replier.interface(request, request.route.realm, finalize); - var bind = request.route.settings.bind; + const reply = request.server._replier.interface(request, request.route.realm, finalize); + const bind = request.route.settings.bind; // Execute handler diff --git a/lib/index.js b/lib/index.js index f1ed5f65a..3cc3c8b65 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,12 +2,12 @@ // Load modules -var Server = require('./server'); +const Server = require('./server'); // Declare internals -var internals = {}; +const internals = {}; exports.Server = Server; diff --git a/lib/methods.js b/lib/methods.js index 96c50cefa..3782b6090 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -2,14 +2,14 @@ // Load modules -var Boom = require('boom'); -var Hoek = require('hoek'); -var Schema = require('./schema'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Schema = require('./schema'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Methods = function (server) { @@ -28,10 +28,9 @@ internals.Methods.prototype.add = function (name, method, options, realm) { // {} or [{}, {}] - var items = [].concat(name); + const items = [].concat(name); for (var i = 0, il = items.length; i < il; ++i) { - var item = items[i]; - item = Schema.apply('methodObject', item); + const item = Schema.apply('methodObject', items[i]); this._add(item.name, item.method, item.options, realm); } }; @@ -49,16 +48,16 @@ internals.Methods.prototype._add = function (name, method, options, realm) { options = Schema.apply('method', options || {}, name); - var settings = Hoek.cloneWithShallow(options, ['bind']); + const settings = Hoek.cloneWithShallow(options, ['bind']); settings.generateKey = settings.generateKey || internals.generateKey; - var bind = settings.bind || realm.settings.bind || null; + const bind = settings.bind || realm.settings.bind || null; - var apply = function () { + const apply = function () { return method.apply(bind, arguments); }; - var bound = bind ? apply : method; + const bound = bind ? apply : method; // Normalize methods @@ -66,12 +65,12 @@ internals.Methods.prototype._add = function (name, method, options, realm) { if (settings.callback === false) { // Defaults to true normalized = function (/* arg1, arg2, ..., argn, methodNext */) { - var args = []; + const args = []; for (var i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - var methodNext = arguments[il - 1]; + const methodNext = arguments[il - 1]; var result = null; var error = null; @@ -97,12 +96,12 @@ internals.Methods.prototype._add = function (name, method, options, realm) { // Promise object - var onFulfilled = function (outcome) { + const onFulfilled = function (outcome) { return methodNext(null, outcome); }; - var onRejected = function (err) { + const onRejected = function (err) { return methodNext(err); }; @@ -128,18 +127,18 @@ internals.Methods.prototype._add = function (name, method, options, realm) { normalized.apply(bind, id.args); }; - var cache = this.server.cache(settings.cache, '#' + name); + const cache = this.server.cache(settings.cache, '#' + name); - var func = function (/* arguments, methodNext */) { + const func = function (/* arguments, methodNext */) { - var args = []; + const args = []; for (var i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - var methodNext = arguments[il - 1]; + const methodNext = arguments[il - 1]; - var key = settings.generateKey.apply(bind, args); + const key = settings.generateKey.apply(bind, args); if (key === null || // Value can be '' typeof key !== 'string') { // When using custom generateKey @@ -152,14 +151,14 @@ internals.Methods.prototype._add = function (name, method, options, realm) { func.cache = { drop: function (/* arguments, callback */) { - var args = []; + const args = []; for (var i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - var methodNext = arguments[il - 1]; + const methodNext = arguments[il - 1]; - var key = settings.generateKey.apply(null, args); + const key = settings.generateKey.apply(null, args); if (key === null) { // Value can be '' return Hoek.nextTick(methodNext)(Boom.badImplementation('Invalid method key')); } @@ -175,7 +174,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { internals.Methods.prototype._assign = function (name, method, normalized) { - var path = name.split('.'); + const path = name.split('.'); var ref = this.methods; for (var i = 0, il = path.length; i < il; ++i) { if (!ref[path[i]]) { @@ -193,7 +192,7 @@ internals.generateKey = function () { var key = ''; for (var i = 0, il = arguments.length; i < il; ++i) { - var arg = arguments[i]; + const arg = arguments[i]; if (typeof arg !== 'string' && typeof arg !== 'number' && typeof arg !== 'boolean') { diff --git a/lib/plugin.js b/lib/plugin.js index 274dc0a5c..f22c58dc2 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -2,24 +2,24 @@ // Load modules -var Catbox = require('catbox'); -var Hoek = require('hoek'); -var Items = require('items'); -var Kilt = require('kilt'); -var Connection = require('./connection'); -var Ext = require('./ext'); -var Package = require('../package.json'); -var Schema = require('./schema'); +const Catbox = require('catbox'); +const Hoek = require('hoek'); +const Items = require('items'); +const Kilt = require('kilt'); +const Connection = require('./connection'); +const Ext = require('./ext'); +const Package = require('../package.json'); +const Schema = require('./schema'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Plugin = function (server, connections, env, parent) { // env can be a realm or plugin name - var self = this; + const self = this; Kilt.call(this, connections, server._events); @@ -82,9 +82,9 @@ exports = module.exports = internals.Plugin = function (server, connections, env // Decorations - var methods = Object.keys(this.root._decorations); + const methods = Object.keys(this.root._decorations); for (var i = 0, il = methods.length; i < il; ++i) { - var method = methods[i]; + const method = methods[i]; this[method] = this.root._decorations[method]; } }; @@ -132,7 +132,7 @@ internals.Plugin.prototype._select = function (labels, plugin) { connections = []; for (var i = 0, il = this.connections.length; i < il; ++i) { - var connection = this.connections[i]; + const connection = this.connections[i]; if (Hoek.intersect(connection.settings.labels, labels).length) { connections.push(connection); } @@ -145,24 +145,24 @@ internals.Plugin.prototype._select = function (labels, plugin) { } } - var env = (plugin !== undefined ? plugin : this.realm); // Allow empty string + const env = (plugin !== undefined ? plugin : this.realm); // Allow empty string return new internals.Plugin(this.root, connections, env, this); }; internals.Plugin.prototype._clone = function (connections, plugin) { - var env = (plugin !== undefined ? plugin : this.realm); // Allow empty string + const env = (plugin !== undefined ? plugin : this.realm); // Allow empty string return new internals.Plugin(this.root, connections, env, this); }; internals.Plugin.prototype.register = function (plugins /*, [options], callback */) { - var self = this; + const self = this; var options = (typeof arguments[1] === 'object' ? arguments[1] : {}); - var callback = (typeof arguments[1] === 'object' ? arguments[2] : arguments[1]); + const callback = (typeof arguments[1] === 'object' ? arguments[2] : arguments[1]); Hoek.assert(typeof callback === 'function', 'A callback function is required to register a plugin'); @@ -179,7 +179,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback options = Schema.apply('register', options); /* - var register = function (server, options, next) { return next(); }; + const register = function (server, options, next) { return next(); }; register.attributes = { pkg: require('../package.json'), name: 'plugin', @@ -190,21 +190,21 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback once: true }; - var item = { + const item = { register: register, options: options // -optional-- }; - OR - - var item = function () {} + const item = function () {} item.register = register; item.options = options; - var plugins = register, items, [register, item] + const plugins = register, items, [register, item] */ - var registrations = []; + const registrations = []; plugins = [].concat(plugins); for (var i = 0, il = plugins.length; i < il; ++i) { var plugin = plugins[i]; @@ -224,8 +224,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback plugin = Schema.apply('plugin', plugin); - var attributes = plugin.register.attributes; - var registration = { + const attributes = plugin.register.attributes; + const registration = { register: plugin.register, name: attributes.name || attributes.pkg.name, version: attributes.version || attributes.pkg.version, @@ -250,12 +250,12 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Items.serial(registrations, function (item, next) { - var selection = self._select(item.options.select, item.name); + const selection = self._select(item.options.select, item.name); selection.realm.modifiers.route.prefix = item.options.routes.prefix; selection.realm.modifiers.route.vhost = item.options.routes.vhost; selection.realm.pluginOptions = item.pluginOptions || {}; - var registrationData = { + const registrationData = { version: item.version, name: item.name, options: item.pluginOptions, @@ -277,10 +277,10 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback } } - var connections = []; + const connections = []; if (selection.connections) { for (var j = 0, jl = selection.connections.length; j < jl; ++j) { - var connection = selection.connections[j]; + const connection = selection.connections[j]; if (connection.registrations[item.name]) { if (item.options.once) { continue; @@ -336,11 +336,11 @@ internals.Plugin.prototype.cache = function (options, _segment) { options = Schema.apply('cachePolicy', options); - var segment = options.segment || _segment || (this.realm.plugin ? '!' + this.realm.plugin : ''); + const segment = options.segment || _segment || (this.realm.plugin ? '!' + this.realm.plugin : ''); Hoek.assert(segment, 'Missing cache segment name'); - var cacheName = options.cache || '_default'; - var cache = this.root._caches[cacheName]; + const cacheName = options.cache || '_default'; + const cache = this.root._caches[cacheName]; Hoek.assert(cache, 'Unknown cache', cacheName); Hoek.assert(!cache.segments[segment] || cache.shared || options.shared, 'Cannot provision the same cache segment more than once'); cache.segments[segment] = true; @@ -402,7 +402,7 @@ internals.Plugin.prototype.expose = function (key, value) { Hoek.assert(this.realm.plugin, 'Cannot call expose() outside of a plugin'); - var plugin = this.realm.plugin; + const plugin = this.realm.plugin; this.root.plugins[plugin] = this.root.plugins[plugin] || {}; if (typeof key === 'string') { @@ -432,7 +432,7 @@ internals.Plugin.prototype._ext = function (event) { event = Hoek.shallow(event); event.plugin = this; - var type = event.type; + const type = event.type; if (!this.root._extensions[type]) { @@ -476,16 +476,16 @@ internals.Plugin.prototype.inject = function (options, callback) { internals.Plugin.prototype.log = function (tags, data, timestamp, _internal) { tags = (Array.isArray(tags) ? tags : [tags]); - var now = (timestamp ? (timestamp instanceof Date ? timestamp.getTime() : timestamp) : Date.now()); + const now = (timestamp ? (timestamp instanceof Date ? timestamp.getTime() : timestamp) : Date.now()); - var event = { + const event = { timestamp: now, tags: tags, data: data, internal: !!_internal }; - var tagsMap = Hoek.mapToObject(event.tags); + const tagsMap = Hoek.mapToObject(event.tags); this.root._events.emit('log', event, tagsMap); if (this.root._settings.debug && @@ -551,9 +551,9 @@ internals.Plugin.prototype.table = function (host) { Hoek.assert(this.connections, 'Cannot request routing table from a connectionless plugin'); - var table = []; + const table = []; for (var i = 0, il = this.connections.length; i < il; ++i) { - var connection = this.connections[i]; + const connection = this.connections[i]; table.push({ info: connection.info, labels: connection.settings.labels, table: connection.table(host) }); } @@ -578,7 +578,7 @@ internals.Plugin.prototype._applyChild = function (type, child, func, args) { Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); for (var i = 0, il = this.connections.length; i < il; ++i) { - var obj = this.connections[i][child]; + const obj = this.connections[i][child]; obj[func].apply(obj, args); } }; diff --git a/lib/protect.js b/lib/protect.js index 3cdc72cfd..eb742d8a5 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -3,18 +3,18 @@ // Load modules var Domain = null; // Loaded as needed -var Boom = require('boom'); -var Hoek = require('hoek'); +const Boom = require('boom'); +const Hoek = require('hoek'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Protect = function (request) { - var self = this; + const self = this; this._error = null; this.logger = request; // Replaced with server when request completes @@ -36,7 +36,7 @@ exports = module.exports = internals.Protect = function (request) { internals.Protect.prototype._onError = function (err) { - var handler = this._error; + const handler = this._error; if (handler) { this._error = null; return handler(err); @@ -48,15 +48,13 @@ internals.Protect.prototype._onError = function (err) { internals.Protect.prototype.run = function (next, enter) { // enter: function (exit) - var self = this; + const self = this; - var finish = function (arg0, arg1, arg2) { + const finish = Hoek.once(function (arg0, arg1, arg2) { self._error = null; return next(arg0, arg1, arg2); - }; - - finish = Hoek.once(finish); + }); if (!this.domain) { return enter(finish); diff --git a/lib/reply.js b/lib/reply.js index d123eaf3e..da9fa6c12 100755 --- a/lib/reply.js +++ b/lib/reply.js @@ -2,13 +2,13 @@ // Load modules -var Hoek = require('hoek'); -var Response = require('./response'); +const Hoek = require('hoek'); +const Response = require('./response'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Reply = function () { @@ -28,7 +28,7 @@ internals.Reply.prototype.decorate = function (property, method) { /* - var handler = function (request, reply) { + const handler = function (request, reply) { reply(error, result, ignore); -> error || result (continue) reply(...).takeover(); -> ... (continue) @@ -36,7 +36,7 @@ internals.Reply.prototype.decorate = function (property, method) { reply.continue(ignore); -> null (continue) }; - var ext = function (request, reply) { + const ext = function (request, reply) { reply(error, result, ignore); -> error || result (respond) reply(...).takeover(); -> ... (respond) @@ -44,7 +44,7 @@ internals.Reply.prototype.decorate = function (property, method) { reply.continue(ignore); -> (continue) }; - var pre = function (request, reply) { + const pre = function (request, reply) { reply(error); -> error (respond) // failAction override reply(null, result, ignore); -> result (continue) @@ -53,7 +53,7 @@ internals.Reply.prototype.decorate = function (property, method) { reply.continue(ignore); -> null (continue) }; - var auth = function (request, reply) { + const auth = function (request, reply) { reply(error, result, data); -> error || result (respond) + data reply(...).takeover(); -> ... (respond) + data @@ -64,7 +64,7 @@ internals.Reply.prototype.decorate = function (property, method) { internals.Reply.prototype.interface = function (request, realm, next) { // next(err || response, data); - var reply = function (err, response, data) { + const reply = function (err, response, data) { reply._data = data; // Held for later return reply.response(err !== null && err !== undefined ? err : response); @@ -84,9 +84,9 @@ internals.Reply.prototype.interface = function (request, realm, next) { // reply.continue = internals.continue; if (this._decorations) { - var methods = Object.keys(this._decorations); + const methods = Object.keys(this._decorations); for (var i = 0, il = methods.length; i < il; ++i) { - var method = methods[i]; + const method = methods[i]; reply[method] = this._decorations[method]; } } @@ -129,12 +129,12 @@ internals.redirect = function (location) { internals.response = function (result) { - var self = this; + const self = this; Hoek.assert(!this._replied, 'reply interface called twice'); this._replied = true; - var response = Response.wrap(result, this.request); + const response = Response.wrap(result, this.request); if (response.isBoom) { this._next(response, this._data); this._next = null; diff --git a/lib/request.js b/lib/request.js index 51f161486..38cfc5e7a 100755 --- a/lib/request.js +++ b/lib/request.js @@ -2,22 +2,22 @@ // Load modules -var Events = require('events'); -var Url = require('url'); -var Accept = require('accept'); -var Boom = require('boom'); -var Hoek = require('hoek'); -var Items = require('items'); -var Peekaboo = require('peekaboo'); -var Qs = require('qs'); -var Protect = require('./protect'); -var Response = require('./response'); -var Transmit = require('./transmit'); +const Events = require('events'); +const Url = require('url'); +const Accept = require('accept'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Items = require('items'); +const Peekaboo = require('peekaboo'); +const Qs = require('qs'); +const Protect = require('./protect'); +const Response = require('./response'); +const Transmit = require('./transmit'); // Declare internals -var internals = { +const internals = { properties: ['connection', 'server', 'url', 'query', 'path', 'method', 'mime', 'setUrl', 'setMethod', 'headers', 'id', 'app', 'plugins', 'route', 'auth', 'session', 'pre', 'preResponses', 'info', 'orig', 'params', 'paramsArray', 'payload', 'state', 'jsonp', 'response', 'raw', 'tail', 'addTail', 'domain', 'log', 'getLog', 'generateResponse'] }; @@ -30,14 +30,14 @@ exports = module.exports = internals.Generator = function () { internals.Generator.prototype.request = function (connection, req, res, options) { - var request = new internals.Request(connection, req, res, options); + const request = new internals.Request(connection, req, res, options); // Decorate if (this._decorations) { - var methods = Object.keys(this._decorations); + const methods = Object.keys(this._decorations); for (var i = 0, il = methods.length; i < il; ++i) { - var method = methods[i]; + const method = methods[i]; request[method] = this._decorations[method]; } } @@ -58,14 +58,14 @@ internals.Generator.prototype.decorate = function (property, method) { internals.Request = function (connection, req, res, options) { - var self = this; + const self = this; Events.EventEmitter.call(this); // Take measurement as soon as possible this._bench = new Hoek.Bench(); - var now = Date.now(); + const now = Date.now(); // Public members @@ -192,7 +192,7 @@ internals.Request = function (connection, req, res, options) { // Log request - var about = { + const about = { method: this.method, url: this.url.href, agent: this.raw.req.headers['user-agent'] @@ -233,9 +233,9 @@ internals.Request.prototype._setMethod = function (method) { internals.Request.prototype.log = function (tags, data, timestamp, _internal) { tags = (Array.isArray(tags) ? tags : [tags]); - var now = (timestamp ? (timestamp instanceof Date ? timestamp.getTime() : timestamp) : Date.now()); + const now = (timestamp ? (timestamp instanceof Date ? timestamp.getTime() : timestamp) : Date.now()); - var event = { + const event = { request: this.id, timestamp: now, tags: tags, @@ -243,7 +243,7 @@ internals.Request.prototype.log = function (tags, data, timestamp, _internal) { internal: !!_internal }; - var tagsMap = Hoek.mapToObject(event.tags); + const tagsMap = Hoek.mapToObject(event.tags); // Add to request array @@ -279,15 +279,15 @@ internals.Request.prototype.getLog = function (tags, internal) { return this._logger; } - var filter = tags.length ? Hoek.mapToObject(tags) : null; - var result = []; + const filter = tags.length ? Hoek.mapToObject(tags) : null; + const result = []; for (var i = 0, il = this._logger.length; i < il; ++i) { - var event = this._logger[i]; + const event = this._logger[i]; if (internal === undefined || event.internal === internal) { if (filter) { for (var t = 0, tl = event.tags.length; t < tl; ++t) { - var tag = event.tags[t]; + const tag = event.tags[t]; if (filter[tag]) { result.push(event); break; @@ -306,7 +306,7 @@ internals.Request.prototype.getLog = function (tags, internal) { internals.Request.prototype._execute = function () { - var self = this; + const self = this; // Execute onRequest extensions (can change request method and url) @@ -323,7 +323,7 @@ internals.Request.prototype._execute = function () { internals.Request.prototype._lifecycle = function (err) { - var self = this; + const self = this; // Undecorate request @@ -342,7 +342,7 @@ internals.Request.prototype._lifecycle = function (err) { // Lookup route - var match = this.connection._router.route(this.method, this.path, this.info.hostname); + const match = this.connection._router.route(this.method, this.path, this.info.hostname); if (!match.route.settings.isInternal || this._allowInternals) { @@ -364,7 +364,7 @@ internals.Request.prototype._lifecycle = function (err) { var serverTimeout = this.route.settings.timeout.server; if (serverTimeout) { serverTimeout = Math.floor(serverTimeout - this._bench.elapsed()); // Calculate the timeout from when the request was constructed - var timeoutReply = function () { + const timeoutReply = function () { self._log(['request', 'server', 'timeout', 'error'], { timeout: serverTimeout, elapsed: self._bench.elapsed() }); self._reply(Boom.serverTimeout()); @@ -400,14 +400,14 @@ internals.Request.prototype._lifecycle = function (err) { internals.Request.prototype._invoke = function (event, callback) { - var self = this; + const self = this; this._protect.run(callback, function (exit) { Items.serial(event.nodes, function (ext, next) { - var reply = self.server._replier.interface(self, ext.plugin.realm, next); - var bind = (ext.bind || ext.plugin.realm.settings.bind); + const reply = self.server._replier.interface(self, ext.plugin.realm, next); + const bind = (ext.bind || ext.plugin.realm.settings.bind); ext.func.call(bind, self, reply); }, exit); @@ -417,7 +417,7 @@ internals.Request.prototype._invoke = function (event, callback) { internals.Request.prototype._reply = function (exit) { - var self = this; + const self = this; if (this._isReplied) { // Prevent any future responses to this request return; @@ -447,7 +447,7 @@ internals.Request.prototype._reply = function (exit) { this._protect.reset(); - var transmit = function (err) { + const transmit = function (err) { if (err) { // err can be valid response or error self._setResponse(Response.wrap(err, self)); @@ -529,14 +529,14 @@ internals.Request.prototype._setResponse = function (response) { internals.Request.prototype._addTail = function (name) { - var self = this; + const self = this; name = name || 'unknown'; - var tailId = this._tailIds++; + const tailId = this._tailIds++; this._tails[tailId] = name; this._log(['tail', 'add'], { name: name, id: tailId }); - var drop = function () { + const drop = function () { if (!self._tails[tailId]) { self._log(['tail', 'remove', 'error'], { name: name, id: tailId }); // Already removed @@ -562,7 +562,7 @@ internals.Request.prototype._addTail = function (name) { internals.Request.prototype._setState = function (name, value, options) { // options: see Defaults.state - var state = { + const state = { name: name, value: value }; @@ -578,7 +578,7 @@ internals.Request.prototype._setState = function (name, value, options) { internals.Request.prototype._clearState = function (name, options) { - var state = { + const state = { name: name }; diff --git a/lib/response.js b/lib/response.js index 51a0e5904..36a583c1b 100755 --- a/lib/response.js +++ b/lib/response.js @@ -2,16 +2,16 @@ // Load modules -var Stream = require('stream'); -var Events = require('events'); -var Boom = require('boom'); -var Hoek = require('hoek'); -var Peekaboo = require('peekaboo'); +const Stream = require('stream'); +const Events = require('events'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Peekaboo = require('peekaboo'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Response = function (source, request, options) { @@ -114,20 +114,20 @@ internals.Response.prototype.header = function (key, value, options) { internals.Response.prototype._header = function (key, value, options) { options = options || {}; - var append = options.append || false; - var separator = options.separator || ','; - var override = options.override !== false; - var duplicate = options.duplicate !== false; + const append = options.append || false; + const separator = options.separator || ','; + const override = options.override !== false; + const duplicate = options.duplicate !== false; // Ensure key and values do not include non-ascii text if (value !== undefined && value !== null) { - var headerValues = [key].concat(value); + const headerValues = [key].concat(value); for (var v = 0, vl = headerValues.length; v < vl; ++v) { - var header = headerValues[v]; - var buffer = Buffer.isBuffer(header) ? header : new Buffer(header.toString()); + const header = headerValues[v]; + const buffer = Buffer.isBuffer(header) ? header : new Buffer(header.toString()); for (var b = 0, bl = buffer.length; b < bl; ++b) { Hoek.assert((buffer[b] & 0x7f) === buffer[b], 'Header value cannot contain or convert into non-ascii characters:', key); } @@ -144,9 +144,9 @@ internals.Response.prototype._header = function (key, value, options) { this.headers[key] = [].concat(this.headers[key], value); } else { - var existing = this.headers[key]; + const existing = this.headers[key]; if (!duplicate) { - var values = existing.split(separator); + const values = existing.split(separator); for (var i = 0, il = values.length; i < il; ++i) { if (values[i] === value) { return this; @@ -381,7 +381,7 @@ internals.Response.prototype.takeover = function () { internals.Response.prototype._prepare = function (data, next) { - var self = this; + const self = this; this._passThrough(); @@ -389,7 +389,7 @@ internals.Response.prototype._prepare = function (data, next) { return this._processPrepare(data, next); } - var onDone = function (source) { + const onDone = function (source) { if (source instanceof Error) { return next(Boom.wrap(source), data); @@ -423,17 +423,17 @@ internals.Response.prototype._passThrough = function () { var headerKeys = Object.keys(this.source.headers); if (headerKeys.length) { - var localHeaders = this.headers; + const localHeaders = this.headers; this.headers = {}; for (var i = 0, il = headerKeys.length; i < il; ++i) { - var key = headerKeys[i]; + const key = headerKeys[i]; this.header(key.toLowerCase(), Hoek.clone(this.source.headers[key])); // Clone arrays } headerKeys = Object.keys(localHeaders); for (i = 0, il = headerKeys.length; i < il; ++i) { - key = headerKeys[i]; + const key = headerKeys[i]; this.header(key, localHeaders[key], { append: key === 'set-cookie' }); } } @@ -459,7 +459,7 @@ internals.Response.prototype._processPrepare = function (data, next) { internals.Response.prototype._marshal = function (next) { - var self = this; + const self = this; if (!this._processors.marshal) { return this._streamify(this.source, next); @@ -496,10 +496,10 @@ internals.Response.prototype._streamify = function (source, next) { source !== null && typeof source !== 'string') { - var options = this.settings.stringify || {}; - var space = options.space || this.request.route.settings.json.space; - var replacer = options.replacer || this.request.route.settings.json.replacer; - var suffix = options.suffix || this.request.route.settings.json.suffix || ''; + const options = this.settings.stringify || {}; + const space = options.space || this.request.route.settings.json.space; + const replacer = options.replacer || this.request.route.settings.json.replacer; + const suffix = options.suffix || this.request.route.settings.json.suffix || ''; try { payload = JSON.stringify(payload, replacer, space); } @@ -532,7 +532,7 @@ internals.Response.prototype._close = function () { this._processors.close(this); } - var stream = this._payload || this.source; + const stream = this._payload || this.source; if (stream instanceof Stream) { if (stream.close) { stream.close(); @@ -541,12 +541,12 @@ internals.Response.prototype._close = function () { stream.destroy(); } else { - var read = function () { + const read = function () { stream.read(); }; - var end = function () { + const end = function () { stream.removeListener('readable', read); stream.removeListener('error', end); diff --git a/lib/route.js b/lib/route.js index 058606f5a..8f9317434 100755 --- a/lib/route.js +++ b/lib/route.js @@ -2,30 +2,30 @@ // Load modules -var Boom = require('boom'); -var Catbox = require('catbox'); -var Hoek = require('hoek'); -var Joi = require('joi'); -var Subtext = require('subtext'); -var Auth = require('./auth'); -var Cors = require('./cors'); -var Defaults = require('./defaults'); -var Ext = require('./ext'); -var Handler = require('./handler'); -var Validation = require('./validation'); -var Schema = require('./schema'); +const Boom = require('boom'); +const Catbox = require('catbox'); +const Hoek = require('hoek'); +const Joi = require('joi'); +const Subtext = require('subtext'); +const Auth = require('./auth'); +const Cors = require('./cors'); +const Defaults = require('./defaults'); +const Ext = require('./ext'); +const Handler = require('./handler'); +const Validation = require('./validation'); +const Schema = require('./schema'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Route = function (options, connection, plugin) { // Apply plugin environment (before schema validation) - var realm = plugin.realm; + const realm = plugin.realm; if (realm.modifiers.route.vhost || realm.modifiers.route.prefix) { @@ -44,20 +44,20 @@ exports = module.exports = internals.Route = function (options, connection, plug options = Schema.apply('route', options, options.path); - var handler = options.handler || options.config.handler; - var method = options.method.toLowerCase(); + const handler = options.handler || options.config.handler; + const method = options.method.toLowerCase(); Hoek.assert(method !== 'head', 'Method name not allowed:', options.method, options.path); // Apply settings in order: {connection} <- {handler} <- {realm} <- {route} - var handlerDefaults = Handler.defaults(method, handler, connection.server); + const handlerDefaults = Handler.defaults(method, handler, connection.server); var base = Hoek.applyToDefaultsWithShallow(connection.settings.routes, handlerDefaults, ['bind', 'cors']); base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind', 'cors']); this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind', 'cors']); this.settings.handler = handler; this.settings = Schema.apply('routeConfig', this.settings, options.path); - var socketTimeout = (this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket); + const socketTimeout = (this.settings.timeout.socket === undefined ? 2 * 60 * 1000 : this.settings.timeout.socket); Hoek.assert(!this.settings.timeout.server || !socketTimeout || this.settings.timeout.server < socketTimeout, 'Server timeout must be shorter than socket timeout:', options.path); Hoek.assert(!this.settings.payload.timeout || !socketTimeout || this.settings.payload.timeout < socketTimeout, 'Payload timeout must be shorter than socket timeout:', options.path); @@ -88,7 +88,7 @@ exports = module.exports = internals.Route = function (options, connection, plug // Validation - var validation = this.settings.validate; + const validation = this.settings.validate; if (this.method === 'get') { // Assert on config, not on merged settings @@ -109,9 +109,9 @@ exports = module.exports = internals.Route = function (options, connection, plug this.settings.response._validate = true; - var rule = this.settings.response.schema; + const rule = this.settings.response.schema; this.settings.response.status = this.settings.response.status || {}; - var statuses = Object.keys(this.settings.response.status); + const statuses = Object.keys(this.settings.response.status); if (rule === true && !statuses.length) { @@ -121,7 +121,7 @@ exports = module.exports = internals.Route = function (options, connection, plug else { this.settings.response.schema = internals.compileRule(rule); for (var i = 0, il = statuses.length; i < il; ++i) { - var code = statuses[i]; + const code = statuses[i]; this.settings.response.status[code] = internals.compileRule(this.settings.response.status[code]); } } @@ -164,7 +164,7 @@ exports = module.exports = internals.Route = function (options, connection, plug if (this.settings.security) { this.settings.security = Hoek.applyToDefaults(Defaults.security, this.settings.security); - var security = this.settings.security; + const security = this.settings.security; if (security.hsts) { if (security.hsts === true) { security._hsts = 'max-age=15768000'; @@ -226,9 +226,9 @@ exports = module.exports = internals.Route = function (options, connection, plug internals.Route.prototype._combineExtensions = function (type, subscribe) { - var ext = new Ext(this.server); + const ext = new Ext(this.server); - var events = this.settings.ext[type]; + const events = this.settings.ext[type]; if (events) { for (var i = 0, il = events.length; i < il; ++i) { var event = events[i]; @@ -239,8 +239,8 @@ internals.Route.prototype._combineExtensions = function (type, subscribe) { } } - var connection = this.connection._extensions[type]; - var realm = this.plugin.realm._extensions[type]; + const connection = this.connection._extensions[type]; + const realm = this.plugin.realm._extensions[type]; ext.merge([connection, realm]); @@ -275,7 +275,7 @@ internals.Route.prototype.rebuild = function (event) { // Builde lifecycle array - var cycle = []; + const cycle = []; // 'onRequest' @@ -291,7 +291,7 @@ internals.Route.prototype.rebuild = function (event) { cycle.push(this._extensions.onPreAuth); } - var authenticate = (this.settings.auth !== false); // Anything other than 'false' can still require authentication + const authenticate = (this.settings.auth !== false); // Anything other than 'false' can still require authentication if (authenticate) { cycle.push(Auth.authenticate); } @@ -352,8 +352,8 @@ internals.state = function (request, next) { request.state = {}; - var req = request.raw.req; - var cookies = req.headers.cookie; + const req = request.raw.req; + const cookies = req.headers.cookie; if (!cookies) { return next(); } @@ -365,7 +365,7 @@ internals.state = function (request, next) { // Clear cookies for (var i = 0, il = failed.length; i < il; ++i) { - var item = failed[i]; + const item = failed[i]; if (item.settings.clearInvalid) { request._clearState(item.name); @@ -394,7 +394,7 @@ internals.payload = function (request, next) { return next(); } - var onParsed = function (err, parsed) { + const onParsed = function (err, parsed) { request.mime = parsed.mime; request.payload = parsed.payload || null; @@ -403,7 +403,7 @@ internals.payload = function (request, next) { return next(); } - var failAction = request.route.settings.payload.failAction; // failAction: 'error', 'log', 'ignore' + const failAction = request.route.settings.payload.failAction; // failAction: 'error', 'log', 'ignore' if (failAction !== 'ignore') { request._log(['payload', 'error'], err); } @@ -426,14 +426,14 @@ internals.payload = function (request, next) { // Flush out any pending request payload not consumed due to errors - var stream = request.raw.req; + const stream = request.raw.req; - var read = function () { + const read = function () { stream.read(); }; - var end = function () { + const end = function () { stream.removeListener('readable', read); stream.removeListener('error', end); @@ -452,7 +452,7 @@ internals.payload = function (request, next) { internals.parseJSONP = function (request, next) { - var jsonp = request.query[request.route.settings.jsonp]; + const jsonp = request.query[request.route.settings.jsonp]; if (jsonp) { if (/^[\w\$\[\]\.]+$/.test(jsonp) === false) { return next(Boom.badRequest('Invalid JSONP parameter value')); diff --git a/lib/schema.js b/lib/schema.js index 9c017e827..a6173baf8 100755 --- a/lib/schema.js +++ b/lib/schema.js @@ -2,18 +2,18 @@ // Load modules -var Joi = require('joi'); -var Hoek = require('hoek'); +const Joi = require('joi'); +const Hoek = require('hoek'); // Declare internals -var internals = {}; +const internals = {}; exports.apply = function (type, options, message) { - var result = Joi.validate(options, internals[type]); + const result = Joi.validate(options, internals[type]); Hoek.assert(!result.error, 'Invalid', type, 'options', message ? '(' + message + ')' : '', result.error && result.error.annotate()); return result.value; }; diff --git a/lib/server.js b/lib/server.js index b83890deb..5b370fcc5 100755 --- a/lib/server.js +++ b/lib/server.js @@ -2,26 +2,26 @@ // Load modules -var Events = require('events'); -var Catbox = require('catbox'); -var CatboxMemory = require('catbox-memory'); -var Heavy = require('heavy'); -var Hoek = require('hoek'); -var Items = require('items'); -var Mimos = require('mimos'); -var Connection = require('./connection'); -var Defaults = require('./defaults'); -var Ext = require('./ext'); -var Methods = require('./methods'); -var Plugin = require('./plugin'); -var Reply = require('./reply'); -var Request = require('./request'); -var Schema = require('./schema'); +const Events = require('events'); +const Catbox = require('catbox'); +const CatboxMemory = require('catbox-memory'); +const Heavy = require('heavy'); +const Hoek = require('hoek'); +const Items = require('items'); +const Mimos = require('mimos'); +const Connection = require('./connection'); +const Defaults = require('./defaults'); +const Ext = require('./ext'); +const Methods = require('./methods'); +const Plugin = require('./plugin'); +const Reply = require('./reply'); +const Request = require('./request'); +const Schema = require('./schema'); // Declare internals -var internals = {}; +const internals = {}; exports = module.exports = internals.Server = function (options) { @@ -61,7 +61,7 @@ exports = module.exports = internals.Server = function (options) { }; if (options.cache) { - var caches = [].concat(options.cache); + const caches = [].concat(options.cache); for (var i = 0, il = caches.length; i < il; ++i) { this._createCache(caches[i]); } @@ -83,7 +83,7 @@ internals.Server.prototype._createCache = function (options) { options = { engine: options }; } - var name = options.name || '_default'; + const name = options.name || '_default'; Hoek.assert(!this._caches[name], 'Cannot configure the same cache more than once: ', name === '_default' ? 'default cache' : name); var client = null; @@ -91,7 +91,7 @@ internals.Server.prototype._createCache = function (options) { client = new Catbox.Client(options.engine); } else { - var settings = Hoek.clone(options); + const settings = Hoek.clone(options); settings.partition = settings.partition || 'hapi-cache'; delete settings.name; delete settings.engine; @@ -110,7 +110,7 @@ internals.Server.prototype._createCache = function (options) { internals.Server.prototype.connection = function (options) { - var root = this.root; // Explicitly use the root reference (for plugin invocation) + const root = this.root; // Explicitly use the root reference (for plugin invocation) var settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']); settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors) || false; @@ -118,14 +118,14 @@ internals.Server.prototype.connection = function (options) { settings = Schema.apply('connection', settings); // Applies validation changes (type cast) - var connection = new Connection(root, settings); + const connection = new Connection(root, settings); root.connections.push(connection); root.addEmitter(connection); root._single(); - var registrations = Object.keys(root._registrations); + const registrations = Object.keys(root._registrations); for (var i = 0, il = registrations.length; i < il; ++i) { - var name = registrations[i]; + const name = registrations[i]; connection.registrations[name] = root._registrations[name]; } @@ -135,7 +135,7 @@ internals.Server.prototype.connection = function (options) { internals.Server.prototype.start = function (callback) { - var self = this; + const self = this; Hoek.assert(typeof callback === 'function', 'Missing required start callback function'); @@ -169,11 +169,11 @@ internals.Server.prototype.start = function (callback) { internals.Server.prototype.initialize = function (callback) { - var self = this; + const self = this; Hoek.assert(callback, 'Missing start callback function'); - var errorCallback = Hoek.nextTick(callback); + const errorCallback = Hoek.nextTick(callback); if (!this.connections.length) { return errorCallback(new Error('No connections to start')); } @@ -189,12 +189,12 @@ internals.Server.prototype.initialize = function (callback) { // Assert dependencies for (var i = 0, il = this._dependencies.length; i < il; ++i) { - var dependency = this._dependencies[i]; + const dependency = this._dependencies[i]; if (dependency.connections) { for (var s = 0, sl = dependency.connections.length; s < sl; ++s) { - var connection = dependency.connections[s]; + const connection = dependency.connections[s]; for (var d = 0, dl = dependency.deps.length; d < dl; ++d) { - var dep = dependency.deps[d]; + const dep = dependency.deps[d]; if (!connection.registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); } @@ -203,7 +203,7 @@ internals.Server.prototype.initialize = function (callback) { } else { for (d = 0, dl = dependency.deps.length; d < dl; ++d) { - dep = dependency.deps[d]; + const dep = dependency.deps[d]; if (!this._registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep)); } @@ -215,7 +215,7 @@ internals.Server.prototype.initialize = function (callback) { // Start cache - var caches = Object.keys(self._caches); + const caches = Object.keys(self._caches); Items.parallel(caches, function (cache, next) { self._caches[cache].client.start(next); @@ -251,7 +251,7 @@ internals.Server.prototype.initialize = function (callback) { internals.Server.prototype._start = function (callback) { - var self = this; + const self = this; this._state = 'starting'; @@ -283,12 +283,12 @@ internals.Server.prototype._start = function (callback) { internals.Server.prototype.stop = function (/* [options], callback */) { - var self = this; + const self = this; Hoek.assert(arguments.length, 'Missing required stop callback function'); - var callback = (arguments.length === 1 ? arguments[0] : arguments[1]); - var options = (arguments.length === 1 ? {} : arguments[0]); + const callback = (arguments.length === 1 ? arguments[0] : arguments[1]); + const options = (arguments.length === 1 ? {} : arguments[0]); options.timeout = options.timeout || 5000; // Default timeout to 5 seconds Hoek.assert(typeof callback === 'function', 'Missing required stop callback function'); @@ -317,7 +317,7 @@ internals.Server.prototype.stop = function (/* [options], callback */) { return callback(err); } - var caches = Object.keys(self._caches); + const caches = Object.keys(self._caches); for (var i = 0, il = caches.length; i < il; ++i) { self._caches[caches[i]].client.stop(); } @@ -341,14 +341,14 @@ internals.Server.prototype.stop = function (/* [options], callback */) { internals.Server.prototype._invoke = function (type, next) { - var exts = this._extensions[type]; + const exts = this._extensions[type]; if (!exts.nodes) { return next(); } Items.serial(exts.nodes, function (ext, nextExt) { - var bind = (ext.bind || ext.plugin.realm.settings.bind); + const bind = (ext.bind || ext.plugin.realm.settings.bind); ext.func.call(bind, ext.plugin._select(), nextExt); }, next); }; diff --git a/lib/transmit.js b/lib/transmit.js index d57e70f75..9a8d1a20b 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -2,27 +2,27 @@ // Load modules -var Http = require('http'); -var Stream = require('stream'); -var Zlib = require('zlib'); -var Ammo = require('ammo'); -var Boom = require('boom'); -var Hoek = require('hoek'); -var Items = require('items'); -var Shot = require('shot'); -var Auth = require('./auth'); -var Cors = require('./cors'); -var Response = require('./response'); +const Http = require('http'); +const Stream = require('stream'); +const Zlib = require('zlib'); +const Ammo = require('ammo'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Items = require('items'); +const Shot = require('shot'); +const Auth = require('./auth'); +const Cors = require('./cors'); +const Response = require('./response'); // Declare internals -var internals = {}; +const internals = {}; exports.send = function (request, callback) { - var response = request.response; + const response = request.response; if (response.isBoom) { return internals.fail(request, response, callback); } @@ -41,7 +41,7 @@ exports.send = function (request, callback) { internals.marshal = function (request, next) { - var response = request.response; + const response = request.response; Cors.headers(response); internals.content(response); @@ -55,15 +55,15 @@ internals.marshal = function (request, next) { // Strong verifier - var ifNoneMatch = request.headers['if-none-match'].split(/\s*,\s*/); + const ifNoneMatch = request.headers['if-none-match'].split(/\s*,\s*/); for (var i = 0, il = ifNoneMatch.length; i < il; ++i) { - var etag = ifNoneMatch[i]; + const etag = ifNoneMatch[i]; if (etag === response.headers.etag) { response.code(304); break; } else if (response.settings.varyEtag) { - var etagBase = response.headers.etag.slice(0, -1); + const etagBase = response.headers.etag.slice(0, -1); if (etag === etagBase + '-gzip"' || etag === etagBase + '-deflate"') { @@ -74,16 +74,16 @@ internals.marshal = function (request, next) { } } else { - var ifModifiedSinceHeader = request.headers['if-modified-since']; - var lastModifiedHeader = response.headers['last-modified']; + const ifModifiedSinceHeader = request.headers['if-modified-since']; + const lastModifiedHeader = response.headers['last-modified']; if (ifModifiedSinceHeader && lastModifiedHeader) { // Weak verifier - var ifModifiedSince = Date.parse(ifModifiedSinceHeader); - var lastModified = Date.parse(lastModifiedHeader); + const ifModifiedSince = Date.parse(ifModifiedSinceHeader); + const lastModified = Date.parse(lastModifiedHeader); if (ifModifiedSince && lastModified && @@ -149,8 +149,8 @@ internals.marshal = function (request, next) { internals.fail = function (request, boom, callback) { - var error = boom.output; - var response = new Response(error.payload, request); + const error = boom.output; + const response = new Response(error.payload, request); response._error = boom; response.code(error.statusCode); response.headers = error.headers; @@ -162,7 +162,7 @@ internals.fail = function (request, boom, callback) { // Failed to marshal an error - replace with minimal representation of original error - var minimal = { + const minimal = { statusCode: error.statusCode, error: Http.STATUS_CODES[error.statusCode], message: boom.message @@ -180,9 +180,9 @@ internals.transmit = function (response, callback) { // Setup source - var request = response.request; - var source = response._payload; - var length = parseInt(response.headers['content-length'], 10); // In case value is a string + const request = response.request; + const source = response._payload; + const length = parseInt(response.headers['content-length'], 10); // In case value is a string // Empty response @@ -196,7 +196,7 @@ internals.transmit = function (response, callback) { // Compression - var mime = request.server.mime.type(response.headers['content-type'] || 'application/octet-stream'); + const mime = request.server.mime.type(response.headers['content-type'] || 'application/octet-stream'); var encoding = (request.connection.settings.compression && mime.compressible && !response.headers['content-encoding'] ? request.info.acceptEncoding : null); encoding = (encoding === 'identity' ? null : encoding); @@ -216,9 +216,9 @@ internals.transmit = function (response, callback) { // Parse header - var ranges = Ammo.header(request.headers.range, length); + const ranges = Ammo.header(request.headers.range, length); if (!ranges) { - var error = Boom.rangeNotSatisfiable(); + const error = Boom.rangeNotSatisfiable(); error.output.headers['content-range'] = 'bytes */' + length; return internals.fail(request, error, callback); } @@ -226,7 +226,7 @@ internals.transmit = function (response, callback) { // Prepare transform if (ranges.length === 1) { // Ignore requests for multiple ranges - var range = ranges[0]; + const range = ranges[0]; var ranger = new Ammo.Stream(range); response.code(206); response.bytes(range.to - range.from + 1); @@ -263,10 +263,10 @@ internals.transmit = function (response, callback) { // Write headers - var headers = Object.keys(response.headers); + const headers = Object.keys(response.headers); for (var h = 0, hl = headers.length; h < hl; ++h) { - var header = headers[h]; - var value = response.headers[header]; + const header = headers[h]; + const value = response.headers[header]; if (value !== undefined) { request.raw.res.setHeader(header, value); } @@ -276,12 +276,12 @@ internals.transmit = function (response, callback) { // Generate tap stream - var tap = response._tap(); + const tap = response._tap(); // Write payload var hasEnded = false; - var end = function (err, event) { + const end = function (err, event) { if (hasEnded) { return; @@ -302,7 +302,7 @@ internals.transmit = function (response, callback) { request.raw.res.removeListener('error', end); request.raw.res.removeListener('finish', end); - var tags = (err ? ['response', 'error'] + const tags = (err ? ['response', 'error'] : (event ? ['response', 'error', event] : ['response'])); @@ -316,12 +316,12 @@ internals.transmit = function (response, callback) { source.once('error', end); - var onAborted = function () { + const onAborted = function () { end(null, 'aborted'); }; - var onClose = function () { + const onClose = function () { end(null, 'close'); }; @@ -333,9 +333,9 @@ internals.transmit = function (response, callback) { request.raw.res.once('error', end); request.raw.res.once('finish', end); - var preview = (tap ? source.pipe(tap) : source); - var compressed = (compressor ? preview.pipe(compressor) : preview); - var ranged = (ranger ? compressed.pipe(ranger) : compressed); + const preview = (tap ? source.pipe(tap) : source); + const compressed = (compressor ? preview.pipe(compressor) : preview); + const ranged = (ranger ? compressed.pipe(ranger) : compressed); ranged.pipe(request.raw.res); // Injection @@ -372,13 +372,13 @@ internals.cache = function (response) { return; } - var request = response.request; - var policy = request._route._cache && (request.route.settings.cache._statuses[response.statusCode] || (response.statusCode === 304 && request.route.settings.cache._statuses['200'])); + const request = response.request; + const policy = request._route._cache && (request.route.settings.cache._statuses[response.statusCode] || (response.statusCode === 304 && request.route.settings.cache._statuses['200'])); if (policy || response.settings.ttl) { - var ttl = (response.settings.ttl !== null ? response.settings.ttl : request._route._cache.ttl()); - var privacy = (request.auth.isAuthenticated || response.headers['set-cookie'] ? 'private' : request.route.settings.cache.privacy || 'default'); + const ttl = (response.settings.ttl !== null ? response.settings.ttl : request._route._cache.ttl()); + const privacy = (request.auth.isAuthenticated || response.headers['set-cookie'] ? 'private' : request.route.settings.cache.privacy || 'default'); response._header('cache-control', 'max-age=' + Math.floor(ttl / 1000) + ', must-revalidate' + (privacy !== 'default' ? ', ' + privacy : '')); } else { @@ -389,9 +389,9 @@ internals.cache = function (response) { internals.security = function (response) { - var request = response.request; + const request = response.request; - var security = request.route.settings.security; + const security = request.route.settings.security; if (security) { if (security._hsts) { response._header('strict-transport-security', security._hsts, { override: false }); @@ -418,9 +418,9 @@ internals.security = function (response) { internals.content = function (response) { - var type = response.headers['content-type']; + const type = response.headers['content-type']; if (!type) { - var charset = (response.settings.charset ? '; charset=' + response.settings.charset : ''); + const charset = (response.settings.charset ? '; charset=' + response.settings.charset : ''); if (typeof response.source === 'string') { response.type('text/html' + charset); @@ -437,7 +437,7 @@ internals.content = function (response) { else if (response.settings.charset && type.match(/^(?:text\/)|(?:application\/(?:json)|(?:javascript))/)) { - var hasParams = (type.indexOf(';') !== -1); + const hasParams = (type.indexOf(';') !== -1); if (!hasParams || !type.match(/[; ]charset=/)) { @@ -449,14 +449,14 @@ internals.content = function (response) { internals.state = function (response, next) { - var request = response.request; + const request = response.request; - var names = {}; - var states = []; + const names = {}; + const states = []; var keys = Object.keys(request._states); for (var i = 0, il = keys.length; i < il; ++i) { - var keyName = keys[i]; + const keyName = keys[i]; names[keyName] = true; states.push(request._states[keyName]); } @@ -464,7 +464,7 @@ internals.state = function (response, next) { keys = Object.keys(request.connection.states.cookies); Items.parallel(keys, function (name, nextKey) { - var autoValue = request.connection.states.cookies[name].autoValue; + const autoValue = request.connection.states.cookies[name].autoValue; if (!autoValue || names[name]) { return nextKey(); } @@ -502,7 +502,7 @@ internals.state = function (response, next) { return next(Boom.wrap(err)); } - var existing = response.headers['set-cookie']; + const existing = response.headers['set-cookie']; if (existing) { header = (Array.isArray(existing) ? existing : [existing]).concat(header); } diff --git a/lib/validation.js b/lib/validation.js index 9294a728b..7635c0066 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -2,14 +2,14 @@ // Load modules -var Boom = require('boom'); -var Hoek = require('hoek'); -var Joi = require('joi'); +const Boom = require('boom'); +const Hoek = require('hoek'); +const Joi = require('joi'); // Declare internals -var internals = {}; +const internals = {}; exports.query = function (request, next) { @@ -48,7 +48,7 @@ internals.input = function (source, request, next) { return next(Boom.unsupportedMediaType(source + ' must represent an object')); } - var postValidate = function (err, value) { + const postValidate = function (err, value) { request.orig[source] = request[source]; if (value !== undefined) { @@ -71,7 +71,7 @@ internals.input = function (source, request, next) { // Prepare error - var error = Boom.badRequest(err.message, err); + const error = Boom.badRequest(err.message, err); error.output.payload.validation = { source: source, keys: [] }; if (err.details) { for (var i = 0, il = err.details.length; i < il; ++i) { @@ -80,9 +80,9 @@ internals.input = function (source, request, next) { } if (request.route.settings.validate.errorFields) { - var fields = Object.keys(request.route.settings.validate.errorFields); + const fields = Object.keys(request.route.settings.validate.errorFields); for (var f = 0, fl = fields.length; f < fl; ++f) { - var field = fields[f]; + const field = fields[f]; error.output.payload[field] = request.route.settings.validate.errorFields[field]; } } @@ -105,12 +105,12 @@ internals.input = function (source, request, next) { request._protect.run(next, function (exit) { - var reply = request.server._replier.interface(request, request.route.realm, exit); + const reply = request.server._replier.interface(request, request.route.realm, exit); request.route.settings.validate.failAction(request, reply, source, error); }); }; - var localOptions = { + const localOptions = { context: { headers: request.headers, params: request.params, @@ -126,7 +126,7 @@ internals.input = function (source, request, next) { delete localOptions.context[source]; Hoek.merge(localOptions, request.route.settings.validate.options); - var schema = request.route.settings.validate[source]; + const schema = request.route.settings.validate[source]; if (typeof schema !== 'function') { return Joi.validate(request[source], schema, localOptions, postValidate); } @@ -141,23 +141,23 @@ internals.input = function (source, request, next) { exports.response = function (request, next) { if (request.route.settings.response.sample) { - var currentSample = Math.ceil((Math.random() * 100)); + const currentSample = Math.ceil((Math.random() * 100)); if (currentSample > request.route.settings.response.sample) { return next(); } } - var response = request.response; - var statusCode = response.isBoom ? response.output.statusCode : response.statusCode; + const response = request.response; + const statusCode = response.isBoom ? response.output.statusCode : response.statusCode; - var statusSchema = request.route.settings.response.status[statusCode]; + const statusSchema = request.route.settings.response.status[statusCode]; if (statusCode >= 400 && !statusSchema) { return next(); // Do not validate errors by default } - var schema = statusSchema || request.route.settings.response.schema; + const schema = statusSchema || request.route.settings.response.schema; if (schema === null) { return next(); // No rules } @@ -168,7 +168,7 @@ exports.response = function (request, next) { return next(Boom.badImplementation('Cannot validate non-object response')); } - var postValidate = function (err, value) { + const postValidate = function (err, value) { if (!err) { if (value !== undefined && @@ -195,7 +195,7 @@ exports.response = function (request, next) { return next(Boom.badImplementation(err.message)); }; - var localOptions = { + const localOptions = { context: { headers: request.headers, params: request.params, @@ -208,7 +208,7 @@ exports.response = function (request, next) { } }; - var source = response.isBoom ? response.output.payload : response.source; + const source = response.isBoom ? response.output.payload : response.source; Hoek.merge(localOptions, request.route.settings.response.options); if (typeof schema !== 'function') { diff --git a/test/auth.js b/test/auth.js index 6335d17b7..229a37f31 100755 --- a/test/auth.js +++ b/test/auth.js @@ -2,39 +2,39 @@ // Load modules -var Path = require('path'); -var Boom = require('boom'); -var Code = require('code'); -var Handlebars = require('handlebars'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); -var Vision = require('vision'); +const Path = require('path'); +const Boom = require('boom'); +const Code = require('code'); +const Handlebars = require('handlebars'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); +const Vision = require('vision'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('authentication', function () { it('requires and authenticates a request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -54,12 +54,12 @@ describe('authentication', function () { it('defaults cache to private if request authenticated', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').ttl(1000); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -77,12 +77,12 @@ describe('authentication', function () { it('fails when options default to null', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true); @@ -97,7 +97,7 @@ describe('authentication', function () { it('throws when strategy missing scheme', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -108,7 +108,7 @@ describe('authentication', function () { it('adds a route to server', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} }, route: true }); @@ -127,14 +127,14 @@ describe('authentication', function () { it('uses views', function (done) { - var implementation = function (server, options) { + const implementation = function (server, options) { server.views({ engines: { 'html': Handlebars }, relativeTo: Path.join(__dirname, '/templates/plugin') }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.view('test', { message: 'steve' }); }; @@ -149,7 +149,7 @@ describe('authentication', function () { }; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); server.connection(); @@ -188,7 +188,7 @@ describe('authentication', function () { it('sets default', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -196,7 +196,7 @@ describe('authentication', function () { server.auth.default('default'); expect(server.connections[0].auth.settings.default).to.deep.equal({ strategies: ['default'], mode: 'required' }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; @@ -217,12 +217,12 @@ describe('authentication', function () { it('sets default with object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -243,7 +243,7 @@ describe('authentication', function () { it('throws when setting default twice', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -257,7 +257,7 @@ describe('authentication', function () { it('throws when setting default without strategy', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -273,7 +273,7 @@ describe('authentication', function () { it('throws when route refers to nonexistent strategy', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('a', 'custom', { users: { steve: {} } }); @@ -304,12 +304,12 @@ describe('authentication', function () { it('returns the route auth config', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.connection.auth.lookup(request.route)); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -332,12 +332,12 @@ describe('authentication', function () { it('setups route with optional authentication', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(!!request.auth.credentials); }; @@ -359,7 +359,7 @@ describe('authentication', function () { it('exposes mode', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -382,7 +382,7 @@ describe('authentication', function () { it('authenticates using multiple strategies', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('first', 'custom', { users: { steve: 'skip' } }); @@ -411,21 +411,21 @@ describe('authentication', function () { it('authenticates using credentials object', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { user: 'steve' } } }); - var doubleHandler = function (request, reply) { + const doubleHandler = function (request, reply) { - var options = { url: '/2', credentials: request.auth.credentials }; + const options = { url: '/2', credentials: request.auth.credentials }; server.inject(options, function (res) { return reply(res.result); }); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; @@ -443,21 +443,21 @@ describe('authentication', function () { it('authenticates using credentials object (with artifacts)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { user: 'steve' } } }); - var doubleHandler = function (request, reply) { + const doubleHandler = function (request, reply) { - var options = { url: '/2', credentials: request.auth.credentials, artifacts: '!' }; + const options = { url: '/2', credentials: request.auth.credentials, artifacts: '!' }; server.inject(options, function (res) { return reply(res.result); }); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user + request.auth.artifacts); }; @@ -475,12 +475,12 @@ describe('authentication', function () { it('authenticates a request with custom auth settings', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -504,12 +504,12 @@ describe('authentication', function () { it('authenticates a request with auth strategy name config', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -531,12 +531,12 @@ describe('authentication', function () { it('tries to authenticate a request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ status: request.auth.isAuthenticated, error: request.auth.error }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', 'try', { users: { steve: {} } }); @@ -567,12 +567,12 @@ describe('authentication', function () { it('errors on invalid authenticate callback missing both error and credentials', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -587,12 +587,12 @@ describe('authentication', function () { it('logs error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -613,12 +613,12 @@ describe('authentication', function () { it('returns a non Error error response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { message: 'in a bottle' } }); @@ -634,7 +634,7 @@ describe('authentication', function () { it('handles errors thrown inside authenticate', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: 'throw' } }); @@ -644,7 +644,7 @@ describe('authentication', function () { expect(err.message).to.equal('Uncaught error: Boom'); }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; @@ -660,12 +660,12 @@ describe('authentication', function () { it('passes non Error error response when set to try ', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', 'try', { users: { message: 'in a bottle' } }); @@ -681,12 +681,12 @@ describe('authentication', function () { it('matches scope (array to single)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: ['one'] } } }); @@ -710,12 +710,12 @@ describe('authentication', function () { it('matches scope (array to array)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: ['one', 'two'] } } }); @@ -739,12 +739,12 @@ describe('authentication', function () { it('matches scope (single to array)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one' } } }); @@ -768,12 +768,12 @@ describe('authentication', function () { it('matches scope (single to single)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one' } } }); @@ -797,7 +797,7 @@ describe('authentication', function () { it('matches dynamic scope (single to single)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one-test' } } }); @@ -824,7 +824,7 @@ describe('authentication', function () { it('matches dynamic scope with multiple parts (single to single)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one-test-admin' } } }); @@ -851,7 +851,7 @@ describe('authentication', function () { it('does not match broken dynamic scope (single to single)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one-test' } } }); @@ -878,12 +878,12 @@ describe('authentication', function () { it('does not match scope (single to single)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: 'one' } } }); @@ -907,12 +907,12 @@ describe('authentication', function () { it('errors on missing scope', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: ['a'] } } }); @@ -936,12 +936,12 @@ describe('authentication', function () { it('errors on missing scope property', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -965,12 +965,12 @@ describe('authentication', function () { it('errors on missing scope using arrays', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { scope: ['a', 'b'] } } }); @@ -994,7 +994,7 @@ describe('authentication', function () { it('ignores default scope when override set to null', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); @@ -1026,7 +1026,7 @@ describe('authentication', function () { it('matches user entity', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { user: 'steve' } } }); @@ -1053,7 +1053,7 @@ describe('authentication', function () { it('errors on missing user entity', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { client: {} } }); @@ -1080,7 +1080,7 @@ describe('authentication', function () { it('matches app entity', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { client: {} } }); @@ -1107,7 +1107,7 @@ describe('authentication', function () { it('errors on missing app entity', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { user: 'steve' } } }); @@ -1134,7 +1134,7 @@ describe('authentication', function () { it('logs error code when authenticate returns a non-error error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('test', function (srv, options) { @@ -1174,7 +1174,7 @@ describe('authentication', function () { it('passes the options.artifacts object, even with an auth filter', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); @@ -1190,7 +1190,7 @@ describe('authentication', function () { } }); - var options = { + const options = { url: '/', headers: { authorization: 'Custom steve' }, credentials: { foo: 'bar' }, @@ -1214,7 +1214,7 @@ describe('authentication', function () { it('authenticates request payload', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } } }); @@ -1241,7 +1241,7 @@ describe('authentication', function () { it('skips when scheme does not support it', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } }, payload: false }); @@ -1265,7 +1265,7 @@ describe('authentication', function () { it('authenticates request payload (required scheme)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } }, options: { payload: true } }); @@ -1290,7 +1290,7 @@ describe('authentication', function () { it('authenticates request payload (required scheme and required route)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } }, options: { payload: true } }); @@ -1317,7 +1317,7 @@ describe('authentication', function () { it('throws when scheme requires payload authentication and route conflicts', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } }, options: { payload: true } }); @@ -1342,9 +1342,9 @@ describe('authentication', function () { it('throws when strategy does not support payload authentication', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var implementation = function () { + const implementation = function () { return { authenticate: internals.implementation().authenticate }; }; @@ -1372,9 +1372,9 @@ describe('authentication', function () { it('throws when no strategy supports optional payload authentication', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var implementation = function () { + const implementation = function () { return { authenticate: internals.implementation().authenticate }; }; @@ -1402,9 +1402,9 @@ describe('authentication', function () { it('allows one strategy to supports optional payload authentication while another does not', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var implementation = function () { + const implementation = function () { return { authenticate: internals.implementation().authenticate }; }; @@ -1435,7 +1435,7 @@ describe('authentication', function () { it('skips request payload by default', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { skip: {} } }); @@ -1459,7 +1459,7 @@ describe('authentication', function () { it('skips request payload when unauthenticated', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { skip: {} } }); @@ -1487,7 +1487,7 @@ describe('authentication', function () { it('skips optional payload', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { optionalPayload: { payload: Boom.unauthorized(null, 'Custom') } } }); @@ -1514,7 +1514,7 @@ describe('authentication', function () { it('errors on missing payload when required', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { optionalPayload: { payload: Boom.unauthorized(null, 'Custom') } } }); @@ -1541,7 +1541,7 @@ describe('authentication', function () { it('errors on invalid payload auth when required', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { optionalPayload: { payload: Boom.unauthorized() } } }); @@ -1568,7 +1568,7 @@ describe('authentication', function () { it('errors on invalid request payload (non error)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { invalidPayload: { payload: 'Payload is invalid' } } }); @@ -1599,12 +1599,12 @@ describe('authentication', function () { it('fails on response error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.credentials.user); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: { response: Boom.internal() } } }); @@ -1622,7 +1622,7 @@ describe('authentication', function () { it('tests a request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.server.auth.test('default', request, function (err, credentials) { @@ -1634,7 +1634,7 @@ describe('authentication', function () { }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: { name: 'steve' } } }); @@ -1660,7 +1660,7 @@ describe('authentication', function () { internals.implementation = function (server, options) { - var settings = Hoek.clone(options); + const settings = Hoek.clone(options); if (settings && settings.route) { @@ -1675,22 +1675,22 @@ internals.implementation = function (server, options) { }); } - var scheme = { + const scheme = { authenticate: function (request, reply) { - var req = request.raw.req; - var authorization = req.headers.authorization; + const req = request.raw.req; + const authorization = req.headers.authorization; if (!authorization) { return reply(Boom.unauthorized(null, 'Custom')); } - var parts = authorization.split(/\s+/); + const parts = authorization.split(/\s+/); if (parts.length !== 2) { return reply.continue(); // Error without error or credentials } - var username = parts[1]; - var credentials = settings.users[username]; + const username = parts[1]; + const credentials = settings.users[username]; if (!credentials) { return reply(Boom.unauthorized('Missing credentials', 'Custom')); diff --git a/test/connection.js b/test/connection.js index 844a44c1e..6bb2ad572 100755 --- a/test/connection.js +++ b/test/connection.js @@ -2,42 +2,42 @@ // Load modules -var ChildProcess = require('child_process'); -var Fs = require('fs'); -var Http = require('http'); -var Https = require('https'); -var Net = require('net'); -var Os = require('os'); -var Path = require('path'); -var Boom = require('boom'); -var Code = require('code'); -var Handlebars = require('handlebars'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Lab = require('lab'); -var Vision = require('vision'); -var Wreck = require('wreck'); +const ChildProcess = require('child_process'); +const Fs = require('fs'); +const Http = require('http'); +const Https = require('https'); +const Net = require('net'); +const Os = require('os'); +const Path = require('path'); +const Boom = require('boom'); +const Code = require('code'); +const Handlebars = require('handlebars'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Lab = require('lab'); +const Vision = require('vision'); +const Wreck = require('wreck'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Connection', function () { it('allows null port and host', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ host: null, port: null }); @@ -47,7 +47,7 @@ describe('Connection', function () { it('removes duplicate labels', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a', 'b', 'a', 'c', 'b'] }); expect(server.connections[0].settings.labels).to.deep.equal(['a', 'b', 'c']); done(); @@ -55,7 +55,7 @@ describe('Connection', function () { it('throws when disabling autoListen and providing a port', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ port: 80, autoListen: false }); @@ -65,8 +65,8 @@ describe('Connection', function () { it('throws when disabling autoListen and providing special host', function (done) { - var server = new Hapi.Server(); - var port = Path.join(__dirname, 'hapi-server.socket'); + const server = new Hapi.Server(); + const port = Path.join(__dirname, 'hapi-server.socket'); expect(function () { server.connection({ port: port, autoListen: false }); @@ -76,7 +76,7 @@ describe('Connection', function () { it('defaults address to 0.0.0.0 or :: when no host is provided', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { @@ -94,7 +94,7 @@ describe('Connection', function () { it('uses address when present instead of host', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost' }); server.start(function (err) { @@ -107,7 +107,7 @@ describe('Connection', function () { it('uses uri when present instead of host and port', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost', uri: 'http://uri.example.com:8080' }); expect(server.info.uri).to.equal('http://uri.example.com:8080'); server.start(function (err) { @@ -122,7 +122,7 @@ describe('Connection', function () { it('throws on uri ending with /', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ uri: 'http://uri.example.com:8080/' }); @@ -132,8 +132,8 @@ describe('Connection', function () { it('creates a server listening on a unix domain socket', { skip: process.platform === 'win32' }, function (done) { - var port = Path.join(__dirname, 'hapi-server.socket'); - var server = new Hapi.Server(); + const port = Path.join(__dirname, 'hapi-server.socket'); + const server = new Hapi.Server(); server.connection({ port: port }); expect(server.connections[0].type).to.equal('socket'); @@ -141,7 +141,7 @@ describe('Connection', function () { server.start(function (err) { expect(err).to.not.exist(); - var absSocketPath = Path.resolve(port); + const absSocketPath = Path.resolve(port); expect(server.info.port).to.equal(absSocketPath); server.stop(function (err) { @@ -157,8 +157,8 @@ describe('Connection', function () { it('creates a server listening on a windows named pipe', function (done) { - var port = '\\\\.\\pipe\\6653e55f-26ec-4268-a4f2-882f4089315c'; - var server = new Hapi.Server(); + const port = '\\\\.\\pipe\\6653e55f-26ec-4268-a4f2-882f4089315c'; + const server = new Hapi.Server(); server.connection({ port: port }); expect(server.connections[0].type).to.equal('socket'); @@ -172,12 +172,12 @@ describe('Connection', function () { it('creates an https server when passed tls options', function (done) { - var tlsOptions = { + const tlsOptions = { key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE\nSC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa\nFvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv\nw00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia\nPQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3\nm1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX\n1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7\n/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans\npCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee\nuctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs\nV/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==\n-----END RSA PRIVATE KEY-----\n', cert: '-----BEGIN CERTIFICATE-----\nMIIDBjCCAe4CCQDvLNml6smHlTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJV\nUzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTE0MDEyNTIxMjIxOFoXDTE1MDEyNTIxMjIxOFowRTELMAkG\nA1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nANFKslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS\nlrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV\nkltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ tls: tlsOptions }); expect(server.listener instanceof Https.Server).to.equal(true); done(); @@ -185,13 +185,13 @@ describe('Connection', function () { it('uses a provided listener', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var listener = Http.createServer(); - var server = new Hapi.Server(); + const listener = Http.createServer(); + const server = new Hapi.Server(); server.connection({ listener: listener }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -209,13 +209,13 @@ describe('Connection', function () { it('uses a provided listener (TLS)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var listener = Http.createServer(); - var server = new Hapi.Server(); + const listener = Http.createServer(); + const server = new Hapi.Server(); server.connection({ listener: listener, tls: true }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -229,13 +229,13 @@ describe('Connection', function () { it('uses a provided listener with manual listen', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var listener = Http.createServer(); - var server = new Hapi.Server(); + const listener = Http.createServer(); + const server = new Hapi.Server(); server.connection({ listener: listener, autoListen: false }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -256,14 +256,14 @@ describe('Connection', function () { it('sets info.uri with default localhost when no hostname', { parallel: false }, function (done) { - var orig = Os.hostname; + const orig = Os.hostname; Os.hostname = function () { Os.hostname = orig; return ''; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ port: 80 }); expect(server.info.uri).to.equal('http://localhost:80'); done(); @@ -271,7 +271,7 @@ describe('Connection', function () { it('sets info.uri without port when 0', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: 'example.com' }); expect(server.info.uri).to.equal('http://example.com'); done(); @@ -279,7 +279,7 @@ describe('Connection', function () { it('closes connection on socket timeout', { parallel: false }, function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { socket: 50 }, payload: { timeout: 45 } } }); server.route({ method: 'GET', path: '/', config: { @@ -307,12 +307,12 @@ describe('Connection', function () { it('disables node socket timeout', { parallel: false }, function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { socket: false } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -321,7 +321,7 @@ describe('Connection', function () { expect(err).to.not.exist(); var timeout; - var orig = Net.Socket.prototype.setTimeout; + const orig = Net.Socket.prototype.setTimeout; Net.Socket.prototype.setTimeout = function () { timeout = 'gotcha'; @@ -345,7 +345,7 @@ describe('Connection', function () { it('starts connection', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { @@ -364,12 +364,12 @@ describe('Connection', function () { it('starts connection (tls)', function (done) { - var tlsOptions = { + const tlsOptions = { key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE\nSC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa\nFvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv\nw00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia\nPQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3\nm1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX\n1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7\n/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans\npCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee\nuctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs\nV/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==\n-----END RSA PRIVATE KEY-----\n', cert: '-----BEGIN CERTIFICATE-----\nMIIDBjCCAe4CCQDvLNml6smHlTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJV\nUzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTE0MDEyNTIxMjIxOFoXDTE1MDEyNTIxMjIxOFowRTELMAkG\nA1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nANFKslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS\nlrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV\nkltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: '0.0.0.0', port: 0, tls: tlsOptions }); server.start(function (err) { @@ -382,14 +382,14 @@ describe('Connection', function () { it('sets info with defaults when missing hostname and address', { parallel: false }, function (done) { - var hostname = Os.hostname; + const hostname = Os.hostname; Os.hostname = function () { Os.hostname = hostname; return ''; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ port: '8000' }); expect(server.info.host).to.equal('localhost'); expect(server.info.uri).to.equal('http://localhost:8000'); @@ -398,7 +398,7 @@ describe('Connection', function () { it('ignored repeated calls', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { @@ -417,7 +417,7 @@ describe('Connection', function () { it('will return an error if the port is already in use', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { @@ -438,13 +438,13 @@ describe('Connection', function () { it('waits to stop until all connections are closed', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { expect(err).to.not.exist(); - var socket1 = new Net.Socket(); - var socket2 = new Net.Socket(); + const socket1 = new Net.Socket(); + const socket2 = new Net.Socket(); socket1.on('error', function () { }); socket2.on('error', function () { }); @@ -477,14 +477,14 @@ describe('Connection', function () { it('waits to destroy connections until after the timeout', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { expect(err).to.not.exist(); - var socket1 = new Net.Socket(); - var socket2 = new Net.Socket(); + const socket1 = new Net.Socket(); + const socket2 = new Net.Socket(); socket1.once('error', function (err) { @@ -503,7 +503,7 @@ describe('Connection', function () { server.listener.getConnections(function (err, count) { expect(count).to.be.greaterThan(0); - var timer = new Hoek.Bench(); + const timer = new Hoek.Bench(); server.stop({ timeout: 20 }, function (err) { @@ -519,14 +519,14 @@ describe('Connection', function () { it('waits to destroy connections if they close by themselves', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { expect(err).to.not.exist(); - var socket1 = new Net.Socket(); - var socket2 = new Net.Socket(); + const socket1 = new Net.Socket(); + const socket2 = new Net.Socket(); socket1.once('error', function (err) { @@ -545,7 +545,7 @@ describe('Connection', function () { server.listener.getConnections(function (err, count1) { expect(count1).to.be.greaterThan(0); - var timer = new Hoek.Bench(); + const timer = new Hoek.Bench(); server.stop(function (err) { @@ -572,19 +572,19 @@ describe('Connection', function () { it('refuses to handle new incoming requests', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.start(function (err) { expect(err).to.not.exist(); - var agent = new Http.Agent({ keepAlive: true, maxSockets: 1 }); + const agent = new Http.Agent({ keepAlive: true, maxSockets: 1 }); var err2; Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, function (err1, res, body) { @@ -609,9 +609,9 @@ describe('Connection', function () { it('removes connection event listeners after it stops', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var initial = server.listener.listeners('connection').length; + const initial = server.listener.listeners('connection').length; server.start(function (err) { expect(err).to.not.exist(); @@ -639,7 +639,7 @@ describe('Connection', function () { it('ignores repeated calls', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.stop(function (err) { @@ -652,12 +652,12 @@ describe('Connection', function () { it('rejects request due to high rss load', { parallel: false }, function (done) { - var server = new Hapi.Server({ load: { sampleInterval: 5 } }); + const server = new Hapi.Server({ load: { sampleInterval: 5 } }); server.connection({ load: { maxRssBytes: 1 } }); - var handler = function (request, reply) { + const handler = function (request, reply) { - var start = Date.now(); + const start = Date.now(); while (Date.now() - start < 10) { } return reply('ok'); }; @@ -695,16 +695,16 @@ describe('Connection', function () { it('keeps the options.credentials object untouched', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - var options = { + const options = { url: '/', credentials: { foo: 'bar' } }; @@ -719,16 +719,16 @@ describe('Connection', function () { it('sets credentials (with host header)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - var options = { + const options = { url: '/', credentials: { foo: 'bar' }, headers: { @@ -746,16 +746,16 @@ describe('Connection', function () { it('sets credentials (with authority)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.headers.host); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - var options = { + const options = { url: '/', credentials: { foo: 'bar' }, authority: 'something' @@ -772,16 +772,16 @@ describe('Connection', function () { it('sets authority', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.headers.host); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - var options = { + const options = { url: '/', authority: 'something' }; @@ -796,16 +796,16 @@ describe('Connection', function () { it('passes the options.artifacts object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.auth.artifacts); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - var options = { + const options = { url: '/', credentials: { foo: 'bar' }, artifacts: { bar: 'baz' } @@ -822,13 +822,13 @@ describe('Connection', function () { it('returns the request object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.app.key = 'value'; return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -842,12 +842,12 @@ describe('Connection', function () { it('can set a client remoteAddress', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.info.remoteAddress); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -861,12 +861,12 @@ describe('Connection', function () { it('sets a default remoteAddress of 127.0.0.1', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.info.remoteAddress); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -880,7 +880,7 @@ describe('Connection', function () { it('sets correct host header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: 'example.com', port: 2080 }); server.route({ method: 'GET', @@ -903,13 +903,13 @@ describe('Connection', function () { it('returns an array of the current routes', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.table()[0].table; + const routes = server.table()[0].table; expect(routes.length).to.equal(2); expect(routes[0].path).to.equal('/test/'); @@ -918,13 +918,13 @@ describe('Connection', function () { it('returns the labels for the connections', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['test'] }); server.route({ path: '/test/', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var connection = server.table()[0]; + const connection = server.table()[0]; expect(connection.labels).to.only.include(['test']); done(); @@ -932,13 +932,13 @@ describe('Connection', function () { it('returns an array of the current routes (connection)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.connections[0].table(); + const routes = server.connections[0].table(); expect(routes.length).to.equal(2); expect(routes[0].path).to.equal('/test/'); @@ -947,7 +947,7 @@ describe('Connection', function () { it('combines global and vhost routes', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); @@ -955,7 +955,7 @@ describe('Connection', function () { server.route({ path: '/test/', vhost: 'two.example.com', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.table()[0].table; + const routes = server.table()[0].table; expect(routes.length).to.equal(4); done(); @@ -963,7 +963,7 @@ describe('Connection', function () { it('combines global and vhost routes and filters based on host', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); @@ -971,7 +971,7 @@ describe('Connection', function () { server.route({ path: '/test/', vhost: 'two.example.com', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.table('one.example.com')[0].table; + const routes = server.table('one.example.com')[0].table; expect(routes.length).to.equal(3); done(); @@ -979,7 +979,7 @@ describe('Connection', function () { it('accepts a list of hosts', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); @@ -987,7 +987,7 @@ describe('Connection', function () { server.route({ path: '/test/', vhost: 'two.example.com', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.table(['one.example.com', 'two.example.com'])[0].table; + const routes = server.table(['one.example.com', 'two.example.com'])[0].table; expect(routes.length).to.equal(4); done(); @@ -995,7 +995,7 @@ describe('Connection', function () { it('ignores unknown host', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test/', method: 'get', handler: function () { } }); @@ -1003,7 +1003,7 @@ describe('Connection', function () { server.route({ path: '/test/', vhost: 'two.example.com', method: 'get', handler: function () { } }); server.route({ path: '/test/{p}/end', method: 'get', handler: function () { } }); - var routes = server.table('three.example.com')[0].table; + const routes = server.table('three.example.com')[0].table; expect(routes.length).to.equal(2); done(); @@ -1014,7 +1014,7 @@ describe('Connection', function () { it('supports adding an array of methods', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreHandler', [ function (request, reply) { @@ -1029,7 +1029,7 @@ describe('Connection', function () { } ]); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.app.x); }; @@ -1045,7 +1045,7 @@ describe('Connection', function () { it('sets bind via options', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreHandler', function (request, reply) { @@ -1053,7 +1053,7 @@ describe('Connection', function () { return reply.continue(); }, { bind: { y: 42 } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.app.x); }; @@ -1069,7 +1069,7 @@ describe('Connection', function () { it('uses server views for ext added via server', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); server.connection(); @@ -1083,7 +1083,7 @@ describe('Connection', function () { return reply.view('test'); }); - var test = function (plugin, options, next) { + const test = function (plugin, options, next) { plugin.views({ engines: { html: Handlebars }, @@ -1110,7 +1110,7 @@ describe('Connection', function () { it('supports reply decorators on empty result', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -1127,7 +1127,7 @@ describe('Connection', function () { it('supports direct reply decorators', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -1146,7 +1146,7 @@ describe('Connection', function () { it('replies with custom response', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -1163,7 +1163,7 @@ describe('Connection', function () { it('replies with error using reply(null, result)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -1171,7 +1171,7 @@ describe('Connection', function () { }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; @@ -1187,7 +1187,7 @@ describe('Connection', function () { it('replies with a view', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); server.connection(); @@ -1201,7 +1201,7 @@ describe('Connection', function () { return reply.view('test', { message: 'hola!' }); }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; @@ -1220,7 +1220,7 @@ describe('Connection', function () { it('replies with custom response', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreResponse', function (request, reply) { @@ -1262,7 +1262,7 @@ describe('Connection', function () { it('intercepts 404 responses', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreResponse', function (request, reply) { @@ -1279,13 +1279,13 @@ describe('Connection', function () { it('intercepts 404 when using directory handler and file is missing', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.ext('onPreResponse', function (request, reply) { - var response = request.response; + const response = request.response; return reply({ isBoom: response.isBoom }); }); @@ -1301,13 +1301,13 @@ describe('Connection', function () { it('intercepts 404 when using file handler and file is missing', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.ext('onPreResponse', function (request, reply) { - var response = request.response; + const response = request.response; return reply({ isBoom: response.isBoom }); }); @@ -1323,7 +1323,7 @@ describe('Connection', function () { it('cleans unused file stream when response is overridden', { skip: process.platform === 'win32' }, function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); @@ -1339,7 +1339,7 @@ describe('Connection', function () { expect(res.statusCode).to.equal(200); expect(res.result.something).to.equal('else'); - var cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); + const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); var lsof = ''; cmd.stdout.on('data', function (buffer) { @@ -1349,7 +1349,7 @@ describe('Connection', function () { cmd.stdout.on('end', function () { var count = 0; - var lines = lsof.split('\n'); + const lines = lsof.split('\n'); for (var i = 0, il = lines.length; i < il; ++i) { count += !!lines[i].match(/package.json/); } @@ -1364,7 +1364,7 @@ describe('Connection', function () { it('executes multiple extensions', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreResponse', function (request, reply) { @@ -1378,7 +1378,7 @@ describe('Connection', function () { return reply.continue(); }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('0'); }; @@ -1398,7 +1398,7 @@ describe('Connection', function () { it('emits route event', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'a' }); server.on('route', function (route, connection, srv) { @@ -1420,12 +1420,12 @@ describe('Connection', function () { it('overrides the default notFound handler', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('found'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: '*', path: '/{p*}', handler: handler }); server.inject({ method: 'GET', url: '/page' }, function (res) { @@ -1438,12 +1438,12 @@ describe('Connection', function () { it('responds to HEAD requests for a GET route', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').etag('test').code(205); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ method: 'HEAD', url: '/' }, function (res) { @@ -1459,12 +1459,12 @@ describe('Connection', function () { it('returns 404 on HEAD requests for non-GET routes', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); server.inject({ method: 'HEAD', url: '/' }, function (res1) { @@ -1483,15 +1483,15 @@ describe('Connection', function () { it('allows methods array', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.route.method); }; - var config = { method: ['GET', 'PUT', 'POST', 'DELETE'], path: '/', handler: handler }; + const config = { method: ['GET', 'PUT', 'POST', 'DELETE'], path: '/', handler: handler }; server.route(config); server.inject({ method: 'HEAD', url: '/' }, function (res1) { @@ -1527,12 +1527,12 @@ describe('Connection', function () { it('adds routes using single and array methods', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route([ { @@ -1562,10 +1562,10 @@ describe('Connection', function () { } ]); - var table = server.table()[0].table; - var paths = table.map(function (route) { + const table = server.table()[0].table; + const paths = table.map(function (route) { - var obj = { + const obj = { method: route.method, path: route.path }; @@ -1586,7 +1586,7 @@ describe('Connection', function () { it('throws on methods array with id', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1611,7 +1611,7 @@ describe('Connection', function () { it('returns 404 when making a request to a route that does not exist', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.inject({ method: 'GET', url: '/nope' }, function (res) { @@ -1622,12 +1622,12 @@ describe('Connection', function () { it('returns 400 on bad request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/a/{p}', handler: handler }); server.inject('/a/%', function (res) { diff --git a/test/cors.js b/test/cors.js index 214ff5f4b..8eb9ec3e5 100755 --- a/test/cors.js +++ b/test/cors.js @@ -2,35 +2,35 @@ // Load modules -var Boom = require('boom'); -var Code = require('code'); -var Hapi = require('..'); -var Lab = require('lab'); +const Boom = require('boom'); +const Code = require('code'); +const Hapi = require('..'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('CORS', function () { it('returns 404 on OPTIONS when cors disabled', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: false } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -43,12 +43,12 @@ describe('CORS', function () { it('returns OPTIONS response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -61,12 +61,12 @@ describe('CORS', function () { it('returns OPTIONS response (server config)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest()); }; - var server = new Hapi.Server({ connections: { routes: { cors: true } } }); + const server = new Hapi.Server({ connections: { routes: { cors: true } } }); server.connection(); server.route({ method: 'GET', path: '/x', handler: handler }); @@ -79,12 +79,12 @@ describe('CORS', function () { it('returns headers on single route', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/b', handler: handler }); @@ -107,12 +107,12 @@ describe('CORS', function () { it('allows headers on multiple routes but not all', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); @@ -143,12 +143,12 @@ describe('CORS', function () { it('allows same headers on multiple routes with same path', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); @@ -164,12 +164,12 @@ describe('CORS', function () { it('returns headers on single route (overrides defaults)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['b'] } } }); server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); server.route({ method: 'GET', path: '/b', handler: handler }); @@ -192,12 +192,12 @@ describe('CORS', function () { it('sets access-control-allow-credentials header', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { credentials: true } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -213,12 +213,12 @@ describe('CORS', function () { it('returns CORS origin (route level)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); @@ -239,12 +239,12 @@ describe('CORS', function () { it('returns CORS origin (GET)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://x.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -259,12 +259,12 @@ describe('CORS', function () { it('returns CORS origin (OPTIONS)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -279,12 +279,12 @@ describe('CORS', function () { it('merges CORS access-control-expose-headers header', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').header('access-control-expose-headers', 'something'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { additionalExposedHeaders: ['xyz'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -299,12 +299,12 @@ describe('CORS', function () { it('returns no CORS headers when route CORS disabled', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: false } }); @@ -319,12 +319,12 @@ describe('CORS', function () { it('returns matching CORS origin', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Tada').header('vary', 'x-test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -340,12 +340,12 @@ describe('CORS', function () { it('returns origin header when matching against *', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Tada').header('vary', 'x-test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['*'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -361,12 +361,12 @@ describe('CORS', function () { it('returns matching CORS origin wildcard', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Tada').header('vary', 'x-test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -382,12 +382,12 @@ describe('CORS', function () { it('returns matching CORS origin wildcard when more than one wildcard', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Tada').header('vary', 'x-test', true); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.b.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -403,12 +403,12 @@ describe('CORS', function () { it('does not set empty CORS expose headers', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { exposedHeaders: [] } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -431,7 +431,7 @@ describe('CORS', function () { it('ignores OPTIONS route', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'OPTIONS', @@ -448,7 +448,7 @@ describe('CORS', function () { it('errors on missing origin header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', @@ -466,7 +466,7 @@ describe('CORS', function () { it('errors on missing access-control-request-method header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', @@ -484,7 +484,7 @@ describe('CORS', function () { it('errors on missing route', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { @@ -496,7 +496,7 @@ describe('CORS', function () { it('errors on mismatching origin header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['a'] } } }); server.route({ method: 'GET', @@ -514,12 +514,12 @@ describe('CORS', function () { it('matches allowed headers', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -541,12 +541,12 @@ describe('CORS', function () { it('matches allowed headers (case insensitive', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -568,12 +568,12 @@ describe('CORS', function () { it('errors on disallowed headers', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -595,7 +595,7 @@ describe('CORS', function () { it('allows credentials', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: { credentials: true } } }); server.route({ method: 'GET', @@ -616,7 +616,7 @@ describe('CORS', function () { it('skips CORS when missing origin header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); server.route({ method: 'GET', diff --git a/test/handler.js b/test/handler.js index c315be580..1ebeb69c7 100755 --- a/test/handler.js +++ b/test/handler.js @@ -2,28 +2,28 @@ // Load modules -var Path = require('path'); -var Boom = require('boom'); -var Code = require('code'); -var Handlebars = require('handlebars'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Lab = require('lab'); -var Vision = require('vision'); +const Path = require('path'); +const Boom = require('boom'); +const Code = require('code'); +const Handlebars = require('handlebars'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Lab = require('lab'); +const Vision = require('vision'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('handler', function () { @@ -32,10 +32,10 @@ describe('handler', function () { it('returns 500 on handler exception (same tick)', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); - var handler = function (request) { + const handler = function (request) { a.b.c; }; @@ -51,7 +51,7 @@ describe('handler', function () { it('returns 500 on handler exception (next tick)', { parallel: false }, function (done) { - var handler = function (request) { + const handler = function (request) { setImmediate(function () { @@ -59,7 +59,7 @@ describe('handler', function () { }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.on('request-error', function (request, err) { @@ -68,7 +68,7 @@ describe('handler', function () { done(); }); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -87,9 +87,9 @@ describe('handler', function () { it('binds handler to route bind object', function (done) { - var item = { x: 123 }; + const item = { x: 123 }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -112,10 +112,10 @@ describe('handler', function () { it('invokes handler with right arguments', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { expect(arguments.length).to.equal(2); expect(reply.send).to.not.exist(); @@ -136,10 +136,10 @@ describe('handler', function () { it('returns a file', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file('../package.json').code(499); }; @@ -159,7 +159,7 @@ describe('handler', function () { it('returns a view', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); server.connection(); @@ -168,7 +168,7 @@ describe('handler', function () { relativeTo: Path.join(__dirname, '/templates/plugin') }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.view('test', { message: 'steve' }); }; @@ -187,17 +187,17 @@ describe('handler', function () { it('shows the complete prerequisite pipeline in the response', function (done) { - var pre1 = function (request, reply) { + const pre1 = function (request, reply) { return reply('Hello').code(444); }; - var pre2 = function (request, reply) { + const pre2 = function (request, reply) { return reply(request.pre.m1 + request.pre.m3 + request.pre.m4); }; - var pre3 = function (request, reply) { + const pre3 = function (request, reply) { process.nextTick(function () { @@ -205,22 +205,22 @@ describe('handler', function () { }); }; - var pre4 = function (request, reply) { + const pre4 = function (request, reply) { return reply('World'); }; - var pre5 = function (request, reply) { + const pre5 = function (request, reply) { return reply(request.pre.m2 + '!'); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.m5); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -248,17 +248,17 @@ describe('handler', function () { it('allows a single prerequisite', function (done) { - var pre = function (request, reply) { + const pre = function (request, reply) { return reply('Hello'); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.p); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ @@ -281,12 +281,12 @@ describe('handler', function () { it('allows an empty prerequisite array', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Hello'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ @@ -307,17 +307,17 @@ describe('handler', function () { it('takes over response', function (done) { - var pre1 = function (request, reply) { + const pre1 = function (request, reply) { return reply('Hello'); }; - var pre2 = function (request, reply) { + const pre2 = function (request, reply) { return reply(request.pre.m1 + request.pre.m3 + request.pre.m4); }; - var pre3 = function (request, reply) { + const pre3 = function (request, reply) { process.nextTick(function () { @@ -325,22 +325,22 @@ describe('handler', function () { }); }; - var pre4 = function (request, reply) { + const pre4 = function (request, reply) { return reply('World'); }; - var pre5 = function (request, reply) { + const pre5 = function (request, reply) { return reply(request.pre.m2 + '!'); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.m5); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -368,22 +368,22 @@ describe('handler', function () { it('returns error if prerequisite returns error', function (done) { - var pre1 = function (request, reply) { + const pre1 = function (request, reply) { return reply('Hello'); }; - var pre2 = function (request, reply) { + const pre2 = function (request, reply) { return reply(Boom.internal('boom')); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.m1); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -406,17 +406,17 @@ describe('handler', function () { it('passes wrapped object', function (done) { - var pre = function (request, reply) { + const pre = function (request, reply) { return reply('Hello').code(444); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.preResponses.p); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -438,23 +438,23 @@ describe('handler', function () { it('returns 500 if prerequisite throws', function (done) { - var pre1 = function (request, reply) { + const pre1 = function (request, reply) { return reply('Hello'); }; - var pre2 = function (request, reply) { + const pre2 = function (request, reply) { a.b.c = 0; }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.m1); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -477,7 +477,7 @@ describe('handler', function () { it('returns a user record using server method', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -508,7 +508,7 @@ describe('handler', function () { it('returns a user record using server method (nested method name)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user.get', function (id, next) { @@ -539,7 +539,7 @@ describe('handler', function () { it('returns a user record using server method in object', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -573,7 +573,7 @@ describe('handler', function () { it('returns a user name using multiple server methods', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -610,7 +610,7 @@ describe('handler', function () { it('returns a user record using server method with trailing space', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -641,7 +641,7 @@ describe('handler', function () { it('returns a user record using server method with leading space', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -672,7 +672,7 @@ describe('handler', function () { it('returns a user record using server method with zero args', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (next) { @@ -703,7 +703,7 @@ describe('handler', function () { it('returns a user record using server method with no args', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (request, next) { @@ -734,7 +734,7 @@ describe('handler', function () { it('returns a user record using server method with nested name', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user.get', function (next) { @@ -765,9 +765,9 @@ describe('handler', function () { it('fails on bad method name', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function () { + const test = function () { server.route({ method: 'GET', @@ -790,9 +790,9 @@ describe('handler', function () { it('fails on bad method syntax name', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function () { + const test = function () { server.route({ method: 'GET', @@ -815,7 +815,7 @@ describe('handler', function () { it('sets pre failAction to error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -846,7 +846,7 @@ describe('handler', function () { it('sets pre failAction to ignore', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -877,7 +877,7 @@ describe('handler', function () { it('sets pre failAction to log', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -921,9 +921,9 @@ describe('handler', function () { it('binds pre to route bind object', function (done) { - var item = { x: 123 }; + const item = { x: 123 }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -952,7 +952,7 @@ describe('handler', function () { it('logs boom error instance as data if handler returns boom error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -988,7 +988,7 @@ describe('handler', function () { it('logs server method using string notation when cache enabled', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('user', function (id, next) { @@ -1026,7 +1026,7 @@ describe('handler', function () { it('uses server method with cache via string notation', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var gen = 0; @@ -1071,7 +1071,7 @@ describe('handler', function () { it('uses string handler', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('handler.get', function (request, reply) { @@ -1092,7 +1092,7 @@ describe('handler', function () { it('returns handler without defaults', function (done) { - var handler = function (route, options) { + const handler = function (route, options) { return function (request, reply) { @@ -1100,7 +1100,7 @@ describe('handler', function () { }; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); @@ -1113,7 +1113,7 @@ describe('handler', function () { it('returns handler with object defaults', function (done) { - var handler = function (route, options) { + const handler = function (route, options) { return function (request, reply) { @@ -1127,7 +1127,7 @@ describe('handler', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); @@ -1140,7 +1140,7 @@ describe('handler', function () { it('returns handler with function defaults', function (done) { - var handler = function (route, options) { + const handler = function (route, options) { return function (request, reply) { @@ -1157,7 +1157,7 @@ describe('handler', function () { }; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); @@ -1170,7 +1170,7 @@ describe('handler', function () { it('throws on handler with invalid defaults', function (done) { - var handler = function (route, options) { + const handler = function (route, options) { return function (request, reply) { @@ -1180,7 +1180,7 @@ describe('handler', function () { handler.defaults = 'invalid'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1195,14 +1195,14 @@ describe('handler', function () { it('returns 500 on ext method exception (same tick)', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.ext('onRequest', function (request, next) { a.b.c; }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('neven gonna happen'); }; diff --git a/test/methods.js b/test/methods.js index 565b6fcc0..5df1718d4 100755 --- a/test/methods.js +++ b/test/methods.js @@ -2,36 +2,36 @@ // Load modules -var Bluebird = require('bluebird'); -var CatboxMemory = require('catbox-memory'); -var Code = require('code'); -var Hapi = require('..'); -var Lab = require('lab'); +const Bluebird = require('bluebird'); +const CatboxMemory = require('catbox-memory'); +const Code = require('code'); +const Hapi = require('..'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Methods', function () { it('registers a method', function (done) { - var add = function (a, b, next) { + const add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add); server.methods.add(1, 5, function (err, result) { @@ -43,12 +43,12 @@ describe('Methods', function () { it('registers a method with leading _', function (done) { - var _add = function (a, b, next) { + const _add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('_add', _add); server.methods._add(1, 5, function (err, result) { @@ -60,12 +60,12 @@ describe('Methods', function () { it('registers a method with leading $', function (done) { - var $add = function (a, b, next) { + const $add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('$add', $add); server.methods.$add(1, 5, function (err, result) { @@ -77,12 +77,12 @@ describe('Methods', function () { it('registers a method with _', function (done) { - var _add = function (a, b, next) { + const _add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add_._that', _add); server.methods.add_._that(1, 5, function (err, result) { @@ -94,12 +94,12 @@ describe('Methods', function () { it('registers a method with $', function (done) { - var $add = function (a, b, next) { + const $add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add$.$that', $add); server.methods.add$.$that(1, 5, function (err, result) { @@ -111,12 +111,12 @@ describe('Methods', function () { it('registers a method (no callback)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return a + b; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); expect(server.methods.add(1, 5)).to.equal(6); @@ -125,14 +125,14 @@ describe('Methods', function () { it('registers a method (promise)', function (done) { - var addAsync = function (a, b, next) { + const addAsync = function (a, b, next) { return next(null, a + b); }; - var add = Bluebird.promisify(addAsync); + const add = Bluebird.promisify(addAsync); - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); server.methods.add(1, 5).then(function (result) { @@ -144,12 +144,12 @@ describe('Methods', function () { it('registers a method with nested name', function (done) { - var add = function (a, b, next) { + const add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('tools.add', add); @@ -167,10 +167,10 @@ describe('Methods', function () { it('registers a method with bind and callback', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var context = { name: 'Bob' }; + const context = { name: 'Bob' }; server.method('user', function (id, next) { return next(null, { id: id, name: this.name }); @@ -199,17 +199,17 @@ describe('Methods', function () { it('registers two methods with shared nested name', function (done) { - var add = function (a, b, next) { + const add = function (a, b, next) { return next(null, a + b); }; - var sub = function (a, b, next) { + const sub = function (a, b, next) { return next(null, a - b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('tools.add', add); server.method('tools.sub', sub); @@ -232,12 +232,12 @@ describe('Methods', function () { it('throws when registering a method with nested name twice', function (done) { - var add = function (a, b, next) { + const add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('tools.add', add); expect(function () { @@ -249,12 +249,12 @@ describe('Methods', function () { it('throws when registering a method with name nested through a function', function (done) { - var add = function (a, b, next) { + const add = function (a, b, next) { return next(null, a + b); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add); expect(function () { @@ -267,12 +267,12 @@ describe('Methods', function () { it('calls non cached method multiple times', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method); @@ -296,12 +296,12 @@ describe('Methods', function () { it('caches method value', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -327,12 +327,12 @@ describe('Methods', function () { it('caches method value (no callback)', function (done) { var gen = 0; - var method = function (id) { + const method = function (id) { return { id: id, gen: gen++ }; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, callback: false }); @@ -358,7 +358,7 @@ describe('Methods', function () { it('caches method value (promise)', function (done) { var gen = 0; - var methodAsync = function (id, next) { + const methodAsync = function (id, next) { if (id === 2) { return next(new Error('boom')); @@ -367,9 +367,9 @@ describe('Methods', function () { return next(null, { id: id, gen: gen++ }); }; - var method = Bluebird.promisify(methodAsync); + const method = Bluebird.promisify(methodAsync); - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, callback: false }); @@ -401,15 +401,15 @@ describe('Methods', function () { it('reuses cached method value with custom key function', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var generateKey = function (id) { + const generateKey = function (id) { return '' + (id + 1); }; @@ -435,15 +435,15 @@ describe('Methods', function () { it('errors when custom key function return null', function (done) { - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var generateKey = function (id) { + const generateKey = function (id) { return null; }; @@ -465,15 +465,15 @@ describe('Methods', function () { it('does not cache when custom key function returns a non-string', function (done) { - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var generateKey = function (id) { + const generateKey = function (id) { return 123; }; @@ -496,12 +496,12 @@ describe('Methods', function () { it('does not cache value when ttl is 0', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }, 0); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -525,12 +525,12 @@ describe('Methods', function () { it('generates new value after cache drop', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('dropTest', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -558,12 +558,12 @@ describe('Methods', function () { it('errors on invalid drop key', function (done) { var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('dropErrTest', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -581,12 +581,12 @@ describe('Methods', function () { it('reports cache stats for each method', function (done) { - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { cache: { generateTimeout: 10 } }); server.method('test2', method, { cache: { generateTimeout: 10 } }); @@ -609,7 +609,7 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method(0, function () { }); }).to.throw('name must be a string'); done(); @@ -619,25 +619,25 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('0', function () { }); }).to.throw('Invalid name: 0'); expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('a..', function () { }); }).to.throw('Invalid name: a..'); expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('a.0', function () { }); }).to.throw('Invalid name: a.0'); expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('.a', function () { }); }).to.throw('Invalid name: .a'); @@ -648,7 +648,7 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('user', 'function'); }).to.throw('method must be a function'); done(); @@ -658,7 +658,7 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('user', function () { }, 'options'); }).to.throw(/Invalid method options \(user\)/); done(); @@ -668,7 +668,7 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('user', function () { }, { generateKey: 'function' }); }).to.throw(/Invalid method options \(user\)/); done(); @@ -678,7 +678,7 @@ describe('Methods', function () { expect(function () { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.method('user', function () { }, { cache: { x: 'y', generateTimeout: 10 } }); }).to.throw(/Invalid cache policy configuration/); done(); @@ -686,7 +686,7 @@ describe('Methods', function () { it('throws an error when generateTimeout is not present', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.method('test', function () { }, { cache: {} }); @@ -697,7 +697,7 @@ describe('Methods', function () { it('allows generateTimeout to be false', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.method('test', function () { }, { cache: { generateTimeout: false } }); @@ -708,9 +708,9 @@ describe('Methods', function () { it('returns a valid result when calling a method without using the cache', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id }); }; @@ -725,13 +725,13 @@ describe('Methods', function () { it('returns a valid result when calling a method when using the cache', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.initialize(function (err) { expect(err).to.not.exist(); - var method = function (id, str, next) { + const method = function (id, str, next) { return next(null, { id: id, str: str }); }; @@ -748,9 +748,9 @@ describe('Methods', function () { it('returns an error result when calling a method that returns an error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); - var method = function (id, next) { + const method = function (id, next) { return next(new Error()); }; @@ -765,10 +765,10 @@ describe('Methods', function () { it('returns a different result when calling a method without using the cache', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: ++gen }); }; @@ -789,11 +789,11 @@ describe('Methods', function () { it('returns a valid result when calling a method using the cache', function (done) { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: ++gen }); }; @@ -804,7 +804,7 @@ describe('Methods', function () { expect(err).to.not.exist(); - var id = Math.random(); + const id = Math.random(); server.methods.user(id, function (err, result1) { expect(result1.id).to.equal(id); @@ -821,11 +821,11 @@ describe('Methods', function () { it('returns timeout when method taking too long using the cache', function (done) { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); var gen = 0; - var method = function (id, next) { + const method = function (id, next) { setTimeout(function () { @@ -839,7 +839,7 @@ describe('Methods', function () { expect(err).to.not.exist(); - var id = Math.random(); + const id = Math.random(); server.methods.user(id, function (err, result1) { expect(err.output.statusCode).to.equal(503); @@ -859,12 +859,12 @@ describe('Methods', function () { it('supports empty key method', function (done) { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); var gen = 0; - var terms = 'I agree to give my house'; - var method = function (next) { + const terms = 'I agree to give my house'; + const method = function (next) { return next(null, { gen: gen++, terms: terms }); }; @@ -891,10 +891,10 @@ describe('Methods', function () { it('returns valid results when calling a method (with different keys) using the cache', function (done) { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); var gen = 0; - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: ++gen }); }; @@ -904,12 +904,12 @@ describe('Methods', function () { expect(err).to.not.exist(); - var id1 = Math.random(); + const id1 = Math.random(); server.methods.user(id1, function (err, result1) { expect(result1.id).to.equal(id1); expect(result1.gen).to.equal(1); - var id2 = Math.random(); + const id2 = Math.random(); server.methods.user(id2, function (err, result2) { expect(result2.id).to.equal(id2); @@ -922,10 +922,10 @@ describe('Methods', function () { it('errors when key generation fails', function (done) { - var server = new Hapi.Server({ cache: CatboxMemory }); + const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id }); }; @@ -952,12 +952,12 @@ describe('Methods', function () { it('sets method bind without cache', function (done) { - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: this.gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { bind: { gen: 7 } }); @@ -980,12 +980,12 @@ describe('Methods', function () { it('sets method bind with cache', function (done) { - var method = function (id, next) { + const method = function (id, next) { return next(null, { id: id, gen: this.gen++ }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { bind: { gen: 7 }, cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -1008,13 +1008,13 @@ describe('Methods', function () { it('shallow copies bind config', function (done) { - var bind = { gen: 7 }; - var method = function (id, next) { + const bind = { gen: 7 }; + const method = function (id, next) { return next(null, { id: id, gen: this.gen++, bound: (this === bind) }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('test', method, { bind: bind, cache: { expiresIn: 1000, generateTimeout: 10 } }); @@ -1040,28 +1040,28 @@ describe('Methods', function () { it('normalizes no callback into callback (direct)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return a + b; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); - var result = server.methods.add(1, 5); + const result = server.methods.add(1, 5); expect(result).to.equal(6); done(); }); it('normalizes no callback into callback (direct error)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); - var result = server.methods.add(1, 5); + const result = server.methods.add(1, 5); expect(result).to.be.instanceof(Error); expect(result.message).to.equal('boom'); done(); @@ -1069,12 +1069,12 @@ describe('Methods', function () { it('normalizes no callback into callback (direct throw)', function (done) { - var add = function (a, b) { + const add = function (a, b) { throw new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); expect(function () { @@ -1085,12 +1085,12 @@ describe('Methods', function () { it('normalizes no callback into callback (normalized)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return a + b; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1102,12 +1102,12 @@ describe('Methods', function () { it('normalizes no callback into callback (normalized error)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1120,12 +1120,12 @@ describe('Methods', function () { it('normalizes no callback into callback (normalized throw)', function (done) { - var add = function (a, b) { + const add = function (a, b) { throw new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1139,12 +1139,12 @@ describe('Methods', function () { it('normalizes no callback into callback (cached)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return a + b; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1156,12 +1156,12 @@ describe('Methods', function () { it('normalizes no callback into callback (cached error)', function (done) { - var add = function (a, b) { + const add = function (a, b) { return new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1174,12 +1174,12 @@ describe('Methods', function () { it('normalizes no callback into callback (cached throw)', function (done) { - var add = function (a, b) { + const add = function (a, b) { throw new Error('boom'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); server._methods._normalized.add(1, 5, function (err, result) { @@ -1192,8 +1192,8 @@ describe('Methods', function () { it('throws an error if unknown keys are present when making a server method using an object', function (done) { - var fn = function () { }; - var server = new Hapi.Server(); + const fn = function () { }; + const server = new Hapi.Server(); expect(function () { diff --git a/test/payload.js b/test/payload.js index 0b8342e9c..4e30d14e8 100755 --- a/test/payload.js +++ b/test/payload.js @@ -2,37 +2,37 @@ // Load modules -var Fs = require('fs'); -var Http = require('http'); -var Path = require('path'); -var Zlib = require('zlib'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); -var Wreck = require('wreck'); +const Fs = require('fs'); +const Http = require('http'); +const Path = require('path'); +const Zlib = require('zlib'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); +const Wreck = require('wreck'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('payload', function () { it('sets payload', function (done) { - var payload = '{"x":"1","y":"2","z":"3"}'; + const payload = '{"x":"1","y":"2","z":"3"}'; - var handler = function (request, reply) { + const handler = function (request, reply) { expect(request.payload).to.exist(); expect(request.payload.z).to.equal('3'); @@ -40,7 +40,7 @@ describe('payload', function () { return reply(request.payload); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); @@ -54,12 +54,12 @@ describe('payload', function () { it('handles request socket error', function (done) { - var handler = function () { + const handler = function () { throw new Error('never called'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); @@ -73,12 +73,12 @@ describe('payload', function () { it('handles request socket close', function (done) { - var handler = function () { + const handler = function () { throw new Error('never called'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); @@ -93,12 +93,12 @@ describe('payload', function () { it('handles aborted request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Success'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { parse: false } } }); @@ -112,7 +112,7 @@ describe('payload', function () { expect(err).to.not.exist(); - var options = { + const options = { hostname: 'localhost', port: server.info.port, path: '/', @@ -122,7 +122,7 @@ describe('payload', function () { } }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { }); @@ -144,15 +144,15 @@ describe('payload', function () { it('errors when payload too big', function (done) { - var payload = '{"x":"1","y":"2","z":"3"}'; + const payload = '{"x":"1","y":"2","z":"3"}'; - var handler = function (request, reply) { + const handler = function (request, reply) { expect(request.payload.toString()).to.equal(payload); return reply(request.payload); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { maxBytes: 10 } } }); @@ -167,14 +167,14 @@ describe('payload', function () { it('returns 400 with response when payload is not consumed', function (done) { - var payload = new Buffer(10 * 1024 * 1024).toString(); + const payload = new Buffer(10 * 1024 * 1024).toString(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { maxBytes: 1024 * 1024 } } }); @@ -182,7 +182,7 @@ describe('payload', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.post(uri, { payload: payload }, function (err, res, body) { @@ -198,9 +198,9 @@ describe('payload', function () { it('peeks at unparsed data', function (done) { var data = null; - var ext = function (request, reply) { + const ext = function (request, reply) { - var chunks = []; + const chunks = []; request.on('peek', function (chunk) { chunks.push(chunk); @@ -214,17 +214,17 @@ describe('payload', function () { return reply.continue(); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', ext); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { parse: false } } }); - var payload = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'; + const payload = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'; server.inject({ method: 'POST', url: '/', payload: payload }, function (res) { expect(res.result).to.equal(payload); @@ -234,19 +234,19 @@ describe('payload', function () { it('handles gzipped payload', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; - var message = { 'msg': 'This message is going to be gzipped.' }; - var server = new Hapi.Server(); + const message = { 'msg': 'This message is going to be gzipped.' }; + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); Zlib.gzip(JSON.stringify(message), function (err, buf) { - var request = { + const request = { method: 'POST', url: '/', headers: { @@ -268,21 +268,21 @@ describe('payload', function () { it('saves a file after content decoding', function (done) { - var path = Path.join(__dirname, './file/image.jpg'); - var sourceContents = Fs.readFileSync(path); - var stats = Fs.statSync(path); + const path = Path.join(__dirname, './file/image.jpg'); + const sourceContents = Fs.readFileSync(path); + const stats = Fs.statSync(path); Zlib.gzip(sourceContents, function (err, compressed) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var receivedContents = Fs.readFileSync(request.payload.path); + const receivedContents = Fs.readFileSync(request.payload.path); Fs.unlinkSync(request.payload.path); expect(receivedContents).to.deep.equal(sourceContents); return reply(request.payload.bytes); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/file', config: { handler: handler, payload: { output: 'file' } } }); server.inject({ method: 'POST', url: '/file', payload: compressed, headers: { 'content-encoding': 'gzip' } }, function (res) { @@ -295,9 +295,9 @@ describe('payload', function () { it('errors saving a file without parse', function (done) { - var handler = function (request, reply) { }; + const handler = function (request, reply) { }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/file', config: { handler: handler, payload: { output: 'file', parse: false, uploads: '/a/b/c/d/not' } } }); server.inject({ method: 'POST', url: '/file', payload: 'abcde' }, function (res) { @@ -309,12 +309,12 @@ describe('payload', function () { it('sets parse mode when route methos is * and request is POST', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload.key); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: '*', path: '/any', handler: handler }); @@ -328,12 +328,12 @@ describe('payload', function () { it('returns an error on unsupported mime type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload.key); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); @@ -341,7 +341,7 @@ describe('payload', function () { expect(err).to.not.exist(); - var options = { + const options = { hostname: 'localhost', port: server.info.port, path: '/?x=4', @@ -352,7 +352,7 @@ describe('payload', function () { } }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect(res.statusCode).to.equal(415); server.stop({ timeout: 1 }, done); @@ -364,12 +364,12 @@ describe('payload', function () { it('ignores unsupported mime type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { failAction: 'ignore' } } }); @@ -383,12 +383,12 @@ describe('payload', function () { it('returns 200 on octet mime type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); @@ -402,12 +402,12 @@ describe('payload', function () { it('returns 200 on text mime type', function (done) { - var textHandler = function (request, reply) { + const textHandler = function (request, reply) { return reply(request.payload + '+456'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/text', config: { handler: textHandler } }); @@ -421,12 +421,12 @@ describe('payload', function () { it('returns 200 on override mime type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload.key); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/override', config: { handler: handler, payload: { override: 'application/json' } } }); @@ -440,12 +440,12 @@ describe('payload', function () { it('returns 200 on text mime type when allowed', function (done) { - var textHandler = function (request, reply) { + const textHandler = function (request, reply) { return reply(request.payload + '+456'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/textOnly', config: { handler: textHandler, payload: { allow: 'text/plain' } } }); @@ -459,12 +459,12 @@ describe('payload', function () { it('returns 415 on non text mime type when disallowed', function (done) { - var textHandler = function (request, reply) { + const textHandler = function (request, reply) { return reply(request.payload + '+456'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/textOnly', config: { handler: textHandler, payload: { allow: 'text/plain' } } }); @@ -477,12 +477,12 @@ describe('payload', function () { it('returns 200 on text mime type when allowed (array)', function (done) { - var textHandler = function (request, reply) { + const textHandler = function (request, reply) { return reply(request.payload + '+456'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/textOnlyArray', config: { handler: textHandler, payload: { allow: ['text/plain'] } } }); @@ -496,12 +496,12 @@ describe('payload', function () { it('returns 415 on non text mime type when disallowed (array)', function (done) { - var textHandler = function (request, reply) { + const textHandler = function (request, reply) { return reply(request.payload + '+456'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/textOnlyArray', config: { handler: textHandler, payload: { allow: ['text/plain'] } } }); @@ -514,7 +514,7 @@ describe('payload', function () { it('parses application/x-www-form-urlencoded with arrays', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ @@ -536,7 +536,7 @@ describe('payload', function () { it('returns parsed multipart data', function (done) { - var multipartPayload = + const multipartPayload = '--AaB03x\r\n' + 'content-disposition: form-data; name="x"\r\n' + '\r\n' + @@ -564,20 +564,20 @@ describe('payload', function () { '... contents of file1.txt ...\r\r\n' + '--AaB03x--\r\n'; - var handler = function (request, reply) { + const handler = function (request, reply) { - var result = {}; - var keys = Object.keys(request.payload); + const result = {}; + const keys = Object.keys(request.payload); for (var i = 0, il = keys.length; i < il; ++i) { - var key = keys[i]; - var value = request.payload[key]; + const key = keys[i]; + const value = request.payload[key]; result[key] = value._readableState ? true : value; } return reply(result); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/echo', config: { handler: handler } }); @@ -594,27 +594,27 @@ describe('payload', function () { it('times out when client request taking too long', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('fast'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/fast', config: { handler: handler } }); server.start(function (err) { expect(err).to.not.exist(); - var timer = new Hoek.Bench(); - var options = { + const timer = new Hoek.Bench(); + const options = { hostname: '127.0.0.1', port: server.info.port, path: '/fast', method: 'POST' }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect(res.statusCode).to.equal(408); expect(timer.elapsed()).to.be.at.least(45); @@ -633,27 +633,27 @@ describe('payload', function () { it('times out when client request taking too long (route override)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('fast'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: false } } }); server.route({ method: 'POST', path: '/fast', config: { payload: { timeout: 50 }, handler: handler } }); server.start(function (err) { expect(err).to.not.exist(); - var timer = new Hoek.Bench(); - var options = { + const timer = new Hoek.Bench(); + const options = { hostname: '127.0.0.1', port: server.info.port, path: '/fast', method: 'POST' }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect(res.statusCode).to.equal(408); expect(timer.elapsed()).to.be.at.least(45); @@ -672,26 +672,26 @@ describe('payload', function () { it('returns payload when timeout is not triggered', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('fast'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/fast', config: { handler: handler } }); server.start(function (err) { expect(err).to.not.exist(); - var options = { + const options = { hostname: '127.0.0.1', port: server.info.port, path: '/fast', method: 'POST' }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect(res.statusCode).to.equal(200); server.stop({ timeout: 1 }, done); diff --git a/test/plugin.js b/test/plugin.js index e032fe200..928016e2a 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -2,29 +2,29 @@ // Load modules -var Path = require('path'); -var Boom = require('boom'); -var CatboxMemory = require('catbox-memory'); -var Code = require('code'); -var Handlebars = require('handlebars'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Lab = require('lab'); -var Vision = require('vision'); +const Path = require('path'); +const Boom = require('boom'); +const CatboxMemory = require('catbox-memory'); +const Code = require('code'); +const Handlebars = require('handlebars'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Lab = require('lab'); +const Vision = require('vision'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Plugin', function () { @@ -33,18 +33,18 @@ describe('Plugin', function () { it('creates a subset of connections for manipulation', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); server.connection({ labels: ['s2', 'a', 'c'] }); server.connection({ labels: ['s3', 'a', 'b', 'd'] }); server.connection({ labels: ['s4', 'b', 'x'] }); - var register = function (srv, options, next) { + const register = function (srv, options, next) { - var a = srv.select('a'); - var ab = a.select('b'); - var memoryx = srv.select('x', 's4'); - var sodd = srv.select(['s2', 's4']); + const a = srv.select('a'); + const ab = a.select('b'); + const memoryx = srv.select('x', 's4'); + const sodd = srv.select(['s2', 's4']); expect(srv.connections.length).to.equal(4); expect(a.connections.length).to.equal(3); @@ -136,12 +136,12 @@ describe('Plugin', function () { it('registers a plugin on selection inside a plugin', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a'] }); server.connection({ labels: ['b'] }); server.connection({ labels: ['c'] }); - var child = function (srv, options, next) { + const child = function (srv, options, next) { srv.expose('key2', srv.connections.length); return next(); @@ -151,7 +151,7 @@ describe('Plugin', function () { name: 'child' }; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.expose('key1', srv.connections.length); srv.select('a').register(child, next); @@ -175,10 +175,10 @@ describe('Plugin', function () { it('registers plugin with options', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); - var test = function (srv, options, next) { + const test = function (srv, options, next) { expect(options.something).to.be.true(); expect(srv.realm.pluginOptions).to.equal(options); @@ -198,10 +198,10 @@ describe('Plugin', function () { it('registers a required plugin', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); - var test = { + const test = { register: function (srv, options, next) { expect(options.something).to.be.true(); @@ -222,7 +222,7 @@ describe('Plugin', function () { it('throws on bad plugin (missing attributes)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.register({ @@ -239,14 +239,14 @@ describe('Plugin', function () { it('throws on bad plugin (missing name)', function (done) { - var register = function (srv, options, next) { + const register = function (srv, options, next) { return next(); }; register.attributes = {}; - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.register(register, function (err) { }); @@ -257,7 +257,7 @@ describe('Plugin', function () { it('throws on bad plugin (empty pkg)', function (done) { - var register = function (srv, options, next) { + const register = function (srv, options, next) { return next(); }; @@ -266,7 +266,7 @@ describe('Plugin', function () { pkg: {} }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.register(register, function (err) { }); @@ -277,10 +277,10 @@ describe('Plugin', function () { it('throws when register is missing a callback function', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); - var test = function (srv, options, next) { + const test = function (srv, options, next) { expect(options.something).to.be.true(); return next(); @@ -299,7 +299,7 @@ describe('Plugin', function () { it('returns plugin error', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { return next(new Error('from plugin')); }; @@ -308,7 +308,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -320,7 +320,7 @@ describe('Plugin', function () { it('sets version to 0.0.0 if missing', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -339,7 +339,7 @@ describe('Plugin', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -356,7 +356,7 @@ describe('Plugin', function () { it('exposes plugin registration information', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -377,7 +377,7 @@ describe('Plugin', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register({ @@ -386,7 +386,7 @@ describe('Plugin', function () { }, function (err) { expect(err).to.not.exist(); - var bob = server.connections[0].registrations.bob; + const bob = server.connections[0].registrations.bob; expect(bob).to.exist(); expect(bob).to.be.an.object(); expect(bob.version).to.equal('1.2.3'); @@ -402,7 +402,7 @@ describe('Plugin', function () { it('prevents plugin from multiple registrations', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -420,7 +420,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ host: 'example.com' }); server.register(test, function (err) { @@ -436,7 +436,7 @@ describe('Plugin', function () { it('allows plugin multiple registrations (attributes)', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.app.x = srv.app.x ? srv.app.x + 1 : 1; return next(); @@ -447,7 +447,7 @@ describe('Plugin', function () { multiple: true }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -463,7 +463,7 @@ describe('Plugin', function () { it('registers multiple plugins', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); var log = null; server.once('log', function (event, tags) { @@ -483,7 +483,7 @@ describe('Plugin', function () { it('registers multiple plugins (verbose)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); var log = null; server.once('log', function (event, tags) { @@ -503,7 +503,7 @@ describe('Plugin', function () { it('registers a child plugin', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(internals.plugins.child, function (err) { @@ -518,7 +518,7 @@ describe('Plugin', function () { it('registers a plugin with routes path prefix', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(internals.plugins.test1, { routes: { prefix: '/xyz' } }, function (err) { @@ -534,7 +534,7 @@ describe('Plugin', function () { it('registers a plugin with routes path prefix (plugin options)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register({ register: internals.plugins.test1, routes: { prefix: '/abc' } }, { routes: { prefix: '/xyz' } }, function (err) { @@ -550,7 +550,7 @@ describe('Plugin', function () { it('registers a plugin with routes path prefix and plugin root route', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -567,7 +567,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(test, { routes: { prefix: '/xyz' } }, function (err) { @@ -582,7 +582,7 @@ describe('Plugin', function () { it('ignores the type of the plugin value', function (done) { - var a = function () { }; + const a = function () { }; a.register = function (srv, options, next) { srv.route({ @@ -598,7 +598,7 @@ describe('Plugin', function () { a.register.attributes = { name: 'a' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(a, { routes: { prefix: '/xyz' } }, function (err) { @@ -613,7 +613,7 @@ describe('Plugin', function () { it('ignores unknown plugin properties', function (done) { - var a = { + const a = { register: function (srv, options, next) { srv.route({ @@ -631,7 +631,7 @@ describe('Plugin', function () { a.register.attributes = { name: 'a' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(a, function (err) { @@ -642,7 +642,7 @@ describe('Plugin', function () { it('ignores unknown plugin properties (with options)', function (done) { - var a = { + const a = { register: function (srv, options, next) { srv.route({ @@ -660,7 +660,7 @@ describe('Plugin', function () { a.register.attributes = { name: 'a' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register({ register: a }, function (err) { @@ -671,7 +671,7 @@ describe('Plugin', function () { it('registers a child plugin with parent routes path prefix', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(internals.plugins.child, { routes: { prefix: '/xyz' } }, function (err) { @@ -686,7 +686,7 @@ describe('Plugin', function () { it('registers a child plugin with parent routes vhost prefix', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(internals.plugins.child, { routes: { vhost: 'example.com' } }, function (err) { @@ -701,7 +701,7 @@ describe('Plugin', function () { it('registers a child plugin with parent routes path prefix and inner register prefix', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register({ register: internals.plugins.child, options: { routes: { prefix: '/inner' } } }, { routes: { prefix: '/xyz' } }, function (err) { @@ -716,7 +716,7 @@ describe('Plugin', function () { it('registers a child plugin with parent routes vhost prefix and inner register vhost', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register({ register: internals.plugins.child, options: { routes: { vhost: 'example.net' } } }, { routes: { vhost: 'example.com' } }, function (err) { @@ -731,7 +731,7 @@ describe('Plugin', function () { it('registers a plugin with routes vhost', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register(internals.plugins.test1, { routes: { vhost: 'example.com' } }, function (err) { @@ -751,7 +751,7 @@ describe('Plugin', function () { it('registers a plugin with routes vhost (plugin options)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'test' }); server.register({ register: internals.plugins.test1, routes: { vhost: 'example.org' } }, { routes: { vhost: 'example.com' } }, function (err) { @@ -771,14 +771,14 @@ describe('Plugin', function () { it('registers plugins with pre-selected label', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a'] }); server.connection({ labels: ['b'] }); - var server1 = server.connections[0]; - var server2 = server.connections[1]; + const server1 = server.connections[0]; + const server2 = server.connections[1]; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -812,16 +812,16 @@ describe('Plugin', function () { it('registers plugins with pre-selected labels', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a'] }); server.connection({ labels: ['b'] }); server.connection({ labels: ['c'] }); - var server1 = server.connections[0]; - var server2 = server.connections[1]; - var server3 = server.connections[2]; + const server1 = server.connections[0]; + const server2 = server.connections[1]; + const server3 = server.connections[2]; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -862,16 +862,16 @@ describe('Plugin', function () { it('registers plugins with pre-selected labels (plugin options)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a'] }); server.connection({ labels: ['b'] }); server.connection({ labels: ['c'] }); - var server1 = server.connections[0]; - var server2 = server.connections[1]; - var server3 = server.connections[2]; + const server1 = server.connections[0]; + const server2 = server.connections[1]; + const server3 = server.connections[2]; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -912,7 +912,7 @@ describe('Plugin', function () { it('sets multiple dependencies in one statement', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.dependency(['b', 'c']); return next(); @@ -922,7 +922,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -931,7 +931,7 @@ describe('Plugin', function () { name: 'b' }; - var c = function (srv, options, next) { + const c = function (srv, options, next) { return next(); }; @@ -940,7 +940,7 @@ describe('Plugin', function () { name: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -960,7 +960,7 @@ describe('Plugin', function () { it('sets multiple dependencies in attributes', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { return next(); }; @@ -970,7 +970,7 @@ describe('Plugin', function () { dependencies: ['b', 'c'] }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -979,7 +979,7 @@ describe('Plugin', function () { name: 'b' }; - var c = function (srv, options, next) { + const c = function (srv, options, next) { return next(); }; @@ -988,7 +988,7 @@ describe('Plugin', function () { name: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1008,7 +1008,7 @@ describe('Plugin', function () { it('sets multiple dependencies in multiple statements', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.dependency('b'); srv.dependency('c'); @@ -1019,7 +1019,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -1028,7 +1028,7 @@ describe('Plugin', function () { name: 'b' }; - var c = function (srv, options, next) { + const c = function (srv, options, next) { return next(); }; @@ -1037,7 +1037,7 @@ describe('Plugin', function () { name: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1057,7 +1057,7 @@ describe('Plugin', function () { it('sets multiple dependencies in multiple locations', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.dependency('b'); return next(); @@ -1068,7 +1068,7 @@ describe('Plugin', function () { dependencies: 'c' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -1077,7 +1077,7 @@ describe('Plugin', function () { name: 'b' }; - var c = function (srv, options, next) { + const c = function (srv, options, next) { return next(); }; @@ -1086,7 +1086,7 @@ describe('Plugin', function () { name: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1106,7 +1106,7 @@ describe('Plugin', function () { it('errors when dependency loaded before connection was added', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { return next(); }; @@ -1116,7 +1116,7 @@ describe('Plugin', function () { dependencies: 'b' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -1125,7 +1125,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1144,7 +1144,7 @@ describe('Plugin', function () { it('set dependency on previously loaded connectionless plugin', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { return next(); }; @@ -1154,7 +1154,7 @@ describe('Plugin', function () { dependencies: 'b' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { expect(srv.connections).to.be.null(); return next(); @@ -1165,7 +1165,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1183,7 +1183,7 @@ describe('Plugin', function () { it('allows multiple connectionless plugin', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { return next(); }; @@ -1193,7 +1193,7 @@ describe('Plugin', function () { dependencies: 'b' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { expect(srv.connections).to.be.null(); return next(); @@ -1205,7 +1205,7 @@ describe('Plugin', function () { multiple: true }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register([b, b], function (err) { @@ -1223,7 +1223,7 @@ describe('Plugin', function () { it('register nested connectionless plugins', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, function (err) { @@ -1236,7 +1236,7 @@ describe('Plugin', function () { connections: false }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -1246,7 +1246,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(a, function (err) { @@ -1257,7 +1257,7 @@ describe('Plugin', function () { it('throws when nested connectionless plugins select', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { expect(function () { @@ -1271,7 +1271,7 @@ describe('Plugin', function () { connections: false }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -1281,7 +1281,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(a, function (err) { @@ -1292,7 +1292,7 @@ describe('Plugin', function () { it('register a plugin once per connection', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, { once: true }, function (err) { @@ -1306,7 +1306,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; return next(); @@ -1316,7 +1316,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1335,7 +1335,7 @@ describe('Plugin', function () { it('register a plugin once per connection (skip empty selection)', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.select('none').register(b, { once: true }, function (err) { @@ -1349,7 +1349,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; return next(); @@ -1359,7 +1359,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connection(); server.register(b, function (err) { @@ -1379,7 +1379,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (empty selection)', function (done) { var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; return next(); @@ -1390,7 +1390,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connection(); server.select('none').register(b, { once: true }, function (err) { @@ -1403,7 +1403,7 @@ describe('Plugin', function () { it('register a plugin once per connection (no selection left)', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, { once: true }, function (err) { @@ -1417,7 +1417,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; return next(); @@ -1427,7 +1427,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connection(); server.register(b, function (err) { @@ -1447,7 +1447,7 @@ describe('Plugin', function () { it('register a plugin once (empty selection)', function (done) { var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; return next(); @@ -1457,7 +1457,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connection(); server.select('none').register(b, { once: true }, function (err) { @@ -1470,7 +1470,7 @@ describe('Plugin', function () { it('register a connectionless plugin once', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, { once: true }, function (err) { @@ -1484,7 +1484,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; expect(srv.connections).to.be.null(); @@ -1496,7 +1496,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1515,7 +1515,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (plugin attributes)', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, function (err) { @@ -1529,7 +1529,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; expect(srv.connections).to.be.null(); @@ -1542,7 +1542,7 @@ describe('Plugin', function () { once: true }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1561,7 +1561,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (plugin options)', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register({ register: b, once: true }, function (err) { @@ -1575,7 +1575,7 @@ describe('Plugin', function () { }; var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; expect(srv.connections).to.be.null(); @@ -1587,7 +1587,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(b, function (err) { @@ -1607,7 +1607,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (first time)', function (done) { var count = 0; - var b = function (srv, options, next) { + const b = function (srv, options, next) { ++count; expect(srv.connections).to.be.null(); @@ -1619,7 +1619,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connection(); server.register(b, { once: true }, function (err) { @@ -1632,7 +1632,7 @@ describe('Plugin', function () { it('throws when once used with plugin options', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { return next(); }; @@ -1641,7 +1641,7 @@ describe('Plugin', function () { name: 'a' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1653,7 +1653,7 @@ describe('Plugin', function () { it('throws when dependencies is an object', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { next(); }; @@ -1662,7 +1662,7 @@ describe('Plugin', function () { dependencies: { b: true } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1674,7 +1674,7 @@ describe('Plugin', function () { it('throws when dependencies contain something else than a string', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { next(); }; @@ -1683,7 +1683,7 @@ describe('Plugin', function () { dependencies: [true] }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1695,10 +1695,10 @@ describe('Plugin', function () { it('exposes server decorations to next register', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.decorate('server', 'a', function () { @@ -1712,7 +1712,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(typeof srv.a === 'function' ? null : new Error('Missing decoration')); }; @@ -1734,10 +1734,10 @@ describe('Plugin', function () { it('exposes server decorations to dependency (dependency first)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.decorate('server', 'a', function () { @@ -1751,7 +1751,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { srv.dependency('a', function (srv2, next2) { @@ -1778,10 +1778,10 @@ describe('Plugin', function () { it('exposes server decorations to dependency (dependency second)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.decorate('server', 'a', function () { @@ -1795,7 +1795,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { srv.realm.x = 1; srv.dependency('a', function (srv2, next2) { @@ -1824,10 +1824,10 @@ describe('Plugin', function () { it('exposes server decorations to next register when nested', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.decorate('server', 'a', function () { @@ -1841,7 +1841,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { srv.register(a, function (err) { @@ -1870,7 +1870,7 @@ describe('Plugin', function () { it('adds auth strategy via plugin', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: 'a' }); server.connection({ labels: 'b' }); server.route({ @@ -1904,9 +1904,9 @@ describe('Plugin', function () { it('sets plugin context', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { - var bind = { + const bind = { value: 'in context', suffix: ' throughout' }; @@ -1934,7 +1934,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -1952,9 +1952,9 @@ describe('Plugin', function () { it('provisions a server cache', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var cache = server.cache({ segment: 'test', expiresIn: 1000 }); + const cache = server.cache({ segment: 'test', expiresIn: 1000 }); server.initialize(function (err) { expect(err).to.not.exist(); @@ -1972,7 +1972,7 @@ describe('Plugin', function () { it('throws when missing segment', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -1983,9 +1983,9 @@ describe('Plugin', function () { it('provisions a server cache with custom partition', function (done) { - var server = new Hapi.Server({ cache: { engine: CatboxMemory, partition: 'hapi-test-other' } }); + const server = new Hapi.Server({ cache: { engine: CatboxMemory, partition: 'hapi-test-other' } }); server.connection(); - var cache = server.cache({ segment: 'test', expiresIn: 1000 }); + const cache = server.cache({ segment: 'test', expiresIn: 1000 }); server.initialize(function (err) { expect(err).to.not.exist(); @@ -2004,7 +2004,7 @@ describe('Plugin', function () { it('throws when allocating an invalid cache segment', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2016,7 +2016,7 @@ describe('Plugin', function () { it('allows allocating a cache segment with empty options', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2028,7 +2028,7 @@ describe('Plugin', function () { it('allows reusing the same cache segment (server)', function (done) { - var server = new Hapi.Server({ cache: { engine: CatboxMemory, shared: true } }); + const server = new Hapi.Server({ cache: { engine: CatboxMemory, shared: true } }); server.connection(); expect(function () { @@ -2040,7 +2040,7 @@ describe('Plugin', function () { it('allows reusing the same cache segment (cache)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2052,9 +2052,9 @@ describe('Plugin', function () { it('uses plugin cache interface', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { - var cache = srv.cache({ expiresIn: 10 }); + const cache = srv.cache({ expiresIn: 10 }); srv.expose({ get: function (key, callback) { @@ -2076,7 +2076,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2112,10 +2112,10 @@ describe('Plugin', function () { it('returns a selection object within the same realm', function (done) { - var plugin = function (srv, options, next) { + const plugin = function (srv, options, next) { srv.bind({ some: 'context' }); - var con = srv.connection(); + const con = srv.connection(); con.route({ method: 'GET', path: '/', @@ -2133,7 +2133,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(plugin, function (err) { expect(err).to.not.exist(); @@ -2150,7 +2150,7 @@ describe('Plugin', function () { it('decorates request', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('request', 'getId', function () { @@ -2177,7 +2177,7 @@ describe('Plugin', function () { it('decorates reply', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('reply', 'success', function () { @@ -2204,7 +2204,7 @@ describe('Plugin', function () { it('throws on double reply decoration', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('reply', 'success', function () { @@ -2221,7 +2221,7 @@ describe('Plugin', function () { it('throws on internal conflict', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2233,7 +2233,7 @@ describe('Plugin', function () { it('decorates server', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('server', 'ok', function (path) { @@ -2260,7 +2260,7 @@ describe('Plugin', function () { it('throws on double server decoration', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('server', 'ok', function (path) { @@ -2284,7 +2284,7 @@ describe('Plugin', function () { it('throws on server decoration root conflict', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2296,7 +2296,7 @@ describe('Plugin', function () { it('throws on server decoration plugin conflict', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2308,7 +2308,7 @@ describe('Plugin', function () { it('throws on invalid decoration name', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -2323,7 +2323,7 @@ describe('Plugin', function () { it('fails to register single plugin with dependencies', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.dependency('none'); return next(); @@ -2333,7 +2333,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2348,7 +2348,7 @@ describe('Plugin', function () { it('fails to register single plugin with dependencies (attributes)', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { return next(); }; @@ -2358,7 +2358,7 @@ describe('Plugin', function () { dependencies: 'none' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2373,7 +2373,7 @@ describe('Plugin', function () { it('fails to register single plugin with dependencies (connectionless)', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.dependency('none'); return next(); @@ -2384,7 +2384,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2399,7 +2399,7 @@ describe('Plugin', function () { it('fails to register plugin with multiple dependencies (connectionless)', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.dependency(['b', 'none']); return next(); @@ -2410,7 +2410,7 @@ describe('Plugin', function () { connections: false }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -2420,7 +2420,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register([test, b], function (err) { @@ -2435,7 +2435,7 @@ describe('Plugin', function () { it('register plugin with multiple dependencies (connectionless)', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.dependency(['b']); return next(); @@ -2446,7 +2446,7 @@ describe('Plugin', function () { connections: false }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -2456,7 +2456,7 @@ describe('Plugin', function () { connections: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register([test, b], function (err) { @@ -2470,7 +2470,7 @@ describe('Plugin', function () { it('fails to register multiple plugins with dependencies', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); server.register([internals.plugins.deps1, internals.plugins.deps3], function (err) { @@ -2485,7 +2485,7 @@ describe('Plugin', function () { it('recognizes dependencies from peer plugins', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, next); }; @@ -2494,7 +2494,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -2503,7 +2503,7 @@ describe('Plugin', function () { name: 'b' }; - var c = function (srv, options, next) { + const c = function (srv, options, next) { srv.dependency('b'); return next(); @@ -2513,7 +2513,7 @@ describe('Plugin', function () { name: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register([a, c], function (err) { @@ -2524,7 +2524,7 @@ describe('Plugin', function () { it('errors when missing inner dependencies', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, next); }; @@ -2533,7 +2533,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { srv.dependency('c'); return next(); @@ -2543,7 +2543,7 @@ describe('Plugin', function () { name: 'b' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); server.register(a, function (err) { @@ -2558,7 +2558,7 @@ describe('Plugin', function () { it('errors when missing inner dependencies (attributes)', function (done) { - var a = function (srv, options, next) { + const a = function (srv, options, next) { srv.register(b, next); }; @@ -2567,7 +2567,7 @@ describe('Plugin', function () { name: 'a' }; - var b = function (srv, options, next) { + const b = function (srv, options, next) { return next(); }; @@ -2577,7 +2577,7 @@ describe('Plugin', function () { dependencies: 'c' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); server.register(a, function (err) { @@ -2595,7 +2595,7 @@ describe('Plugin', function () { it('plugin event handlers receive more than 2 arguments when they exist', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.once('request-internal', function () { @@ -2610,7 +2610,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2621,17 +2621,17 @@ describe('Plugin', function () { it('listens to events on selected connections', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a'] }); server.connection({ labels: ['b'] }); server.connection({ labels: ['c'] }); - var server1 = server.connections[0]; - var server2 = server.connections[1]; - var server3 = server.connections[2]; + const server1 = server.connections[0]; + const server2 = server.connections[1]; + const server3 = server.connections[2]; var counter = 0; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.select(['a', 'b']).on('test', function () { @@ -2676,7 +2676,7 @@ describe('Plugin', function () { it('exposes an api', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); server.connection({ labels: ['s2', 'a', 'test'] }); server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); @@ -2703,7 +2703,7 @@ describe('Plugin', function () { it('extends onRequest point', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.route({ method: 'GET', @@ -2727,7 +2727,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -2744,12 +2744,12 @@ describe('Plugin', function () { it('adds multiple ext functions with simple dependencies', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['a', 'b', '0'] }); server.connection({ labels: ['a', 'c', '1'] }); server.connection({ labels: ['c', 'b', '2'] }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.app.deps); }; @@ -2790,9 +2790,9 @@ describe('Plugin', function () { // Generate a plugin with a specific index and ext dependencies. - var pluginCurrier = function (num, deps) { + const pluginCurrier = function (num, deps) { - var plugin = function (server, options, next) { + const plugin = function (server, options, next) { server.ext('onRequest', function (request, reply) { @@ -2811,12 +2811,12 @@ describe('Plugin', function () { return plugin; }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.app.complexDeps); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2844,7 +2844,7 @@ describe('Plugin', function () { it('throws when adding ext without connections', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.ext('onRequest', function () { }); @@ -2855,10 +2855,10 @@ describe('Plugin', function () { it('binds server ext to context (options)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var bind = { + const bind = { state: false }; @@ -2878,10 +2878,10 @@ describe('Plugin', function () { it('binds server ext to context (realm)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var bind = { + const bind = { state: false }; @@ -2902,7 +2902,7 @@ describe('Plugin', function () { it('extends server actions', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var result = ''; @@ -2946,7 +2946,7 @@ describe('Plugin', function () { it('extends server actions (single call)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var result = ''; @@ -3001,7 +3001,7 @@ describe('Plugin', function () { it('combine route extensions', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreAuth', function (request, reply) { @@ -3010,7 +3010,7 @@ describe('Plugin', function () { return reply.continue(); }); - var plugin = function (srv, options, next) { + const plugin = function (srv, options, next) { srv.route({ method: 'GET', @@ -3075,7 +3075,7 @@ describe('Plugin', function () { it('calls method after plugin', function (done) { - var x = function (srv, options, next) { + const x = function (srv, options, next) { srv.expose('a', 'b'); return next(); @@ -3085,7 +3085,7 @@ describe('Plugin', function () { name: 'x' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(server.plugins.x).to.not.exist(); @@ -3112,7 +3112,7 @@ describe('Plugin', function () { it('calls method before start', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var called = false; @@ -3132,7 +3132,7 @@ describe('Plugin', function () { it('calls method before start even if plugin not registered', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var called = false; @@ -3152,7 +3152,7 @@ describe('Plugin', function () { it('fails to start server when after method fails', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.ext('onPreStart', function (inner, finish) { @@ -3171,7 +3171,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -3186,7 +3186,7 @@ describe('Plugin', function () { it('errors when added after initialization', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.initialize(function (err) { @@ -3205,7 +3205,7 @@ describe('Plugin', function () { it('add new handler', function (done) { - var test = function (srv, options1, next) { + const test = function (srv, options1, next) { srv.handler('bar', function (route, options2) { @@ -3222,7 +3222,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(test, function (err) { @@ -3245,7 +3245,7 @@ describe('Plugin', function () { it('errors on duplicate handler', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); @@ -3258,7 +3258,7 @@ describe('Plugin', function () { it('errors on unknown handler', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3270,7 +3270,7 @@ describe('Plugin', function () { it('errors on non-string name', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3282,7 +3282,7 @@ describe('Plugin', function () { it('errors on non-function handler', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3297,7 +3297,7 @@ describe('Plugin', function () { it('emits a log event', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var count = 0; @@ -3329,7 +3329,7 @@ describe('Plugin', function () { it('emits a log event and print to console', { parallel: false }, function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.once('log', function (event) { @@ -3337,7 +3337,7 @@ describe('Plugin', function () { expect(event.data).to.equal('log event 1'); }); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -3352,10 +3352,10 @@ describe('Plugin', function () { it('outputs log data to debug console', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -3370,10 +3370,10 @@ describe('Plugin', function () { it('outputs log error data to debug console', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -3388,10 +3388,10 @@ describe('Plugin', function () { it('outputs log data to debug console without data', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -3406,11 +3406,11 @@ describe('Plugin', function () { it('does not output events when debug disabled', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; @@ -3425,11 +3425,11 @@ describe('Plugin', function () { it('does not output events when debug.log disabled', function (done) { - var server = new Hapi.Server({ debug: { log: false } }); + const server = new Hapi.Server({ debug: { log: false } }); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; @@ -3444,11 +3444,11 @@ describe('Plugin', function () { it('does not output non-implementation events by default', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; @@ -3464,7 +3464,7 @@ describe('Plugin', function () { it('emits server log events once', function (done) { var pc = 0; - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.on('log', function (event, tags) { @@ -3478,7 +3478,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var sc = 0; @@ -3502,7 +3502,7 @@ describe('Plugin', function () { it('returns route based on id', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -3517,7 +3517,7 @@ describe('Plugin', function () { } }); - var root = server.lookup('root'); + const root = server.lookup('root'); expect(root.path).to.equal('/'); expect(root.settings.app.test).to.equal(123); done(); @@ -3525,16 +3525,16 @@ describe('Plugin', function () { it('returns null on unknown route', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var root = server.lookup('root'); + const root = server.lookup('root'); expect(root).to.be.null(); done(); }); it('throws on missing id', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3548,7 +3548,7 @@ describe('Plugin', function () { it('returns route based on path', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ @@ -3624,7 +3624,7 @@ describe('Plugin', function () { it('throws on missing method', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3635,7 +3635,7 @@ describe('Plugin', function () { it('throws on invalid method', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3646,7 +3646,7 @@ describe('Plugin', function () { it('throws on missing path', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3657,7 +3657,7 @@ describe('Plugin', function () { it('throws on invalid path type', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3668,7 +3668,7 @@ describe('Plugin', function () { it('throws on invalid path prefix', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3679,7 +3679,7 @@ describe('Plugin', function () { it('throws on invalid path', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -3701,7 +3701,7 @@ describe('Plugin', function () { it('throws on invalid host type', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3715,10 +3715,10 @@ describe('Plugin', function () { it('adds server method using arguments', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.method('log', function (methodNext) { @@ -3740,10 +3740,10 @@ describe('Plugin', function () { it('adds server method with plugin bind', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.bind({ x: 1 }); srv.method('log', function (methodNext) { @@ -3770,10 +3770,10 @@ describe('Plugin', function () { it('adds server method with method bind', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.method('log', function (methodNext) { @@ -3799,10 +3799,10 @@ describe('Plugin', function () { it('adds server method with method and ext bind', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.bind({ x: 1 }); srv.method('log', function (methodNext) { @@ -3832,7 +3832,7 @@ describe('Plugin', function () { it('sets local path for directory route handler', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.path(Path.join(__dirname, '..')); @@ -3853,7 +3853,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); server.register(test, function (err) { @@ -3869,7 +3869,7 @@ describe('Plugin', function () { it('throws when plugin sets undefined path', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.path(); return next(); @@ -3879,7 +3879,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -3893,7 +3893,7 @@ describe('Plugin', function () { it('renders view', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); server.connection(); server.views({ @@ -3914,7 +3914,7 @@ describe('Plugin', function () { it('throws when adding state without connections', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.state('sid', { encoding: 'base64' }); @@ -3928,11 +3928,11 @@ describe('Plugin', function () { it('requires plugin with views', function (done) { - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.path(__dirname); - var views = { + const views = { engines: { 'html': Handlebars }, path: './templates/plugin' }; @@ -3970,7 +3970,7 @@ describe('Plugin', function () { name: 'test' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register([Inert, Vision], Hoek.ignore); server.connection(); server.register({ register: test, options: { message: 'viewing it' } }, function (err) { @@ -3999,13 +3999,13 @@ describe('Plugin', function () { internals.routesList = function (server, label) { - var tables = server.select(label || []).table(); + const tables = server.select(label || []).table(); - var list = []; + const list = []; for (var c = 0, cl = tables.length; c < cl; ++c) { - var routes = tables[c].table; + const routes = tables[c].table; for (var i = 0, il = routes.length; i < il; ++i) { - var route = routes[i]; + const route = routes[i]; if (route.method === 'get') { list.push(route.path); } @@ -4021,18 +4021,18 @@ internals.plugins = { server.auth.scheme('basic', function (srv, authOptions) { - var settings = Hoek.clone(authOptions); + const settings = Hoek.clone(authOptions); - var scheme = { + const scheme = { authenticate: function (request, reply) { - var req = request.raw.req; - var authorization = req.headers.authorization; + const req = request.raw.req; + const authorization = req.headers.authorization; if (!authorization) { return reply(Boom.unauthorized(null, 'Basic')); } - var parts = authorization.split(/\s+/); + const parts = authorization.split(/\s+/); if (parts[0] && parts[0].toLowerCase() !== 'basic') { @@ -4044,13 +4044,13 @@ internals.plugins = { return reply(Boom.badRequest('Bad HTTP authentication header format', 'Basic')); } - var credentialsParts = new Buffer(parts[1], 'base64').toString().split(':'); + const credentialsParts = new Buffer(parts[1], 'base64').toString().split(':'); if (credentialsParts.length !== 2) { return reply(Boom.badRequest('Bad header internal syntax', 'Basic')); } - var username = credentialsParts[0]; - var password = credentialsParts[1]; + const username = credentialsParts[0]; + const password = credentialsParts[1]; settings.validateFunc(username, password, function (err, isValid, credentials) { @@ -4066,7 +4066,7 @@ internals.plugins = { return scheme; }); - var loadUser = function (username, password, callback) { + const loadUser = function (username, password, callback) { if (username === 'john') { return callback(null, password === '12345', { user: 'john' }); @@ -4102,7 +4102,7 @@ internals.plugins = { return nxt(); }); - var selection = server.select('a'); + const selection = server.select('a'); if (selection.connections.length) { selection.ext('onRequest', function (request, reply) { @@ -4116,7 +4116,7 @@ internals.plugins = { }, deps2: function (server, options, next) { - var selection = server.select('b'); + const selection = server.select('b'); if (selection.connections.length) { selection.ext('onRequest', function (request, reply) { @@ -4132,7 +4132,7 @@ internals.plugins = { }, deps3: function (server, options, next) { - var selection = server.select('c'); + const selection = server.select('c'); if (selection.connections.length) { selection.ext('onRequest', function (request, reply) { @@ -4146,7 +4146,7 @@ internals.plugins = { }, test1: function (server, options, next) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('testing123' + ((server.settings.app && server.settings.app.my) || '')); }; diff --git a/test/protect.js b/test/protect.js index 269dd8ec7..c7c8949c4 100755 --- a/test/protect.js +++ b/test/protect.js @@ -2,35 +2,35 @@ // Load modules -var Events = require('events'); -var Domain = require('domain'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); +const Events = require('events'); +const Domain = require('domain'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Protect', function () { it('does not handle errors when useDomains is false', function (done) { - var server = new Hapi.Server({ useDomains: false, debug: false }); + const server = new Hapi.Server({ useDomains: false, debug: false }); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { process.nextTick(function () { @@ -39,7 +39,7 @@ describe('Protect', function () { }; server.route({ method: 'GET', path: '/', handler: handler }); - var domain = Domain.createDomain(); + const domain = Domain.createDomain(); domain.once('error', function (err) { expect(err.message).to.equal('no domain'); @@ -54,10 +54,10 @@ describe('Protect', function () { it('catches error when handler throws after reply() is called', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { reply('ok'); process.nextTick(function () { @@ -76,10 +76,10 @@ describe('Protect', function () { it('catches error when handler throws twice after reply() is called', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { reply('ok'); @@ -104,18 +104,18 @@ describe('Protect', function () { it('catches errors thrown during request handling in non-request domain', function (done) { - var Client = function () { + const Client = function () { Events.EventEmitter.call(this); }; Hoek.inherits(Client, Events.EventEmitter); - var test = function (srv, options, next) { + const test = function (srv, options, next) { srv.ext('onPreStart', function (plugin, afterNext) { - var client = new Client(); // Created in the global domain + const client = new Client(); // Created in the global domain plugin.bind({ client: client }); afterNext(); }); @@ -141,7 +141,7 @@ describe('Protect', function () { name: 'test' }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.register(test, function (err) { @@ -160,7 +160,7 @@ describe('Protect', function () { it('logs to console after request completed', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { reply('ok'); setTimeout(function () { @@ -169,7 +169,7 @@ describe('Protect', function () { }, 10); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.on('log', function (event, tags) { diff --git a/test/reply.js b/test/reply.js index 46143e49d..f7ee74965 100755 --- a/test/reply.js +++ b/test/reply.js @@ -2,39 +2,39 @@ // Load modules -var Http = require('http'); -var Stream = require('stream'); -var Bluebird = require('bluebird'); -var Boom = require('boom'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); +const Http = require('http'); +const Stream = require('stream'); +const Bluebird = require('bluebird'); +const Boom = require('boom'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Reply', function () { it('throws when reply called twice', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { reply('ok'); return reply('not ok'); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -46,12 +46,12 @@ describe('Reply', function () { it('redirects from handler', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.redirect('/elsewhere'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -66,12 +66,12 @@ describe('Reply', function () { it('uses reply(null, result) for result', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(null, 'steve'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -84,12 +84,12 @@ describe('Reply', function () { it('uses reply(null, err) for err', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(null, Boom.badRequest()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -101,12 +101,12 @@ describe('Reply', function () { it('ignores result when err provided in reply(err, result)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest(), 'steve'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -121,12 +121,12 @@ describe('Reply', function () { it('returns null', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(null, null); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -141,12 +141,12 @@ describe('Reply', function () { it('returns a buffer reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new Buffer('Tada1')).code(299); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -161,12 +161,12 @@ describe('Reply', function () { it('returns an object response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -180,12 +180,12 @@ describe('Reply', function () { it('returns false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(false); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -198,12 +198,12 @@ describe('Reply', function () { it('returns an error reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new Error('boom')); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -217,12 +217,12 @@ describe('Reply', function () { it('returns an empty reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().code(299); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -237,7 +237,7 @@ describe('Reply', function () { it('returns a stream reply', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -256,12 +256,12 @@ describe('Reply', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()).ttl(2000); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); @@ -283,23 +283,23 @@ describe('Reply', function () { it('errors on non-readable stream reply', function (done) { - var streamHandler = function (request, reply) { + const streamHandler = function (request, reply) { - var stream = new Stream(); + const stream = new Stream(); stream.writable = true; reply(stream); }; - var writableHandler = function (request, reply) { + const writableHandler = function (request, reply) { - var writable = new Stream.Writable(); + const writable = new Stream.Writable(); writable._write = function () {}; reply(writable); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/stream', handler: streamHandler }); server.route({ method: 'GET', path: '/writable', handler: writableHandler }); @@ -334,17 +334,17 @@ describe('Reply', function () { it('errors on an http client stream reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { reply('just a string'); }; - var streamHandler = function (request, reply) { + const streamHandler = function (request, reply) { reply(Http.get(request.server.info + '/')); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.route({ method: 'GET', path: '/stream', handler: streamHandler }); @@ -363,7 +363,7 @@ describe('Reply', function () { it('errors on objectMode stream reply', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this, { objectMode: true }); }; @@ -382,12 +382,12 @@ describe('Reply', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -402,7 +402,7 @@ describe('Reply', function () { it('returns a stream', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); @@ -423,12 +423,12 @@ describe('Reply', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Bluebird.resolve(new TestStream())).ttl(2000).code(299); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); @@ -442,12 +442,12 @@ describe('Reply', function () { it('returns a buffer', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Bluebird.resolve(new Buffer('buffer content'))).code(299).type('something/special'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -466,10 +466,10 @@ describe('Reply', function () { it('undo scheduled next tick in reply interface', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('123').hold().send(); }; @@ -485,12 +485,12 @@ describe('Reply', function () { it('sends reply after timed handler', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { - var response = reply('123').hold(); + const response = reply('123').hold(); setTimeout(function () { response.send(); @@ -511,13 +511,13 @@ describe('Reply', function () { it('returns a reply with manual end', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.raw.res.end(); return reply.close({ end: false }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -530,12 +530,12 @@ describe('Reply', function () { it('returns a reply with auto end', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.close(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -551,12 +551,12 @@ describe('Reply', function () { it('sets empty reply on continue in handler', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.continue(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -571,17 +571,17 @@ describe('Reply', function () { it('sets empty reply on continue in prerequisite', function (done) { - var pre1 = function (request, reply) { + const pre1 = function (request, reply) { return reply.continue(); }; - var pre2 = function (request, reply) { + const pre2 = function (request, reply) { return reply.continue(); }; - var pre3 = function (request, reply) { + const pre3 = function (request, reply) { return reply({ m1: request.pre.m1, @@ -589,12 +589,12 @@ describe('Reply', function () { }); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.pre.m3); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', diff --git a/test/request.js b/test/request.js index e9291e6cd..43a9a8931 100755 --- a/test/request.js +++ b/test/request.js @@ -2,35 +2,35 @@ // Load modules -var Http = require('http'); -var Net = require('net'); -var Stream = require('stream'); -var Boom = require('boom'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); -var Wreck = require('wreck'); +const Http = require('http'); +const Net = require('net'); +const Stream = require('stream'); +const Boom = require('boom'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); +const Wreck = require('wreck'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Request.Generator', function () { it('decorates request multiple times', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.decorate('request', 'x2', function () { @@ -62,10 +62,10 @@ describe('Request', function () { it('sets client address', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { var expectedClientAddress = '127.0.0.1'; if (Net.isIPv6(server.listener.address().address)) { @@ -93,10 +93,10 @@ describe('Request', function () { it('sets referrer', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { expect(request.info.referrer).to.equal('http://site.com'); return reply('ok'); @@ -113,10 +113,10 @@ describe('Request', function () { it('sets referer', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { expect(request.info.referrer).to.equal('http://site.com'); return reply('ok'); @@ -133,12 +133,12 @@ describe('Request', function () { it('sets headers', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.headers['user-agent']); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -151,12 +151,12 @@ describe('Request', function () { it('generates unique request id', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.id); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connections[0]._requestCounter = { value: 10, min: 10, max: 11 }; server.route({ method: 'GET', path: '/', handler: handler }); @@ -179,7 +179,7 @@ describe('Request', function () { it('returns 400 on invalid path', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.inject('invalid', function (res) { @@ -190,15 +190,15 @@ describe('Request', function () { it('returns error response on ext error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('OK'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var ext = function (request, reply) { + const ext = function (request, reply) { return reply(Boom.badRequest()); }; @@ -215,9 +215,9 @@ describe('Request', function () { it('handles aborted requests', { parallel: false }, function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -235,11 +235,11 @@ describe('Request', function () { this.emit('data', 'success'); }; - var stream = new TestStream(); + const stream = new TestStream(); return reply(stream); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -259,9 +259,9 @@ describe('Request', function () { expect(err).to.not.exist(); var total = 2; - var createConnection = function () { + const createConnection = function () { - var client = Net.connect(server.info.port, function () { + const client = Net.connect(server.info.port, function () { client.write('GET / HTTP/1.1\r\n\r\n'); client.write('GET / HTTP/1.1\r\n\r\n'); @@ -274,7 +274,7 @@ describe('Request', function () { }); }; - var check = function () { + const check = function () { if (total) { createConnection(); @@ -292,12 +292,12 @@ describe('Request', function () { it('returns empty params array when none present', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.params); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -310,7 +310,7 @@ describe('Request', function () { it('returns empty params array when none present (not found)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreResponse', function (request, reply) { @@ -328,7 +328,7 @@ describe('Request', function () { var clientRequest; - var handler = function (request, reply) { + const handler = function (request, reply) { clientRequest.abort(); @@ -342,7 +342,7 @@ describe('Request', function () { }, 10); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -363,7 +363,7 @@ describe('Request', function () { it('does not fail on abort (onPreHandler)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: Hoek.ignore }); @@ -401,7 +401,7 @@ describe('Request', function () { var clientRequest; - var handler = function (request, reply) { + const handler = function (request, reply) { clientRequest.abort(); setTimeout(function () { @@ -410,7 +410,7 @@ describe('Request', function () { }, 10); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -441,7 +441,7 @@ describe('Request', function () { it('returns not found on internal only route (external)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -469,7 +469,7 @@ describe('Request', function () { it('returns not found on internal only route (inject)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -492,7 +492,7 @@ describe('Request', function () { it('allows internal only route (inject with allowInternals)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -518,12 +518,12 @@ describe('Request', function () { it('generate response event', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -538,11 +538,11 @@ describe('Request', function () { it('closes response after server timeout', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { setTimeout(function () { - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); stream._read = function (size) { this.push('value'); @@ -558,7 +558,7 @@ describe('Request', function () { }, 100); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', @@ -574,7 +574,7 @@ describe('Request', function () { it('does not attempt to close error response after server timeout', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { setTimeout(function () { @@ -582,7 +582,7 @@ describe('Request', function () { }, 10); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 5 } } }); server.route({ method: 'GET', @@ -599,7 +599,7 @@ describe('Request', function () { it('emits request-error once', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); var errs = 0; @@ -617,7 +617,7 @@ describe('Request', function () { return reply(new Error('boom2')); }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new Error('boom1')); }; @@ -641,7 +641,7 @@ describe('Request', function () { it('emits request-error on implementation error', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); var errs = 0; @@ -654,7 +654,7 @@ describe('Request', function () { req = request; }); - var handler = function (request, reply) { + const handler = function (request, reply) { throw new Error('boom'); }; @@ -678,7 +678,7 @@ describe('Request', function () { it('does not emit request-error when error is replaced with valid response', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); var errs = 0; @@ -692,7 +692,7 @@ describe('Request', function () { return reply('ok'); }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new Error('boom1')); }; @@ -717,10 +717,10 @@ describe('Request', function () { it('generates tail event', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var t1 = request.addTail('t1'); - var t2 = request.addTail('t2'); + const t1 = request.addTail('t1'); + const t2 = request.addTail('t2'); reply('Done'); @@ -729,7 +729,7 @@ describe('Request', function () { setTimeout(t2, 10); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -749,14 +749,14 @@ describe('Request', function () { it('generates tail event without name', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var tail = request.tail(); + const tail = request.tail(); reply('Done'); tail(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -775,7 +775,7 @@ describe('Request', function () { it('changes method with a lowercase version of the value passed in', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); @@ -794,7 +794,7 @@ describe('Request', function () { it('errors on missing method', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); @@ -812,7 +812,7 @@ describe('Request', function () { it('errors on invalid method type', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); @@ -833,12 +833,12 @@ describe('Request', function () { it('parses nested query string', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.query); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -851,8 +851,8 @@ describe('Request', function () { it('sets url, path, and query', function (done) { - var url = 'http://localhost/page?param1=something'; - var server = new Hapi.Server(); + const url = 'http://localhost/page?param1=something'; + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); @@ -871,12 +871,12 @@ describe('Request', function () { it('normalizes a path', function (done) { - var rawPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%20%21%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f%40%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f%50%51%52%53%54%55%56%57%58%59%5a%5b%5c%5d%5e%5f%60%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f%70%71%72%73%74%75%76%77%78%79%7a%7b%7c%7d%7e%7f%80%81%82%83%84%85%86%87%88%89%8a%8b%8c%8d%8e%8f%90%91%92%93%94%95%96%97%98%99%9a%9b%9c%9d%9e%9f%a0%a1%a2%a3%a4%a5%a6%a7%a8%a9%aa%ab%ac%ad%ae%af%b0%b1%b2%b3%b4%b5%b6%b7%b8%b9%ba%bb%bc%bd%be%bf%c0%c1%c2%c3%c4%c5%c6%c7%c8%c9%ca%cb%cc%cd%ce%cf%d0%d1%d2%d3%d4%d5%d6%d7%d8%d9%da%db%dc%dd%de%df%e0%e1%e2%e3%e4%e5%e6%e7%e8%e9%ea%eb%ec%ed%ee%ef%f0%f1%f2%f3%f4%f5%f6%f7%f8%f9%fa%fb%fc%fd%fe%ff%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37%38%39%3A%3B%3C%3D%3E%3F%40%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A%5B%5C%5D%5E%5F%60%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A%7B%7C%7D%7E%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; - var normPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; + const rawPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%20%21%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f%40%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f%50%51%52%53%54%55%56%57%58%59%5a%5b%5c%5d%5e%5f%60%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f%70%71%72%73%74%75%76%77%78%79%7a%7b%7c%7d%7e%7f%80%81%82%83%84%85%86%87%88%89%8a%8b%8c%8d%8e%8f%90%91%92%93%94%95%96%97%98%99%9a%9b%9c%9d%9e%9f%a0%a1%a2%a3%a4%a5%a6%a7%a8%a9%aa%ab%ac%ad%ae%af%b0%b1%b2%b3%b4%b5%b6%b7%b8%b9%ba%bb%bc%bd%be%bf%c0%c1%c2%c3%c4%c5%c6%c7%c8%c9%ca%cb%cc%cd%ce%cf%d0%d1%d2%d3%d4%d5%d6%d7%d8%d9%da%db%dc%dd%de%df%e0%e1%e2%e3%e4%e5%e6%e7%e8%e9%ea%eb%ec%ed%ee%ef%f0%f1%f2%f3%f4%f5%f6%f7%f8%f9%fa%fb%fc%fd%fe%ff%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37%38%39%3A%3B%3C%3D%3E%3F%40%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A%5B%5C%5D%5E%5F%60%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A%7B%7C%7D%7E%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; + const normPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; - var url = 'http://localhost' + rawPath + '?param1=something'; + const url = 'http://localhost' + rawPath + '?param1=something'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); @@ -895,7 +895,7 @@ describe('Request', function () { it('allows missing path', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -912,12 +912,12 @@ describe('Request', function () { it('strips trailing slash', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/test', handler: handler }); server.inject('/test/', function (res) { @@ -929,12 +929,12 @@ describe('Request', function () { it('does not strip trailing slash on /', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -946,12 +946,12 @@ describe('Request', function () { it('strips trailing slash with query', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/test', handler: handler }); server.inject('/test/?a=b', function (res) { @@ -963,11 +963,11 @@ describe('Request', function () { it('accepts querystring parser options', function (done) { - var url = 'http://localhost/page?a=1&b=1&c=1&d=1&e=1&f=1&g=1&h=1&i=1&j=1&k=1&l=1&m=1&n=1&o=1&p=1&q=1&r=1&s=1&t=1&u=1&v=1&w=1&x=1&y=1&z=1'; - var qsParserOptions = { + const url = 'http://localhost/page?a=1&b=1&c=1&d=1&e=1&f=1&g=1&h=1&i=1&j=1&k=1&l=1&m=1&n=1&o=1&p=1&q=1&r=1&s=1&t=1&u=1&v=1&w=1&x=1&y=1&z=1'; + const qsParserOptions = { parameterLimit: 26 }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -988,7 +988,7 @@ describe('Request', function () { it('overrides qs settings', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ query: { qs: { @@ -1020,17 +1020,17 @@ describe('Request', function () { it('outputs log data to debug console', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['implementation'], 'data'); return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var orig = console.error; + const orig = console.error; console.error = function () { expect(arguments[0]).to.equal('Debug:'); @@ -1048,7 +1048,7 @@ describe('Request', function () { it('emits a request event', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { server.on('request', function (req, event, tags) { @@ -1062,7 +1062,7 @@ describe('Request', function () { request.log(['test'], 'data'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1075,17 +1075,17 @@ describe('Request', function () { it('outputs log to debug console without data', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['implementation']); return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var orig = console.error; + const orig = console.error; console.error = function () { expect(arguments[0]).to.equal('Debug:'); @@ -1103,17 +1103,17 @@ describe('Request', function () { it('outputs log to debug console with error data', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['implementation'], new Error('boom')); return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var orig = console.error; + const orig = console.error; console.error = function () { expect(arguments[0]).to.equal('Debug:'); @@ -1131,20 +1131,20 @@ describe('Request', function () { it('handles invalid log data object stringify', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var obj = {}; + const obj = {}; obj.a = obj; request.log(['implementation'], obj); return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var orig = console.error; + const orig = console.error; console.error = function () { console.error = orig; @@ -1162,7 +1162,7 @@ describe('Request', function () { it('adds a log event to the request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request.log('1', 'log event 1', Date.now()); request.log(['2'], 'log event 2', new Date(Date.now())); @@ -1175,7 +1175,7 @@ describe('Request', function () { return reply([request.getLog('1').length, request.getLog('4').length, request.getLog(['4']).length, request.getLog('0').length, request.getLog(['1', '2', '3', '4']).length, request.getLog().length >= 7].join('|')); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1188,17 +1188,17 @@ describe('Request', function () { it('does not output events when debug disabled', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; }; - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['implementation']); return reply(); @@ -1217,17 +1217,17 @@ describe('Request', function () { it('does not output events when debug.request disabled', function (done) { - var server = new Hapi.Server({ debug: { request: false } }); + const server = new Hapi.Server({ debug: { request: false } }); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; }; - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['implementation']); return reply(); @@ -1246,17 +1246,17 @@ describe('Request', function () { it('does not output non-implementation events by default', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var i = 0; - var orig = console.error; + const orig = console.error; console.error = function () { ++i; }; - var handler = function (request, reply) { + const handler = function (request, reply) { request.log(['xyz']); return reply(); @@ -1278,7 +1278,7 @@ describe('Request', function () { it('emits a request-internal event', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.once('request-internal', function (request, event, tags) { @@ -1294,7 +1294,7 @@ describe('Request', function () { it('returns the selected logs', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { request._log('1'); request.log('1'); @@ -1302,7 +1302,7 @@ describe('Request', function () { return reply([request.getLog('1').length, request.getLog('1', true).length, request.getLog('1', false).length, request.getLog(true).length, request.getLog(false).length, request.getLog().length].join('|')); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1318,16 +1318,16 @@ describe('Request', function () { it('leaves the response open when the same response is set again', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPostHandler', function (request, reply) { return reply(request.response); }); - var handler = function (request, reply) { + const handler = function (request, reply) { - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); stream._read = function (size) { this.push('value'); @@ -1348,16 +1348,16 @@ describe('Request', function () { it('leaves the response open when the same response source is set again', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPostHandler', function (request, reply) { return reply(request.response.source); }); - var handler = function (request, reply) { + const handler = function (request, reply) { - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); stream._read = function (size) { this.push('value'); @@ -1381,13 +1381,13 @@ describe('Request', function () { it('returns server error message when server taking too long', function (done) { - var timeoutHandler = function (request, reply) { }; + const timeoutHandler = function (request, reply) { }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/timeout', config: { handler: timeoutHandler } }); - var timer = new Hoek.Bench(); + const timer = new Hoek.Bench(); server.inject('/timeout', function (res) { @@ -1399,7 +1399,7 @@ describe('Request', function () { it('returns server error message when server timeout happens during request execution (and handler yields)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { setTimeout(function () { @@ -1407,7 +1407,7 @@ describe('Request', function () { }, 20); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 10 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -1425,7 +1425,7 @@ describe('Request', function () { it('returns server error message when server timeout is short and already occurs when request executes', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 2 } } }); server.route({ method: 'GET', path: '/', config: { handler: function () { } } }); server.ext('onRequest', function (request, reply) { @@ -1445,12 +1445,12 @@ describe('Request', function () { it('handles server handler timeout with onPreResponse ext', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { setTimeout(reply, 20); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 10 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); server.ext('onPreResponse', function (request, reply) { @@ -1467,7 +1467,7 @@ describe('Request', function () { it('does not return an error response when server is slow but faster than timeout', function (done) { - var slowHandler = function (request, reply) { + const slowHandler = function (request, reply) { setTimeout(function () { @@ -1475,11 +1475,11 @@ describe('Request', function () { }, 30); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/slow', config: { handler: slowHandler } }); - var timer = new Hoek.Bench(); + const timer = new Hoek.Bench(); server.inject('/slow', function (res) { expect(timer.elapsed()).to.be.at.least(20); @@ -1491,9 +1491,9 @@ describe('Request', function () { it('does not return an error when server is responding when the timeout occurs', function (done) { var ended = false; - var handler = function (request, reply) { + const handler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -1502,7 +1502,7 @@ describe('Request', function () { TestStream.prototype._read = function (size) { - var self = this; + const self = this; if (this.isDone) { return; @@ -1521,9 +1521,9 @@ describe('Request', function () { return reply(new TestStream()); }; - var timer = new Hoek.Bench(); + const timer = new Hoek.Bench(); - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 100 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); server.start(function (err) { @@ -1541,9 +1541,9 @@ describe('Request', function () { it('does not return an error response when server is slower than timeout but response has started', function (done) { - var streamHandler = function (request, reply) { + const streamHandler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -1552,7 +1552,7 @@ describe('Request', function () { TestStream.prototype._read = function (size) { - var self = this; + const self = this; if (this.isDone) { return; @@ -1573,21 +1573,21 @@ describe('Request', function () { return reply(new TestStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/stream', config: { handler: streamHandler } }); server.start(function (err) { expect(err).to.not.exist(); - var options = { + const options = { hostname: '127.0.0.1', port: server.info.port, path: '/stream', method: 'GET' }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect(res.statusCode).to.equal(200); server.stop({ timeout: 1 }, done); @@ -1598,12 +1598,12 @@ describe('Request', function () { it('does not return an error response when server takes less than timeout to respond', function (done) { - var fastHandler = function (request, reply) { + const fastHandler = function (request, reply) { return reply('Fast'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/fast', config: { handler: fastHandler } }); @@ -1616,9 +1616,9 @@ describe('Request', function () { it('handles race condition between equal client and server timeouts', function (done) { - var timeoutHandler = function (request, reply) { }; + const timeoutHandler = function (request, reply) { }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 }, payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/timeout', config: { handler: timeoutHandler } }); @@ -1626,15 +1626,15 @@ describe('Request', function () { expect(err).to.not.exist(); - var timer = new Hoek.Bench(); - var options = { + const timer = new Hoek.Bench(); + const options = { hostname: '127.0.0.1', port: server.info.port, path: '/timeout', method: 'POST' }; - var req = Http.request(options, function (res) { + const req = Http.request(options, function (res) { expect([503, 408]).to.contain(res.statusCode); expect(timer.elapsed()).to.be.at.least(45); diff --git a/test/response.js b/test/response.js index c4e1ffa95..ee6db8a5d 100755 --- a/test/response.js +++ b/test/response.js @@ -2,36 +2,36 @@ // Load modules -var Stream = require('stream'); -var Bluebird = require('bluebird'); -var Boom = require('boom'); -var Code = require('code'); -var Handlebars = require('handlebars'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Lab = require('lab'); -var Vision = require('vision'); +const Stream = require('stream'); +const Bluebird = require('bluebird'); +const Boom = require('boom'); +const Code = require('code'); +const Handlebars = require('handlebars'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Lab = require('lab'); +const Vision = require('vision'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Response', function () { it('returns a reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text') .type('text/plain') @@ -49,7 +49,7 @@ describe('Response', function () { .code(200); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { expiresIn: 9999 } } }); server.state('sid', { encoding: 'base64' }); @@ -79,7 +79,7 @@ describe('Response', function () { it('returns an empty string reply', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -103,7 +103,7 @@ describe('Response', function () { it('returns a null reply', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -126,7 +126,7 @@ describe('Response', function () { it('returns an undefined reply', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -152,12 +152,12 @@ describe('Response', function () { it('appends to set-cookie header', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').header('set-cookie', 'A').header('set-cookie', 'B', { append: true }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -170,12 +170,12 @@ describe('Response', function () { it('sets null header', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').header('set-cookie', null); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -190,7 +190,7 @@ describe('Response', function () { var thrown = false; - var handler = function (request, reply) { + const handler = function (request, reply) { try { return reply('ok').header('set-cookie', decodeURIComponent('%E0%B4%8Aset-cookie:%20foo=bar')); @@ -201,7 +201,7 @@ describe('Response', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -215,9 +215,9 @@ describe('Response', function () { var thrown = false; - var handler = function (request, reply) { + const handler = function (request, reply) { - var badName = decodeURIComponent('%E0%B4%8Aset-cookie:%20foo=bar'); + const badName = decodeURIComponent('%E0%B4%8Aset-cookie:%20foo=bar'); try { return reply('ok').header(badName, 'value'); } @@ -227,7 +227,7 @@ describe('Response', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -241,7 +241,7 @@ describe('Response', function () { var thrown = false; - var handler = function (request, reply) { + const handler = function (request, reply) { try { return reply('ok').header('set-cookie', new Buffer(decodeURIComponent('%E0%B4%8Aset-cookie:%20foo=bar'))); @@ -252,7 +252,7 @@ describe('Response', function () { } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -267,12 +267,12 @@ describe('Response', function () { it('returns a stream reply (created)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1 }).created('/special'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); @@ -288,12 +288,12 @@ describe('Response', function () { it('returns error on created with GET', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().created('/something'); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -309,12 +309,12 @@ describe('Response', function () { it('returns an error on bad cookie', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').state(';sid', 'abcdefg123456'); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -333,12 +333,12 @@ describe('Response', function () { it('allows options', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().unstate('session', { path: '/unset', isSecure: true }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -355,12 +355,12 @@ describe('Response', function () { it('sets Vary header with single value', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('x'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -375,12 +375,12 @@ describe('Response', function () { it('sets Vary header with multiple values', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('x').vary('y'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -395,12 +395,12 @@ describe('Response', function () { it('sets Vary header with *', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('*'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -415,12 +415,12 @@ describe('Response', function () { it('leaves Vary header with * on additional values', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('*').vary('x'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -435,12 +435,12 @@ describe('Response', function () { it('drops other Vary header values when set to *', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('x').vary('*'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -455,12 +455,12 @@ describe('Response', function () { it('sets Vary header with multiple similar and identical values', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').vary('x').vary('xyz').vary('xy').vary('x'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -478,12 +478,12 @@ describe('Response', function () { it('sets etag', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').etag('abc'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -496,12 +496,12 @@ describe('Response', function () { it('sets weak etag', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').etag('abc', { weak: true }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -514,13 +514,13 @@ describe('Response', function () { it('ignores varyEtag when etag header is removed', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var response = reply('ok').etag('abc').vary('x'); + const response = reply('ok').etag('abc').vary('x'); delete response.headers.etag; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -533,12 +533,12 @@ describe('Response', function () { it('leaves etag header when varyEtag is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').etag('abc', { vary: false }).vary('x'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res1) { @@ -557,14 +557,14 @@ describe('Response', function () { it('applies varyEtag when returning 304 due to if-modified-since match', function (done) { - var mdate = new Date().toUTCString(); + const mdate = new Date().toUTCString(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').etag('abc').header('last-modified', mdate); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ url: '/', headers: { 'if-modified-since': mdate, 'accept-encoding': 'gzip' } }, function (res) { @@ -580,7 +580,7 @@ describe('Response', function () { it('passes stream headers and code through', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this.statusCode = 299; @@ -600,12 +600,12 @@ describe('Response', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -620,7 +620,7 @@ describe('Response', function () { it('excludes stream headers and code when passThrough is false', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this.statusCode = 299; @@ -640,12 +640,12 @@ describe('Response', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()).passThrough(false); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -660,7 +660,7 @@ describe('Response', function () { it('ignores stream headers when empty', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this.statusCode = 299; @@ -680,12 +680,12 @@ describe('Response', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -700,7 +700,7 @@ describe('Response', function () { it('retains local headers with stream headers pass-through', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this.headers = { xcustom: 'some value', 'set-cookie': 'a=1' }; @@ -719,12 +719,12 @@ describe('Response', function () { this.push(null); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()).header('xcustom', 'other value').state('b', '2'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -742,12 +742,12 @@ describe('Response', function () { it('errors when called on wrong type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('x').replacer(['x']); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -762,12 +762,12 @@ describe('Response', function () { it('errors when called on wrong type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('x').spaces(2); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -782,12 +782,12 @@ describe('Response', function () { it('errors when called on wrong type', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('x').suffix('x'); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -802,10 +802,10 @@ describe('Response', function () { it('returns a file in the response with the correct headers using custom mime type', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file('../LICENSE').type('application/example'); }; @@ -824,12 +824,12 @@ describe('Response', function () { it('returns a redirection reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Please wait while we send your elsewhere').redirect('/example'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -844,12 +844,12 @@ describe('Response', function () { it('returns a redirection reply using verbose call', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('We moved!').redirect().location('/examplex'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -865,12 +865,12 @@ describe('Response', function () { it('returns a 301 redirection reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').permanent().rewritable(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -883,12 +883,12 @@ describe('Response', function () { it('returns a 302 redirection reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').temporary().rewritable(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -901,12 +901,12 @@ describe('Response', function () { it('returns a 307 redirection reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').temporary().rewritable(false); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -919,12 +919,12 @@ describe('Response', function () { it('returns a 308 redirection reply', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').permanent().rewritable(false); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -937,12 +937,12 @@ describe('Response', function () { it('returns a 301 redirection reply (reveresed methods)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').rewritable().permanent(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -955,12 +955,12 @@ describe('Response', function () { it('returns a 302 redirection reply (reveresed methods)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').rewritable().temporary(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -973,12 +973,12 @@ describe('Response', function () { it('returns a 307 redirection reply (reveresed methods)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').rewritable(false).temporary(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -991,12 +991,12 @@ describe('Response', function () { it('returns a 308 redirection reply (reveresed methods)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').rewritable(false).permanent(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -1009,12 +1009,12 @@ describe('Response', function () { it('returns a 302 redirection reply (flip flop)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().redirect('example').permanent().temporary(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); @@ -1030,12 +1030,12 @@ describe('Response', function () { it('handles promises that resolve', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Bluebird.resolve('promised response')).code(201); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1049,12 +1049,12 @@ describe('Response', function () { it('handles promises that resolve (object)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Bluebird.resolve({ status: 'ok' })).code(201); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1068,12 +1068,12 @@ describe('Response', function () { it('handles promises that resolve (response object)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Bluebird.resolve(request.generateResponse({ status: 'ok' }).code(201))); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1087,15 +1087,15 @@ describe('Response', function () { it('handles promises that reject', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var promise = Bluebird.reject(Boom.forbidden('this is not allowed!')); + const promise = Bluebird.reject(Boom.forbidden('this is not allowed!')); promise.catch(Hoek.ignore); return reply(promise).code(299); // Code ignored }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1112,7 +1112,7 @@ describe('Response', function () { it('emits request-error when view file for handler not found', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.register(Vision, Hoek.ignore); server.connection(); @@ -1143,12 +1143,12 @@ describe('Response', function () { it('returns a formatted response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { json: { replacer: ['a'], space: 4, suffix: '\n' } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1161,12 +1161,12 @@ describe('Response', function () { it('returns a response with options', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }).type('application/x-test').spaces(2).replacer(['a']).suffix('\n'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1180,12 +1180,12 @@ describe('Response', function () { it('returns a response with options (different order)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }).type('application/x-test').replacer(['a']).suffix('\n').spaces(2); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1199,14 +1199,14 @@ describe('Response', function () { it('captures object which cannot be stringify', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var obj = {}; + const obj = {}; obj.a = obj; return reply(obj); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1222,7 +1222,7 @@ describe('Response', function () { it('peeks into the response stream', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); var output = ''; @@ -1231,7 +1231,7 @@ describe('Response', function () { path: '/', handler: function (request, reply) { - var response = reply('1234567890'); + const response = reply('1234567890'); response.on('peek', function (chunk) { @@ -1258,17 +1258,17 @@ describe('Response', function () { it('calls custom close processor', function (done) { var closed = false; - var close = function (response) { + const close = function (response) { closed = true; }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.generateResponse(null, { close: close })); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); diff --git a/test/route.js b/test/route.js index 19fa7a35b..d6834756b 100755 --- a/test/route.js +++ b/test/route.js @@ -2,25 +2,25 @@ // Load modules -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Joi = require('joi'); -var Lab = require('lab'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Joi = require('joi'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Route', function () { @@ -29,7 +29,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', handler: function () { } }); }).to.throw('Route missing path'); @@ -40,7 +40,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.route({ method: 'GET', path: '/dork', handler: function () { } }); }).to.throw('Cannot add a route without any connections'); done(); @@ -50,7 +50,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/', handler: function () { } }); }).to.throw(/"method" is required/); @@ -61,7 +61,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: '"GET"', path: '/', handler: function () { } }); }).to.throw(/Invalid method name/); @@ -72,7 +72,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'HEAD', path: '/', handler: function () { } }); }).to.throw(/Method name not allowed/); @@ -83,7 +83,7 @@ describe('Route', function () { expect(function () { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ path: '/test', method: 'put' }); }).to.throw('Missing or undefined handler: put /test'); @@ -92,7 +92,7 @@ describe('Route', function () { it('throws when handler is missing in config', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -103,7 +103,7 @@ describe('Route', function () { it('throws when path has trailing slash and server set to strip', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); expect(function () { @@ -114,7 +114,7 @@ describe('Route', function () { it('allows / when path has trailing slash and server set to strip', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); expect(function () { @@ -125,12 +125,12 @@ describe('Route', function () { it('sets route plugins and app settings', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.route.settings.app.x + request.route.settings.plugins.x.y); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler, app: { x: 'o' }, plugins: { x: { y: 'k' } } } }); server.inject('/', function (res) { @@ -142,7 +142,7 @@ describe('Route', function () { it('throws when validation is set without payload parsing', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -153,7 +153,7 @@ describe('Route', function () { it('throws when validation is set on GET', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -164,7 +164,7 @@ describe('Route', function () { it('throws when payload parsing is set on GET', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); expect(function () { @@ -175,12 +175,12 @@ describe('Route', function () { it('ignores validation on * route when request is GET', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: '*', path: '/', handler: handler, config: { validate: { payload: { a: Joi.required() } } } }); server.inject('/', function (res) { @@ -192,12 +192,12 @@ describe('Route', function () { it('ignores default validation on GET', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { validate: { payload: { a: Joi.required() } } } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -209,9 +209,9 @@ describe('Route', function () { it('shallow copies route config bind', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var context = { key: 'is ' }; + const context = { key: 'is ' }; var count = 0; Object.defineProperty(context, 'test', { @@ -223,7 +223,7 @@ describe('Route', function () { } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(this.key + (this === context)); }; @@ -239,9 +239,9 @@ describe('Route', function () { it('shallow copies route config bind (server.bind())', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var context = { key: 'is ' }; + const context = { key: 'is ' }; var count = 0; Object.defineProperty(context, 'test', { @@ -253,7 +253,7 @@ describe('Route', function () { } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(this.key + (this === context)); }; @@ -270,8 +270,8 @@ describe('Route', function () { it('shallow copies route config bind (connection defaults)', function (done) { - var server = new Hapi.Server(); - var context = { key: 'is ' }; + const server = new Hapi.Server(); + const context = { key: 'is ' }; var count = 0; Object.defineProperty(context, 'test', { @@ -283,7 +283,7 @@ describe('Route', function () { } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(this.key + (this === context)); }; @@ -300,7 +300,7 @@ describe('Route', function () { it('shallow copies route config bind (server defaults)', function (done) { - var context = { key: 'is ' }; + const context = { key: 'is ' }; var count = 0; Object.defineProperty(context, 'test', { @@ -312,12 +312,12 @@ describe('Route', function () { } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(this.key + (this === context)); }; - var server = new Hapi.Server({ connections: { routes: { bind: context } } }); + const server = new Hapi.Server({ connections: { routes: { bind: context } } }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject('/', function (res) { @@ -330,10 +330,10 @@ describe('Route', function () { it('overrides server relativeTo', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file('../package.json'); }; @@ -349,7 +349,7 @@ describe('Route', function () { it('throws when server timeout is more then socket timeout', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ routes: { timeout: { server: 60000, socket: 12000 } } }); @@ -359,7 +359,7 @@ describe('Route', function () { it('throws when server timeout is more then socket timeout (node default)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ routes: { timeout: { server: 6000000 } } }); @@ -369,7 +369,7 @@ describe('Route', function () { it('ignores large server timeout when socket timeout disabled', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ routes: { timeout: { server: 6000000, socket: false } } }); @@ -379,7 +379,7 @@ describe('Route', function () { it('overrides qs settings', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', @@ -408,7 +408,7 @@ describe('Route', function () { it('combine connection extensions (route last)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -464,7 +464,7 @@ describe('Route', function () { it('combine connection extensions (route first)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ @@ -521,7 +521,7 @@ describe('Route', function () { it('combine connection extensions (route middle)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onRequest', function (request, reply) { @@ -577,7 +577,7 @@ describe('Route', function () { it('combine connection extensions (mixed sources)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreAuth', function (request, reply) { diff --git a/test/security.js b/test/security.js index ac5a27eca..d5a07f788 100755 --- a/test/security.js +++ b/test/security.js @@ -2,33 +2,33 @@ // Load modules -var Code = require('code'); -var Hapi = require('..'); -var Joi = require('joi'); -var Lab = require('lab'); +const Code = require('code'); +const Hapi = require('..'); +const Joi = require('joi'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('security', function () { it('blocks response splitting through the request.create method', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var createItemHandler = function (request, reply) { + const createItemHandler = function (request, reply) { return reply('Moved').created('/item/' + request.payload.name); }; @@ -48,12 +48,12 @@ describe('security', function () { it('prevents xss with invalid content types', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Success'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('encoded', { encoding: 'iron' }); server.route({ method: 'POST', path: '/', handler: handler }); @@ -73,12 +73,12 @@ describe('security', function () { it('prevents xss with invalid cookie values in the request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Success'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('encoded', { encoding: 'iron' }); server.route({ method: 'POST', path: '/', handler: handler }); @@ -98,12 +98,12 @@ describe('security', function () { it('prevents xss with invalid cookie name in the request', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Success'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('encoded', { encoding: 'iron' }); server.route({ method: 'POST', path: '/', handler: handler }); @@ -123,7 +123,7 @@ describe('security', function () { it('prevents xss in path validation response message', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('encoded', { encoding: 'iron' }); @@ -151,7 +151,7 @@ describe('security', function () { it('prevents xss in payload validation response message', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/fail/payload', handler: function (request, reply) { @@ -179,7 +179,7 @@ describe('security', function () { it('prevents xss in query validation response message', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/fail/query', handler: function (request, reply) { diff --git a/test/server.js b/test/server.js index 0d15e3599..815dff929 100755 --- a/test/server.js +++ b/test/server.js @@ -2,30 +2,30 @@ // Load modules -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Lab = require('lab'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('Server', function () { it('sets connections defaults', function (done) { - var server = new Hapi.Server({ connections: { app: { message: 'test defaults' } } }); + const server = new Hapi.Server({ connections: { app: { message: 'test defaults' } } }); server.connection(); expect(server.connections[0].settings.app.message).to.equal('test defaults'); done(); @@ -33,7 +33,7 @@ describe('Server', function () { it('overrides mime settings', function (done) { - var options = { + const options = { mime: { override: { 'node/module': { @@ -46,7 +46,7 @@ describe('Server', function () { } }; - var server = new Hapi.Server(options); + const server = new Hapi.Server(options); expect(server.mime.path('file.npm').type).to.equal('node/module'); expect(server.mime.path('file.npm').source).to.equal('steve'); done(); @@ -56,7 +56,7 @@ describe('Server', function () { it('starts and stops', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); server.connection({ labels: ['s2', 'a', 'test'] }); server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); @@ -100,7 +100,7 @@ describe('Server', function () { it('initializes, starts, and stops', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); server.connection({ labels: ['s2', 'a', 'test'] }); server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); @@ -149,13 +149,13 @@ describe('Server', function () { it('returns connection start error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { expect(err).to.not.exist(); - var port = server.info.port; + const port = server.info.port; server.connection({ port: port }); server.connection({ port: port }); @@ -174,7 +174,7 @@ describe('Server', function () { it('returns onPostStart error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPostStart', function (srv, next) { @@ -192,7 +192,7 @@ describe('Server', function () { it('errors on bad cache start', function (done) { - var cache = { + const cache = { engine: { start: function (callback) { @@ -202,7 +202,7 @@ describe('Server', function () { } }; - var server = new Hapi.Server({ cache: cache }); + const server = new Hapi.Server({ cache: cache }); server.connection(); server.start(function (err) { @@ -213,7 +213,7 @@ describe('Server', function () { it('fails to start server without connections', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.start(function (err) { expect(err).to.exist(); @@ -224,10 +224,10 @@ describe('Server', function () { it('fails to start server when registration incomplete', function (done) { - var plugin = function () { }; + const plugin = function () { }; plugin.attributes = { name: 'plugin' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.register(plugin, Hoek.ignore); server.start(function (err) { @@ -240,7 +240,7 @@ describe('Server', function () { it('fails to start when no callback is passed', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { @@ -251,10 +251,10 @@ describe('Server', function () { it('fails to initialize server when not stopped', function (done) { - var plugin = function () { }; + const plugin = function () { }; plugin.attributes = { name: 'plugin' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(function (err) { @@ -269,10 +269,10 @@ describe('Server', function () { it('fails to start server when starting', function (done) { - var plugin = function () { }; + const plugin = function () { }; plugin.attributes = { name: 'plugin' }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.start(Hoek.ignore); server.start(function (err) { @@ -288,9 +288,9 @@ describe('Server', function () { it('stops the cache', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var cache = server.cache({ segment: 'test', expiresIn: 1000 }); + const cache = server.cache({ segment: 'test', expiresIn: 1000 }); server.initialize(function (err) { expect(err).to.not.exist(); @@ -316,7 +316,7 @@ describe('Server', function () { it('returns an extension error (onPreStop)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPreStop', function (srv, next) { @@ -336,7 +336,7 @@ describe('Server', function () { it('returns an extension error (onPostStop)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.ext('onPostStop', function (srv, next) { @@ -356,7 +356,7 @@ describe('Server', function () { it('returns a connection stop error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.connections[0]._stop = function (options, next) { @@ -376,7 +376,7 @@ describe('Server', function () { it('errors when stopping a stopping server', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.stop(Hoek.ignore); @@ -393,9 +393,9 @@ describe('Server', function () { it('returns a server with only the selected connection', function (done) { - var server = new Hapi.Server(); - var p1 = server.connection({ port: 1 }); - var p2 = server.connection({ port: 2 }); + const server = new Hapi.Server(); + const p1 = server.connection({ port: 1 }); + const p2 = server.connection({ port: 2 }); expect(server.connections.length).to.equal(2); expect(p1.connections.length).to.equal(1); @@ -407,7 +407,7 @@ describe('Server', function () { it('throws on invalid config', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(function () { server.connection({ something: false }); @@ -417,7 +417,7 @@ describe('Server', function () { it('combines configuration from server and connection (cors)', function (done) { - var server = new Hapi.Server({ connections: { routes: { cors: true } } }); + const server = new Hapi.Server({ connections: { routes: { cors: true } } }); server.connection({ routes: { cors: { origin: ['example.com'] } } }); expect(server.connections[0].settings.routes.cors.origin).to.deep.equal(['example.com']); done(); @@ -425,7 +425,7 @@ describe('Server', function () { it('combines configuration from server and connection (security)', function (done) { - var server = new Hapi.Server({ connections: { routes: { security: { hsts: 1, xss: false } } } }); + const server = new Hapi.Server({ connections: { routes: { security: { hsts: 1, xss: false } } } }); server.connection({ routes: { security: { hsts: 2 } } }); expect(server.connections[0].settings.routes.security.hsts).to.equal(2); expect(server.connections[0].settings.routes.security.xss).to.be.false(); @@ -435,7 +435,7 @@ describe('Server', function () { it('decorates and clears single connection shortcuts', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); expect(server.info).to.not.exist(); server.connection(); expect(server.info).to.exist(); @@ -450,12 +450,12 @@ describe('Server', function () { it('measures loop delay', function (done) { - var server = new Hapi.Server({ load: { sampleInterval: 4 } }); + const server = new Hapi.Server({ load: { sampleInterval: 4 } }); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { - var start = Date.now(); + const start = Date.now(); while (Date.now() - start < 5) { } return reply('ok'); }; diff --git a/test/state.js b/test/state.js index def9cca42..94f1a3cc0 100755 --- a/test/state.js +++ b/test/state.js @@ -2,34 +2,34 @@ // Load modules -var Code = require('code'); -var Hapi = require('..'); -var Lab = require('lab'); +const Code = require('code'); +const Hapi = require('..'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('state', function () { it('parses cookies', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.state); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ method: 'GET', url: '/', headers: { cookie: 'v=a' } }, function (res) { @@ -42,12 +42,12 @@ describe('state', function () { it('skips parsing cookies', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.state); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { state: { parse: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); server.inject({ method: 'GET', url: '/', headers: { cookie: 'v=a' } }, function (res) { @@ -60,7 +60,7 @@ describe('state', function () { it('does not clear invalid cookie if cannot parse', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('vab', { encoding: 'base64json', clearInvalid: true }); server.inject({ method: 'GET', url: '/', headers: { cookie: 'vab' } }, function (res) { @@ -73,13 +73,13 @@ describe('state', function () { it('ignores invalid cookies (state level config)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var log = request.getLog('state'); + const log = request.getLog('state'); return reply(log.length); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('a', { ignoreErrors: true, encoding: 'base64json' }); server.route({ path: '/', method: 'GET', handler: handler }); @@ -93,13 +93,13 @@ describe('state', function () { it('ignores invalid cookies (header)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var log = request.getLog('state'); + const log = request.getLog('state'); return reply(log.length); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { state: { failAction: 'ignore' } } }); server.route({ path: '/', method: 'GET', handler: handler }); server.inject({ method: 'GET', url: '/', headers: { cookie: 'a=x;;' } }, function (res) { @@ -112,13 +112,13 @@ describe('state', function () { it('logs invalid cookie (value)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var log = request.getLog('state'); + const log = request.getLog('state'); return reply(log.length); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { state: { failAction: 'log' } } }); server.state('a', { encoding: 'base64json', clearInvalid: true }); server.route({ path: '/', method: 'GET', handler: handler }); @@ -132,12 +132,12 @@ describe('state', function () { it('clears invalid cookies (state level config)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('a', { ignoreErrors: true, encoding: 'base64json', clearInvalid: true }); server.route({ path: '/', method: 'GET', handler: handler }); @@ -151,12 +151,12 @@ describe('state', function () { it('sets cookie value automatically', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.state('always', { autoValue: 'present' }); @@ -171,12 +171,12 @@ describe('state', function () { it('appends handler set-cookie to server state', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().header('set-cookie', ['onecookie=yes', 'twocookie=no']); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.state('always', { autoValue: 'present' }); @@ -191,17 +191,17 @@ describe('state', function () { it('sets cookie value automatically using function', function (done) { - var present = function (request, next) { + const present = function (request, next) { return next(null, request.params.x); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/{x}', handler: handler }); server.state('always', { autoValue: present }); @@ -216,17 +216,17 @@ describe('state', function () { it('fails to set cookie value automatically using function', function (done) { - var present = function (request, next) { + const present = function (request, next) { return next(new Error()); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); server.state('always', { autoValue: present }); @@ -241,12 +241,12 @@ describe('state', function () { it('sets cookie value with null ttl', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').state('a', 'b'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.state('a', { ttl: null }); server.route({ method: 'GET', path: '/', handler: handler }); diff --git a/test/transmit.js b/test/transmit.js index 16eede88a..ef527c650 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -2,33 +2,33 @@ // Load modules -var ChildProcess = require('child_process'); -var Fs = require('fs'); -var Http = require('http'); -var Path = require('path'); -var Stream = require('stream'); -var Zlib = require('zlib'); -var Boom = require('boom'); -var CatboxMemory = require('catbox-memory'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Lab = require('lab'); -var Wreck = require('wreck'); +const ChildProcess = require('child_process'); +const Fs = require('fs'); +const Http = require('http'); +const Path = require('path'); +const Stream = require('stream'); +const Zlib = require('zlib'); +const Boom = require('boom'); +const CatboxMemory = require('catbox-memory'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Lab = require('lab'); +const Wreck = require('wreck'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('transmission', function () { @@ -37,7 +37,7 @@ describe('transmission', function () { it('returns valid http date responses in last-modified header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -52,7 +52,7 @@ describe('transmission', function () { it('returns 200 if if-modified-since is invalid', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -66,9 +66,9 @@ describe('transmission', function () { it('returns 200 if last-modified is invalid', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').header('last-modified', 'some crap'); }; @@ -84,7 +84,7 @@ describe('transmission', function () { it('closes file handlers when not reading file stream', { skip: process.platform === 'win32' }, function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -94,7 +94,7 @@ describe('transmission', function () { server.inject({ url: '/file', headers: { 'if-modified-since': res1.headers.date } }, function (res2) { expect(res2.statusCode).to.equal(304); - var cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); + const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); var lsof = ''; cmd.stdout.on('data', function (buffer) { @@ -104,7 +104,7 @@ describe('transmission', function () { cmd.stdout.on('end', function () { var count = 0; - var lines = lsof.split('\n'); + const lines = lsof.split('\n'); for (var i = 0, il = lines.length; i < il; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -120,10 +120,10 @@ describe('transmission', function () { it('closes file handlers when not using a manually open file stream', { skip: process.platform === 'win32' }, function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Fs.createReadStream(__dirname + '/../package.json')).header('etag', 'abc'); }; @@ -135,7 +135,7 @@ describe('transmission', function () { server.inject({ url: '/file', headers: { 'if-none-match': res1.headers.etag } }, function (res2) { expect(res2.statusCode).to.equal(304); - var cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); + const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); var lsof = ''; cmd.stdout.on('data', function (buffer) { @@ -145,7 +145,7 @@ describe('transmission', function () { cmd.stdout.on('end', function () { var count = 0; - var lines = lsof.split('\n'); + const lines = lsof.split('\n'); for (var i = 0, il = lines.length; i < il; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -161,14 +161,14 @@ describe('transmission', function () { it('returns a 304 when the request has if-modified-since and the response has not been modified since (larger)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); server.inject('/file', function (res1) { - var last = new Date(Date.parse(res1.headers['last-modified']) + 1000); + const last = new Date(Date.parse(res1.headers['last-modified']) + 1000); server.inject({ url: '/file', headers: { 'if-modified-since': last.toUTCString() } }, function (res2) { expect(res2.statusCode).to.equal(304); @@ -182,7 +182,7 @@ describe('transmission', function () { it('returns a 304 when the request has if-modified-since and the response has not been modified since (equal)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -202,7 +202,7 @@ describe('transmission', function () { it('matches etag with content-encoding', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/', handler: { file: __dirname + '/../package.json' } }); @@ -221,8 +221,8 @@ describe('transmission', function () { expect(res2.headers.etag).to.exist(); expect(res2.headers.etag).to.not.contain('-'); - var baseTag = res2.headers.etag.slice(0, -1); - var gzipTag = baseTag + '-gzip"'; + const baseTag = res2.headers.etag.slice(0, -1); + const gzipTag = baseTag + '-gzip"'; // Conditional request @@ -278,10 +278,10 @@ describe('transmission', function () { it('returns 304 when manually set to 304', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().code(304); }; @@ -297,9 +297,9 @@ describe('transmission', function () { it('returns a stream reply with custom response headers', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var HeadersStream = function () { + const HeadersStream = function () { Stream.Readable.call(this); this.headers = { custom: 'header' }; @@ -321,7 +321,7 @@ describe('transmission', function () { return reply(new HeadersStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', handler: handler }); @@ -335,9 +335,9 @@ describe('transmission', function () { it('returns a stream reply with custom response status code', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var HeadersStream = function () { + const HeadersStream = function () { Stream.Readable.call(this); this.statusCode = 201; @@ -359,7 +359,7 @@ describe('transmission', function () { return reply(new HeadersStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', handler: handler }); @@ -372,12 +372,12 @@ describe('transmission', function () { it('returns an JSONP response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'value' }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -392,12 +392,12 @@ describe('transmission', function () { it('returns an JSONP response (no charset)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'value' }).charset(''); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -412,12 +412,12 @@ describe('transmission', function () { it('returns a X-Content-Type-Options: nosniff header on JSONP responses', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'value' }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -431,12 +431,12 @@ describe('transmission', function () { it('returns a normal response when JSONP enabled but not requested', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'value' }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -449,13 +449,13 @@ describe('transmission', function () { it('returns an JSONP response with compression', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var parts = request.params.name.split('/'); + const parts = request.params.name.split('/'); return reply({ first: parts[0], last: parts[1] }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -482,12 +482,12 @@ describe('transmission', function () { it('returns an JSONP response when response is a buffer', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new Buffer('value')); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -501,12 +501,12 @@ describe('transmission', function () { it('returns response on bad JSONP parameter', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'value' }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -520,12 +520,12 @@ describe('transmission', function () { it('returns an JSONP handler error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest('wrong')); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -539,12 +539,12 @@ describe('transmission', function () { it('returns an JSONP state error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); @@ -566,7 +566,7 @@ describe('transmission', function () { it('sets caching headers', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/public/{path*}', config: { cache: { privacy: 'public', expiresIn: 24 * 60 * 60 * 1000 } }, handler: { directory: { path: __dirname, listing: false, index: false } } }); @@ -584,10 +584,10 @@ describe('transmission', function () { it('sends empty payload on 204', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').code(204); }; @@ -603,10 +603,10 @@ describe('transmission', function () { it('sends 204 on empty payload', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { response: { emptyStatusCode: 204 } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; @@ -622,12 +622,12 @@ describe('transmission', function () { it('does not send 204 for chunked transfer payloads', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { response: { emptyStatusCode: 204 } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -640,7 +640,7 @@ describe('transmission', function () { this.push(null); }; - var stream = new TestStream(); + const stream = new TestStream(); return reply(stream); }; @@ -655,10 +655,10 @@ describe('transmission', function () { it('skips compression on empty', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply().type('text/html'); }; @@ -675,12 +675,12 @@ describe('transmission', function () { it('does not skip compression for chunked transfer payloads', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -693,7 +693,7 @@ describe('transmission', function () { this.push(null); }; - var stream = new TestStream(); + const stream = new TestStream(); return reply(stream).type('text/html'); }; @@ -708,10 +708,10 @@ describe('transmission', function () { it('sets vary header when accept-encoding is present but does not match', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('abc'); }; @@ -727,9 +727,9 @@ describe('transmission', function () { it('handles stream errors on the response after the response has been piped', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); }; @@ -738,7 +738,7 @@ describe('transmission', function () { TestStream.prototype._read = function (size) { - var self = this; + const self = this; if (this.isDone) { return; @@ -753,11 +753,11 @@ describe('transmission', function () { }); }; - var stream = new TestStream(); + const stream = new TestStream(); return reply(stream); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -770,7 +770,7 @@ describe('transmission', function () { it('matches etag header list value', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -793,7 +793,7 @@ describe('transmission', function () { it('changes etag when content encoding is used', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -821,10 +821,10 @@ describe('transmission', function () { it('returns a gzipped file in the response when the request accepts gzip', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file(__dirname + '/../package.json'); }; @@ -843,10 +843,10 @@ describe('transmission', function () { it('returns a plain file when not compressible', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file(__dirname + '/file/image.png'); }; @@ -865,10 +865,10 @@ describe('transmission', function () { it('returns a plain file when compression disabled', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } }, compression: false }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file(__dirname + '/../package.json'); }; @@ -886,10 +886,10 @@ describe('transmission', function () { it('returns a deflated file in the response when the request accepts deflate', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply.file(__dirname + '/../package.json'); }; @@ -908,12 +908,12 @@ describe('transmission', function () { it('returns a gzipped stream reply without a content-length header when accept-encoding is gzip', function (done) { - var streamHandler = function (request, reply) { + const streamHandler = function (request, reply) { return reply(new internals.TimerStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', handler: streamHandler }); @@ -927,12 +927,12 @@ describe('transmission', function () { it('returns a deflated stream reply without a content-length header when accept-encoding is deflate', function (done) { - var streamHandler = function (request, reply) { + const streamHandler = function (request, reply) { return reply(new internals.TimerStream()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', handler: streamHandler }); @@ -946,12 +946,12 @@ describe('transmission', function () { it('returns a gzip response on a post request when accept-encoding: gzip is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -961,7 +961,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -977,12 +977,12 @@ describe('transmission', function () { it('returns a gzip response on a get request when accept-encoding: gzip is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -992,7 +992,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -1008,12 +1008,12 @@ describe('transmission', function () { it('returns a gzip response on a post request when accept-encoding: * is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1023,7 +1023,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.post(uri, { headers: { 'accept-encoding': '*' }, payload: data }, function (err, res, body) { @@ -1036,12 +1036,12 @@ describe('transmission', function () { it('returns a gzip response on a get request when accept-encoding: * is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -1051,7 +1051,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.get(uri, { headers: { 'accept-encoding': '*' } }, function (err, res, body) { @@ -1064,11 +1064,11 @@ describe('transmission', function () { it('returns a deflate response on a post request when accept-encoding: deflate is requested', function (done) { - var data = '{"test":"true"}'; - var server = new Hapi.Server(); + const data = '{"test":"true"}'; + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1078,7 +1078,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.deflate(new Buffer(data), function (err, deflated) { @@ -1094,11 +1094,11 @@ describe('transmission', function () { it('returns a deflate response on a get request when accept-encoding: deflate is requested', function (done) { - var data = '{"test":"true"}'; - var server = new Hapi.Server(); + const data = '{"test":"true"}'; + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -1108,7 +1108,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.deflate(new Buffer(data), function (err, deflated) { @@ -1124,12 +1124,12 @@ describe('transmission', function () { it('returns a gzip response on a post request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1139,7 +1139,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -1155,12 +1155,12 @@ describe('transmission', function () { it('returns a gzip response on a get request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -1170,7 +1170,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -1186,12 +1186,12 @@ describe('transmission', function () { it('returns a deflate response on a post request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1201,7 +1201,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.deflate(new Buffer(data), function (err, deflated) { @@ -1217,12 +1217,12 @@ describe('transmission', function () { it('returns a deflate response on a get request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -1232,7 +1232,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.deflate(new Buffer(data), function (err, deflated) { @@ -1248,12 +1248,12 @@ describe('transmission', function () { it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1263,7 +1263,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -1279,12 +1279,12 @@ describe('transmission', function () { it('returns a gzip response on a get request when accept-encoding: deflate, gzip is requested', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(data); }; @@ -1294,7 +1294,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Zlib.gzip(new Buffer(data), function (err, zipped) { @@ -1310,12 +1310,12 @@ describe('transmission', function () { it('returns an identity response on a post request when accept-encoding is missing', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(request.payload); }; @@ -1325,7 +1325,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.post(uri, { payload: data }, function (err, res, body) { @@ -1338,9 +1338,9 @@ describe('transmission', function () { it('returns an identity response on a get request when accept-encoding is missing', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1355,7 +1355,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.get(uri, {}, function (err, res, body) { @@ -1368,14 +1368,14 @@ describe('transmission', function () { it('returns a gzip response when forced by the handler', function (done) { - var data = '{"test":"true"}'; + const data = '{"test":"true"}'; Zlib.gzip(new Buffer(data), function (err, zipped) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(zipped).type('text/plain').header('content-encoding', 'gzip'); }; @@ -1385,7 +1385,7 @@ describe('transmission', function () { expect(err).to.not.exist(); - var uri = 'http://localhost:' + server.info.port; + const uri = 'http://localhost:' + server.info.port; Wreck.post(uri, { headers: { 'accept-encoding': 'gzip' }, payload: data }, function (err, res, body) { @@ -1400,7 +1400,7 @@ describe('transmission', function () { it('does not open file stream on 304', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' } }); @@ -1427,12 +1427,12 @@ describe('transmission', function () { it('object listeners are maintained after transmission is complete', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1456,7 +1456,7 @@ describe('transmission', function () { it('stops processing the stream when the request closes', function (done) { - var ErrStream = function (request) { + const ErrStream = function (request) { Stream.Readable.call(this); @@ -1467,7 +1467,7 @@ describe('transmission', function () { ErrStream.prototype._read = function (size) { - var self = this; + const self = this; if (this.isDone) { return; @@ -1484,12 +1484,12 @@ describe('transmission', function () { }); }; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new ErrStream(request)).bytes(0); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/stream', handler: handler }); @@ -1502,18 +1502,18 @@ describe('transmission', function () { it('does not truncate the response when stream finishes before response is done', function (done) { - var chunkTimes = 10; - var filePath = __dirname + '/response.js'; - var block = Fs.readFileSync(filePath).toString(); + const chunkTimes = 10; + const filePath = __dirname + '/response.js'; + const block = Fs.readFileSync(filePath).toString(); var expectedBody = ''; for (var i = 0, il = chunkTimes; i < il; ++i) { expectedBody += block; } - var fileHandler = function (request, reply) { + const fileHandler = function (request, reply) { - var fileStream = new Stream.Readable(); + const fileStream = new Stream.Readable(); var readTimes = 0; fileStream._read = function (size) { @@ -1529,7 +1529,7 @@ describe('transmission', function () { return reply(fileStream); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: fileHandler }); server.start(function (err) { @@ -1547,18 +1547,18 @@ describe('transmission', function () { it('does not truncate the response when stream finishes before response is done using https', function (done) { - var chunkTimes = 10; - var filePath = __dirname + '/response.js'; - var block = Fs.readFileSync(filePath).toString(); + const chunkTimes = 10; + const filePath = __dirname + '/response.js'; + const block = Fs.readFileSync(filePath).toString(); var expectedBody = ''; for (var i = 0, il = chunkTimes; i < il; ++i) { expectedBody += block; } - var fileHandler = function (request, reply) { + const fileHandler = function (request, reply) { - var fileStream = new Stream.Readable(); + const fileStream = new Stream.Readable(); var readTimes = 0; fileStream._read = function (size) { @@ -1574,14 +1574,14 @@ describe('transmission', function () { return reply(fileStream); }; - var config = { + const config = { tls: { key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE\nSC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa\nFvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv\nw00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia\nPQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3\nm1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX\n1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7\n/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans\npCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee\nuctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs\nV/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==\n-----END RSA PRIVATE KEY-----\n', cert: '-----BEGIN CERTIFICATE-----\nMIIDBjCCAe4CCQDvLNml6smHlTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJV\nUzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTE0MDEyNTIxMjIxOFoXDTE1MDEyNTIxMjIxOFowRTELMAkG\nA1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\nANFKslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS\nlrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV\nkltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' } }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(config); server.route({ method: 'GET', path: '/', handler: fileHandler }); server.start(function (err) { @@ -1601,15 +1601,15 @@ describe('transmission', function () { var destroyed = false; - var handler = function (request, reply) { + const handler = function (request, reply) { - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); stream._read = function (size) { - var self = this; + const self = this; - var chunk = new Array(size).join('x'); + const chunk = new Array(size).join('x'); if (destroyed) { this.push(chunk); @@ -1632,7 +1632,7 @@ describe('transmission', function () { return reply(stream); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1657,12 +1657,12 @@ describe('transmission', function () { var destroyed = false; - var handler = function (request, reply) { + const handler = function (request, reply) { - var stream = new Stream(); + const stream = new Stream(); stream.readable = true; - var _read = function () { + const _read = function () { setImmediate(function () { @@ -1670,7 +1670,7 @@ describe('transmission', function () { return; } - var chunk = new Array(1024).join('x'); + const chunk = new Array(1024).join('x'); if (destroyed) { stream.emit('data', chunk); @@ -1706,7 +1706,7 @@ describe('transmission', function () { return reply(stream); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1729,9 +1729,9 @@ describe('transmission', function () { it('does not leak stream data when request timeouts before stream drains', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); var count = 0; stream._read = function (size) { @@ -1754,7 +1754,7 @@ describe('transmission', function () { return reply(stream); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 20, socket: 40 }, payload: { timeout: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1774,18 +1774,18 @@ describe('transmission', function () { var clientRequest; - var handler = function (request, reply) { + const handler = function (request, reply) { clientRequest.abort(); - var stream = new Stream.Readable(); + const stream = new Stream.Readable(); var responded = false; stream._read = function (size) { - var self = this; + const self = this; - var chunk = new Array(size).join('x'); + const chunk = new Array(size).join('x'); if (responded) { this.push(chunk); @@ -1811,7 +1811,7 @@ describe('transmission', function () { }, 100); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -1831,9 +1831,9 @@ describe('transmission', function () { it('changes etag when content-encoding set manually', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('x').header('content-encoding', 'gzip').etag('abc'); }; @@ -1851,9 +1851,9 @@ describe('transmission', function () { it('head request retains content-length header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('x').bytes(1); }; @@ -1870,20 +1870,20 @@ describe('transmission', function () { it('does not set accept-encoding multiple times', function (done) { - var headersHandler = function (request, reply) { + const headersHandler = function (request, reply) { reply({ status: 'success' }) .vary('X-Custom3'); }; - var upstream = new Hapi.Server(); + const upstream = new Hapi.Server(); upstream.connection(); upstream.route({ method: 'GET', path: '/headers', handler: headersHandler }); upstream.start(function () { - var proxyHandler = function (request, reply) { + const proxyHandler = function (request, reply) { - var options = {}; + const options = {}; options.headers = Hoek.clone(request.headers); delete options.headers.host; @@ -1893,7 +1893,7 @@ describe('transmission', function () { }); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/headers', handler: proxyHandler }); @@ -1909,15 +1909,15 @@ describe('transmission', function () { describe('response range', function () { - var fileStreamHandler = function (request, reply) { + const fileStreamHandler = function (request, reply) { - var filePath = Path.join(__dirname, 'file', 'image.png'); + const filePath = Path.join(__dirname, 'file', 'image.png'); return reply(Fs.createReadStream(filePath)).bytes(Fs.statSync(filePath).size); }; it('returns a subset of a fileStream (start)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -1934,7 +1934,7 @@ describe('transmission', function () { it('returns a subset of a fileStream (middle)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -1951,7 +1951,7 @@ describe('transmission', function () { it('returns a subset of a fileStream (-to)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -1968,7 +1968,7 @@ describe('transmission', function () { it('returns a subset of a fileStream (from-)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -1985,7 +1985,7 @@ describe('transmission', function () { it('returns a subset of a fileStream (beyond end)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2002,7 +2002,7 @@ describe('transmission', function () { it('returns a subset of a fileStream (if-range)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2025,7 +2025,7 @@ describe('transmission', function () { it('returns 200 on incorrect if-range', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2038,7 +2038,7 @@ describe('transmission', function () { it('returns 416 on invalid range (unit)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2052,7 +2052,7 @@ describe('transmission', function () { it('returns 416 on invalid range (inversed)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2066,7 +2066,7 @@ describe('transmission', function () { it('returns 416 on invalid range (format)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2080,7 +2080,7 @@ describe('transmission', function () { it('returns 416 on invalid range (empty range)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2094,7 +2094,7 @@ describe('transmission', function () { it('returns 200 on multiple ranges', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/file', handler: fileStreamHandler }); @@ -2108,7 +2108,7 @@ describe('transmission', function () { it('returns a subset of a stream', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this._count = -1; @@ -2137,9 +2137,9 @@ describe('transmission', function () { return 10; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()); }; @@ -2159,7 +2159,7 @@ describe('transmission', function () { it('returns a consolidated range', function (done) { - var TestStream = function () { + const TestStream = function () { Stream.Readable.call(this); this._count = -1; @@ -2188,9 +2188,9 @@ describe('transmission', function () { return 10; }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(new TestStream()); }; @@ -2211,10 +2211,10 @@ describe('transmission', function () { it('skips undefined header values', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('ok').header('x', undefined); }; @@ -2233,7 +2233,7 @@ describe('transmission', function () { it('sets max-age value (method and route)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.method('profile', function (id, next) { @@ -2244,7 +2244,7 @@ describe('transmission', function () { }); }, { cache: { expiresIn: 120000, generateTimeout: 10 } }); - var profileHandler = function (request, reply) { + const profileHandler = function (request, reply) { server.methods.profile(0, reply); }; @@ -2264,10 +2264,10 @@ describe('transmission', function () { it('sets max-age value (expiresAt)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(); }; @@ -2287,12 +2287,12 @@ describe('transmission', function () { it('returns no-cache on error', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { expiresIn: 120000 } } }); server.inject('/', function (res) { @@ -2304,12 +2304,12 @@ describe('transmission', function () { it('sets cache-control on error with status override', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(Boom.badRequest()); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { cache: { statuses: [200, 400] } } }); server.route({ method: 'GET', path: '/', config: { handler: handler, cache: { expiresIn: 120000 } } }); server.inject('/', function (res) { @@ -2321,9 +2321,9 @@ describe('transmission', function () { it('does not return max-age value when route is not cached', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var activeItemHandler = function (request, reply) { + const activeItemHandler = function (request, reply) { return reply({ 'id': '55cf687663', @@ -2341,10 +2341,10 @@ describe('transmission', function () { it('caches using non default cache', function (done) { - var server = new Hapi.Server({ cache: { name: 'primary', engine: CatboxMemory } }); + const server = new Hapi.Server({ cache: { name: 'primary', engine: CatboxMemory } }); server.connection(); - var defaults = server.cache({ segment: 'a', expiresIn: 2000 }); - var primary = server.cache({ segment: 'a', expiresIn: 2000, cache: 'primary' }); + const defaults = server.cache({ segment: 'a', expiresIn: 2000 }); + const primary = server.cache({ segment: 'a', expiresIn: 2000, cache: 'primary' }); server.start(function (err) { @@ -2377,13 +2377,13 @@ describe('transmission', function () { it('leaves existing cache-control header', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').code(400) .header('cache-control', 'some value'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2397,12 +2397,12 @@ describe('transmission', function () { it('sets cache-control header from ttl without policy', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').ttl(10000); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2415,12 +2415,12 @@ describe('transmission', function () { it('leaves existing cache-control header (ttl)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').ttl(1000).header('cache-control', 'none'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2434,7 +2434,7 @@ describe('transmission', function () { it('includes caching header with 304', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection(); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' }, config: { cache: { expiresIn: 60000 } } }); @@ -2452,7 +2452,7 @@ describe('transmission', function () { it('forbids caching on 304 if 200 is not included', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { cache: { statuses: [400] } } }); server.route({ method: 'GET', path: '/file', handler: { file: __dirname + '/../package.json' }, config: { cache: { expiresIn: 60000 } } }); @@ -2473,12 +2473,12 @@ describe('transmission', function () { it('does not set security headers by default', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2497,12 +2497,12 @@ describe('transmission', function () { it('returns default security headers when security is true', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: true } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2521,16 +2521,16 @@ describe('transmission', function () { it('does not set default security headers when the route sets security false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var config = { + const config = { security: false }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: true } }); server.route({ method: 'GET', path: '/', handler: handler, config: config }); @@ -2550,12 +2550,12 @@ describe('transmission', function () { it('does not return hsts header when secuirty.hsts is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2575,12 +2575,12 @@ describe('transmission', function () { it('returns only default hsts header when security.hsts is true', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: true } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2595,12 +2595,12 @@ describe('transmission', function () { it('returns correct hsts header when security.hsts is a number', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: 123456789 } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2615,12 +2615,12 @@ describe('transmission', function () { it('returns correct hsts header when security.hsts is an object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: { maxAge: 123456789, includeSubDomains: true } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2635,12 +2635,12 @@ describe('transmission', function () { it('returns the correct hsts header when security.hsts is an object only sepcifying maxAge', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: { maxAge: 123456789 } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2655,12 +2655,12 @@ describe('transmission', function () { it('returns correct hsts header when security.hsts is an object only specifying includeSubdomains', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: { includeSubdomains: true } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2675,12 +2675,12 @@ describe('transmission', function () { it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: { includeSubDomains: true } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2695,12 +2695,12 @@ describe('transmission', function () { it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains and preload', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { hsts: { includeSubDomains: true, preload: true } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2715,12 +2715,12 @@ describe('transmission', function () { it('does not return the xframe header whe security.xframe is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2739,12 +2739,12 @@ describe('transmission', function () { it('returns only default xframe header when security.xframe is true', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: true } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2759,12 +2759,12 @@ describe('transmission', function () { it('returns correct xframe header when security.xframe is a string', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: 'sameorigin' } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2779,12 +2779,12 @@ describe('transmission', function () { it('returns correct xframe header when security.xframe is an object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: { rule: 'allow-from', source: 'http://example.com' } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2799,12 +2799,12 @@ describe('transmission', function () { it('returns correct xframe header when security.xframe is an object', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: { rule: 'deny' } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2819,12 +2819,12 @@ describe('transmission', function () { it('returns sameorigin xframe header when rule is allow-from but source is unspecified', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xframe: { rule: 'allow-from' } } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2839,12 +2839,12 @@ describe('transmission', function () { it('does not set x-download-options if noOpen is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { noOpen: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2859,12 +2859,12 @@ describe('transmission', function () { it('does not set x-content-type-options if noSniff is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { noSniff: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2879,12 +2879,12 @@ describe('transmission', function () { it('does not set the x-xss-protection header when security.xss is false', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('Test'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { security: { xss: false } } }); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2906,12 +2906,12 @@ describe('transmission', function () { it('does not modify content-type header when charset manually set', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').type('text/plain; charset=ISO-8859-1'); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2925,12 +2925,12 @@ describe('transmission', function () { it('does not modify content-type header when charset is unset', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').type('text/plain').charset(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2944,12 +2944,12 @@ describe('transmission', function () { it('does not modify content-type header when charset is unset (default type)', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply('text').charset(); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); @@ -2973,7 +2973,7 @@ Hoek.inherits(internals.TimerStream, Stream.Readable); internals.TimerStream.prototype._read = function (size) { - var self = this; + const self = this; if (this.isDone) { return; diff --git a/test/validation.js b/test/validation.js index 193e7cb3e..84cc41bea 100755 --- a/test/validation.js +++ b/test/validation.js @@ -2,33 +2,33 @@ // Load modules -var Boom = require('boom'); -var Code = require('code'); -var Hapi = require('..'); -var Hoek = require('hoek'); -var Inert = require('inert'); -var Joi = require('joi'); -var Lab = require('lab'); +const Boom = require('boom'); +const Code = require('code'); +const Hapi = require('..'); +const Hoek = require('hoek'); +const Inert = require('inert'); +const Joi = require('joi'); +const Lab = require('lab'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const expect = Code.expect; describe('validation', function () { it('validates valid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -55,7 +55,7 @@ describe('validation', function () { it('validates both params and query', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -86,7 +86,7 @@ describe('validation', function () { it('validates valid input using context', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -128,7 +128,7 @@ describe('validation', function () { it('validates valid input using auth context', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.auth.scheme('none', function (authServer, options) { @@ -188,7 +188,7 @@ describe('validation', function () { it('fails valid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -215,7 +215,7 @@ describe('validation', function () { it('validates valid input with validation options', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection({ routes: { validate: { options: { convert: false } } } }); server.route({ method: 'GET', @@ -242,7 +242,7 @@ describe('validation', function () { it('allows any input when set to null', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -267,7 +267,7 @@ describe('validation', function () { it('validates using custom validation', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -301,7 +301,7 @@ describe('validation', function () { it('catches error thrown in custom validation', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -329,7 +329,7 @@ describe('validation', function () { it('casts input to desired type', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -357,7 +357,7 @@ describe('validation', function () { it('uses original value before schema conversion', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -385,7 +385,7 @@ describe('validation', function () { it('invalidates forbidden input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -410,7 +410,7 @@ describe('validation', function () { it('retains the validation error', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -441,7 +441,7 @@ describe('validation', function () { it('validates valid input (Object root)', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -468,7 +468,7 @@ describe('validation', function () { it('fails on invalid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -500,7 +500,7 @@ describe('validation', function () { it('ignores invalid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -528,13 +528,13 @@ describe('validation', function () { it('logs invalid input', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var item = request.getLog('validation')[0]; + const item = request.getLog('validation')[0]; return reply(item); }; - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -560,7 +560,7 @@ describe('validation', function () { it('replaces error with message on invalid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -592,7 +592,7 @@ describe('validation', function () { it('catches error thrown in failAction', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -623,7 +623,7 @@ describe('validation', function () { it('customizes error on invalid input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -664,7 +664,7 @@ describe('validation', function () { it('fails on invalid payload', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', @@ -696,7 +696,7 @@ describe('validation', function () { it('fails on text input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', @@ -723,7 +723,7 @@ describe('validation', function () { it('fails on null input', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', @@ -751,7 +751,7 @@ describe('validation', function () { it('fails on no payload', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', @@ -783,7 +783,7 @@ describe('validation', function () { it('samples responses', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -803,7 +803,7 @@ describe('validation', function () { }); var count = 0; - var action = function (next) { + const action = function (next) { server.inject('/', function (res) { @@ -823,12 +823,12 @@ describe('validation', function () { it('validates response', function (done) { var i = 0; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -858,12 +858,12 @@ describe('validation', function () { it('validates response with context', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: 'thing', more: 'stuff' }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -895,14 +895,14 @@ describe('validation', function () { it('validates error response', function (done) { var i = 0; - var handler = function (request, reply) { + const handler = function (request, reply) { - var error = Boom.badRequest('Kaboom'); + const error = Boom.badRequest('Kaboom'); error.output.payload.custom = i++; return reply(error); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -936,19 +936,19 @@ describe('validation', function () { it('validates error response and ignore 200', function (done) { var i = 0; - var handler = function (request, reply) { + const handler = function (request, reply) { if (i === 0) { ++i; return reply({ a: 1, b: 2 }); } - var error = Boom.badRequest('Kaboom'); + const error = Boom.badRequest('Kaboom'); error.output.payload.custom = i++; return reply(error); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -986,12 +986,12 @@ describe('validation', function () { it('validates and modifies response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1017,14 +1017,14 @@ describe('validation', function () { it('validates and modifies error response', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { - var error = Boom.badRequest('Kaboom'); + const error = Boom.badRequest('Kaboom'); error.output.payload.custom = '123'; return reply(error); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1055,7 +1055,7 @@ describe('validation', function () { it('validates empty response', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1082,12 +1082,12 @@ describe('validation', function () { it('throws on sample with response modify', function (done) { - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ a: 1, b: 2 }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); expect(function () { @@ -1112,12 +1112,12 @@ describe('validation', function () { it('validates response using custom validation function', function (done) { var i = 0; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1149,12 +1149,12 @@ describe('validation', function () { it('catches error thrown by custom validation function', function (done) { var i = 0; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1179,7 +1179,7 @@ describe('validation', function () { it('skips response validation when sample is zero', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1199,7 +1199,7 @@ describe('validation', function () { }); var count = 0; - var action = function (next) { + const action = function (next) { server.inject('/', function (res) { @@ -1218,7 +1218,7 @@ describe('validation', function () { it('does not delete the response object from the route when sample is 0', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1248,7 +1248,7 @@ describe('validation', function () { it('fails response validation with options', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection({ routes: { response: { options: { convert: false } } } }); server.route({ method: 'GET', @@ -1275,7 +1275,7 @@ describe('validation', function () { it('skips response validation when schema is true', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1300,7 +1300,7 @@ describe('validation', function () { it('skips response validation when status is empty', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1325,7 +1325,7 @@ describe('validation', function () { it('forbids response when schema is false', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1350,7 +1350,7 @@ describe('validation', function () { it('ignores error responses', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1377,7 +1377,7 @@ describe('validation', function () { it('errors on non-plain-object responses', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.register(Inert, Hoek.ignore); server.connection(); server.route({ @@ -1405,7 +1405,7 @@ describe('validation', function () { it('logs invalid responses', function (done) { - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1441,12 +1441,12 @@ describe('validation', function () { it('validates string response', function (done) { var value = 'abcd'; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(value); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1476,12 +1476,12 @@ describe('validation', function () { it('validates boolean response', function (done) { var value = 'abcd'; - var handler = function (request, reply) { + const handler = function (request, reply) { return reply(value); }; - var server = new Hapi.Server({ debug: false }); + const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', @@ -1511,7 +1511,7 @@ describe('validation', function () { it('validates valid header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1531,7 +1531,7 @@ describe('validation', function () { } }); - var settings = { + const settings = { url: '/', method: 'GET', headers: { @@ -1548,7 +1548,7 @@ describe('validation', function () { it('rejects invalid header', function (done) { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1567,7 +1567,7 @@ describe('validation', function () { } }); - var settings = { + const settings = { url: '/', method: 'GET', headers: { @@ -1588,8 +1588,8 @@ internals.times = function (count, method, callback) { var counter = 0; - var results = []; - var done = function (err, result) { + const results = []; + const done = function (err, result) { if (callback) { results.push(result); From 38f90bbd6779d269457917de4908b76c53837645 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 11:24:08 -0700 Subject: [PATCH 0099/1139] Replace var with let. Closes #2874 --- API.md | 322 ++++++++++++++++++++++----------------------- lib/auth.js | 12 +- lib/connection.js | 8 +- lib/cors.js | 10 +- lib/ext.js | 6 +- lib/handler.js | 10 +- lib/methods.js | 28 ++-- lib/plugin.js | 28 ++-- lib/protect.js | 2 +- lib/reply.js | 2 +- lib/request.js | 8 +- lib/response.js | 14 +- lib/route.js | 11 +- lib/server.js | 18 +-- lib/transmit.js | 26 ++-- lib/validation.js | 4 +- test/auth.js | 2 +- test/connection.js | 16 +-- test/handler.js | 6 +- test/methods.js | 26 ++-- test/payload.js | 6 +- test/plugin.js | 50 +++---- test/reply.js | 2 +- test/request.js | 32 ++--- test/response.js | 10 +- test/route.js | 8 +- test/server.js | 8 +- test/transmit.js | 40 +++--- test/validation.js | 22 ++-- 29 files changed, 369 insertions(+), 368 deletions(-) diff --git a/API.md b/API.md index e41a7e34d..88e98a38a 100755 --- a/API.md +++ b/API.md @@ -203,8 +203,8 @@ Note that the `options` object is deeply cloned and cannot contain any values th perform deep copy on. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server({ +const Hapi = require('hapi'); +const server = new Hapi.Server({ cache: require('catbox-redis'), load: { sampleInterval: 1000 @@ -221,11 +221,11 @@ conflicts with the framework internals. The data can be accessed whenever the se accessible. Initialized with an empty object. ```js -var Hapi = require('hapi'); +const Hapi = require('hapi'); server = new Hapi.Server(); server.app.key = 'value'; -var handler = function (request, reply) { +const handler = function (request, reply) { return reply(request.server.app.key); }; @@ -238,13 +238,13 @@ An array containing the server's connections. When the server object is returned matching the selection criteria. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80, labels: 'a' }); server.connection({ port: 8080, labels: 'b' }); // server.connections.length === 2 -var a = server.select('a'); +const a = server.select('a'); // a.connections.length === 1 ``` @@ -277,7 +277,7 @@ When the server contains more than one connection, each [`server.connections`](# array member provides its own `connection.info`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); // server.info.port === 80 @@ -297,8 +297,8 @@ An object containing the process load metrics (when `load.sampleInterval` is ena ```js -var Hapi = require('hapi'); -var server = new Hapi.Server({ load: { sampleInterval: 1000 } }); +const Hapi = require('hapi'); +const server = new Hapi.Server({ load: { sampleInterval: 1000 } }); console.log(server.load.rss); ``` @@ -312,13 +312,13 @@ When the server contains more than one connection, each [`server.connections`](# array member provides its own `connection.listener`. ```js -var Hapi = require('hapi'); -var SocketIO = require('socket.io'); +const Hapi = require('hapi'); +const SocketIO = require('socket.io'); -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var io = SocketIO.listen(server.listener); +const io = SocketIO.listen(server.listener); io.sockets.on('connection', function(socket) { socket.emit({ msg: 'welcome' }); @@ -331,8 +331,8 @@ An object providing access to the [server methods](#servermethodname-method-opti server method name is an object property. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.method('add', function (a, b, next) { @@ -351,9 +351,9 @@ Provides access to the server MIME database used for setting content-type inform must not be modified directly but only through the `mime` server setting. ```js -var Hapi = require('hapi'); +const Hapi = require('hapi'); -var options = { +const options = { mime: { override: { 'node/module': { @@ -366,7 +366,7 @@ var options = { } }; -var server = new Hapi.Server(options); +const server = new Hapi.Server(options); // server.mime.path('code.js').type === 'application/javascript' // server.mime.path('file.npm').type === 'node/module' ``` @@ -450,8 +450,8 @@ The root server object containing all the connections and the root server method The server configuration object after defaults applied. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server({ +const Hapi = require('hapi'); +const server = new Hapi.Server({ app: { key: 'value' } @@ -465,8 +465,8 @@ var server = new Hapi.Server({ The **hapi** module version number. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); // server.version === '8.0.0' ``` @@ -489,7 +489,7 @@ The default auth strategy configuration can be accessed via `connection.auth.set obtain the active authentication configuration of a route, use `connection.auth.lookup(request.route)`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.auth.scheme('custom', scheme); @@ -561,16 +561,16 @@ failed due to bad payload. If the error has no message but includes a scheme nam `auth.payload` configuration is set to `'optional'`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var scheme = function (server, options) { +const scheme = function (server, options) { return { authenticate: function (request, reply) { - var req = request.raw.req; - var authorization = req.headers.authorization; + const req = request.raw.req; + const authorization = req.headers.authorization; if (!authorization) { return reply(Boom.unauthorized(null, 'Custom')); } @@ -596,7 +596,7 @@ Registers an authentication strategy where: - `options` - scheme options based on the scheme requirements. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.auth.scheme('custom', scheme); @@ -630,7 +630,7 @@ also does not perform payload authentication. It is limited to the basic strateg execution. It does not include verifying scope, entity, or other route properties. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.auth.scheme('custom', scheme); @@ -663,14 +663,14 @@ When setting context inside a plugin, the context is applied only to methods set Note that the context applies only to routes and extensions added after it has been set. ```js -var handler = function (request, reply) { +const handler = function (request, reply) { return reply(this.message); }; exports.register = function (server, options, next) { - var bind = { + const bind = { message: 'hello' }; @@ -720,10 +720,10 @@ Provisions a cache segment within the server cache facility where: `false`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); +const cache = server.cache({ segment: 'countries', expiresIn: 60 * 60 * 1000 }); cache.set('norway', { capital: 'oslo' }, null, function (err) { cache.get('norway', function (err, value, cached, log) { @@ -773,11 +773,11 @@ Note that the `options` object is deeply cloned (with the exception of `listener shallowly copied) and cannot contain any values that are unsafe to perform deep copy on. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); -var web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); -var admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); +const web = server.connection({ port: 8000, host: 'example.com', labels: ['web'] }); +const admin = server.connection({ port: 8001, host: 'example.com', labels: ['admin'] }); // server.connections.length === 2 // web.connections.length === 1 @@ -808,7 +808,7 @@ exports.register = function (srv, options, next) { // Use the 'srv' argument to add a new connection - var server = srv.connection(); + const server = srv.connection(); // Use the 'server' return value to manage the new connection @@ -844,8 +844,8 @@ Note that decorations apply to the entire server and all its connections regardl selection. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.decorate('reply', 'success', function () { @@ -885,15 +885,15 @@ plugins (those with `attributes.connections` set to `false`) can only depend on plugins (server initialization will fail even of the dependency is loaded but is not connectionless). ```js -exports.register = function (server, options, next) { +const after = function (server, next) { - server.dependency('yar', after); + // Additional plugin registration logic return next(); }; -var after = function (server, next) { +exports.register = function (server, options, next) { - // Additional plugin registration logic + server.dependency('yar', after); return next(); }; ``` @@ -979,8 +979,8 @@ Registers an extension function in one of the available extension points where: to. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext({ @@ -993,7 +993,7 @@ server.ext({ } }); -var handler = function (request, reply) { +const handler = function (request, reply) { return reply({ status: 'ok' }); }; @@ -1010,8 +1010,8 @@ Registers a single extension event using the same properties as used in [`server.ext(events)`](#serverextevents), but passed as arguments. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onRequest', function (request, reply) { @@ -1021,7 +1021,7 @@ server.ext('onRequest', function (request, reply) { return reply.continue(); }); -var handler = function (request, reply) { +const handler = function (request, reply) { return reply({ status: 'ok' }); }; @@ -1043,8 +1043,8 @@ Registers a new handler type to be used in routes where: - `options` - the configuration object provided in the handler config. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ host: 'localhost', port: 8000 }); // Defines new handler for routes on this server @@ -1071,11 +1071,11 @@ property is set to a function, the function uses the signature `function(method) route default configuration. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ host: 'localhost', port: 8000 }); -var handler = function (route, options) { +const handler = function (route, options) { return function (request, reply) { @@ -1111,9 +1111,9 @@ server fails to start properly. If you must try to resume after an error, call ` first to reset the server state. ```js -var Hapi = require('hapi'); -var Hoek = require('hoek'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const Hoek = require('hoek'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.initialize(function (err) { @@ -1173,11 +1173,11 @@ When the server contains more than one connection, each [`server.connections`](# array member provides its own `connection.inject()`. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var handler = function (request, reply) { +const handler = function (request, reply) { return reply('Success!'); }; @@ -1203,8 +1203,8 @@ information or output to the console. The arguments are: - `timestamp` - an optional timestamp expressed in milliseconds. Defaults to `Date.now()` (now). ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.on('log', function (event, tags) { @@ -1225,7 +1225,7 @@ When the server contains exactly one connection, looks up a route configuration returns the [route public interface](#route-public-interface) object if found, otherwise `null`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1236,7 +1236,7 @@ server.route({ } }); -var route = server.lookup('root'); +const route = server.lookup('root'); ``` When the server contains more than one connection, each [`server.connections`](#serverconnections) @@ -1252,7 +1252,7 @@ When the server contains exactly one connection, looks up a route configuration returns the [route public interface](#route-public-interface) object if found, otherwise `null`. ```js -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', @@ -1263,7 +1263,7 @@ server.route({ } }); -var route = server.match('get', '/'); +const route = server.match('get', '/'); ``` When the server contains more than one connection, each [`server.connections`](#serverconnections) @@ -1315,13 +1315,13 @@ Methods are registered via `server.method(name, method, [options])` where: returns a unique string (or `null` if no key can be generated). ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); // Simple arguments -var add = function (a, b, next) { +const add = function (a, b, next) { return next(null, a + b); }; @@ -1335,9 +1335,9 @@ server.methods.sum(4, 5, function (err, result) { // Object argument -var addArray = function (array, next) { +const addArray = function (array, next) { - var sum = 0; + let sum = 0; array.forEach(function (item) { sum += item; @@ -1361,7 +1361,7 @@ server.methods.sumObj([5, 6], function (err, result) { // Synchronous method with cache -var addSync = function (a, b) { +const addSync = function (a, b) { return a + b; }; @@ -1385,7 +1385,7 @@ where: - `options` - optional settings. ```js -var add = function (a, b, next) { +const add = function (a, b, next) { next(null, a + b); }; @@ -1475,8 +1475,8 @@ Adds a connection route where: objects. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.route({ method: 'GET', path: '/', handler: function (request, reply) { return reply('ok'); } }); @@ -1496,14 +1496,14 @@ Returns a server object with `connections` set to the requested subset. Selectin selection operates as a logic AND statement between the individual selections. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80, labels: ['a', 'b'] }); server.connection({ port: 8080, labels: ['a', 'c'] }); server.connection({ port: 8081, labels: ['b', 'c'] }); -var a = server.select('a'); // 80, 8080 -var ac = a.select('c'); // 8080 +const a = server.select('a'); // 80, 8080 +const ac = a.select('c'); // 8080 ``` ### `server.start(callback)` @@ -1527,9 +1527,9 @@ add after the initial `start()` was called. No events will be emitted and no ext invoked. ```js -var Hapi = require('hapi'); -var Hoek = require('hoek'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const Hoek = require('hoek'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.start(function (err) { @@ -1583,8 +1583,8 @@ across multiple requests. Registers a cookie definitions where: State defaults can be modified via the server `connections.routes.state` configuration option. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); // Set cookie definition @@ -1598,9 +1598,9 @@ server.state('session', { // Set state in route handler -var handler = function (request, reply) { +const handler = function (request, reply) { - var session = request.state.session; + let session = request.state.session; if (!session) { session = { user: 'joe' }; } @@ -1619,8 +1619,8 @@ a `'request-internal'` event. To capture these errors subscribe to the `'request and filter on `'error'` and `'state'` tags: ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.on('request-internal', function (request, event, tags) { @@ -1642,8 +1642,8 @@ connections will continue until closed or timeout), where: connections have ended and it is safe to exit the process. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.stop({ timeout: 60 * 1000 }, function (err) { @@ -1670,24 +1670,24 @@ Note that if the server has not been started and multiple connections use port ` will override each other and will produce an incomplete result. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80, host: 'example.com' }); server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); -var table = server.table(); +const table = server.table(); ``` When calling `connection.table()` directly on each connection, the return value is the same as the array `table` item value of an individual connection: ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80, host: 'example.com' }); server.route({ method: 'GET', path: '/example', handler: function (request, reply) { return reply(); } }); -var table = server.connections[0].table(); +const table = server.connections[0].table(); /* [ @@ -1896,7 +1896,7 @@ The plugin function must include an `attributes` function property with the foll Defaults to `undefined` (registration will be based on the `server.register()` option `once`). ```js -var register = function (server, options, next) { +const register = function (server, options, next) { server.route({ method: 'GET', @@ -2010,13 +2010,13 @@ Note that the `options` object is deeply cloned (with the exception of `bind` wh copied) and cannot contain any values that are unsafe to perform deep copy on. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); // Handler in top level -var status = function (request, reply) { +const status = function (request, reply) { return reply('ok'); }; @@ -2025,7 +2025,7 @@ server.route({ method: 'GET', path: '/status', handler: status }); // Handler in config -var user = { +const user = { cache: { expiresIn: 5000 }, handler: function (request, reply) { @@ -2397,11 +2397,11 @@ if the parameter is at the ends of the path or only covers part of the segment a `request.params.id` set to an empty string `''`. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var getAlbum = function (request, reply) { +const getAlbum = function (request, reply) { return reply('You asked for ' + (request.params.song ? request.params.song + ' from ' : '') + @@ -2421,13 +2421,13 @@ can be anything, then use `*` without a number (matching any number of segments the last path segment). ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var getPerson = function (request, reply) { +const getPerson = function (request, reply) { - var nameParts = request.params.name.split('/'); + const nameParts = request.params.name.split('/'); return reply({ first: nameParts[0], last: nameParts[1] }); }; @@ -2464,11 +2464,11 @@ catch-all route for a specific method or all methods. Only one catch-all route c server connection. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var handler = function (request, reply) { +const handler = function (request, reply) { return reply('The page was not found').code(404); }; @@ -2485,7 +2485,7 @@ The route handler function uses the signature `function(request, reply)` where: return control back to the framework. ```js -var handler = function (request, reply) { +const handler = function (request, reply) { return reply('success'); }; @@ -2529,21 +2529,21 @@ retain the result value and pass it on to the next step. Errors end the lifecycl less consistent, this allows easier code reusability. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var pre1 = function (request, reply) { +const pre1 = function (request, reply) { return reply('Hello'); }; -var pre2 = function (request, reply) { +const pre2 = function (request, reply) { return reply('World'); }; -var pre3 = function (request, reply) { +const pre3 = function (request, reply) { return reply(request.pre.m1 + ' ' + request.pre.m2); }; @@ -2656,8 +2656,8 @@ Changes the request URI before the router begins processing the request where: parsing defaults. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onRequest', function (request, reply) { @@ -2676,8 +2676,8 @@ Changes the request method before the router begins processing the request where - `method` - is the request HTTP method (e.g. `'GET'`). ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onRequest', function (request, reply) { @@ -2699,9 +2699,9 @@ Returns a [`response`](#response-object) which you can pass into the [reply inte For example it can be used inside a promise to create a response object which has a non-error code to resolve with the [reply interface](#reply-interface): ```js -var handler = function (request, reply) { +const handler = function (request, reply) { - var result = promiseMethod().then(function (thing) { + const result = promiseMethod().then(function (thing) { if (!thing) { return request.generateResponse().code(214); @@ -2728,8 +2728,8 @@ Any logs generated by the server internally will be emitted only on the `'reques channel and will include the `event.internal` flag set to `true`. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.on('request', function (request, event, tags) { @@ -2739,7 +2739,7 @@ server.on('request', function (request, event, tags) { } }); -var handler = function (request, reply) { +const handler = function (request, reply) { request.log(['test', 'error'], 'Test event'); return reply(); @@ -2783,13 +2783,13 @@ the activity with the request when logging it (or an error associated with it). When all tails completed, the server emits a `'tail'` event. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); -var get = function (request, reply) { +const get = function (request, reply) { - var dbTail = request.tail('write to database'); + const dbTail = request.tail('write to database'); db.save('key', 'value', function () { @@ -2818,14 +2818,14 @@ The [request object](#request-object) supports the following events: - `'disconnect'` - emitted when a request errors or aborts unexpectedly. ```js -var Crypto = require('crypto'); -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Crypto = require('crypto'); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onRequest', function (request, reply) { - var hash = Crypto.createHash('sha1'); + const hash = Crypto.createHash('sha1'); request.on('peek', function (chunk) { hash.update(chunk); @@ -2889,7 +2889,7 @@ Both `err` and `result` can be set to: - any other object or array ```js -var handler = function (request, reply) { +const handler = function (request, reply) { return reply('success'); }; @@ -2909,16 +2909,16 @@ The [response flow control rules](#flow-control) apply. ```js // Detailed notation -var handler = function (request, reply) { +const handler = function (request, reply) { - var response = reply('success'); + const response = reply('success'); response.type('text/plain'); response.header('X-Custom', 'some-value'); }; // Chained notation -var handler = function (request, reply) { +const handler = function (request, reply) { return reply('success') .type('text/plain') @@ -3073,19 +3073,19 @@ The response object supports the following events: is ended. The event method signature is `function ()`. ```js -var Crypto = require('crypto'); -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Crypto = require('crypto'); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onPreResponse', function (request, reply) { - var response = request.response; + const response = request.response; if (response.isBoom) { return reply(); } - var hash = Crypto.createHash('sha1'); + const hash = Crypto.createHash('sha1'); response.on('peek', function (chunk) { hash.update(chunk); @@ -3111,10 +3111,10 @@ When the error is sent back to the client, the responses contains a JSON object `statusCode`, `error`, and `message` keys. ```js -var Hapi = require('hapi'); -var Boom = require('boom'); +const Hapi = require('hapi'); +const Boom = require('boom'); -var server = new Hapi.Server(); +const server = new Hapi.Server(); server.route({ method: 'GET', @@ -3159,11 +3159,11 @@ It also supports the following method: - `reformat()` - rebuilds `error.output` using the other object properties. ```js -var Boom = require('boom'); +const Boom = require('boom'); -var handler = function (request, reply) { +const handler = function (request, reply) { - var error = Boom.badRequest('Cannot feed after midnight'); + const error = Boom.badRequest('Cannot feed after midnight'); error.output.statusCode = 499; // Assign a custom error code error.reformat(); @@ -3178,9 +3178,9 @@ format, the `'onPreResponse'` extension point may be used to identify errors and a different response object. ```js -var Hapi = require('hapi'); -var Vision = require('vision'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const Vision = require('vision'); +const server = new Hapi.Server(); server.register(Vision, function (err) { server.views({ engines: { @@ -3193,15 +3193,15 @@ server.connection({ port: 80 }); server.ext('onPreResponse', function (request, reply) { - var response = request.response; + const response = request.response; if (!response.isBoom) { return reply.continue(); } // Replace error with friendly HTML - var error = response; - var ctx = { + const error = response; + const ctx = { message: (error.output.statusCode === 404 ? 'page not found' : 'something went wrong') }; @@ -3222,9 +3222,9 @@ will resume as soon as the handler method exits. To suspend this behavior, the r `response.hold()` is called and until `response.send()` is invoked once. ```js -var handler = function (request, reply) { +const handler = function (request, reply) { - var response = reply('success').hold(); + const response = reply('success').hold(); setTimeout(function () { @@ -3240,8 +3240,8 @@ response defaults to an empty payload with status code `200`. The `data` argumen passing back authentication data and is ignored elsewhere. ```js -var Hapi = require('hapi'); -var server = new Hapi.Server(); +const Hapi = require('hapi'); +const server = new Hapi.Server(); server.connection({ port: 80 }); server.ext('onRequest', function (request, reply) { @@ -3273,7 +3273,7 @@ Returns a [response object](#response-object). The [response flow control rules](#flow-control) apply. ```js -var handler = function (request, reply) { +const handler = function (request, reply) { return reply.redirect('http://example.com'); }; diff --git a/lib/auth.js b/lib/auth.js index 66b2e3c80..634c91f1e 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -71,7 +71,7 @@ internals.Auth.prototype.default = function (options) { options = Schema.apply('auth', options, 'default strategy'); Hoek.assert(!this.settings.default, 'Cannot set default strategy more than once'); - var settings = Hoek.clone(options); // options can be reused + let settings = Hoek.clone(options); // options can be reused if (typeof settings === 'string') { settings = { @@ -132,7 +132,7 @@ internals.Auth.prototype._setupRoute = function (options, path) { options.scope = [options.scope]; } - for (var i = 0, il = options.scope.length; i < il; ++i) { + for (let i = 0, il = options.scope.length; i < il; ++i) { if (/{([^}]+)}/.test(options.scope[i])) { options.hasScopeParameters = true; break; @@ -147,7 +147,7 @@ internals.Auth.prototype._setupRoute = function (options, path) { options.payload = 'required'; } - var hasAuthenticatePayload = false; + let hasAuthenticatePayload = false; options.strategies.forEach(function (name) { const strategy = self._strategies[name]; @@ -192,7 +192,7 @@ internals.Auth.prototype._authenticate = function (request, next) { request.auth.mode = config.mode; const authErrors = []; - var strategyPos = 0; + let strategyPos = 0; const authenticate = function () { @@ -285,11 +285,11 @@ internals.Auth.prototype._authenticate = function (request, next) { // Check scope if (config.scope) { - var scopes = config.scope; + let scopes = config.scope; if (config.hasScopeParameters) { scopes = []; const context = { params: request.params, query: request.query }; - for (var i = 0, il = config.scope.length; i < il; ++i) { + for (let i = 0, il = config.scope.length; i < il; ++i) { scopes[i] = Hoek.reachTemplate(context, config.scope[i]); } } diff --git a/lib/connection.js b/lib/connection.js index 6369db089..6ee21a4d0 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -261,7 +261,7 @@ internals.Connection.prototype._dispatch = function (options) { internals.Connection.prototype.inject = function (options, callback) { - var settings = options; + let settings = options; if (typeof settings === 'string') { settings = { url: settings }; } @@ -348,11 +348,11 @@ internals.Connection.prototype._ext = function (event) { internals.Connection.prototype._route = function (configs, plugin) { configs = [].concat(configs); - for (var i = 0, il = configs.length; i < il; ++i) { + for (let i = 0, il = configs.length; i < il; ++i) { const config = configs[i]; if (Array.isArray(config.method)) { - for (var m = 0, ml = config.method.length; m < ml; ++m) { + for (let m = 0, ml = config.method.length; m < ml; ++m) { const method = config.method[m]; const settings = Hoek.shallow(config); @@ -372,7 +372,7 @@ internals.Connection.prototype._addRoute = function (config, plugin) { const route = new Route(config, this, plugin); // Do no use config beyond this point, use route members const vhosts = [].concat(route.settings.vhost || '*'); - for (var i = 0, il = vhosts.length; i < il; ++i) { + for (let i = 0, il = vhosts.length; i < il; ++i) { const vhost = vhosts[i]; const record = this._router.add({ method: route.method, path: route.path, vhost: vhost, analysis: route._analysis, id: route.settings.id }, route); route.fingerprint = record.fingerprint; diff --git a/lib/cors.js b/lib/cors.js index e1f4c7ce4..a25de45ea 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -5,7 +5,7 @@ const Boom = require('boom'); const Hoek = require('hoek'); const Defaults = require('./defaults'); -var Route = null; // Delayed load due to circular dependency +let Route = null; // Delayed load due to circular dependency // Declare internals @@ -22,7 +22,7 @@ exports.route = function (options) { settings._headers = settings.headers.concat(settings.additionalHeaders); settings._headersString = settings._headers.join(','); - for (var i = 0, il = settings._headers.length; i < il; ++i) { + for (let i = 0, il = settings._headers.length; i < il; ++i) { settings._headers[i] = settings._headers[i].toLowerCase(); } @@ -38,7 +38,7 @@ exports.route = function (options) { wildcards: [] }; - for (var c = 0, cl = settings.origin.length; c < cl; ++c) { + for (let c = 0, cl = settings.origin.length; c < cl; ++c) { const origin = settings.origin[c]; if (origin.indexOf('*') !== -1) { settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); @@ -121,7 +121,7 @@ internals.handler = function (request, reply) { // Validate allowed headers - var headers = request.headers['access-control-request-headers']; + let headers = request.headers['access-control-request-headers']; if (headers) { headers = headers.toLowerCase().split(/\s*,\s*/); if (Hoek.intersect(headers, settings._headers).length !== headers.length) { @@ -185,7 +185,7 @@ internals.matchOrigin = function (origin, settings) { return true; } - for (var i = 0, il = settings._origin.wildcards.length; i < il; ++i) { + for (let i = 0, il = settings._origin.wildcards.length; i < il; ++i) { if (origin.match(settings._origin.wildcards[i])) { return true; } diff --git a/lib/ext.js b/lib/ext.js index 3398fc5e5..c517765d7 100755 --- a/lib/ext.js +++ b/lib/ext.js @@ -25,7 +25,7 @@ internals.Ext.prototype.add = function (event) { const methods = [].concat(event.method); const options = event.options; - for (var i = 0, il = methods.length; i < il; ++i) { + for (let i = 0, il = methods.length; i < il; ++i) { const settings = { before: options.before, after: options.after, @@ -46,7 +46,7 @@ internals.Ext.prototype.add = function (event) { // Notify routes - for (i = 0, il = this._routes.length; i < il; ++i) { + for (let i = 0, il = this._routes.length; i < il; ++i) { this._routes[i].rebuild(event); } }; @@ -55,7 +55,7 @@ internals.Ext.prototype.add = function (event) { internals.Ext.prototype.merge = function (others) { const merge = []; - for (var i = 0, il = others.length; i < il; ++i) { + for (let i = 0, il = others.length; i < il; ++i) { merge.push(others[i]._topo); } diff --git a/lib/handler.js b/lib/handler.js index 073f9aede..3a93b2dae 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -98,7 +98,7 @@ internals.handler = function (request, callback) { exports.defaults = function (method, handler, server) { - var defaults = null; + let defaults = null; if (typeof handler === 'object') { const type = Object.keys(handler)[0]; @@ -160,12 +160,12 @@ exports.prerequisites = function (config, server) { const prerequisites = []; - for (var i = 0, il = config.length; i < il; ++i) { + for (let i = 0, il = config.length; i < il; ++i) { const pres = [].concat(config[i]); const set = []; - for (var p = 0, pl = pres.length; p < pl; ++p) { - var pre = pres[p]; + for (let p = 0, pl = pres.length; p < pl; ++p) { + let pre = pres[p]; if (typeof pre !== 'object') { pre = { method: pre }; } @@ -224,7 +224,7 @@ internals.fromString = function (type, notation, server) { }; const args = []; - for (var i = 0, il = methodArgs.length; i < il; ++i) { + for (let i = 0, il = methodArgs.length; i < il; ++i) { const arg = methodArgs[i]; if (arg) { args.push(Hoek.reach(request, arg)); diff --git a/lib/methods.js b/lib/methods.js index 3782b6090..65c36e27c 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -29,7 +29,7 @@ internals.Methods.prototype.add = function (name, method, options, realm) { // {} or [{}, {}] const items = [].concat(name); - for (var i = 0, il = items.length; i < il; ++i) { + for (let i = 0, il = items.length; i < il; ++i) { const item = Schema.apply('methodObject', items[i]); this._add(item.name, item.method, item.options, realm); } @@ -61,19 +61,19 @@ internals.Methods.prototype._add = function (name, method, options, realm) { // Normalize methods - var normalized = bound; + let normalized = bound; if (settings.callback === false) { // Defaults to true normalized = function (/* arg1, arg2, ..., argn, methodNext */) { const args = []; - for (var i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - const methodNext = arguments[il - 1]; + const methodNext = arguments[arguments.length - 1]; - var result = null; - var error = null; + let result = null; + let error = null; try { result = method.apply(bind, args); @@ -132,11 +132,11 @@ internals.Methods.prototype._add = function (name, method, options, realm) { const func = function (/* arguments, methodNext */) { const args = []; - for (var i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - const methodNext = arguments[il - 1]; + const methodNext = arguments[arguments.length - 1]; const key = settings.generateKey.apply(bind, args); if (key === null || // Value can be '' @@ -152,11 +152,11 @@ internals.Methods.prototype._add = function (name, method, options, realm) { drop: function (/* arguments, callback */) { const args = []; - for (var i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0, il = arguments.length; i < il - 1; ++i) { args.push(arguments[i]); } - const methodNext = arguments[il - 1]; + const methodNext = arguments[arguments.length - 1]; const key = settings.generateKey.apply(null, args); if (key === null) { // Value can be '' @@ -175,8 +175,8 @@ internals.Methods.prototype._add = function (name, method, options, realm) { internals.Methods.prototype._assign = function (name, method, normalized) { const path = name.split('.'); - var ref = this.methods; - for (var i = 0, il = path.length; i < il; ++i) { + let ref = this.methods; + for (let i = 0, il = path.length; i < il; ++i) { if (!ref[path[i]]) { ref[path[i]] = (i + 1 === il ? method : {}); } @@ -190,8 +190,8 @@ internals.Methods.prototype._assign = function (name, method, normalized) { internals.generateKey = function () { - var key = ''; - for (var i = 0, il = arguments.length; i < il; ++i) { + let key = ''; + for (let i = 0, il = arguments.length; i < il; ++i) { const arg = arguments[i]; if (typeof arg !== 'string' && typeof arg !== 'number' && diff --git a/lib/plugin.js b/lib/plugin.js index f22c58dc2..81c93c236 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -83,7 +83,7 @@ exports = module.exports = internals.Plugin = function (server, connections, env // Decorations const methods = Object.keys(this.root._decorations); - for (var i = 0, il = methods.length; i < il; ++i) { + for (let i = 0, il = methods.length; i < il; ++i) { const method = methods[i]; this[method] = this.root._decorations[method]; } @@ -111,8 +111,8 @@ internals.Plugin.prototype._single = function () { internals.Plugin.prototype.select = function (/* labels */) { - var labels = []; - for (var i = 0, il = arguments.length; i < il; ++i) { + let labels = []; + for (let i = 0, il = arguments.length; i < il; ++i) { labels.push(arguments[i]); } @@ -123,7 +123,7 @@ internals.Plugin.prototype.select = function (/* labels */) { internals.Plugin.prototype._select = function (labels, plugin) { - var connections = this.connections; + let connections = this.connections; if (labels && labels.length) { // Captures both empty arrays and empty strings @@ -131,7 +131,7 @@ internals.Plugin.prototype._select = function (labels, plugin) { Hoek.assert(this.connections, 'Cannot select inside a connectionless plugin'); connections = []; - for (var i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0, il = this.connections.length; i < il; ++i) { const connection = this.connections[i]; if (Hoek.intersect(connection.settings.labels, labels).length) { connections.push(connection); @@ -161,7 +161,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback const self = this; - var options = (typeof arguments[1] === 'object' ? arguments[1] : {}); + let options = (typeof arguments[1] === 'object' ? arguments[1] : {}); const callback = (typeof arguments[1] === 'object' ? arguments[2] : arguments[1]); Hoek.assert(typeof callback === 'function', 'A callback function is required to register a plugin'); @@ -206,8 +206,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback const registrations = []; plugins = [].concat(plugins); - for (var i = 0, il = plugins.length; i < il; ++i) { - var plugin = plugins[i]; + for (let i = 0, il = plugins.length; i < il; ++i) { + let plugin = plugins[i]; if (typeof plugin === 'function') { if (!plugin.register) { // plugin is register() function @@ -279,7 +279,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback const connections = []; if (selection.connections) { - for (var j = 0, jl = selection.connections.length; j < jl; ++j) { + for (let j = 0, jl = selection.connections.length; j < jl; ++j) { const connection = selection.connections[j]; if (connection.registrations[item.name]) { if (item.options.once) { @@ -376,7 +376,7 @@ internals.Plugin.prototype.decorate = function (type, property, method) { this.root._decorations[property] = method; this[property] = method; - var parent = this._parent; + let parent = this._parent; while (parent) { parent[property] = method; parent = parent._parent; @@ -422,7 +422,7 @@ internals.Plugin.prototype.ext = function (events) { // (event, method, o events = Schema.apply('exts', events); - for (var i = 0, il = events.length; i < il; ++i) { + for (let i = 0, il = events.length; i < il; ++i) { this._ext(events[i]); } }; @@ -552,7 +552,7 @@ internals.Plugin.prototype.table = function (host) { Hoek.assert(this.connections, 'Cannot request routing table from a connectionless plugin'); const table = []; - for (var i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0, il = this.connections.length; i < il; ++i) { const connection = this.connections[i]; table.push({ info: connection.info, labels: connection.settings.labels, table: connection.table(host) }); } @@ -566,7 +566,7 @@ internals.Plugin.prototype._apply = function (type, func, args) { Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); - for (var i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0, il = this.connections.length; i < il; ++i) { func.apply(this.connections[i], args); } }; @@ -577,7 +577,7 @@ internals.Plugin.prototype._applyChild = function (type, child, func, args) { Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); - for (var i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0, il = this.connections.length; i < il; ++i) { const obj = this.connections[i][child]; obj[func].apply(obj, args); } diff --git a/lib/protect.js b/lib/protect.js index eb742d8a5..89ddb404a 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -2,7 +2,7 @@ // Load modules -var Domain = null; // Loaded as needed +let Domain = null; // Loaded as needed const Boom = require('boom'); const Hoek = require('hoek'); diff --git a/lib/reply.js b/lib/reply.js index da9fa6c12..6a49d8c0a 100755 --- a/lib/reply.js +++ b/lib/reply.js @@ -85,7 +85,7 @@ internals.Reply.prototype.interface = function (request, realm, next) { // if (this._decorations) { const methods = Object.keys(this._decorations); - for (var i = 0, il = methods.length; i < il; ++i) { + for (let i = 0, il = methods.length; i < il; ++i) { const method = methods[i]; reply[method] = this._decorations[method]; } diff --git a/lib/request.js b/lib/request.js index 38cfc5e7a..dae980b8f 100755 --- a/lib/request.js +++ b/lib/request.js @@ -36,7 +36,7 @@ internals.Generator.prototype.request = function (connection, req, res, options) if (this._decorations) { const methods = Object.keys(this._decorations); - for (var i = 0, il = methods.length; i < il; ++i) { + for (let i = 0, il = methods.length; i < il; ++i) { const method = methods[i]; request[method] = this._decorations[method]; } @@ -282,11 +282,11 @@ internals.Request.prototype.getLog = function (tags, internal) { const filter = tags.length ? Hoek.mapToObject(tags) : null; const result = []; - for (var i = 0, il = this._logger.length; i < il; ++i) { + for (let i = 0, il = this._logger.length; i < il; ++i) { const event = this._logger[i]; if (internal === undefined || event.internal === internal) { if (filter) { - for (var t = 0, tl = event.tags.length; t < tl; ++t) { + for (let t = 0, tl = event.tags.length; t < tl; ++t) { const tag = event.tags[t]; if (filter[tag]) { result.push(event); @@ -361,7 +361,7 @@ internals.Request.prototype._lifecycle = function (err) { this.raw.req.socket.setTimeout(this.route.settings.timeout.socket || 0); // Value can be false or positive } - var serverTimeout = this.route.settings.timeout.server; + let serverTimeout = this.route.settings.timeout.server; if (serverTimeout) { serverTimeout = Math.floor(serverTimeout - this._bench.elapsed()); // Calculate the timeout from when the request was constructed const timeoutReply = function () { diff --git a/lib/response.js b/lib/response.js index 36a583c1b..bd1587b3d 100755 --- a/lib/response.js +++ b/lib/response.js @@ -125,10 +125,10 @@ internals.Response.prototype._header = function (key, value, options) { value !== null) { const headerValues = [key].concat(value); - for (var v = 0, vl = headerValues.length; v < vl; ++v) { + for (let v = 0, vl = headerValues.length; v < vl; ++v) { const header = headerValues[v]; const buffer = Buffer.isBuffer(header) ? header : new Buffer(header.toString()); - for (var b = 0, bl = buffer.length; b < bl; ++b) { + for (let b = 0, bl = buffer.length; b < bl; ++b) { Hoek.assert((buffer[b] & 0x7f) === buffer[b], 'Header value cannot contain or convert into non-ascii characters:', key); } } @@ -147,7 +147,7 @@ internals.Response.prototype._header = function (key, value, options) { const existing = this.headers[key]; if (!duplicate) { const values = existing.split(separator); - for (var i = 0, il = values.length; i < il; ++i) { + for (let i = 0, il = values.length; i < il; ++i) { if (values[i] === value) { return this; } @@ -420,19 +420,19 @@ internals.Response.prototype._passThrough = function () { } if (this.source.headers) { - var headerKeys = Object.keys(this.source.headers); + let headerKeys = Object.keys(this.source.headers); if (headerKeys.length) { const localHeaders = this.headers; this.headers = {}; - for (var i = 0, il = headerKeys.length; i < il; ++i) { + for (let i = 0, il = headerKeys.length; i < il; ++i) { const key = headerKeys[i]; this.header(key.toLowerCase(), Hoek.clone(this.source.headers[key])); // Clone arrays } headerKeys = Object.keys(localHeaders); - for (i = 0, il = headerKeys.length; i < il; ++i) { + for (let i = 0, il = headerKeys.length; i < il; ++i) { const key = headerKeys[i]; this.header(key, localHeaders[key], { append: key === 'set-cookie' }); } @@ -491,7 +491,7 @@ internals.Response.prototype._streamify = function (source, next) { return next(); } - var payload = source; + let payload = source; if (this.variety === 'plain' && source !== null && typeof source !== 'string') { diff --git a/lib/route.js b/lib/route.js index 8f9317434..67ba711da 100755 --- a/lib/route.js +++ b/lib/route.js @@ -51,7 +51,7 @@ exports = module.exports = internals.Route = function (options, connection, plug // Apply settings in order: {connection} <- {handler} <- {realm} <- {route} const handlerDefaults = Handler.defaults(method, handler, connection.server); - var base = Hoek.applyToDefaultsWithShallow(connection.settings.routes, handlerDefaults, ['bind', 'cors']); + let base = Hoek.applyToDefaultsWithShallow(connection.settings.routes, handlerDefaults, ['bind', 'cors']); base = Hoek.applyToDefaultsWithShallow(base, realm.settings, ['bind', 'cors']); this.settings = Hoek.applyToDefaultsWithShallow(base, options.config || {}, ['bind', 'cors']); this.settings.handler = handler; @@ -120,7 +120,7 @@ exports = module.exports = internals.Route = function (options, connection, plug } else { this.settings.response.schema = internals.compileRule(rule); - for (var i = 0, il = statuses.length; i < il; ++i) { + for (let i = 0, il = statuses.length; i < il; ++i) { const code = statuses[i]; this.settings.response.status[code] = internals.compileRule(this.settings.response.status[code]); } @@ -230,10 +230,9 @@ internals.Route.prototype._combineExtensions = function (type, subscribe) { const events = this.settings.ext[type]; if (events) { - for (var i = 0, il = events.length; i < il; ++i) { - var event = events[i]; + for (let i = 0, il = events.length; i < il; ++i) { + const event = Hoek.shallow(events[i]); Hoek.assert(!event.options.sandbox, 'Cannot specify sandbox option for route extension'); - event = Hoek.shallow(event); event.plugin = this.plugin; ext.add(event); } @@ -364,7 +363,7 @@ internals.state = function (request, next) { // Clear cookies - for (var i = 0, il = failed.length; i < il; ++i) { + for (let i = 0, il = failed.length; i < il; ++i) { const item = failed[i]; if (item.settings.clearInvalid) { diff --git a/lib/server.js b/lib/server.js index 5b370fcc5..4f34729f7 100755 --- a/lib/server.js +++ b/lib/server.js @@ -62,7 +62,7 @@ exports = module.exports = internals.Server = function (options) { if (options.cache) { const caches = [].concat(options.cache); - for (var i = 0, il = caches.length; i < il; ++i) { + for (let i = 0, il = caches.length; i < il; ++i) { this._createCache(caches[i]); } } @@ -86,7 +86,7 @@ internals.Server.prototype._createCache = function (options) { const name = options.name || '_default'; Hoek.assert(!this._caches[name], 'Cannot configure the same cache more than once: ', name === '_default' ? 'default cache' : name); - var client = null; + let client = null; if (typeof options.engine === 'object') { client = new Catbox.Client(options.engine); } @@ -112,7 +112,7 @@ internals.Server.prototype.connection = function (options) { const root = this.root; // Explicitly use the root reference (for plugin invocation) - var settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']); + let settings = Hoek.applyToDefaultsWithShallow(root._settings.connections, options || {}, ['listener', 'routes.bind']); settings.routes.cors = Hoek.applyToDefaults(root._settings.connections.routes.cors || Defaults.cors, settings.routes.cors) || false; settings.routes.security = Hoek.applyToDefaults(root._settings.connections.routes.security || Defaults.security, settings.routes.security); @@ -124,7 +124,7 @@ internals.Server.prototype.connection = function (options) { root._single(); const registrations = Object.keys(root._registrations); - for (var i = 0, il = registrations.length; i < il; ++i) { + for (let i = 0, il = registrations.length; i < il; ++i) { const name = registrations[i]; connection.registrations[name] = root._registrations[name]; } @@ -188,12 +188,12 @@ internals.Server.prototype.initialize = function (callback) { // Assert dependencies - for (var i = 0, il = this._dependencies.length; i < il; ++i) { + for (let i = 0, il = this._dependencies.length; i < il; ++i) { const dependency = this._dependencies[i]; if (dependency.connections) { - for (var s = 0, sl = dependency.connections.length; s < sl; ++s) { + for (let s = 0, sl = dependency.connections.length; s < sl; ++s) { const connection = dependency.connections[s]; - for (var d = 0, dl = dependency.deps.length; d < dl; ++d) { + for (let d = 0, dl = dependency.deps.length; d < dl; ++d) { const dep = dependency.deps[d]; if (!connection.registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); @@ -202,7 +202,7 @@ internals.Server.prototype.initialize = function (callback) { } } else { - for (d = 0, dl = dependency.deps.length; d < dl; ++d) { + for (let d = 0, dl = dependency.deps.length; d < dl; ++d) { const dep = dependency.deps[d]; if (!this._registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep)); @@ -318,7 +318,7 @@ internals.Server.prototype.stop = function (/* [options], callback */) { } const caches = Object.keys(self._caches); - for (var i = 0, il = caches.length; i < il; ++i) { + for (let i = 0, il = caches.length; i < il; ++i) { self._caches[caches[i]].client.stop(); } diff --git a/lib/transmit.js b/lib/transmit.js index 9a8d1a20b..f62dc0652 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -56,7 +56,7 @@ internals.marshal = function (request, next) { // Strong verifier const ifNoneMatch = request.headers['if-none-match'].split(/\s*,\s*/); - for (var i = 0, il = ifNoneMatch.length; i < il; ++i) { + for (let i = 0, il = ifNoneMatch.length; i < il; ++i) { const etag = ifNoneMatch[i]; if (etag === response.headers.etag) { response.code(304); @@ -197,11 +197,12 @@ internals.transmit = function (response, callback) { // Compression const mime = request.server.mime.type(response.headers['content-type'] || 'application/octet-stream'); - var encoding = (request.connection.settings.compression && mime.compressible && !response.headers['content-encoding'] ? request.info.acceptEncoding : null); + let encoding = (request.connection.settings.compression && mime.compressible && !response.headers['content-encoding'] ? request.info.acceptEncoding : null); encoding = (encoding === 'identity' ? null : encoding); // Range + let ranger = null; if (request.method === 'get' && response.statusCode === 200 && length > 0 && @@ -227,7 +228,7 @@ internals.transmit = function (response, callback) { if (ranges.length === 1) { // Ignore requests for multiple ranges const range = ranges[0]; - var ranger = new Ammo.Stream(range); + ranger = new Ammo.Stream(range); response.code(206); response.bytes(range.to - range.from + 1); response._header('content-range', 'bytes ' + range.from + '-' + range.to + '/' + length); @@ -244,6 +245,7 @@ internals.transmit = function (response, callback) { response.vary('accept-encoding'); } + let compressor = null; if (encoding && length !== 0 && response._isPayloadSupported()) { @@ -251,7 +253,7 @@ internals.transmit = function (response, callback) { delete response.headers['content-length']; response._header('content-encoding', encoding); - var compressor = (encoding === 'gzip' ? Zlib.createGzip() : Zlib.createDeflate()); + compressor = (encoding === 'gzip' ? Zlib.createGzip() : Zlib.createDeflate()); } if ((response.headers['content-encoding'] || encoding) && @@ -264,7 +266,7 @@ internals.transmit = function (response, callback) { // Write headers const headers = Object.keys(response.headers); - for (var h = 0, hl = headers.length; h < hl; ++h) { + for (let h = 0, hl = headers.length; h < hl; ++h) { const header = headers[h]; const value = response.headers[header]; if (value !== undefined) { @@ -280,7 +282,7 @@ internals.transmit = function (response, callback) { // Write payload - var hasEnded = false; + let hasEnded = false; const end = function (err, event) { if (hasEnded) { @@ -454,14 +456,14 @@ internals.state = function (response, next) { const names = {}; const states = []; - var keys = Object.keys(request._states); - for (var i = 0, il = keys.length; i < il; ++i) { - const keyName = keys[i]; - names[keyName] = true; - states.push(request._states[keyName]); + const requestStates = Object.keys(request._states); + for (let i = 0, il = requestStates.length; i < il; ++i) { + const stateName = requestStates[i]; + names[stateName] = true; + states.push(request._states[stateName]); } - keys = Object.keys(request.connection.states.cookies); + const keys = Object.keys(request.connection.states.cookies); Items.parallel(keys, function (name, nextKey) { const autoValue = request.connection.states.cookies[name].autoValue; diff --git a/lib/validation.js b/lib/validation.js index 7635c0066..61a91b844 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -74,14 +74,14 @@ internals.input = function (source, request, next) { const error = Boom.badRequest(err.message, err); error.output.payload.validation = { source: source, keys: [] }; if (err.details) { - for (var i = 0, il = err.details.length; i < il; ++i) { + for (let i = 0, il = err.details.length; i < il; ++i) { error.output.payload.validation.keys.push(Hoek.escapeHtml(err.details[i].path)); } } if (request.route.settings.validate.errorFields) { const fields = Object.keys(request.route.settings.validate.errorFields); - for (var f = 0, fl = fields.length; f < fl; ++f) { + for (let f = 0, fl = fields.length; f < fl; ++f) { const field = fields[f]; error.output.payload[field] = request.route.settings.validate.errorFields[field]; } diff --git a/test/auth.js b/test/auth.js index 229a37f31..7daa78a1f 100755 --- a/test/auth.js +++ b/test/auth.js @@ -1157,7 +1157,7 @@ describe('authentication', function () { } }); - var result; + let result; server.on('request-internal', function (request, event, tags) { if (tags.unauthenticated) { diff --git a/test/connection.js b/test/connection.js index 6bb2ad572..10f5e08af 100755 --- a/test/connection.js +++ b/test/connection.js @@ -82,7 +82,7 @@ describe('Connection', function () { expect(err).to.not.exist(); - var expectedBoundAddress = '0.0.0.0'; + let expectedBoundAddress = '0.0.0.0'; if (Net.isIPv6(server.listener.address().address)) { expectedBoundAddress = '::'; } @@ -320,7 +320,7 @@ describe('Connection', function () { expect(err).to.not.exist(); - var timeout; + let timeout; const orig = Net.Socket.prototype.setTimeout; Net.Socket.prototype.setTimeout = function () { @@ -350,7 +350,7 @@ describe('Connection', function () { server.start(function (err) { expect(err).to.not.exist(); - var expectedBoundAddress = '0.0.0.0'; + let expectedBoundAddress = '0.0.0.0'; if (Net.isIPv6(server.listener.address().address)) { expectedBoundAddress = '::'; } @@ -585,7 +585,7 @@ describe('Connection', function () { expect(err).to.not.exist(); const agent = new Http.Agent({ keepAlive: true, maxSockets: 1 }); - var err2; + let err2; Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, function (err1, res, body) { @@ -662,7 +662,7 @@ describe('Connection', function () { return reply('ok'); }; - var logged = null; + let logged = null; server.once('log', function (event, tags) { logged = (event.internal && tags.load && event.data); @@ -1340,7 +1340,7 @@ describe('Connection', function () { expect(res.result.something).to.equal('else'); const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); - var lsof = ''; + let lsof = ''; cmd.stdout.on('data', function (buffer) { lsof += buffer.toString(); @@ -1348,9 +1348,9 @@ describe('Connection', function () { cmd.stdout.on('end', function () { - var count = 0; + let count = 0; const lines = lsof.split('\n'); - for (var i = 0, il = lines.length; i < il; ++i) { + for (let i = 0, il = lines.length; i < il; ++i) { count += !!lines[i].match(/package.json/); } diff --git a/test/handler.js b/test/handler.js index 1ebeb69c7..eb04df1a0 100755 --- a/test/handler.js +++ b/test/handler.js @@ -900,7 +900,7 @@ describe('handler', function () { } }); - var log = null; + let log = null; server.on('request-internal', function (request, event, tags) { if (event.internal && @@ -965,7 +965,7 @@ describe('handler', function () { } }); - var log = null; + let log = null; server.on('request-internal', function (request, event, tags) { if (event.internal && @@ -1029,7 +1029,7 @@ describe('handler', function () { const server = new Hapi.Server(); server.connection(); - var gen = 0; + let gen = 0; server.method('user', function (id, next) { return next(null, { id: id, name: 'Bob', gen: gen++ }); diff --git a/test/methods.js b/test/methods.js index 5df1718d4..40feab992 100755 --- a/test/methods.js +++ b/test/methods.js @@ -266,7 +266,7 @@ describe('Methods', function () { it('calls non cached method multiple times', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }); @@ -295,7 +295,7 @@ describe('Methods', function () { it('caches method value', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }); @@ -326,7 +326,7 @@ describe('Methods', function () { it('caches method value (no callback)', function (done) { - var gen = 0; + let gen = 0; const method = function (id) { return { id: id, gen: gen++ }; @@ -357,7 +357,7 @@ describe('Methods', function () { it('caches method value (promise)', function (done) { - var gen = 0; + let gen = 0; const methodAsync = function (id, next) { if (id === 2) { @@ -400,7 +400,7 @@ describe('Methods', function () { it('reuses cached method value with custom key function', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }); @@ -495,7 +495,7 @@ describe('Methods', function () { it('does not cache value when ttl is 0', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }, 0); @@ -524,7 +524,7 @@ describe('Methods', function () { it('generates new value after cache drop', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }); @@ -557,7 +557,7 @@ describe('Methods', function () { it('errors on invalid drop key', function (done) { - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: gen++ }); @@ -767,7 +767,7 @@ describe('Methods', function () { const server = new Hapi.Server(); - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: ++gen }); @@ -792,7 +792,7 @@ describe('Methods', function () { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: ++gen }); @@ -824,7 +824,7 @@ describe('Methods', function () { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); - var gen = 0; + let gen = 0; const method = function (id, next) { setTimeout(function () { @@ -862,7 +862,7 @@ describe('Methods', function () { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); - var gen = 0; + let gen = 0; const terms = 'I agree to give my house'; const method = function (next) { @@ -893,7 +893,7 @@ describe('Methods', function () { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); - var gen = 0; + let gen = 0; const method = function (id, next) { return next(null, { id: id, gen: ++gen }); diff --git a/test/payload.js b/test/payload.js index 4e30d14e8..b686dc722 100755 --- a/test/payload.js +++ b/test/payload.js @@ -102,7 +102,7 @@ describe('payload', function () { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { parse: false } } }); - var message = null; + let message = null; server.on('log', function (event, tags) { message = event.data.message; @@ -197,7 +197,7 @@ describe('payload', function () { it('peeks at unparsed data', function (done) { - var data = null; + let data = null; const ext = function (request, reply) { const chunks = []; @@ -568,7 +568,7 @@ describe('payload', function () { const result = {}; const keys = Object.keys(request.payload); - for (var i = 0, il = keys.length; i < il; ++i) { + for (let i = 0, il = keys.length; i < il; ++i) { const key = keys[i]; const value = request.payload[key]; result[key] = value._readableState ? true : value; diff --git a/test/plugin.js b/test/plugin.js index 928016e2a..edd3ee3c8 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -465,7 +465,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - var log = null; + let log = null; server.once('log', function (event, tags) { log = [event, tags]; @@ -485,7 +485,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - var log = null; + let log = null; server.once('log', function (event, tags) { log = [event, tags]; @@ -1305,7 +1305,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1348,7 +1348,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1378,7 +1378,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (empty selection)', function (done) { - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1416,7 +1416,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1446,7 +1446,7 @@ describe('Plugin', function () { it('register a plugin once (empty selection)', function (done) { - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1483,7 +1483,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1528,7 +1528,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1574,7 +1574,7 @@ describe('Plugin', function () { name: 'a' }; - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -1606,7 +1606,7 @@ describe('Plugin', function () { it('register a connectionless plugin once (first time)', function (done) { - var count = 0; + let count = 0; const b = function (srv, options, next) { ++count; @@ -2630,7 +2630,7 @@ describe('Plugin', function () { const server2 = server.connections[1]; const server3 = server.connections[2]; - var counter = 0; + let counter = 0; const test = function (srv, options, next) { srv.select(['a', 'b']).on('test', function () { @@ -2905,7 +2905,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var result = ''; + let result = ''; server.ext('onPreStart', function (srv, next) { result += '1'; @@ -2949,7 +2949,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var result = ''; + let result = ''; server.ext([ { type: 'onPreStart', @@ -3090,7 +3090,7 @@ describe('Plugin', function () { expect(server.plugins.x).to.not.exist(); - var called = false; + let called = false; server.ext('onPreStart', function (srv, next) { expect(srv.plugins.x.a).to.equal('b'); @@ -3115,7 +3115,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var called = false; + let called = false; server.ext('onPreStart', function (srv, next) { called = true; @@ -3135,7 +3135,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var called = false; + let called = false; server.ext('onPreStart', function (srv, next) { called = true; @@ -3300,7 +3300,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var count = 0; + let count = 0; server.once('log', function (event) { ++count; @@ -3409,7 +3409,7 @@ describe('Plugin', function () { const server = new Hapi.Server({ debug: false }); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -3428,7 +3428,7 @@ describe('Plugin', function () { const server = new Hapi.Server({ debug: { log: false } }); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -3447,7 +3447,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -3463,7 +3463,7 @@ describe('Plugin', function () { it('emits server log events once', function (done) { - var pc = 0; + let pc = 0; const test = function (srv, options, next) { srv.on('log', function (event, tags) { @@ -3481,7 +3481,7 @@ describe('Plugin', function () { const server = new Hapi.Server(); server.connection(); - var sc = 0; + let sc = 0; server.on('log', function (event, tags) { ++sc; @@ -4002,9 +4002,9 @@ internals.routesList = function (server, label) { const tables = server.select(label || []).table(); const list = []; - for (var c = 0, cl = tables.length; c < cl; ++c) { + for (let c = 0, cl = tables.length; c < cl; ++c) { const routes = tables[c].table; - for (var i = 0, il = routes.length; i < il; ++i) { + for (let i = 0, il = routes.length; i < il; ++i) { const route = routes[i]; if (route.method === 'get') { list.push(route.path); diff --git a/test/reply.js b/test/reply.js index f7ee74965..dcae52739 100755 --- a/test/reply.js +++ b/test/reply.js @@ -304,7 +304,7 @@ describe('Reply', function () { server.route({ method: 'GET', path: '/stream', handler: streamHandler }); server.route({ method: 'GET', path: '/writable', handler: writableHandler }); - var requestError; + let requestError; server.on('request-error', function (request, err) { requestError = err; diff --git a/test/request.js b/test/request.js index 43a9a8931..301f6a52a 100755 --- a/test/request.js +++ b/test/request.js @@ -67,7 +67,7 @@ describe('Request', function () { const handler = function (request, reply) { - var expectedClientAddress = '127.0.0.1'; + let expectedClientAddress = '127.0.0.1'; if (Net.isIPv6(server.listener.address().address)) { expectedClientAddress = '::ffff:127.0.0.1'; } @@ -243,7 +243,7 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var disconnected = 0; + let disconnected = 0; server.ext('onRequest', function (request, reply) { request.once('disconnect', function () { @@ -258,7 +258,7 @@ describe('Request', function () { expect(err).to.not.exist(); - var total = 2; + let total = 2; const createConnection = function () { const client = Net.connect(server.info.port, function () { @@ -326,7 +326,7 @@ describe('Request', function () { it('does not fail on abort', function (done) { - var clientRequest; + let clientRequest; const handler = function (request, reply) { @@ -367,7 +367,7 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: Hoek.ignore }); - var clientRequest; + let clientRequest; server.ext('onPreHandler', function (request, reply) { @@ -399,7 +399,7 @@ describe('Request', function () { it('does not fail on abort with ext', function (done) { - var clientRequest; + let clientRequest; const handler = function (request, reply) { @@ -602,8 +602,8 @@ describe('Request', function () { const server = new Hapi.Server({ debug: false }); server.connection(); - var errs = 0; - var req = null; + let errs = 0; + let req = null; server.on('request-error', function (request, err) { errs++; @@ -644,8 +644,8 @@ describe('Request', function () { const server = new Hapi.Server({ debug: false }); server.connection(); - var errs = 0; - var req = null; + let errs = 0; + let req = null; server.on('request-error', function (request, err) { ++errs; @@ -681,7 +681,7 @@ describe('Request', function () { const server = new Hapi.Server({ debug: false }); server.connection(); - var errs = 0; + let errs = 0; server.on('request-error', function (request, err) { errs++; @@ -733,7 +733,7 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var result = null; + let result = null; server.once('tail', function () { @@ -1191,7 +1191,7 @@ describe('Request', function () { const server = new Hapi.Server({ debug: false }); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -1220,7 +1220,7 @@ describe('Request', function () { const server = new Hapi.Server({ debug: { request: false } }); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -1249,7 +1249,7 @@ describe('Request', function () { const server = new Hapi.Server(); server.connection(); - var i = 0; + let i = 0; const orig = console.error; console.error = function () { @@ -1490,7 +1490,7 @@ describe('Request', function () { it('does not return an error when server is responding when the timeout occurs', function (done) { - var ended = false; + let ended = false; const handler = function (request, reply) { const TestStream = function () { diff --git a/test/response.js b/test/response.js index ee6db8a5d..236b51b1c 100755 --- a/test/response.js +++ b/test/response.js @@ -188,7 +188,7 @@ describe('Response', function () { it('throws error on non-ascii value', function (done) { - var thrown = false; + let thrown = false; const handler = function (request, reply) { @@ -213,7 +213,7 @@ describe('Response', function () { it('throws error on non-ascii value (header name)', function (done) { - var thrown = false; + let thrown = false; const handler = function (request, reply) { @@ -239,7 +239,7 @@ describe('Response', function () { it('throws error on non-ascii value (buffer)', function (done) { - var thrown = false; + let thrown = false; const handler = function (request, reply) { @@ -1225,7 +1225,7 @@ describe('Response', function () { const server = new Hapi.Server(); server.connection(); - var output = ''; + let output = ''; server.route({ method: 'GET', path: '/', @@ -1257,7 +1257,7 @@ describe('Response', function () { it('calls custom close processor', function (done) { - var closed = false; + let closed = false; const close = function (response) { closed = true; diff --git a/test/route.js b/test/route.js index d6834756b..a5312c8db 100755 --- a/test/route.js +++ b/test/route.js @@ -213,7 +213,7 @@ describe('Route', function () { server.connection(); const context = { key: 'is ' }; - var count = 0; + let count = 0; Object.defineProperty(context, 'test', { enumerable: true, configurable: true, @@ -243,7 +243,7 @@ describe('Route', function () { server.connection(); const context = { key: 'is ' }; - var count = 0; + let count = 0; Object.defineProperty(context, 'test', { enumerable: true, configurable: true, @@ -273,7 +273,7 @@ describe('Route', function () { const server = new Hapi.Server(); const context = { key: 'is ' }; - var count = 0; + let count = 0; Object.defineProperty(context, 'test', { enumerable: true, configurable: true, @@ -302,7 +302,7 @@ describe('Route', function () { const context = { key: 'is ' }; - var count = 0; + let count = 0; Object.defineProperty(context, 'test', { enumerable: true, configurable: true, diff --git a/test/server.js b/test/server.js index 815dff929..a0397961d 100755 --- a/test/server.js +++ b/test/server.js @@ -62,8 +62,8 @@ describe('Server', function () { server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); server.connection({ labels: ['s4', 'b', 'test', 'cache'] }); - var started = 0; - var stopped = 0; + let started = 0; + let stopped = 0; server.on('start', function () { @@ -106,8 +106,8 @@ describe('Server', function () { server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); server.connection({ labels: ['s4', 'b', 'test', 'cache'] }); - var started = 0; - var stopped = 0; + let started = 0; + let stopped = 0; server.on('start', function () { diff --git a/test/transmit.js b/test/transmit.js index ef527c650..2e0d783ea 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -95,7 +95,7 @@ describe('transmission', function () { expect(res2.statusCode).to.equal(304); const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); - var lsof = ''; + let lsof = ''; cmd.stdout.on('data', function (buffer) { lsof += buffer.toString(); @@ -103,9 +103,9 @@ describe('transmission', function () { cmd.stdout.on('end', function () { - var count = 0; + let count = 0; const lines = lsof.split('\n'); - for (var i = 0, il = lines.length; i < il; ++i) { + for (let i = 0, il = lines.length; i < il; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -136,7 +136,7 @@ describe('transmission', function () { expect(res2.statusCode).to.equal(304); const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); - var lsof = ''; + let lsof = ''; cmd.stdout.on('data', function (buffer) { lsof += buffer.toString(); @@ -144,9 +144,9 @@ describe('transmission', function () { cmd.stdout.on('end', function () { - var count = 0; + let count = 0; const lines = lsof.split('\n'); - for (var i = 0, il = lines.length; i < il; ++i) { + for (let i = 0, il = lines.length; i < il; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -548,7 +548,7 @@ describe('transmission', function () { server.connection(); server.route({ method: 'GET', path: '/', config: { jsonp: 'callback', handler: handler } }); - var validState = false; + let validState = false; server.ext('onPreResponse', function (request, reply) { validState = request.state && typeof request.state === 'object'; @@ -1436,7 +1436,7 @@ describe('transmission', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - var response; + let response; server.ext('onPreResponse', function (request, reply) { response = request.response; @@ -1506,8 +1506,8 @@ describe('transmission', function () { const filePath = __dirname + '/response.js'; const block = Fs.readFileSync(filePath).toString(); - var expectedBody = ''; - for (var i = 0, il = chunkTimes; i < il; ++i) { + let expectedBody = ''; + for (let i = 0, il = chunkTimes; i < il; ++i) { expectedBody += block; } @@ -1515,7 +1515,7 @@ describe('transmission', function () { const fileStream = new Stream.Readable(); - var readTimes = 0; + let readTimes = 0; fileStream._read = function (size) { ++readTimes; @@ -1551,8 +1551,8 @@ describe('transmission', function () { const filePath = __dirname + '/response.js'; const block = Fs.readFileSync(filePath).toString(); - var expectedBody = ''; - for (var i = 0, il = chunkTimes; i < il; ++i) { + let expectedBody = ''; + for (let i = 0, il = chunkTimes; i < il; ++i) { expectedBody += block; } @@ -1560,7 +1560,7 @@ describe('transmission', function () { const fileStream = new Stream.Readable(); - var readTimes = 0; + let readTimes = 0; fileStream._read = function (size) { ++readTimes; @@ -1599,7 +1599,7 @@ describe('transmission', function () { it('does not leak stream data when request aborts before stream drains', function (done) { - var destroyed = false; + let destroyed = false; const handler = function (request, reply) { @@ -1655,7 +1655,7 @@ describe('transmission', function () { it('does not leak classic stream data when passed to request and aborted', function (done) { - var destroyed = false; + let destroyed = false; const handler = function (request, reply) { @@ -1683,7 +1683,7 @@ describe('transmission', function () { }); }; - var paused = true; + let paused = true; stream.resume = function () { if (paused) { @@ -1732,7 +1732,7 @@ describe('transmission', function () { const handler = function (request, reply) { const stream = new Stream.Readable(); - var count = 0; + let count = 0; stream._read = function (size) { setTimeout(function () { @@ -1772,14 +1772,14 @@ describe('transmission', function () { it('does not leak stream data when request aborts before stream is returned', function (done) { - var clientRequest; + let clientRequest; const handler = function (request, reply) { clientRequest.abort(); const stream = new Stream.Readable(); - var responded = false; + let responded = false; stream._read = function (size) { diff --git a/test/validation.js b/test/validation.js index 84cc41bea..f8b28efcb 100755 --- a/test/validation.js +++ b/test/validation.js @@ -802,7 +802,7 @@ describe('validation', function () { } }); - var count = 0; + let count = 0; const action = function (next) { server.inject('/', function (res) { @@ -822,7 +822,7 @@ describe('validation', function () { it('validates response', function (done) { - var i = 0; + let i = 0; const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); @@ -894,7 +894,7 @@ describe('validation', function () { it('validates error response', function (done) { - var i = 0; + let i = 0; const handler = function (request, reply) { const error = Boom.badRequest('Kaboom'); @@ -935,7 +935,7 @@ describe('validation', function () { it('validates error response and ignore 200', function (done) { - var i = 0; + let i = 0; const handler = function (request, reply) { if (i === 0) { @@ -1111,7 +1111,7 @@ describe('validation', function () { it('validates response using custom validation function', function (done) { - var i = 0; + let i = 0; const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); @@ -1148,7 +1148,7 @@ describe('validation', function () { it('catches error thrown by custom validation function', function (done) { - var i = 0; + let i = 0; const handler = function (request, reply) { return reply({ some: i++ ? null : 'value' }); @@ -1198,7 +1198,7 @@ describe('validation', function () { } }); - var count = 0; + let count = 0; const action = function (next) { server.inject('/', function (res) { @@ -1440,7 +1440,7 @@ describe('validation', function () { it('validates string response', function (done) { - var value = 'abcd'; + let value = 'abcd'; const handler = function (request, reply) { return reply(value); @@ -1475,7 +1475,7 @@ describe('validation', function () { it('validates boolean response', function (done) { - var value = 'abcd'; + let value = 'abcd'; const handler = function (request, reply) { return reply(value); @@ -1586,7 +1586,7 @@ describe('validation', function () { internals.times = function (count, method, callback) { - var counter = 0; + let counter = 0; const results = []; const done = function (err, result) { @@ -1606,7 +1606,7 @@ internals.times = function (count, method, callback) { } }; - for (var i = 0; i < count; ++i) { + for (let i = 0; i < count; ++i) { method(done); } }; From 32cf03c8ec9e5cb74b0197708402fb544b68b5e9 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 11:57:43 -0700 Subject: [PATCH 0100/1139] for style change. Closes #2875 --- lib/auth.js | 4 ++-- lib/connection.js | 8 ++++---- lib/cors.js | 8 ++++---- lib/ext.js | 6 +++--- lib/handler.js | 8 ++++---- lib/methods.js | 14 +++++++------- lib/plugin.js | 20 ++++++++++---------- lib/reply.js | 2 +- lib/request.js | 8 ++++---- lib/response.js | 14 +++++++------- lib/route.js | 6 +++--- lib/server.js | 20 ++++++++++---------- lib/transmit.js | 8 ++++---- lib/validation.js | 6 +++--- test/connection.js | 2 +- test/payload.js | 2 +- test/plugin.js | 8 ++++---- test/transmit.js | 8 ++++---- 18 files changed, 76 insertions(+), 76 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 634c91f1e..eaa1d5422 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -132,7 +132,7 @@ internals.Auth.prototype._setupRoute = function (options, path) { options.scope = [options.scope]; } - for (let i = 0, il = options.scope.length; i < il; ++i) { + for (let i = 0; i < options.scope.length; ++i) { if (/{([^}]+)}/.test(options.scope[i])) { options.hasScopeParameters = true; break; @@ -289,7 +289,7 @@ internals.Auth.prototype._authenticate = function (request, next) { if (config.hasScopeParameters) { scopes = []; const context = { params: request.params, query: request.query }; - for (let i = 0, il = config.scope.length; i < il; ++i) { + for (let i = 0; i < config.scope.length; ++i) { scopes[i] = Hoek.reachTemplate(context, config.scope[i]); } } diff --git a/lib/connection.js b/lib/connection.js index 6ee21a4d0..2ab6c699f 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -348,12 +348,12 @@ internals.Connection.prototype._ext = function (event) { internals.Connection.prototype._route = function (configs, plugin) { configs = [].concat(configs); - for (let i = 0, il = configs.length; i < il; ++i) { + for (let i = 0; i < configs.length; ++i) { const config = configs[i]; if (Array.isArray(config.method)) { - for (let m = 0, ml = config.method.length; m < ml; ++m) { - const method = config.method[m]; + for (let j = 0; j < config.method.length; ++j) { + const method = config.method[j]; const settings = Hoek.shallow(config); settings.method = method; @@ -372,7 +372,7 @@ internals.Connection.prototype._addRoute = function (config, plugin) { const route = new Route(config, this, plugin); // Do no use config beyond this point, use route members const vhosts = [].concat(route.settings.vhost || '*'); - for (let i = 0, il = vhosts.length; i < il; ++i) { + for (let i = 0; i < vhosts.length; ++i) { const vhost = vhosts[i]; const record = this._router.add({ method: route.method, path: route.path, vhost: vhost, analysis: route._analysis, id: route.settings.id }, route); route.fingerprint = record.fingerprint; diff --git a/lib/cors.js b/lib/cors.js index a25de45ea..b066b8097 100755 --- a/lib/cors.js +++ b/lib/cors.js @@ -22,7 +22,7 @@ exports.route = function (options) { settings._headers = settings.headers.concat(settings.additionalHeaders); settings._headersString = settings._headers.join(','); - for (let i = 0, il = settings._headers.length; i < il; ++i) { + for (let i = 0; i < settings._headers.length; ++i) { settings._headers[i] = settings._headers[i].toLowerCase(); } @@ -38,8 +38,8 @@ exports.route = function (options) { wildcards: [] }; - for (let c = 0, cl = settings.origin.length; c < cl; ++c) { - const origin = settings.origin[c]; + for (let i = 0; i < settings.origin.length; ++i) { + const origin = settings.origin[i]; if (origin.indexOf('*') !== -1) { settings._origin.wildcards.push(new RegExp('^' + Hoek.escapeRegex(origin).replace(/\\\*/g, '.*').replace(/\\\?/g, '.') + '$')); } @@ -185,7 +185,7 @@ internals.matchOrigin = function (origin, settings) { return true; } - for (let i = 0, il = settings._origin.wildcards.length; i < il; ++i) { + for (let i = 0; i < settings._origin.wildcards.length; ++i) { if (origin.match(settings._origin.wildcards[i])) { return true; } diff --git a/lib/ext.js b/lib/ext.js index c517765d7..cae5de25f 100755 --- a/lib/ext.js +++ b/lib/ext.js @@ -25,7 +25,7 @@ internals.Ext.prototype.add = function (event) { const methods = [].concat(event.method); const options = event.options; - for (let i = 0, il = methods.length; i < il; ++i) { + for (let i = 0; i < methods.length; ++i) { const settings = { before: options.before, after: options.after, @@ -46,7 +46,7 @@ internals.Ext.prototype.add = function (event) { // Notify routes - for (let i = 0, il = this._routes.length; i < il; ++i) { + for (let i = 0; i < this._routes.length; ++i) { this._routes[i].rebuild(event); } }; @@ -55,7 +55,7 @@ internals.Ext.prototype.add = function (event) { internals.Ext.prototype.merge = function (others) { const merge = []; - for (let i = 0, il = others.length; i < il; ++i) { + for (let i = 0; i < others.length; ++i) { merge.push(others[i]._topo); } diff --git a/lib/handler.js b/lib/handler.js index 3a93b2dae..e9af83393 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -160,12 +160,12 @@ exports.prerequisites = function (config, server) { const prerequisites = []; - for (let i = 0, il = config.length; i < il; ++i) { + for (let i = 0; i < config.length; ++i) { const pres = [].concat(config[i]); const set = []; - for (let p = 0, pl = pres.length; p < pl; ++p) { - let pre = pres[p]; + for (let j = 0; j < pres.length; ++j) { + let pre = pres[j]; if (typeof pre !== 'object') { pre = { method: pre }; } @@ -224,7 +224,7 @@ internals.fromString = function (type, notation, server) { }; const args = []; - for (let i = 0, il = methodArgs.length; i < il; ++i) { + for (let i = 0; i < methodArgs.length; ++i) { const arg = methodArgs[i]; if (arg) { args.push(Hoek.reach(request, arg)); diff --git a/lib/methods.js b/lib/methods.js index 65c36e27c..bd40d86fa 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -29,7 +29,7 @@ internals.Methods.prototype.add = function (name, method, options, realm) { // {} or [{}, {}] const items = [].concat(name); - for (let i = 0, il = items.length; i < il; ++i) { + for (let i = 0; i < items.length; ++i) { const item = Schema.apply('methodObject', items[i]); this._add(item.name, item.method, item.options, realm); } @@ -66,7 +66,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { normalized = function (/* arg1, arg2, ..., argn, methodNext */) { const args = []; - for (let i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0; i < arguments.length - 1; ++i) { args.push(arguments[i]); } @@ -132,7 +132,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { const func = function (/* arguments, methodNext */) { const args = []; - for (let i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0; i < arguments.length - 1; ++i) { args.push(arguments[i]); } @@ -152,7 +152,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { drop: function (/* arguments, callback */) { const args = []; - for (let i = 0, il = arguments.length; i < il - 1; ++i) { + for (let i = 0; i < arguments.length - 1; ++i) { args.push(arguments[i]); } @@ -176,9 +176,9 @@ internals.Methods.prototype._assign = function (name, method, normalized) { const path = name.split('.'); let ref = this.methods; - for (let i = 0, il = path.length; i < il; ++i) { + for (let i = 0; i < path.length; ++i) { if (!ref[path[i]]) { - ref[path[i]] = (i + 1 === il ? method : {}); + ref[path[i]] = (i + 1 === path.length ? method : {}); } ref = ref[path[i]]; @@ -191,7 +191,7 @@ internals.Methods.prototype._assign = function (name, method, normalized) { internals.generateKey = function () { let key = ''; - for (let i = 0, il = arguments.length; i < il; ++i) { + for (let i = 0; i < arguments.length; ++i) { const arg = arguments[i]; if (typeof arg !== 'string' && typeof arg !== 'number' && diff --git a/lib/plugin.js b/lib/plugin.js index 81c93c236..645fa3648 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -83,7 +83,7 @@ exports = module.exports = internals.Plugin = function (server, connections, env // Decorations const methods = Object.keys(this.root._decorations); - for (let i = 0, il = methods.length; i < il; ++i) { + for (let i = 0; i < methods.length; ++i) { const method = methods[i]; this[method] = this.root._decorations[method]; } @@ -112,7 +112,7 @@ internals.Plugin.prototype._single = function () { internals.Plugin.prototype.select = function (/* labels */) { let labels = []; - for (let i = 0, il = arguments.length; i < il; ++i) { + for (let i = 0; i < arguments.length; ++i) { labels.push(arguments[i]); } @@ -131,7 +131,7 @@ internals.Plugin.prototype._select = function (labels, plugin) { Hoek.assert(this.connections, 'Cannot select inside a connectionless plugin'); connections = []; - for (let i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0; i < this.connections.length; ++i) { const connection = this.connections[i]; if (Hoek.intersect(connection.settings.labels, labels).length) { connections.push(connection); @@ -206,7 +206,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback const registrations = []; plugins = [].concat(plugins); - for (let i = 0, il = plugins.length; i < il; ++i) { + for (let i = 0; i < plugins.length; ++i) { let plugin = plugins[i]; if (typeof plugin === 'function') { @@ -279,8 +279,8 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback const connections = []; if (selection.connections) { - for (let j = 0, jl = selection.connections.length; j < jl; ++j) { - const connection = selection.connections[j]; + for (let i = 0; i < selection.connections.length; ++i) { + const connection = selection.connections[i]; if (connection.registrations[item.name]) { if (item.options.once) { continue; @@ -422,7 +422,7 @@ internals.Plugin.prototype.ext = function (events) { // (event, method, o events = Schema.apply('exts', events); - for (let i = 0, il = events.length; i < il; ++i) { + for (let i = 0; i < events.length; ++i) { this._ext(events[i]); } }; @@ -552,7 +552,7 @@ internals.Plugin.prototype.table = function (host) { Hoek.assert(this.connections, 'Cannot request routing table from a connectionless plugin'); const table = []; - for (let i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0; i < this.connections.length; ++i) { const connection = this.connections[i]; table.push({ info: connection.info, labels: connection.settings.labels, table: connection.table(host) }); } @@ -566,7 +566,7 @@ internals.Plugin.prototype._apply = function (type, func, args) { Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); - for (let i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0; i < this.connections.length; ++i) { func.apply(this.connections[i], args); } }; @@ -577,7 +577,7 @@ internals.Plugin.prototype._applyChild = function (type, child, func, args) { Hoek.assert(this.connections, 'Cannot add ' + type + ' from a connectionless plugin'); Hoek.assert(this.connections.length, 'Cannot add ' + type + ' without a connection'); - for (let i = 0, il = this.connections.length; i < il; ++i) { + for (let i = 0; i < this.connections.length; ++i) { const obj = this.connections[i][child]; obj[func].apply(obj, args); } diff --git a/lib/reply.js b/lib/reply.js index 6a49d8c0a..aacbfc522 100755 --- a/lib/reply.js +++ b/lib/reply.js @@ -85,7 +85,7 @@ internals.Reply.prototype.interface = function (request, realm, next) { // if (this._decorations) { const methods = Object.keys(this._decorations); - for (let i = 0, il = methods.length; i < il; ++i) { + for (let i = 0; i < methods.length; ++i) { const method = methods[i]; reply[method] = this._decorations[method]; } diff --git a/lib/request.js b/lib/request.js index dae980b8f..330e7d669 100755 --- a/lib/request.js +++ b/lib/request.js @@ -36,7 +36,7 @@ internals.Generator.prototype.request = function (connection, req, res, options) if (this._decorations) { const methods = Object.keys(this._decorations); - for (let i = 0, il = methods.length; i < il; ++i) { + for (let i = 0; i < methods.length; ++i) { const method = methods[i]; request[method] = this._decorations[method]; } @@ -282,12 +282,12 @@ internals.Request.prototype.getLog = function (tags, internal) { const filter = tags.length ? Hoek.mapToObject(tags) : null; const result = []; - for (let i = 0, il = this._logger.length; i < il; ++i) { + for (let i = 0; i < this._logger.length; ++i) { const event = this._logger[i]; if (internal === undefined || event.internal === internal) { if (filter) { - for (let t = 0, tl = event.tags.length; t < tl; ++t) { - const tag = event.tags[t]; + for (let j = 0; j < event.tags.length; ++j) { + const tag = event.tags[j]; if (filter[tag]) { result.push(event); break; diff --git a/lib/response.js b/lib/response.js index bd1587b3d..b1e72b642 100755 --- a/lib/response.js +++ b/lib/response.js @@ -125,11 +125,11 @@ internals.Response.prototype._header = function (key, value, options) { value !== null) { const headerValues = [key].concat(value); - for (let v = 0, vl = headerValues.length; v < vl; ++v) { - const header = headerValues[v]; + for (let i = 0; i < headerValues.length; ++i) { + const header = headerValues[i]; const buffer = Buffer.isBuffer(header) ? header : new Buffer(header.toString()); - for (let b = 0, bl = buffer.length; b < bl; ++b) { - Hoek.assert((buffer[b] & 0x7f) === buffer[b], 'Header value cannot contain or convert into non-ascii characters:', key); + for (let j = 0; j < buffer.length; ++j) { + Hoek.assert((buffer[j] & 0x7f) === buffer[j], 'Header value cannot contain or convert into non-ascii characters:', key); } } } @@ -147,7 +147,7 @@ internals.Response.prototype._header = function (key, value, options) { const existing = this.headers[key]; if (!duplicate) { const values = existing.split(separator); - for (let i = 0, il = values.length; i < il; ++i) { + for (let i = 0; i < values.length; ++i) { if (values[i] === value) { return this; } @@ -426,13 +426,13 @@ internals.Response.prototype._passThrough = function () { const localHeaders = this.headers; this.headers = {}; - for (let i = 0, il = headerKeys.length; i < il; ++i) { + for (let i = 0; i < headerKeys.length; ++i) { const key = headerKeys[i]; this.header(key.toLowerCase(), Hoek.clone(this.source.headers[key])); // Clone arrays } headerKeys = Object.keys(localHeaders); - for (let i = 0, il = headerKeys.length; i < il; ++i) { + for (let i = 0; i < headerKeys.length; ++i) { const key = headerKeys[i]; this.header(key, localHeaders[key], { append: key === 'set-cookie' }); } diff --git a/lib/route.js b/lib/route.js index 67ba711da..3ed1247be 100755 --- a/lib/route.js +++ b/lib/route.js @@ -120,7 +120,7 @@ exports = module.exports = internals.Route = function (options, connection, plug } else { this.settings.response.schema = internals.compileRule(rule); - for (let i = 0, il = statuses.length; i < il; ++i) { + for (let i = 0; i < statuses.length; ++i) { const code = statuses[i]; this.settings.response.status[code] = internals.compileRule(this.settings.response.status[code]); } @@ -230,7 +230,7 @@ internals.Route.prototype._combineExtensions = function (type, subscribe) { const events = this.settings.ext[type]; if (events) { - for (let i = 0, il = events.length; i < il; ++i) { + for (let i = 0; i < events.length; ++i) { const event = Hoek.shallow(events[i]); Hoek.assert(!event.options.sandbox, 'Cannot specify sandbox option for route extension'); event.plugin = this.plugin; @@ -363,7 +363,7 @@ internals.state = function (request, next) { // Clear cookies - for (let i = 0, il = failed.length; i < il; ++i) { + for (let i = 0; i < failed.length; ++i) { const item = failed[i]; if (item.settings.clearInvalid) { diff --git a/lib/server.js b/lib/server.js index 4f34729f7..70644aad1 100755 --- a/lib/server.js +++ b/lib/server.js @@ -62,7 +62,7 @@ exports = module.exports = internals.Server = function (options) { if (options.cache) { const caches = [].concat(options.cache); - for (let i = 0, il = caches.length; i < il; ++i) { + for (let i = 0; i < caches.length; ++i) { this._createCache(caches[i]); } } @@ -124,7 +124,7 @@ internals.Server.prototype.connection = function (options) { root._single(); const registrations = Object.keys(root._registrations); - for (let i = 0, il = registrations.length; i < il; ++i) { + for (let i = 0; i < registrations.length; ++i) { const name = registrations[i]; connection.registrations[name] = root._registrations[name]; } @@ -188,13 +188,13 @@ internals.Server.prototype.initialize = function (callback) { // Assert dependencies - for (let i = 0, il = this._dependencies.length; i < il; ++i) { + for (let i = 0; i < this._dependencies.length; ++i) { const dependency = this._dependencies[i]; if (dependency.connections) { - for (let s = 0, sl = dependency.connections.length; s < sl; ++s) { - const connection = dependency.connections[s]; - for (let d = 0, dl = dependency.deps.length; d < dl; ++d) { - const dep = dependency.deps[d]; + for (let j = 0; j < dependency.connections.length; ++j) { + const connection = dependency.connections[j]; + for (let k = 0; k < dependency.deps.length; ++k) { + const dep = dependency.deps[k]; if (!connection.registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep + ' in connection: ' + connection.info.uri)); } @@ -202,8 +202,8 @@ internals.Server.prototype.initialize = function (callback) { } } else { - for (let d = 0, dl = dependency.deps.length; d < dl; ++d) { - const dep = dependency.deps[d]; + for (let j = 0; j < dependency.deps.length; ++j) { + const dep = dependency.deps[j]; if (!this._registrations[dep]) { return errorCallback(new Error('Plugin ' + dependency.plugin + ' missing dependency ' + dep)); } @@ -318,7 +318,7 @@ internals.Server.prototype.stop = function (/* [options], callback */) { } const caches = Object.keys(self._caches); - for (let i = 0, il = caches.length; i < il; ++i) { + for (let i = 0; i < caches.length; ++i) { self._caches[caches[i]].client.stop(); } diff --git a/lib/transmit.js b/lib/transmit.js index f62dc0652..f7c8c8f6d 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -56,7 +56,7 @@ internals.marshal = function (request, next) { // Strong verifier const ifNoneMatch = request.headers['if-none-match'].split(/\s*,\s*/); - for (let i = 0, il = ifNoneMatch.length; i < il; ++i) { + for (let i = 0; i < ifNoneMatch.length; ++i) { const etag = ifNoneMatch[i]; if (etag === response.headers.etag) { response.code(304); @@ -266,8 +266,8 @@ internals.transmit = function (response, callback) { // Write headers const headers = Object.keys(response.headers); - for (let h = 0, hl = headers.length; h < hl; ++h) { - const header = headers[h]; + for (let i = 0; i < headers.length; ++i) { + const header = headers[i]; const value = response.headers[header]; if (value !== undefined) { request.raw.res.setHeader(header, value); @@ -457,7 +457,7 @@ internals.state = function (response, next) { const states = []; const requestStates = Object.keys(request._states); - for (let i = 0, il = requestStates.length; i < il; ++i) { + for (let i = 0; i < requestStates.length; ++i) { const stateName = requestStates[i]; names[stateName] = true; states.push(request._states[stateName]); diff --git a/lib/validation.js b/lib/validation.js index 61a91b844..656e1f6f0 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -74,15 +74,15 @@ internals.input = function (source, request, next) { const error = Boom.badRequest(err.message, err); error.output.payload.validation = { source: source, keys: [] }; if (err.details) { - for (let i = 0, il = err.details.length; i < il; ++i) { + for (let i = 0; i < err.details.length; ++i) { error.output.payload.validation.keys.push(Hoek.escapeHtml(err.details[i].path)); } } if (request.route.settings.validate.errorFields) { const fields = Object.keys(request.route.settings.validate.errorFields); - for (let f = 0, fl = fields.length; f < fl; ++f) { - const field = fields[f]; + for (let i = 0; i < fields.length; ++i) { + const field = fields[i]; error.output.payload[field] = request.route.settings.validate.errorFields[field]; } } diff --git a/test/connection.js b/test/connection.js index 10f5e08af..b47a21d1e 100755 --- a/test/connection.js +++ b/test/connection.js @@ -1350,7 +1350,7 @@ describe('Connection', function () { let count = 0; const lines = lsof.split('\n'); - for (let i = 0, il = lines.length; i < il; ++i) { + for (let i = 0; i < lines.length; ++i) { count += !!lines[i].match(/package.json/); } diff --git a/test/payload.js b/test/payload.js index b686dc722..784e22d7c 100755 --- a/test/payload.js +++ b/test/payload.js @@ -568,7 +568,7 @@ describe('payload', function () { const result = {}; const keys = Object.keys(request.payload); - for (let i = 0, il = keys.length; i < il; ++i) { + for (let i = 0; i < keys.length; ++i) { const key = keys[i]; const value = request.payload[key]; result[key] = value._readableState ? true : value; diff --git a/test/plugin.js b/test/plugin.js index edd3ee3c8..fb0329aaa 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -4002,10 +4002,10 @@ internals.routesList = function (server, label) { const tables = server.select(label || []).table(); const list = []; - for (let c = 0, cl = tables.length; c < cl; ++c) { - const routes = tables[c].table; - for (let i = 0, il = routes.length; i < il; ++i) { - const route = routes[i]; + for (let i = 0; i < tables.length; ++i) { + const routes = tables[i].table; + for (let j = 0; j < routes.length; ++j) { + const route = routes[j]; if (route.method === 'get') { list.push(route.path); } diff --git a/test/transmit.js b/test/transmit.js index 2e0d783ea..5795c2f15 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -105,7 +105,7 @@ describe('transmission', function () { let count = 0; const lines = lsof.split('\n'); - for (let i = 0, il = lines.length; i < il; ++i) { + for (let i = 0; i < lines.length; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -146,7 +146,7 @@ describe('transmission', function () { let count = 0; const lines = lsof.split('\n'); - for (let i = 0, il = lines.length; i < il; ++i) { + for (let i = 0; i < lines.length; ++i) { count += (lines[i].match(/package.json/) === null ? 0 : 1); } @@ -1507,7 +1507,7 @@ describe('transmission', function () { const block = Fs.readFileSync(filePath).toString(); let expectedBody = ''; - for (let i = 0, il = chunkTimes; i < il; ++i) { + for (let i = 0; i < chunkTimes; ++i) { expectedBody += block; } @@ -1552,7 +1552,7 @@ describe('transmission', function () { const block = Fs.readFileSync(filePath).toString(); let expectedBody = ''; - for (let i = 0, il = chunkTimes; i < il; ++i) { + for (let i = 0; i < chunkTimes; ++i) { expectedBody += block; } From 7ec0ae33364a29d20335d6adde7d0f7ce282edd5 Mon Sep 17 00:00:00 2001 From: Fabriece Sumuni Date: Fri, 23 Oct 2015 22:16:41 +0200 Subject: [PATCH 0101/1139] Update API.md fixed a typo. --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 88e98a38a..652a4a5f6 100755 --- a/API.md +++ b/API.md @@ -2583,7 +2583,7 @@ Each request object includes the following properties: potential conflicts with the framework. Should not be used by [plugins](#plugins) which should use `plugins[name]`. - `auth` - authentication information: - - `isAuthenticated` - `true` is the request has been successfully authenticated, otherwise + - `isAuthenticated` - `true` if the request has been successfully authenticated, otherwise `false`. - `credentials` - the `credential` object received during the authentication process. The presence of an object does not mean successful authentication. From a7b3ad753febbacc10e17e352de6385dfc642dca Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Fri, 23 Oct 2015 13:55:10 -0700 Subject: [PATCH 0102/1139] Initial transition to arrow functions. For #2877 --- lib/auth.js | 26 ++-- lib/connection.js | 94 ++++++------ lib/handler.js | 25 ++-- lib/methods.js | 6 +- lib/plugin.js | 34 ++--- lib/protect.js | 14 +- lib/reply.js | 42 +++--- lib/request.js | 91 +++++------- lib/response.js | 22 ++- lib/route.js | 12 +- lib/server.js | 81 +++++------ lib/transmit.js | 27 ++-- lib/validation.js | 10 +- test/auth.js | 136 ++++++++--------- test/connection.js | 180 +++++++++++------------ test/cors.js | 66 ++++----- test/handler.js | 90 ++++++------ test/methods.js | 130 ++++++++--------- test/payload.js | 54 +++---- test/plugin.js | 356 ++++++++++++++++++++++----------------------- test/protect.js | 12 +- test/reply.js | 60 ++++---- test/request.js | 162 ++++++++++----------- test/response.js | 148 +++++++++---------- test/route.js | 60 ++++---- test/security.js | 16 +- test/server.js | 64 ++++---- test/state.js | 26 ++-- test/transmit.js | 254 ++++++++++++++++---------------- test/validation.js | 98 ++++++------- 30 files changed, 1184 insertions(+), 1212 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index eaa1d5422..106bc3981 100755 --- a/lib/auth.js +++ b/lib/auth.js @@ -96,7 +96,7 @@ internals.Auth.prototype.test = function (name, request, next) { const strategy = this._strategies[name]; Hoek.assert(strategy, 'Unknown authentication strategy:', name); - const transfer = function (response, data) { + const transfer = (response, data) => { return next(response, data && data.credentials); }; @@ -108,8 +108,6 @@ internals.Auth.prototype.test = function (name, request, next) { internals.Auth.prototype._setupRoute = function (options, path) { - const self = this; - if (!options) { return options; // Preseve the difference between undefined and false } @@ -148,9 +146,9 @@ internals.Auth.prototype._setupRoute = function (options, path) { } let hasAuthenticatePayload = false; - options.strategies.forEach(function (name) { + options.strategies.forEach((name) => { - const strategy = self._strategies[name]; + const strategy = this._strategies[name]; Hoek.assert(strategy, 'Unknown authentication strategy:', name, 'in path:', path); Hoek.assert(strategy.methods.payload || options.payload !== 'required', 'Payload validation can only be required when all strategies support it in path:', path); hasAuthenticatePayload = hasAuthenticatePayload || strategy.methods.payload; @@ -182,8 +180,6 @@ internals.Auth.authenticate = function (request, next) { internals.Auth.prototype._authenticate = function (request, next) { - const self = this; - const config = this.lookup(request.route); if (!config) { return next(); @@ -194,7 +190,7 @@ internals.Auth.prototype._authenticate = function (request, next) { const authErrors = []; let strategyPos = 0; - const authenticate = function () { + const authenticate = () => { // Find next strategy @@ -217,20 +213,20 @@ internals.Auth.prototype._authenticate = function (request, next) { const name = config.strategies[strategyPos]; ++strategyPos; - request._protect.run(validate, function (exit) { + request._protect.run(validate, (exit) => { - const transfer = function (response, data) { + const transfer = (response, data) => { exit(response, name, data); }; - const strategy = self._strategies[name]; + const strategy = this._strategies[name]; const reply = request.server._replier.interface(request, strategy.realm, transfer); strategy.methods.authenticate(request, reply); }); }; - const validate = function (err, name, result) { // err can be Boom, Error, or a valid response object + const validate = (err, name, result) => { // err can be Boom, Error, or a valid response object if (!name) { return next(err); @@ -372,7 +368,7 @@ internals.Auth.payload = function (request, next) { return next(); } - const finalize = function (response) { + const finalize = (response) => { if (response && response.isBoom && @@ -384,7 +380,7 @@ internals.Auth.payload = function (request, next) { return next(response); }; - request._protect.run(finalize, function (exit) { + request._protect.run(finalize, (exit) => { const reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.payload(request, reply); @@ -408,7 +404,7 @@ internals.Auth.response = function (request, next) { return next(); } - request._protect.run(next, function (exit) { + request._protect.run(next, (exit) => { const reply = request.server._replier.interface(request, strategy.realm, exit); strategy.methods.response(request, reply); diff --git a/lib/connection.js b/lib/connection.js index 2ab6c699f..45d45264b 100755 --- a/lib/connection.js +++ b/lib/connection.js @@ -30,8 +30,6 @@ const internals = { exports = module.exports = internals.Connection = function (server, options) { - const self = this; - const now = Date.now(); Events.EventEmitter.call(this); @@ -92,9 +90,9 @@ exports = module.exports = internals.Connection = function (server, options) { this.listener.on('request', this._dispatch()); this._init(); - this.listener.on('clientError', function (err, socket) { + this.listener.on('clientError', (err, socket) => { - self.server._log(['connection', 'client', 'error'], err); + this.server._log(['connection', 'client', 'error'], err); }); // Connection information @@ -118,41 +116,37 @@ Hoek.inherits(internals.Connection, Events.EventEmitter); internals.Connection.prototype._init = function () { - const self = this; - // Setup listener - this.listener.once('listening', function () { + this.listener.once('listening', () => { // Update the address, port, and uri with active values - if (self.type === 'tcp') { - const address = self.listener.address(); - self.info.address = address.address; - self.info.port = address.port; - self.info.uri = (self.settings.uri || (self.info.protocol + '://' + self.info.host + ':' + self.info.port)); + if (this.type === 'tcp') { + const address = this.listener.address(); + this.info.address = address.address; + this.info.port = address.port; + this.info.uri = (this.settings.uri || (this.info.protocol + '://' + this.info.host + ':' + this.info.port)); } - self._onConnection = function (connection) { + this._onConnection = (connection) => { const key = connection.remoteAddress + ':' + connection.remotePort; - self._connections[key] = connection; + this._connections[key] = connection; - connection.once('close', function () { + connection.once('close', () => { - delete self._connections[key]; + delete this._connections[key]; }); }; - self.listener.on('connection', self._onConnection); + this.listener.on('connection', this._onConnection); }); }; internals.Connection.prototype._start = function (callback) { - const self = this; - if (this._started) { return process.nextTick(callback); } @@ -164,17 +158,17 @@ internals.Connection.prototype._start = function (callback) { return process.nextTick(callback); } - const onError = function (err) { + const onError = (err) => { - self._started = false; + this._started = false; return callback(err); }; this.listener.once('error', onError); - const finalize = function () { + const finalize = () => { - self.listener.removeListener('error', onError); + this.listener.removeListener('error', onError); callback(); }; @@ -190,8 +184,6 @@ internals.Connection.prototype._start = function (callback) { internals.Connection.prototype._stop = function (options, callback) { - const self = this; - if (!this._started) { return process.nextTick(callback); } @@ -199,23 +191,23 @@ internals.Connection.prototype._stop = function (options, callback) { this._started = false; this.info.started = 0; - const timeoutId = setTimeout(function () { + const timeoutId = setTimeout(() => { - Object.keys(self._connections).forEach(function (key) { + Object.keys(this._connections).forEach((key) => { - self._connections[key].destroy(); + this._connections[key].destroy(); }); - self._connections = {}; + this._connections = {}; }, options.timeout); - this.listener.close(function () { + this.listener.close(() => { - self.listener.removeListener('connection', self._onConnection); + this.listener.removeListener('connection', this._onConnection); clearTimeout(timeoutId); - self._init(); + this._init(); return callback(); }); }; @@ -223,13 +215,11 @@ internals.Connection.prototype._stop = function (options, callback) { internals.Connection.prototype._dispatch = function (options) { - const self = this; - options = options || {}; - return function (req, res) { + return (req, res) => { - if (!self._started && + if (!this._started && !Shot.isInjection(req)) { return req.connection.end(); @@ -237,20 +227,20 @@ internals.Connection.prototype._dispatch = function (options) { // Create request - const request = self.server._requestor.request(self, req, res, options); + const request = this.server._requestor.request(this, req, res, options); // Check load - const overload = self._load.check(); + const overload = this._load.check(); if (overload) { - self.server._log(['load'], self.server.load); + this.server._log(['load'], this.server.load); request._reply(overload); } else { // Execute request lifecycle - request._protect.enter(function () { + request._protect.enter(() => { request._execute(); }); @@ -284,7 +274,7 @@ internals.Connection.prototype.inject = function (options, callback) { allowInternals: options.allowInternals }); - Shot.inject(needle, settings, function (res) { + Shot.inject(needle, settings, (res) => { if (res.raw.res._hapi) { res.result = res.raw.res._hapi.result; @@ -390,10 +380,7 @@ internals.Connection.prototype._defaultRoutes = function () { path: '/{p*}', config: { auth: false, // Override any defaults - handler: function (request, reply) { - - return reply(Boom.notFound()); - } + handler: internals.notFound } }, this, this.server); @@ -404,10 +391,7 @@ internals.Connection.prototype._defaultRoutes = function () { path: '/{p*}', config: { auth: false, // Override any defaults - handler: function (request, reply) { - - return reply(Boom.badRequest()); - } + handler: internals.badRequest } }, this, this.server); @@ -417,3 +401,15 @@ internals.Connection.prototype._defaultRoutes = function () { Cors.handler(this); } }; + + +internals.notFound = function (request, reply) { + + return reply(Boom.notFound()); +}; + + +internals.badRequest = function (request, reply) { + + return reply(Boom.badRequest()); +}; diff --git a/lib/handler.js b/lib/handler.js index e9af83393..672a6d50a 100755 --- a/lib/handler.js +++ b/lib/handler.js @@ -15,13 +15,13 @@ const internals = {}; exports.execute = function (request, next) { - const finalize = function (err, result) { + const finalize = (err, result) => { request._setResponse(err || result); return next(); // Must not include an argument }; - request._protect.run(finalize, function (exit) { + request._protect.run(finalize, (exit) => { if (request._route._prerequisites) { internals.prerequisites(request, Hoek.once(exit)); @@ -35,11 +35,11 @@ exports.execute = function (request, next) { internals.prerequisites = function (request, callback) { - Items.serial(request._route._prerequisites, function (set, nextSet) { + const each = (set, nextSet) => { - Items.parallel(set, function (pre, next) { + Items.parallel(set, (pre, next) => { - pre(request, function (err, result) { + pre(request, (err, result) => { if (err) { return next(err); @@ -52,8 +52,9 @@ internals.prerequisites = function (request, callback) { return callback(null, result); }); }, nextSet); - }, - function (err) { + }; + + Items.serial(request._route._prerequisites, each, (err) => { if (err) { return callback(err); @@ -67,7 +68,7 @@ internals.prerequisites = function (request, callback) { internals.handler = function (request, callback) { const timer = new Hoek.Bench(); - const finalize = function (response, data) { + const finalize = (response, data) => { if (response === null) { // reply.continue() response = Response.wrap(null, request); @@ -208,13 +209,13 @@ internals.fromString = function (type, notation, server) { const argsNotation = !!methodParts[2]; const methodArgs = (argsNotation ? (methodParts[3] || '').split(/\s*\,\s*/) : null); - result.method = function (request, reply) { + result.method = (request, reply) => { if (!argsNotation) { return method(request, reply); // Method is already bound to context } - const finalize = function (err, value, cached, report) { + const finalize = (err, value, cached, report) => { if (report) { request._log([type, 'method', name], report); @@ -249,10 +250,10 @@ internals.pre = function (pre) { } */ - return function (request, next) { + return (request, next) => { const timer = new Hoek.Bench(); - const finalize = function (response, data) { + const finalize = (response, data) => { if (response === null) { // reply.continue() response = Response.wrap(null, request); diff --git a/lib/methods.js b/lib/methods.js index bd40d86fa..d99be2c57 100755 --- a/lib/methods.js +++ b/lib/methods.js @@ -96,12 +96,12 @@ internals.Methods.prototype._add = function (name, method, options, realm) { // Promise object - const onFulfilled = function (outcome) { + const onFulfilled = (outcome) => { return methodNext(null, outcome); }; - const onRejected = function (err) { + const onRejected = (err) => { return methodNext(err); }; @@ -121,7 +121,7 @@ internals.Methods.prototype._add = function (name, method, options, realm) { Hoek.assert(!settings.cache.generateFunc, 'Cannot set generateFunc with method caching:', name); Hoek.assert(settings.cache.generateTimeout !== undefined, 'Method caching requires a timeout value in generateTimeout:', name); - settings.cache.generateFunc = function (id, next) { + settings.cache.generateFunc = (id, next) => { id.args.push(next); // function (err, result, ttl) normalized.apply(bind, id.args); diff --git a/lib/plugin.js b/lib/plugin.js index 645fa3648..17155045e 100755 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -19,8 +19,6 @@ const internals = {}; exports = module.exports = internals.Plugin = function (server, connections, env, parent) { // env can be a realm or plugin name - const self = this; - Kilt.call(this, connections, server._events); this._parent = parent; @@ -60,19 +58,19 @@ exports = module.exports = internals.Plugin = function (server, connections, env }; this.auth = { - default: function (opts) { + default: (opts) => { - self._applyChild('auth.default', 'auth', 'default', [opts]); + this._applyChild('auth.default', 'auth', 'default', [opts]); }, - scheme: function (name, scheme) { + scheme: (name, scheme) => { - self._applyChild('auth.scheme', 'auth', 'scheme', [name, scheme]); + this._applyChild('auth.scheme', 'auth', 'scheme', [name, scheme]); }, - strategy: function (name, scheme, mode, opts) { + strategy: (name, scheme, mode, opts) => { - self._applyChild('auth.strategy', 'auth', 'strategy', [name, scheme, mode, opts]); + this._applyChild('auth.strategy', 'auth', 'strategy', [name, scheme, mode, opts]); }, - test: function (name, request, next) { + test: (name, request, next) => { return request.connection.auth.test(name, request, next); } @@ -159,8 +157,6 @@ internals.Plugin.prototype._clone = function (connections, plugin) { internals.Plugin.prototype.register = function (plugins /*, [options], callback */) { - const self = this; - let options = (typeof arguments[1] === 'object' ? arguments[1] : {}); const callback = (typeof arguments[1] === 'object' ? arguments[2] : arguments[1]); @@ -248,9 +244,9 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback this.root._registring = true; - Items.serial(registrations, function (item, next) { + const each = (item, next) => { - const selection = self._select(item.options.select, item.name); + const selection = this._select(item.options.select, item.name); selection.realm.modifiers.route.prefix = item.options.routes.prefix; selection.realm.modifiers.route.vhost = item.options.routes.vhost; selection.realm.pluginOptions = item.pluginOptions || {}; @@ -265,7 +261,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback // Protect against multiple registrations if (!item.connections) { - if (self.root._registrations[item.name]) { + if (this.root._registrations[item.name]) { if (item.options.once) { return next(); } @@ -273,7 +269,7 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback Hoek.assert(item.multiple, 'Plugin', item.name, 'already registered'); } else { - self.root._registrations[item.name] = registrationData; + this.root._registrations[item.name] = registrationData; } } @@ -311,15 +307,17 @@ internals.Plugin.prototype.register = function (plugins /*, [options], callback } if (!item.connections) { - selection.connection = self.connection; + selection.connection = this.connection; } // Register item.register(selection, item.pluginOptions || {}, next); - }, function (err) { + }; + + Items.serial(registrations, each, (err) => { - self.root._registring = false; + this.root._registring = false; return callback(err); }); }; diff --git a/lib/protect.js b/lib/protect.js index 89ddb404a..b9a2842b0 100755 --- a/lib/protect.js +++ b/lib/protect.js @@ -14,8 +14,6 @@ const internals = {}; exports = module.exports = internals.Protect = function (request) { - const self = this; - this._error = null; this.logger = request; // Replaced with server when request completes @@ -27,9 +25,9 @@ exports = module.exports = internals.Protect = function (request) { Domain = Domain || require('domain'); this.domain = Domain.create(); - this.domain.on('error', function (err) { + this.domain.on('error', (err) => { - return self._onError(err); + return this._onError(err); }); }; @@ -48,11 +46,9 @@ internals.Protect.prototype._onError = function (err) { internals.Protect.prototype.run = function (next, enter) { // enter: function (exit) - const self = this; - - const finish = Hoek.once(function (arg0, arg1, arg2) { + const finish = Hoek.once((arg0, arg1, arg2) => { - self._error = null; + this._error = null; return next(arg0, arg1, arg2); }); @@ -60,7 +56,7 @@ internals.Protect.prototype.run = function (next, enter) { // enter return enter(finish); } - this._error = function (err) { + this._error = (err) => { return finish(Boom.badImplementation('Uncaught error', err)); }; diff --git a/lib/reply.js b/lib/reply.js index aacbfc522..61fa7bf25 100755 --- a/lib/reply.js +++ b/lib/reply.js @@ -64,7 +64,7 @@ internals.Reply.prototype.decorate = function (property, method) { internals.Reply.prototype.interface = function (request, realm, next) { // next(err || response, data); - const reply = function (err, response, data) { + const reply = (err, response, data) => { reply._data = data; // Held for later return reply.response(err !== null && err !== undefined ? err : response); @@ -129,8 +129,6 @@ internals.redirect = function (location) { internals.response = function (result) { - const self = this; - Hoek.assert(!this._replied, 'reply interface called twice'); this._replied = true; @@ -141,30 +139,36 @@ internals.response = function (result) { return response; } - response.hold = function () { - - this.hold = undefined; - this.send = function () { - - this.send = undefined; - this._prepare(self._data, self._next); - this._next = null; - }; - - return this; - }; + response.hold = internals.hold(this); - process.nextTick(function () { + process.nextTick(() => { response.hold = undefined; if (!response.send && - self._next) { + this._next) { - response._prepare(self._data, self._next); - self._next = null; + response._prepare(this._data, this._next); + this._next = null; } }); return response; }; + + +internals.hold = function (reply) { + + return function () { + + this.hold = undefined; + this.send = () => { + + this.send = undefined; + this._prepare(reply._data, reply._next); + this._next = null; + }; + + return this; + }; +}; diff --git a/lib/request.js b/lib/request.js index 330e7d669..3563dd168 100755 --- a/lib/request.js +++ b/lib/request.js @@ -58,8 +58,6 @@ internals.Generator.prototype.decorate = function (property, method) { internals.Request = function (connection, req, res, options) { - const self = this; - Events.EventEmitter.call(this); // Take measurement as soon as possible @@ -166,26 +164,26 @@ internals.Request = function (connection, req, res, options) { // Listen to request state - this._onEnd = function () { + this._onEnd = () => { - self._isPayloadPending = false; + this._isPayloadPending = false; }; this.raw.req.once('end', this._onEnd); - this._onClose = function () { + this._onClose = () => { - self._log(['request', 'closed', 'error']); - self._isPayloadPending = false; - self._isBailed = true; + this._log(['request', 'closed', 'error']); + this._isPayloadPending = false; + this._isBailed = true; }; this.raw.req.once('close', this._onClose); - this._onError = function (err) { + this._onError = (err) => { - self._log(['request', 'error'], err); - self._isPayloadPending = false; + this._log(['request', 'error'], err); + this._isPayloadPending = false; }; this.raw.req.once('error', this._onError); @@ -306,25 +304,21 @@ internals.Request.prototype.getLog = function (tags, internal) { internals.Request.prototype._execute = function () { - const self = this; - // Execute onRequest extensions (can change request method and url) if (!this.connection._extensions.onRequest.nodes) { return this._lifecycle(); } - this._invoke(this.connection._extensions.onRequest, function (err) { + this._invoke(this.connection._extensions.onRequest, (err) => { - return self._lifecycle(err); + return this._lifecycle(err); }); }; internals.Request.prototype._lifecycle = function (err) { - const self = this; - // Undecorate request this.setUrl = undefined; @@ -364,10 +358,10 @@ internals.Request.prototype._lifecycle = function (err) { let serverTimeout = this.route.settings.timeout.server; if (serverTimeout) { serverTimeout = Math.floor(serverTimeout - this._bench.elapsed()); // Calculate the timeout from when the request was constructed - const timeoutReply = function () { + const timeoutReply = () => { - self._log(['request', 'server', 'timeout', 'error'], { timeout: serverTimeout, elapsed: self._bench.elapsed() }); - self._reply(Boom.serverTimeout()); + this._log(['request', 'server', 'timeout', 'error'], { timeout: serverTimeout, elapsed: this._bench.elapsed() }); + this._reply(Boom.serverTimeout()); }; if (serverTimeout <= 0) { @@ -377,39 +371,38 @@ internals.Request.prototype._lifecycle = function (err) { this._serverTimeoutId = setTimeout(timeoutReply, serverTimeout); } - Items.serial(this._route._cycle, function (func, next) { + const each = (func, next) => { - if (self._isReplied || - self._isBailed) { + if (this._isReplied || + this._isBailed) { return next(Boom.internal('Already closed')); // Error is not used } if (typeof func !== 'function') { // Extension point - return self._invoke(func, next); + return this._invoke(func, next); } - return func(self, next); - }, - function (err) { + return func(this, next); + }; + + Items.serial(this._route._cycle, each, (err) => { - return self._reply(err); + return this._reply(err); }); }; internals.Request.prototype._invoke = function (event, callback) { - const self = this; - - this._protect.run(callback, function (exit) { + this._protect.run(callback, (exit) => { - Items.serial(event.nodes, function (ext, next) { + Items.serial(event.nodes, (ext, next) => { - const reply = self.server._replier.interface(self, ext.plugin.realm, next); + const reply = this.server._replier.interface(this, ext.plugin.realm, next); const bind = (ext.bind || ext.plugin.realm.settings.bind); - ext.func.call(bind, self, reply); + ext.func.call(bind, this, reply); }, exit); }); }; @@ -417,8 +410,6 @@ internals.Request.prototype._invoke = function (event, callback) { internals.Request.prototype._reply = function (exit) { - const self = this; - if (this._isReplied) { // Prevent any future responses to this request return; } @@ -447,15 +438,15 @@ internals.Request.prototype._reply = function (exit) { this._protect.reset(); - const transmit = function (err) { + const transmit = (err) => { if (err) { // err can be valid response or error - self._setResponse(Response.wrap(err, self)); + this._setResponse(Response.wrap(err, this)); } - Transmit.send(self, function () { + Transmit.send(this, () => { - return self._finalize(); + return this._finalize(); }); }; @@ -529,30 +520,28 @@ internals.Request.prototype._setResponse = function (response) { internals.Request.prototype._addTail = function (name) { - const self = this; - name = name || 'unknown'; const tailId = this._tailIds++; this._tails[tailId] = name; this._log(['tail', 'add'], { name: name, id: tailId }); - const drop = function () { + const drop = () => { - if (!self._tails[tailId]) { - self._log(['tail', 'remove', 'error'], { name: name, id: tailId }); // Already removed + if (!this._tails[tailId]) { + this._log(['tail', 'remove', 'error'], { name: name, id: tailId }); // Already removed return; } - delete self._tails[tailId]; + delete this._tails[tailId]; - if (Object.keys(self._tails).length === 0 && - self._isFinalized) { + if (Object.keys(this._tails).length === 0 && + this._isFinalized) { - self._log(['tail', 'remove', 'last'], { name: name, id: tailId }); - self.connection.emit('tail', self); + this._log(['tail', 'remove', 'last'], { name: name, id: tailId }); + this.connection.emit('tail', this); } else { - self._log(['tail', 'remove'], { name: name, id: tailId }); + this._log(['tail', 'remove'], { name: name, id: tailId }); } }; diff --git a/lib/response.js b/lib/response.js index b1e72b642..493cd7ae8 100755 --- a/lib/response.js +++ b/lib/response.js @@ -381,15 +381,13 @@ internals.Response.prototype.takeover = function () { internals.Response.prototype._prepare = function (data, next) { - const self = this; - this._passThrough(); if (this.variety !== 'promise') { return this._processPrepare(data, next); } - const onDone = function (source) { + const onDone = (source) => { if (source instanceof Error) { return next(Boom.wrap(source), data); @@ -399,9 +397,9 @@ internals.Response.prototype._prepare = function (data, next) { return source._processPrepare(data, next); } - self._setSource(source); - self._passThrough(); - self._processPrepare(data, next); + this._setSource(source); + this._passThrough(); + this._processPrepare(data, next); }; this.source.then(onDone, onDone); @@ -450,7 +448,7 @@ internals.Response.prototype._processPrepare = function (data, next) { return next(this, data); } - this._processors.prepare(this, function (prepared) { + this._processors.prepare(this, (prepared) => { return next(prepared, data); }); @@ -459,19 +457,17 @@ internals.Response.prototype._processPrepare = function (data, next) { internals.Response.prototype._marshal = function (next) { - const self = this; - if (!this._processors.marshal) { return this._streamify(this.source, next); } - this._processors.marshal(this, function (err, source) { + this._processors.marshal(this, (err, source) => { if (err) { return next(err); } - return self._streamify(source, next); + return this._streamify(source, next); }); }; @@ -541,12 +537,12 @@ internals.Response.prototype._close = function () { stream.destroy(); } else { - const read = function () { + const read = () => { stream.read(); }; - const end = function () { + const end = () => { stream.removeListener('readable', read); stream.removeListener('error', end); diff --git a/lib/route.js b/lib/route.js index 3ed1247be..678b3cefb 100755 --- a/lib/route.js +++ b/lib/route.js @@ -99,7 +99,7 @@ exports = module.exports = internals.Route = function (options, connection, plug validation.payload = null; } - ['headers', 'params', 'query', 'payload'].forEach(function (type) { + ['headers', 'params', 'query', 'payload'].forEach((type) => { validation[type] = internals.compileRule(validation[type]); }); @@ -357,7 +357,7 @@ internals.state = function (request, next) { return next(); } - request.connection.states.parse(cookies, function (err, state, failed) { + request.connection.states.parse(cookies, (err, state, failed) => { request.state = state || {}; @@ -393,7 +393,7 @@ internals.payload = function (request, next) { return next(); } - const onParsed = function (err, parsed) { + const onParsed = (err, parsed) => { request.mime = parsed.mime; request.payload = parsed.payload || null; @@ -414,7 +414,7 @@ internals.payload = function (request, next) { return next(); }; - Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, function (err, parsed) { + Subtext.parse(request.raw.req, request._tap(), request.route.settings.payload, (err, parsed) => { if (!err || !request._isPayloadPending) { @@ -427,12 +427,12 @@ internals.payload = function (request, next) { const stream = request.raw.req; - const read = function () { + const read = () => { stream.read(); }; - const end = function () { + const end = () => { stream.removeListener('readable', read); stream.removeListener('error', end); diff --git a/lib/server.js b/lib/server.js index 70644aad1..b983103af 100755 --- a/lib/server.js +++ b/lib/server.js @@ -135,8 +135,6 @@ internals.Server.prototype.connection = function (options) { internals.Server.prototype.start = function (callback) { - const self = this; - Hoek.assert(typeof callback === 'function', 'Missing required start callback function'); if (this._state === 'initialized') { @@ -144,7 +142,7 @@ internals.Server.prototype.start = function (callback) { } if (this._state === 'started') { - Items.serial(this.connections, function (connectionItem, next) { + Items.serial(this.connections, (connectionItem, next) => { connectionItem._start(next); }, callback); @@ -156,21 +154,19 @@ internals.Server.prototype.start = function (callback) { return Hoek.nextTick(callback)(new Error('Cannot start server while it is in ' + this._state + ' state')); } - this.initialize(function (err) { + this.initialize((err) => { if (err) { return callback(err); } - self._start(callback); + this._start(callback); }); }; internals.Server.prototype.initialize = function (callback) { - const self = this; - Hoek.assert(callback, 'Missing start callback function'); const errorCallback = Hoek.nextTick(callback); @@ -215,34 +211,35 @@ internals.Server.prototype.initialize = function (callback) { // Start cache - const caches = Object.keys(self._caches); - Items.parallel(caches, function (cache, next) { + const each = (cache, next) => { - self._caches[cache].client.start(next); - }, - function (err) { + this._caches[cache].client.start(next); + }; + + const caches = Object.keys(this._caches); + Items.parallel(caches, each, (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } // After hooks - self._invoke('onPreStart', function (err) { + this._invoke('onPreStart', (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } // Load measurements - self._heavy.start(); + this._heavy.start(); // Listen to connections - self._state = 'initialized'; + this._state = 'initialized'; return callback(); }); }); @@ -251,30 +248,29 @@ internals.Server.prototype.initialize = function (callback) { internals.Server.prototype._start = function (callback) { - const self = this; - this._state = 'starting'; - Items.serial(this.connections, function (connectionItem, next) { + const each = (connectionItem, next) => { connectionItem._start(next); - }, - function (err) { + }; + + Items.serial(this.connections, each, (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } - self._events.emit('start'); - self._invoke('onPostStart', function (err) { + this._events.emit('start'); + this._invoke('onPostStart', (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } - self._state = 'started'; + this._state = 'started'; return callback(); }); }); @@ -283,8 +279,6 @@ internals.Server.prototype._start = function (callback) { internals.Server.prototype.stop = function (/* [options], callback */) { - const self = this; - Hoek.assert(arguments.length, 'Missing required stop callback function'); const callback = (arguments.length === 1 ? arguments[0] : arguments[1]); @@ -299,39 +293,40 @@ internals.Server.prototype.stop = function (/* [options], callback */) { this._state = 'stopping'; - this._invoke('onPreStop', function (err) { + this._invoke('onPreStop', (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } - Items.serial(self.connections, function (connection, next) { + const each = (connection, next) => { connection._stop(options, next); - }, - function (err) { + }; + + Items.serial(this.connections, each, (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } - const caches = Object.keys(self._caches); + const caches = Object.keys(this._caches); for (let i = 0; i < caches.length; ++i) { - self._caches[caches[i]].client.stop(); + this._caches[caches[i]].client.stop(); } - self._events.emit('stop'); - self._heavy.stop(); - self._invoke('onPostStop', function (err) { + this._events.emit('stop'); + this._heavy.stop(); + this._invoke('onPostStop', (err) => { if (err) { - self._state = 'invalid'; + this._state = 'invalid'; return callback(err); } - self._state = 'stopped'; + this._state = 'stopped'; return callback(); }); }); @@ -346,7 +341,7 @@ internals.Server.prototype._invoke = function (type, next) { return next(); } - Items.serial(exts.nodes, function (ext, nextExt) { + Items.serial(exts.nodes, (ext, nextExt) => { const bind = (ext.bind || ext.plugin.realm.settings.bind); ext.func.call(bind, ext.plugin._select(), nextExt); diff --git a/lib/transmit.js b/lib/transmit.js index f7c8c8f6d..5ab2d0d48 100755 --- a/lib/transmit.js +++ b/lib/transmit.js @@ -27,7 +27,7 @@ exports.send = function (request, callback) { return internals.fail(request, response, callback); } - internals.marshal(request, function (err) { + internals.marshal(request, (err) => { if (err) { request._setResponse(err); @@ -95,7 +95,7 @@ internals.marshal = function (request, next) { } } - internals.state(response, function (err) { + internals.state(response, (err) => { if (err) { request._log(['state', 'response', 'error'], err); @@ -121,7 +121,7 @@ internals.marshal = function (request, next) { return Auth.response(request, next); // Must be last in case requires access to headers } - response._marshal(function (err) { + response._marshal((err) => { if (err) { return next(Boom.wrap(err)); @@ -156,7 +156,7 @@ internals.fail = function (request, boom, callback) { response.headers = error.headers; request.response = response; // Not using request._setResponse() to avoid double log - internals.marshal(request, function (err) { + internals.marshal(request, (err) => { if (err) { @@ -283,7 +283,7 @@ internals.transmit = function (response, callback) { // Write payload let hasEnded = false; - const end = function (err, event) { + const end = (err, event) => { if (hasEnded) { return; @@ -318,12 +318,12 @@ internals.transmit = function (response, callback) { source.once('error', end); - const onAborted = function () { + const onAborted = () => { end(null, 'aborted'); }; - const onClose = function () { + const onClose = () => { end(null, 'close'); }; @@ -463,8 +463,7 @@ internals.state = function (response, next) { states.push(request._states[stateName]); } - const keys = Object.keys(request.connection.states.cookies); - Items.parallel(keys, function (name, nextKey) { + const each = (name, nextKey) => { const autoValue = request.connection.states.cookies[name].autoValue; if (!autoValue || names[name]) { @@ -478,7 +477,7 @@ internals.state = function (response, next) { return nextKey(); } - autoValue(request, function (err, value) { + autoValue(request, (err, value) => { if (err) { return nextKey(err); @@ -487,8 +486,10 @@ internals.state = function (response, next) { states.push({ name: name, value: value }); return nextKey(); }); - }, - function (err) { + }; + + const keys = Object.keys(request.connection.states.cookies); + Items.parallel(keys, each, (err) => { if (err) { return next(Boom.wrap(err)); @@ -498,7 +499,7 @@ internals.state = function (response, next) { return next(); } - request.connection.states.format(states, function (err, header) { + request.connection.states.format(states, (err, header) => { if (err) { return next(Boom.wrap(err)); diff --git a/lib/validation.js b/lib/validation.js index 656e1f6f0..032083ab0 100755 --- a/lib/validation.js +++ b/lib/validation.js @@ -48,7 +48,7 @@ internals.input = function (source, request, next) { return next(Boom.unsupportedMediaType(source + ' must represent an object')); } - const postValidate = function (err, value) { + const postValidate = (err, value) => { request.orig[source] = request[source]; if (value !== undefined) { @@ -103,7 +103,7 @@ internals.input = function (source, request, next) { // Custom handler - request._protect.run(next, function (exit) { + request._protect.run(next, (exit) => { const reply = request.server._replier.interface(request, request.route.realm, exit); request.route.settings.validate.failAction(request, reply, source, error); @@ -131,7 +131,7 @@ internals.input = function (source, request, next) { return Joi.validate(request[source], schema, localOptions, postValidate); } - request._protect.run(postValidate, function (exit) { + request._protect.run(postValidate, (exit) => { return schema(request[source], localOptions, exit); }); @@ -168,7 +168,7 @@ exports.response = function (request, next) { return next(Boom.badImplementation('Cannot validate non-object response')); } - const postValidate = function (err, value) { + const postValidate = (err, value) => { if (!err) { if (value !== undefined && @@ -215,7 +215,7 @@ exports.response = function (request, next) { return Joi.validate(source, schema, localOptions, postValidate); } - request._protect.run(postValidate, function (exit) { + request._protect.run(postValidate, (exit) => { return schema(source, localOptions, exit); }); diff --git a/test/auth.js b/test/auth.js index 7daa78a1f..40054802a 100755 --- a/test/auth.js +++ b/test/auth.js @@ -25,9 +25,9 @@ const it = lab.it; const expect = Code.expect; -describe('authentication', function () { +describe('authentication', () => { - it('requires and authenticates a request', function (done) { + it('requires and authenticates a request', (done) => { const handler = function (request, reply) { @@ -52,7 +52,7 @@ describe('authentication', function () { }); }); - it('defaults cache to private if request authenticated', function (done) { + it('defaults cache to private if request authenticated', (done) => { const handler = function (request, reply) { @@ -73,9 +73,9 @@ describe('authentication', function () { }); }); - describe('strategy()', function () { + describe('strategy()', () => { - it('fails when options default to null', function (done) { + it('fails when options default to null', (done) => { const handler = function (request, reply) { @@ -95,7 +95,7 @@ describe('authentication', function () { }); }); - it('throws when strategy missing scheme', function (done) { + it('throws when strategy missing scheme', (done) => { const server = new Hapi.Server(); server.connection(); @@ -106,7 +106,7 @@ describe('authentication', function () { done(); }); - it('adds a route to server', function (done) { + it('adds a route to server', (done) => { const server = new Hapi.Server(); server.connection(); @@ -125,7 +125,7 @@ describe('authentication', function () { }); }); - it('uses views', function (done) { + it('uses views', (done) => { const implementation = function (server, options) { @@ -184,9 +184,9 @@ describe('authentication', function () { }); }); - describe('default()', function () { + describe('default()', () => { - it('sets default', function (done) { + it('sets default', (done) => { const server = new Hapi.Server(); server.connection(); @@ -215,7 +215,7 @@ describe('authentication', function () { }); }); - it('sets default with object', function (done) { + it('sets default with object', (done) => { const handler = function (request, reply) { @@ -241,7 +241,7 @@ describe('authentication', function () { }); }); - it('throws when setting default twice', function (done) { + it('throws when setting default twice', (done) => { const server = new Hapi.Server(); server.connection(); @@ -255,7 +255,7 @@ describe('authentication', function () { done(); }); - it('throws when setting default without strategy', function (done) { + it('throws when setting default without strategy', (done) => { const server = new Hapi.Server(); server.connection(); @@ -269,9 +269,9 @@ describe('authentication', function () { }); }); - describe('_setupRoute()', function () { + describe('_setupRoute()', () => { - it('throws when route refers to nonexistent strategy', function (done) { + it('throws when route refers to nonexistent strategy', (done) => { const server = new Hapi.Server(); server.connection(); @@ -300,9 +300,9 @@ describe('authentication', function () { }); }); - describe('lookup', function () { + describe('lookup', () => { - it('returns the route auth config', function (done) { + it('returns the route auth config', (done) => { const handler = function (request, reply) { @@ -328,9 +328,9 @@ describe('authentication', function () { }); }); - describe('authenticate()', function () { + describe('authenticate()', () => { - it('setups route with optional authentication', function (done) { + it('setups route with optional authentication', (done) => { const server = new Hapi.Server(); server.connection(); @@ -357,7 +357,7 @@ describe('authentication', function () { }); }); - it('exposes mode', function (done) { + it('exposes mode', (done) => { const server = new Hapi.Server(); server.connection(); @@ -380,7 +380,7 @@ describe('authentication', function () { }); }); - it('authenticates using multiple strategies', function (done) { + it('authenticates using multiple strategies', (done) => { const server = new Hapi.Server(); server.connection(); @@ -409,7 +409,7 @@ describe('authentication', function () { }); }); - it('authenticates using credentials object', function (done) { + it('authenticates using credentials object', (done) => { const server = new Hapi.Server(); server.connection(); @@ -441,7 +441,7 @@ describe('authentication', function () { }); }); - it('authenticates using credentials object (with artifacts)', function (done) { + it('authenticates using credentials object (with artifacts)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -473,7 +473,7 @@ describe('authentication', function () { }); }); - it('authenticates a request with custom auth settings', function (done) { + it('authenticates a request with custom auth settings', (done) => { const handler = function (request, reply) { @@ -502,7 +502,7 @@ describe('authentication', function () { }); }); - it('authenticates a request with auth strategy name config', function (done) { + it('authenticates a request with auth strategy name config', (done) => { const handler = function (request, reply) { @@ -529,7 +529,7 @@ describe('authentication', function () { }); }); - it('tries to authenticate a request', function (done) { + it('tries to authenticate a request', (done) => { const handler = function (request, reply) { @@ -565,7 +565,7 @@ describe('authentication', function () { }); }); - it('errors on invalid authenticate callback missing both error and credentials', function (done) { + it('errors on invalid authenticate callback missing both error and credentials', (done) => { const handler = function (request, reply) { @@ -585,7 +585,7 @@ describe('authentication', function () { }); }); - it('logs error', function (done) { + it('logs error', (done) => { const handler = function (request, reply) { @@ -611,7 +611,7 @@ describe('authentication', function () { }); }); - it('returns a non Error error response', function (done) { + it('returns a non Error error response', (done) => { const handler = function (request, reply) { @@ -632,7 +632,7 @@ describe('authentication', function () { }); }); - it('handles errors thrown inside authenticate', function (done) { + it('handles errors thrown inside authenticate', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -658,7 +658,7 @@ describe('authentication', function () { }); }); - it('passes non Error error response when set to try ', function (done) { + it('passes non Error error response when set to try ', (done) => { const handler = function (request, reply) { @@ -679,7 +679,7 @@ describe('authentication', function () { }); }); - it('matches scope (array to single)', function (done) { + it('matches scope (array to single)', (done) => { const handler = function (request, reply) { @@ -708,7 +708,7 @@ describe('authentication', function () { }); }); - it('matches scope (array to array)', function (done) { + it('matches scope (array to array)', (done) => { const handler = function (request, reply) { @@ -737,7 +737,7 @@ describe('authentication', function () { }); }); - it('matches scope (single to array)', function (done) { + it('matches scope (single to array)', (done) => { const handler = function (request, reply) { @@ -766,7 +766,7 @@ describe('authentication', function () { }); }); - it('matches scope (single to single)', function (done) { + it('matches scope (single to single)', (done) => { const handler = function (request, reply) { @@ -795,7 +795,7 @@ describe('authentication', function () { }); }); - it('matches dynamic scope (single to single)', function (done) { + it('matches dynamic scope (single to single)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -822,7 +822,7 @@ describe('authentication', function () { }); }); - it('matches dynamic scope with multiple parts (single to single)', function (done) { + it('matches dynamic scope with multiple parts (single to single)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -849,7 +849,7 @@ describe('authentication', function () { }); }); - it('does not match broken dynamic scope (single to single)', function (done) { + it('does not match broken dynamic scope (single to single)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -876,7 +876,7 @@ describe('authentication', function () { }); }); - it('does not match scope (single to single)', function (done) { + it('does not match scope (single to single)', (done) => { const handler = function (request, reply) { @@ -905,7 +905,7 @@ describe('authentication', function () { }); }); - it('errors on missing scope', function (done) { + it('errors on missing scope', (done) => { const handler = function (request, reply) { @@ -934,7 +934,7 @@ describe('authentication', function () { }); }); - it('errors on missing scope property', function (done) { + it('errors on missing scope property', (done) => { const handler = function (request, reply) { @@ -963,7 +963,7 @@ describe('authentication', function () { }); }); - it('errors on missing scope using arrays', function (done) { + it('errors on missing scope using arrays', (done) => { const handler = function (request, reply) { @@ -992,7 +992,7 @@ describe('authentication', function () { }); }); - it('ignores default scope when override set to null', function (done) { + it('ignores default scope when override set to null', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1024,7 +1024,7 @@ describe('authentication', function () { }); }); - it('matches user entity', function (done) { + it('matches user entity', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1051,7 +1051,7 @@ describe('authentication', function () { }); }); - it('errors on missing user entity', function (done) { + it('errors on missing user entity', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1078,7 +1078,7 @@ describe('authentication', function () { }); }); - it('matches app entity', function (done) { + it('matches app entity', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1105,7 +1105,7 @@ describe('authentication', function () { }); }); - it('errors on missing app entity', function (done) { + it('errors on missing app entity', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1132,7 +1132,7 @@ describe('authentication', function () { }); }); - it('logs error code when authenticate returns a non-error error', function (done) { + it('logs error code when authenticate returns a non-error error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1172,7 +1172,7 @@ describe('authentication', function () { }); }); - it('passes the options.artifacts object, even with an auth filter', function (done) { + it('passes the options.artifacts object, even with an auth filter', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1210,9 +1210,9 @@ describe('authentication', function () { }); - describe('payload()', function () { + describe('payload()', () => { - it('authenticates request payload', function (done) { + it('authenticates request payload', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1239,7 +1239,7 @@ describe('authentication', function () { }); }); - it('skips when scheme does not support it', function (done) { + it('skips when scheme does not support it', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1263,7 +1263,7 @@ describe('authentication', function () { }); }); - it('authenticates request payload (required scheme)', function (done) { + it('authenticates request payload (required scheme)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1288,7 +1288,7 @@ describe('authentication', function () { }); }); - it('authenticates request payload (required scheme and required route)', function (done) { + it('authenticates request payload (required scheme and required route)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1315,7 +1315,7 @@ describe('authentication', function () { }); }); - it('throws when scheme requires payload authentication and route conflicts', function (done) { + it('throws when scheme requires payload authentication and route conflicts', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1340,7 +1340,7 @@ describe('authentication', function () { done(); }); - it('throws when strategy does not support payload authentication', function (done) { + it('throws when strategy does not support payload authentication', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1370,7 +1370,7 @@ describe('authentication', function () { done(); }); - it('throws when no strategy supports optional payload authentication', function (done) { + it('throws when no strategy supports optional payload authentication', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1400,7 +1400,7 @@ describe('authentication', function () { done(); }); - it('allows one strategy to supports optional payload authentication while another does not', function (done) { + it('allows one strategy to supports optional payload authentication while another does not', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1433,7 +1433,7 @@ describe('authentication', function () { done(); }); - it('skips request payload by default', function (done) { + it('skips request payload by default', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1457,7 +1457,7 @@ describe('authentication', function () { }); }); - it('skips request payload when unauthenticated', function (done) { + it('skips request payload when unauthenticated', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1485,7 +1485,7 @@ describe('authentication', function () { }); }); - it('skips optional payload', function (done) { + it('skips optional payload', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1512,7 +1512,7 @@ describe('authentication', function () { }); }); - it('errors on missing payload when required', function (done) { + it('errors on missing payload when required', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1539,7 +1539,7 @@ describe('authentication', function () { }); }); - it('errors on invalid payload auth when required', function (done) { + it('errors on invalid payload auth when required', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1566,7 +1566,7 @@ describe('authentication', function () { }); }); - it('errors on invalid request payload (non error)', function (done) { + it('errors on invalid request payload (non error)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1595,9 +1595,9 @@ describe('authentication', function () { }); }); - describe('response()', function () { + describe('response()', () => { - it('fails on response error', function (done) { + it('fails on response error', (done) => { const handler = function (request, reply) { @@ -1618,9 +1618,9 @@ describe('authentication', function () { }); }); - describe('test()', function () { + describe('test()', () => { - it('tests a request', function (done) { + it('tests a request', (done) => { const handler = function (request, reply) { diff --git a/test/connection.js b/test/connection.js index b47a21d1e..88f3b22bd 100755 --- a/test/connection.js +++ b/test/connection.js @@ -33,9 +33,9 @@ const it = lab.it; const expect = Code.expect; -describe('Connection', function () { +describe('Connection', () => { - it('allows null port and host', function (done) { + it('allows null port and host', (done) => { const server = new Hapi.Server(); expect(function () { @@ -45,7 +45,7 @@ describe('Connection', function () { done(); }); - it('removes duplicate labels', function (done) { + it('removes duplicate labels', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a', 'b', 'a', 'c', 'b'] }); @@ -53,7 +53,7 @@ describe('Connection', function () { done(); }); - it('throws when disabling autoListen and providing a port', function (done) { + it('throws when disabling autoListen and providing a port', (done) => { const server = new Hapi.Server(); expect(function () { @@ -63,7 +63,7 @@ describe('Connection', function () { done(); }); - it('throws when disabling autoListen and providing special host', function (done) { + it('throws when disabling autoListen and providing special host', (done) => { const server = new Hapi.Server(); const port = Path.join(__dirname, 'hapi-server.socket'); @@ -74,7 +74,7 @@ describe('Connection', function () { done(); }); - it('defaults address to 0.0.0.0 or :: when no host is provided', function (done) { + it('defaults address to 0.0.0.0 or :: when no host is provided', (done) => { const server = new Hapi.Server(); server.connection(); @@ -92,7 +92,7 @@ describe('Connection', function () { }); }); - it('uses address when present instead of host', function (done) { + it('uses address when present instead of host', (done) => { const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost' }); @@ -105,7 +105,7 @@ describe('Connection', function () { }); }); - it('uses uri when present instead of host and port', function (done) { + it('uses uri when present instead of host and port', (done) => { const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost', uri: 'http://uri.example.com:8080' }); @@ -120,7 +120,7 @@ describe('Connection', function () { }); }); - it('throws on uri ending with /', function (done) { + it('throws on uri ending with /', (done) => { const server = new Hapi.Server(); expect(function () { @@ -130,7 +130,7 @@ describe('Connection', function () { done(); }); - it('creates a server listening on a unix domain socket', { skip: process.platform === 'win32' }, function (done) { + it('creates a server listening on a unix domain socket', { skip: process.platform === 'win32' }, (done) => { const port = Path.join(__dirname, 'hapi-server.socket'); const server = new Hapi.Server(); @@ -155,7 +155,7 @@ describe('Connection', function () { }); }); - it('creates a server listening on a windows named pipe', function (done) { + it('creates a server listening on a windows named pipe', (done) => { const port = '\\\\.\\pipe\\6653e55f-26ec-4268-a4f2-882f4089315c'; const server = new Hapi.Server(); @@ -170,7 +170,7 @@ describe('Connection', function () { }); }); - it('creates an https server when passed tls options', function (done) { + it('creates an https server when passed tls options', (done) => { const tlsOptions = { key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE\nSC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa\nFvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv\nw00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia\nPQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3\nm1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX\n1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7\n/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans\npCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee\nuctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs\nV/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==\n-----END RSA PRIVATE KEY-----\n', @@ -183,7 +183,7 @@ describe('Connection', function () { done(); }); - it('uses a provided listener', function (done) { + it('uses a provided listener', (done) => { const handler = function (request, reply) { @@ -207,7 +207,7 @@ describe('Connection', function () { }); }); - it('uses a provided listener (TLS)', function (done) { + it('uses a provided listener (TLS)', (done) => { const handler = function (request, reply) { @@ -227,7 +227,7 @@ describe('Connection', function () { }); }); - it('uses a provided listener with manual listen', function (done) { + it('uses a provided listener with manual listen', (done) => { const handler = function (request, reply) { @@ -239,7 +239,7 @@ describe('Connection', function () { server.connection({ listener: listener, autoListen: false }); server.route({ method: 'GET', path: '/', handler: handler }); - listener.listen(0, 'localhost', function () { + listener.listen(0, 'localhost', () => { server.start(function (err) { @@ -254,7 +254,7 @@ describe('Connection', function () { }); }); - it('sets info.uri with default localhost when no hostname', { parallel: false }, function (done) { + it('sets info.uri with default localhost when no hostname', { parallel: false }, (done) => { const orig = Os.hostname; Os.hostname = function () { @@ -269,7 +269,7 @@ describe('Connection', function () { done(); }); - it('sets info.uri without port when 0', function (done) { + it('sets info.uri without port when 0', (done) => { const server = new Hapi.Server(); server.connection({ host: 'example.com' }); @@ -277,7 +277,7 @@ describe('Connection', function () { done(); }); - it('closes connection on socket timeout', { parallel: false }, function (done) { + it('closes connection on socket timeout', { parallel: false }, (done) => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { socket: 50 }, payload: { timeout: 45 } } }); @@ -305,7 +305,7 @@ describe('Connection', function () { }); }); - it('disables node socket timeout', { parallel: false }, function (done) { + it('disables node socket timeout', { parallel: false }, (done) => { const handler = function (request, reply) { @@ -341,9 +341,9 @@ describe('Connection', function () { }); }); - describe('_start()', function () { + describe('_start()', () => { - it('starts connection', function (done) { + it('starts connection', (done) => { const server = new Hapi.Server(); server.connection(); @@ -362,7 +362,7 @@ describe('Connection', function () { }); }); - it('starts connection (tls)', function (done) { + it('starts connection (tls)', (done) => { const tlsOptions = { key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE\nSC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa\nFvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv\nw00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia\nPQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3\nm1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX\n1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7\n/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans\npCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee\nuctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs\nV/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==\n-----END RSA PRIVATE KEY-----\n', @@ -380,7 +380,7 @@ describe('Connection', function () { }); }); - it('sets info with defaults when missing hostname and address', { parallel: false }, function (done) { + it('sets info with defaults when missing hostname and address', { parallel: false }, (done) => { const hostname = Os.hostname; Os.hostname = function () { @@ -396,7 +396,7 @@ describe('Connection', function () { done(); }); - it('ignored repeated calls', function (done) { + it('ignored repeated calls', (done) => { const server = new Hapi.Server(); server.connection(); @@ -415,7 +415,7 @@ describe('Connection', function () { }); }); - it('will return an error if the port is already in use', function (done) { + it('will return an error if the port is already in use', (done) => { const server = new Hapi.Server(); server.connection(); @@ -434,9 +434,9 @@ describe('Connection', function () { }); }); - describe('_stop()', function () { + describe('_stop()', () => { - it('waits to stop until all connections are closed', function (done) { + it('waits to stop until all connections are closed', (done) => { const server = new Hapi.Server(); server.connection(); @@ -445,12 +445,12 @@ describe('Connection', function () { expect(err).to.not.exist(); const socket1 = new Net.Socket(); const socket2 = new Net.Socket(); - socket1.on('error', function () { }); - socket2.on('error', function () { }); + socket1.on('error', () => { }); + socket2.on('error', () => { }); - socket1.connect(server.info.port, '127.0.0.1', function () { + socket1.connect(server.info.port, '127.0.0.1', () => { - socket2.connect(server.info.port, '127.0.0.1', function () { + socket2.connect(server.info.port, '127.0.0.1', () => { server.listener.getConnections(function (err, count1) { @@ -475,7 +475,7 @@ describe('Connection', function () { }); }); - it('waits to destroy connections until after the timeout', function (done) { + it('waits to destroy connections until after the timeout', (done) => { const server = new Hapi.Server(); server.connection(); @@ -496,9 +496,9 @@ describe('Connection', function () { expect(err.errno).to.equal('ECONNRESET'); }); - socket1.connect(server.info.port, server.connections[0].settings.host, function () { + socket1.connect(server.info.port, server.connections[0].settings.host, () => { - socket2.connect(server.info.port, server.connections[0].settings.host, function () { + socket2.connect(server.info.port, server.connections[0].settings.host, () => { server.listener.getConnections(function (err, count) { @@ -517,7 +517,7 @@ describe('Connection', function () { }); }); - it('waits to destroy connections if they close by themselves', function (done) { + it('waits to destroy connections if they close by themselves', (done) => { const server = new Hapi.Server(); server.connection(); @@ -538,9 +538,9 @@ describe('Connection', function () { expect(err.errno).to.equal('ECONNRESET'); }); - socket1.connect(server.info.port, server.connections[0].settings.host, function () { + socket1.connect(server.info.port, server.connections[0].settings.host, () => { - socket2.connect(server.info.port, server.connections[0].settings.host, function () { + socket2.connect(server.info.port, server.connections[0].settings.host, () => { server.listener.getConnections(function (err, count1) { @@ -570,7 +570,7 @@ describe('Connection', function () { }); }); - it('refuses to handle new incoming requests', function (done) { + it('refuses to handle new incoming requests', (done) => { const handler = function (request, reply) { @@ -607,7 +607,7 @@ describe('Connection', function () { }); }); - it('removes connection event listeners after it stops', function (done) { + it('removes connection event listeners after it stops', (done) => { const server = new Hapi.Server(); server.connection(); @@ -637,7 +637,7 @@ describe('Connection', function () { }); }); - it('ignores repeated calls', function (done) { + it('ignores repeated calls', (done) => { const server = new Hapi.Server(); server.connection(); @@ -648,9 +648,9 @@ describe('Connection', function () { }); }); - describe('_dispatch()', function () { + describe('_dispatch()', () => { - it('rejects request due to high rss load', { parallel: false }, function (done) { + it('rejects request due to high rss load', { parallel: false }, (done) => { const server = new Hapi.Server({ load: { sampleInterval: 5 } }); server.connection({ load: { maxRssBytes: 1 } }); @@ -691,9 +691,9 @@ describe('Connection', function () { }); }); - describe('inject()', function () { + describe('inject()', () => { - it('keeps the options.credentials object untouched', function (done) { + it('keeps the options.credentials object untouched', (done) => { const handler = function (request, reply) { @@ -717,7 +717,7 @@ describe('Connection', function () { }); }); - it('sets credentials (with host header)', function (done) { + it('sets credentials (with host header)', (done) => { const handler = function (request, reply) { @@ -744,7 +744,7 @@ describe('Connection', function () { }); }); - it('sets credentials (with authority)', function (done) { + it('sets credentials (with authority)', (done) => { const handler = function (request, reply) { @@ -770,7 +770,7 @@ describe('Connection', function () { }); }); - it('sets authority', function (done) { + it('sets authority', (done) => { const handler = function (request, reply) { @@ -794,7 +794,7 @@ describe('Connection', function () { }); }); - it('passes the options.artifacts object', function (done) { + it('passes the options.artifacts object', (done) => { const handler = function (request, reply) { @@ -820,7 +820,7 @@ describe('Connection', function () { }); }); - it('returns the request object', function (done) { + it('returns the request object', (done) => { const handler = function (request, reply) { @@ -840,7 +840,7 @@ describe('Connection', function () { }); }); - it('can set a client remoteAddress', function (done) { + it('can set a client remoteAddress', (done) => { const handler = function (request, reply) { @@ -859,7 +859,7 @@ describe('Connection', function () { }); }); - it('sets a default remoteAddress of 127.0.0.1', function (done) { + it('sets a default remoteAddress of 127.0.0.1', (done) => { const handler = function (request, reply) { @@ -878,7 +878,7 @@ describe('Connection', function () { }); }); - it('sets correct host header', function (done) { + it('sets correct host header', (done) => { const server = new Hapi.Server(); server.connection({ host: 'example.com', port: 2080 }); @@ -899,9 +899,9 @@ describe('Connection', function () { }); }); - describe('table()', function () { + describe('table()', () => { - it('returns an array of the current routes', function (done) { + it('returns an array of the current routes', (done) => { const server = new Hapi.Server(); server.connection(); @@ -916,7 +916,7 @@ describe('Connection', function () { done(); }); - it('returns the labels for the connections', function (done) { + it('returns the labels for the connections', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['test'] }); @@ -930,7 +930,7 @@ describe('Connection', function () { done(); }); - it('returns an array of the current routes (connection)', function (done) { + it('returns an array of the current routes (connection)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -945,7 +945,7 @@ describe('Connection', function () { done(); }); - it('combines global and vhost routes', function (done) { + it('combines global and vhost routes', (done) => { const server = new Hapi.Server(); server.connection(); @@ -961,7 +961,7 @@ describe('Connection', function () { done(); }); - it('combines global and vhost routes and filters based on host', function (done) { + it('combines global and vhost routes and filters based on host', (done) => { const server = new Hapi.Server(); server.connection(); @@ -977,7 +977,7 @@ describe('Connection', function () { done(); }); - it('accepts a list of hosts', function (done) { + it('accepts a list of hosts', (done) => { const server = new Hapi.Server(); server.connection(); @@ -993,7 +993,7 @@ describe('Connection', function () { done(); }); - it('ignores unknown host', function (done) { + it('ignores unknown host', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1010,9 +1010,9 @@ describe('Connection', function () { }); }); - describe('ext()', function () { + describe('ext()', () => { - it('supports adding an array of methods', function (done) { + it('supports adding an array of methods', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1043,7 +1043,7 @@ describe('Connection', function () { }); }); - it('sets bind via options', function (done) { + it('sets bind via options', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1067,7 +1067,7 @@ describe('Connection', function () { }); }); - it('uses server views for ext added via server', function (done) { + it('uses server views for ext added via server', (done) => { const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); @@ -1108,7 +1108,7 @@ describe('Connection', function () { }); }); - it('supports reply decorators on empty result', function (done) { + it('supports reply decorators on empty result', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1125,7 +1125,7 @@ describe('Connection', function () { }); }); - it('supports direct reply decorators', function (done) { + it('supports direct reply decorators', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1142,9 +1142,9 @@ describe('Connection', function () { }); }); - describe('onRequest', function (done) { + describe('onRequest', (done) => { - it('replies with custom response', function (done) { + it('replies with custom response', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1161,7 +1161,7 @@ describe('Connection', function () { }); }); - it('replies with error using reply(null, result)', function (done) { + it('replies with error using reply(null, result)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1185,7 +1185,7 @@ describe('Connection', function () { }); }); - it('replies with a view', function (done) { + it('replies with a view', (done) => { const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); @@ -1216,9 +1216,9 @@ describe('Connection', function () { }); }); - describe('onPreResponse', function (done) { + describe('onPreResponse', (done) => { - it('replies with custom response', function (done) { + it('replies with custom response', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1260,7 +1260,7 @@ describe('Connection', function () { }); }); - it('intercepts 404 responses', function (done) { + it('intercepts 404 responses', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1277,7 +1277,7 @@ describe('Connection', function () { }); }); - it('intercepts 404 when using directory handler and file is missing', function (done) { + it('intercepts 404 when using directory handler and file is missing', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -1299,7 +1299,7 @@ describe('Connection', function () { }); }); - it('intercepts 404 when using file handler and file is missing', function (done) { + it('intercepts 404 when using file handler and file is missing', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -1321,7 +1321,7 @@ describe('Connection', function () { }); }); - it('cleans unused file stream when response is overridden', { skip: process.platform === 'win32' }, function (done) { + it('cleans unused file stream when response is overridden', { skip: process.platform === 'win32' }, (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -1346,7 +1346,7 @@ describe('Connection', function () { lsof += buffer.toString(); }); - cmd.stdout.on('end', function () { + cmd.stdout.on('end', () => { let count = 0; const lines = lsof.split('\n'); @@ -1362,7 +1362,7 @@ describe('Connection', function () { }); }); - it('executes multiple extensions', function (done) { + it('executes multiple extensions', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1394,9 +1394,9 @@ describe('Connection', function () { }); }); - describe('route()', function () { + describe('route()', () => { - it('emits route event', function (done) { + it('emits route event', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'a' }); @@ -1418,7 +1418,7 @@ describe('Connection', function () { }); }); - it('overrides the default notFound handler', function (done) { + it('overrides the default notFound handler', (done) => { const handler = function (request, reply) { @@ -1436,7 +1436,7 @@ describe('Connection', function () { }); }); - it('responds to HEAD requests for a GET route', function (done) { + it('responds to HEAD requests for a GET route', (done) => { const handler = function (request, reply) { @@ -1457,7 +1457,7 @@ describe('Connection', function () { }); }); - it('returns 404 on HEAD requests for non-GET routes', function (done) { + it('returns 404 on HEAD requests for non-GET routes', (done) => { const handler = function (request, reply) { @@ -1481,7 +1481,7 @@ describe('Connection', function () { }); }); - it('allows methods array', function (done) { + it('allows methods array', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1525,7 +1525,7 @@ describe('Connection', function () { }); }); - it('adds routes using single and array methods', function (done) { + it('adds routes using single and array methods', (done) => { const handler = function (request, reply) { @@ -1584,7 +1584,7 @@ describe('Connection', function () { done(); }); - it('throws on methods array with id', function (done) { + it('throws on methods array with id', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1607,9 +1607,9 @@ describe('Connection', function () { }); }); - describe('_defaultRoutes()', function () { + describe('_defaultRoutes()', () => { - it('returns 404 when making a request to a route that does not exist', function (done) { + it('returns 404 when making a request to a route that does not exist', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1620,7 +1620,7 @@ describe('Connection', function () { }); }); - it('returns 400 on bad request', function (done) { + it('returns 400 on bad request', (done) => { const handler = function (request, reply) { diff --git a/test/cors.js b/test/cors.js index 8eb9ec3e5..a7f9b95e5 100755 --- a/test/cors.js +++ b/test/cors.js @@ -21,9 +21,9 @@ const it = lab.it; const expect = Code.expect; -describe('CORS', function () { +describe('CORS', () => { - it('returns 404 on OPTIONS when cors disabled', function (done) { + it('returns 404 on OPTIONS when cors disabled', (done) => { const handler = function (request, reply) { @@ -41,7 +41,7 @@ describe('CORS', function () { }); }); - it('returns OPTIONS response', function (done) { + it('returns OPTIONS response', (done) => { const handler = function (request, reply) { @@ -59,7 +59,7 @@ describe('CORS', function () { }); }); - it('returns OPTIONS response (server config)', function (done) { + it('returns OPTIONS response (server config)', (done) => { const handler = function (request, reply) { @@ -77,7 +77,7 @@ describe('CORS', function () { }); }); - it('returns headers on single route', function (done) { + it('returns headers on single route', (done) => { const handler = function (request, reply) { @@ -105,7 +105,7 @@ describe('CORS', function () { }); }); - it('allows headers on multiple routes but not all', function (done) { + it('allows headers on multiple routes but not all', (done) => { const handler = function (request, reply) { @@ -141,7 +141,7 @@ describe('CORS', function () { }); }); - it('allows same headers on multiple routes with same path', function (done) { + it('allows same headers on multiple routes with same path', (done) => { const handler = function (request, reply) { @@ -162,7 +162,7 @@ describe('CORS', function () { }); }); - it('returns headers on single route (overrides defaults)', function (done) { + it('returns headers on single route (overrides defaults)', (done) => { const handler = function (request, reply) { @@ -190,7 +190,7 @@ describe('CORS', function () { }); }); - it('sets access-control-allow-credentials header', function (done) { + it('sets access-control-allow-credentials header', (done) => { const handler = function (request, reply) { @@ -209,9 +209,9 @@ describe('CORS', function () { }); }); - describe('headers()', function () { + describe('headers()', () => { - it('returns CORS origin (route level)', function (done) { + it('returns CORS origin (route level)', (done) => { const handler = function (request, reply) { @@ -237,7 +237,7 @@ describe('CORS', function () { }); }); - it('returns CORS origin (GET)', function (done) { + it('returns CORS origin (GET)', (done) => { const handler = function (request, reply) { @@ -257,7 +257,7 @@ describe('CORS', function () { }); }); - it('returns CORS origin (OPTIONS)', function (done) { + it('returns CORS origin (OPTIONS)', (done) => { const handler = function (request, reply) { @@ -277,7 +277,7 @@ describe('CORS', function () { }); }); - it('merges CORS access-control-expose-headers header', function (done) { + it('merges CORS access-control-expose-headers header', (done) => { const handler = function (request, reply) { @@ -297,7 +297,7 @@ describe('CORS', function () { }); }); - it('returns no CORS headers when route CORS disabled', function (done) { + it('returns no CORS headers when route CORS disabled', (done) => { const handler = function (request, reply) { @@ -317,7 +317,7 @@ describe('CORS', function () { }); }); - it('returns matching CORS origin', function (done) { + it('returns matching CORS origin', (done) => { const handler = function (request, reply) { @@ -338,7 +338,7 @@ describe('CORS', function () { }); }); - it('returns origin header when matching against *', function (done) { + it('returns origin header when matching against *', (done) => { const handler = function (request, reply) { @@ -359,7 +359,7 @@ describe('CORS', function () { }); }); - it('returns matching CORS origin wildcard', function (done) { + it('returns matching CORS origin wildcard', (done) => { const handler = function (request, reply) { @@ -380,7 +380,7 @@ describe('CORS', function () { }); }); - it('returns matching CORS origin wildcard when more than one wildcard', function (done) { + it('returns matching CORS origin wildcard when more than one wildcard', (done) => { const handler = function (request, reply) { @@ -401,7 +401,7 @@ describe('CORS', function () { }); }); - it('does not set empty CORS expose headers', function (done) { + it('does not set empty CORS expose headers', (done) => { const handler = function (request, reply) { @@ -427,9 +427,9 @@ describe('CORS', function () { }); }); - describe('options()', function () { + describe('options()', () => { - it('ignores OPTIONS route', function (done) { + it('ignores OPTIONS route', (done) => { const server = new Hapi.Server(); server.connection(); @@ -444,9 +444,9 @@ describe('CORS', function () { }); }); - describe('handler()', function () { + describe('handler()', () => { - it('errors on missing origin header', function (done) { + it('errors on missing origin header', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); @@ -464,7 +464,7 @@ describe('CORS', function () { }); }); - it('errors on missing access-control-request-method header', function (done) { + it('errors on missing access-control-request-method header', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); @@ -482,7 +482,7 @@ describe('CORS', function () { }); }); - it('errors on missing route', function (done) { + it('errors on missing route', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); @@ -494,7 +494,7 @@ describe('CORS', function () { }); }); - it('errors on mismatching origin header', function (done) { + it('errors on mismatching origin header', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: { origin: ['a'] } } }); @@ -512,7 +512,7 @@ describe('CORS', function () { }); }); - it('matches allowed headers', function (done) { + it('matches allowed headers', (done) => { const handler = function (request, reply) { @@ -539,7 +539,7 @@ describe('CORS', function () { }); }); - it('matches allowed headers (case insensitive', function (done) { + it('matches allowed headers (case insensitive', (done) => { const handler = function (request, reply) { @@ -566,7 +566,7 @@ describe('CORS', function () { }); }); - it('errors on disallowed headers', function (done) { + it('errors on disallowed headers', (done) => { const handler = function (request, reply) { @@ -593,7 +593,7 @@ describe('CORS', function () { }); }); - it('allows credentials', function (done) { + it('allows credentials', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: { credentials: true } } }); @@ -612,9 +612,9 @@ describe('CORS', function () { }); }); - describe('headers()', function () { + describe('headers()', () => { - it('skips CORS when missing origin header', function (done) { + it('skips CORS when missing origin header', (done) => { const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); diff --git a/test/handler.js b/test/handler.js index eb04df1a0..fa8d055e0 100755 --- a/test/handler.js +++ b/test/handler.js @@ -26,11 +26,11 @@ const it = lab.it; const expect = Code.expect; -describe('handler', function () { +describe('handler', () => { - describe('execute()', function () { + describe('execute()', () => { - it('returns 500 on handler exception (same tick)', function (done) { + it('returns 500 on handler exception (same tick)', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -49,7 +49,7 @@ describe('handler', function () { }); }); - it('returns 500 on handler exception (next tick)', { parallel: false }, function (done) { + it('returns 500 on handler exception (next tick)', { parallel: false }, (done) => { const handler = function (request) { @@ -83,9 +83,9 @@ describe('handler', function () { }); }); - describe('handler()', function () { + describe('handler()', () => { - it('binds handler to route bind object', function (done) { + it('binds handler to route bind object', (done) => { const item = { x: 123 }; @@ -110,7 +110,7 @@ describe('handler', function () { }); }); - it('invokes handler with right arguments', function (done) { + it('invokes handler with right arguments', (done) => { const server = new Hapi.Server(); server.connection(); @@ -132,9 +132,9 @@ describe('handler', function () { }); }); - describe('register()', function () { + describe('register()', () => { - it('returns a file', function (done) { + it('returns a file', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -157,7 +157,7 @@ describe('handler', function () { }); }); - it('returns a view', function (done) { + it('returns a view', (done) => { const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); @@ -183,9 +183,9 @@ describe('handler', function () { }); }); - describe('prerequisites()', function () { + describe('prerequisites()', () => { - it('shows the complete prerequisite pipeline in the response', function (done) { + it('shows the complete prerequisite pipeline in the response', (done) => { const pre1 = function (request, reply) { @@ -246,7 +246,7 @@ describe('handler', function () { }); }); - it('allows a single prerequisite', function (done) { + it('allows a single prerequisite', (done) => { const pre = function (request, reply) { @@ -279,7 +279,7 @@ describe('handler', function () { }); }); - it('allows an empty prerequisite array', function (done) { + it('allows an empty prerequisite array', (done) => { const handler = function (request, reply) { @@ -305,7 +305,7 @@ describe('handler', function () { }); }); - it('takes over response', function (done) { + it('takes over response', (done) => { const pre1 = function (request, reply) { @@ -366,7 +366,7 @@ describe('handler', function () { }); }); - it('returns error if prerequisite returns error', function (done) { + it('returns error if prerequisite returns error', (done) => { const pre1 = function (request, reply) { @@ -404,7 +404,7 @@ describe('handler', function () { }); }); - it('passes wrapped object', function (done) { + it('passes wrapped object', (done) => { const pre = function (request, reply) { @@ -436,7 +436,7 @@ describe('handler', function () { }); }); - it('returns 500 if prerequisite throws', function (done) { + it('returns 500 if prerequisite throws', (done) => { const pre1 = function (request, reply) { @@ -475,7 +475,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method', function (done) { + it('returns a user record using server method', (done) => { const server = new Hapi.Server(); server.connection(); @@ -506,7 +506,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method (nested method name)', function (done) { + it('returns a user record using server method (nested method name)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -537,7 +537,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method in object', function (done) { + it('returns a user record using server method in object', (done) => { const server = new Hapi.Server(); server.connection(); @@ -571,7 +571,7 @@ describe('handler', function () { }); }); - it('returns a user name using multiple server methods', function (done) { + it('returns a user name using multiple server methods', (done) => { const server = new Hapi.Server(); server.connection(); @@ -608,7 +608,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method with trailing space', function (done) { + it('returns a user record using server method with trailing space', (done) => { const server = new Hapi.Server(); server.connection(); @@ -639,7 +639,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method with leading space', function (done) { + it('returns a user record using server method with leading space', (done) => { const server = new Hapi.Server(); server.connection(); @@ -670,7 +670,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method with zero args', function (done) { + it('returns a user record using server method with zero args', (done) => { const server = new Hapi.Server(); server.connection(); @@ -701,7 +701,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method with no args', function (done) { + it('returns a user record using server method with no args', (done) => { const server = new Hapi.Server(); server.connection(); @@ -732,7 +732,7 @@ describe('handler', function () { }); }); - it('returns a user record using server method with nested name', function (done) { + it('returns a user record using server method with nested name', (done) => { const server = new Hapi.Server(); server.connection(); @@ -763,7 +763,7 @@ describe('handler', function () { }); }); - it('fails on bad method name', function (done) { + it('fails on bad method name', (done) => { const server = new Hapi.Server(); server.connection(); @@ -788,7 +788,7 @@ describe('handler', function () { done(); }); - it('fails on bad method syntax name', function (done) { + it('fails on bad method syntax name', (done) => { const server = new Hapi.Server(); server.connection(); @@ -813,7 +813,7 @@ describe('handler', function () { done(); }); - it('sets pre failAction to error', function (done) { + it('sets pre failAction to error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -844,7 +844,7 @@ describe('handler', function () { }); }); - it('sets pre failAction to ignore', function (done) { + it('sets pre failAction to ignore', (done) => { const server = new Hapi.Server(); server.connection(); @@ -875,7 +875,7 @@ describe('handler', function () { }); }); - it('sets pre failAction to log', function (done) { + it('sets pre failAction to log', (done) => { const server = new Hapi.Server(); server.connection(); @@ -919,7 +919,7 @@ describe('handler', function () { }); }); - it('binds pre to route bind object', function (done) { + it('binds pre to route bind object', (done) => { const item = { x: 123 }; @@ -950,7 +950,7 @@ describe('handler', function () { }); }); - it('logs boom error instance as data if handler returns boom error', function (done) { + it('logs boom error instance as data if handler returns boom error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -986,7 +986,7 @@ describe('handler', function () { }); }); - it('logs server method using string notation when cache enabled', function (done) { + it('logs server method using string notation when cache enabled', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1024,7 +1024,7 @@ describe('handler', function () { }); }); - it('uses server method with cache via string notation', function (done) { + it('uses server method with cache via string notation', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1067,9 +1067,9 @@ describe('handler', function () { }); }); - describe('fromString()', function () { + describe('fromString()', () => { - it('uses string handler', function (done) { + it('uses string handler', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1088,9 +1088,9 @@ describe('handler', function () { }); }); - describe('defaults()', function () { + describe('defaults()', () => { - it('returns handler without defaults', function (done) { + it('returns handler without defaults', (done) => { const handler = function (route, options) { @@ -1111,7 +1111,7 @@ describe('handler', function () { }); }); - it('returns handler with object defaults', function (done) { + it('returns handler with object defaults', (done) => { const handler = function (route, options) { @@ -1138,7 +1138,7 @@ describe('handler', function () { }); }); - it('returns handler with function defaults', function (done) { + it('returns handler with function defaults', (done) => { const handler = function (route, options) { @@ -1168,7 +1168,7 @@ describe('handler', function () { }); }); - it('throws on handler with invalid defaults', function (done) { + it('throws on handler with invalid defaults', (done) => { const handler = function (route, options) { @@ -1191,9 +1191,9 @@ describe('handler', function () { }); }); - describe('invoke()', function () { + describe('invoke()', () => { - it('returns 500 on ext method exception (same tick)', function (done) { + it('returns 500 on ext method exception (same tick)', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); diff --git a/test/methods.js b/test/methods.js index 40feab992..1c38ac37b 100755 --- a/test/methods.js +++ b/test/methods.js @@ -22,9 +22,9 @@ const it = lab.it; const expect = Code.expect; -describe('Methods', function () { +describe('Methods', () => { - it('registers a method', function (done) { + it('registers a method', (done) => { const add = function (a, b, next) { @@ -41,7 +41,7 @@ describe('Methods', function () { }); }); - it('registers a method with leading _', function (done) { + it('registers a method with leading _', (done) => { const _add = function (a, b, next) { @@ -58,7 +58,7 @@ describe('Methods', function () { }); }); - it('registers a method with leading $', function (done) { + it('registers a method with leading $', (done) => { const $add = function (a, b, next) { @@ -75,7 +75,7 @@ describe('Methods', function () { }); }); - it('registers a method with _', function (done) { + it('registers a method with _', (done) => { const _add = function (a, b, next) { @@ -92,7 +92,7 @@ describe('Methods', function () { }); }); - it('registers a method with $', function (done) { + it('registers a method with $', (done) => { const $add = function (a, b, next) { @@ -109,7 +109,7 @@ describe('Methods', function () { }); }); - it('registers a method (no callback)', function (done) { + it('registers a method (no callback)', (done) => { const add = function (a, b) { @@ -123,7 +123,7 @@ describe('Methods', function () { done(); }); - it('registers a method (promise)', function (done) { + it('registers a method (promise)', (done) => { const addAsync = function (a, b, next) { @@ -142,7 +142,7 @@ describe('Methods', function () { }); }); - it('registers a method with nested name', function (done) { + it('registers a method with nested name', (done) => { const add = function (a, b, next) { @@ -165,7 +165,7 @@ describe('Methods', function () { }); }); - it('registers a method with bind and callback', function (done) { + it('registers a method with bind and callback', (done) => { const server = new Hapi.Server(); server.connection(); @@ -197,7 +197,7 @@ describe('Methods', function () { }); }); - it('registers two methods with shared nested name', function (done) { + it('registers two methods with shared nested name', (done) => { const add = function (a, b, next) { @@ -230,7 +230,7 @@ describe('Methods', function () { }); }); - it('throws when registering a method with nested name twice', function (done) { + it('throws when registering a method with nested name twice', (done) => { const add = function (a, b, next) { @@ -247,7 +247,7 @@ describe('Methods', function () { done(); }); - it('throws when registering a method with name nested through a function', function (done) { + it('throws when registering a method with name nested through a function', (done) => { const add = function (a, b, next) { @@ -264,7 +264,7 @@ describe('Methods', function () { done(); }); - it('calls non cached method multiple times', function (done) { + it('calls non cached method multiple times', (done) => { let gen = 0; const method = function (id, next) { @@ -293,7 +293,7 @@ describe('Methods', function () { }); }); - it('caches method value', function (done) { + it('caches method value', (done) => { let gen = 0; const method = function (id, next) { @@ -324,7 +324,7 @@ describe('Methods', function () { }); }); - it('caches method value (no callback)', function (done) { + it('caches method value (no callback)', (done) => { let gen = 0; const method = function (id) { @@ -355,7 +355,7 @@ describe('Methods', function () { }); }); - it('caches method value (promise)', function (done) { + it('caches method value (promise)', (done) => { let gen = 0; const methodAsync = function (id, next) { @@ -398,7 +398,7 @@ describe('Methods', function () { }); }); - it('reuses cached method value with custom key function', function (done) { + it('reuses cached method value with custom key function', (done) => { let gen = 0; const method = function (id, next) { @@ -433,7 +433,7 @@ describe('Methods', function () { }); }); - it('errors when custom key function return null', function (done) { + it('errors when custom key function return null', (done) => { const method = function (id, next) { @@ -463,7 +463,7 @@ describe('Methods', function () { }); }); - it('does not cache when custom key function returns a non-string', function (done) { + it('does not cache when custom key function returns a non-string', (done) => { const method = function (id, next) { @@ -493,7 +493,7 @@ describe('Methods', function () { }); }); - it('does not cache value when ttl is 0', function (done) { + it('does not cache value when ttl is 0', (done) => { let gen = 0; const method = function (id, next) { @@ -522,7 +522,7 @@ describe('Methods', function () { }); }); - it('generates new value after cache drop', function (done) { + it('generates new value after cache drop', (done) => { let gen = 0; const method = function (id, next) { @@ -555,7 +555,7 @@ describe('Methods', function () { }); }); - it('errors on invalid drop key', function (done) { + it('errors on invalid drop key', (done) => { let gen = 0; const method = function (id, next) { @@ -579,7 +579,7 @@ describe('Methods', function () { }); }); - it('reports cache stats for each method', function (done) { + it('reports cache stats for each method', (done) => { const method = function (id, next) { @@ -605,46 +605,46 @@ describe('Methods', function () { }); }); - it('throws an error when name is not a string', function (done) { + it('throws an error when name is not a string', (done) => { expect(function () { const server = new Hapi.Server(); - server.method(0, function () { }); + server.method(0, () => { }); }).to.throw('name must be a string'); done(); }); - it('throws an error when name is invalid', function (done) { + it('throws an error when name is invalid', (done) => { expect(function () { const server = new Hapi.Server(); - server.method('0', function () { }); + server.method('0', () => { }); }).to.throw('Invalid name: 0'); expect(function () { const server = new Hapi.Server(); - server.method('a..', function () { }); + server.method('a..', () => { }); }).to.throw('Invalid name: a..'); expect(function () { const server = new Hapi.Server(); - server.method('a.0', function () { }); + server.method('a.0', () => { }); }).to.throw('Invalid name: a.0'); expect(function () { const server = new Hapi.Server(); - server.method('.a', function () { }); + server.method('.a', () => { }); }).to.throw('Invalid name: .a'); done(); }); - it('throws an error when method is not a function', function (done) { + it('throws an error when method is not a function', (done) => { expect(function () { @@ -654,59 +654,59 @@ describe('Methods', function () { done(); }); - it('throws an error when options is not an object', function (done) { + it('throws an error when options is not an object', (done) => { expect(function () { const server = new Hapi.Server(); - server.method('user', function () { }, 'options'); + server.method('user', () => { }, 'options'); }).to.throw(/Invalid method options \(user\)/); done(); }); - it('throws an error when options.generateKey is not a function', function (done) { + it('throws an error when options.generateKey is not a function', (done) => { expect(function () { const server = new Hapi.Server(); - server.method('user', function () { }, { generateKey: 'function' }); + server.method('user', () => { }, { generateKey: 'function' }); }).to.throw(/Invalid method options \(user\)/); done(); }); - it('throws an error when options.cache is not valid', function (done) { + it('throws an error when options.cache is not valid', (done) => { expect(function () { const server = new Hapi.Server({ cache: CatboxMemory }); - server.method('user', function () { }, { cache: { x: 'y', generateTimeout: 10 } }); + server.method('user', () => { }, { cache: { x: 'y', generateTimeout: 10 } }); }).to.throw(/Invalid cache policy configuration/); done(); }); - it('throws an error when generateTimeout is not present', function (done) { + it('throws an error when generateTimeout is not present', (done) => { const server = new Hapi.Server(); expect(function () { - server.method('test', function () { }, { cache: {} }); + server.method('test', () => { }, { cache: {} }); }).to.throw('Method caching requires a timeout value in generateTimeout: test'); done(); }); - it('allows generateTimeout to be false', function (done) { + it('allows generateTimeout to be false', (done) => { const server = new Hapi.Server(); expect(function () { - server.method('test', function () { }, { cache: { generateTimeout: false } }); + server.method('test', () => { }, { cache: { generateTimeout: false } }); }).to.not.throw(); done(); }); - it('returns a valid result when calling a method without using the cache', function (done) { + it('returns a valid result when calling a method without using the cache', (done) => { const server = new Hapi.Server(); @@ -723,7 +723,7 @@ describe('Methods', function () { }); }); - it('returns a valid result when calling a method when using the cache', function (done) { + it('returns a valid result when calling a method when using the cache', (done) => { const server = new Hapi.Server(); server.connection(); @@ -746,7 +746,7 @@ describe('Methods', function () { }); }); - it('returns an error result when calling a method that returns an error', function (done) { + it('returns an error result when calling a method that returns an error', (done) => { const server = new Hapi.Server(); @@ -763,7 +763,7 @@ describe('Methods', function () { }); }); - it('returns a different result when calling a method without using the cache', function (done) { + it('returns a different result when calling a method without using the cache', (done) => { const server = new Hapi.Server(); @@ -787,7 +787,7 @@ describe('Methods', function () { }); }); - it('returns a valid result when calling a method using the cache', function (done) { + it('returns a valid result when calling a method using the cache', (done) => { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); @@ -819,7 +819,7 @@ describe('Methods', function () { }); }); - it('returns timeout when method taking too long using the cache', function (done) { + it('returns timeout when method taking too long using the cache', (done) => { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); @@ -857,7 +857,7 @@ describe('Methods', function () { }); }); - it('supports empty key method', function (done) { + it('supports empty key method', (done) => { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); @@ -889,7 +889,7 @@ describe('Methods', function () { }); }); - it('returns valid results when calling a method (with different keys) using the cache', function (done) { + it('returns valid results when calling a method (with different keys) using the cache', (done) => { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); @@ -920,7 +920,7 @@ describe('Methods', function () { }); }); - it('errors when key generation fails', function (done) { + it('errors when key generation fails', (done) => { const server = new Hapi.Server({ cache: CatboxMemory }); server.connection(); @@ -950,7 +950,7 @@ describe('Methods', function () { }); }); - it('sets method bind without cache', function (done) { + it('sets method bind without cache', (done) => { const method = function (id, next) { @@ -978,7 +978,7 @@ describe('Methods', function () { }); }); - it('sets method bind with cache', function (done) { + it('sets method bind with cache', (done) => { const method = function (id, next) { @@ -1006,7 +1006,7 @@ describe('Methods', function () { }); }); - it('shallow copies bind config', function (done) { + it('shallow copies bind config', (done) => { const bind = { gen: 7 }; const method = function (id, next) { @@ -1036,9 +1036,9 @@ describe('Methods', function () { }); }); - describe('_add()', function () { + describe('_add()', () => { - it('normalizes no callback into callback (direct)', function (done) { + it('normalizes no callback into callback (direct)', (done) => { const add = function (a, b) { @@ -1052,7 +1052,7 @@ describe('Methods', function () { done(); }); - it('normalizes no callback into callback (direct error)', function (done) { + it('normalizes no callback into callback (direct error)', (done) => { const add = function (a, b) { @@ -1067,7 +1067,7 @@ describe('Methods', function () { done(); }); - it('normalizes no callback into callback (direct throw)', function (done) { + it('normalizes no callback into callback (direct throw)', (done) => { const add = function (a, b) { @@ -1083,7 +1083,7 @@ describe('Methods', function () { done(); }); - it('normalizes no callback into callback (normalized)', function (done) { + it('normalizes no callback into callback (normalized)', (done) => { const add = function (a, b) { @@ -1100,7 +1100,7 @@ describe('Methods', function () { }); }); - it('normalizes no callback into callback (normalized error)', function (done) { + it('normalizes no callback into callback (normalized error)', (done) => { const add = function (a, b) { @@ -1118,7 +1118,7 @@ describe('Methods', function () { }); }); - it('normalizes no callback into callback (normalized throw)', function (done) { + it('normalizes no callback into callback (normalized throw)', (done) => { const add = function (a, b) { @@ -1137,7 +1137,7 @@ describe('Methods', function () { }); }); - it('normalizes no callback into callback (cached)', function (done) { + it('normalizes no callback into callback (cached)', (done) => { const add = function (a, b) { @@ -1154,7 +1154,7 @@ describe('Methods', function () { }); }); - it('normalizes no callback into callback (cached error)', function (done) { + it('normalizes no callback into callback (cached error)', (done) => { const add = function (a, b) { @@ -1172,7 +1172,7 @@ describe('Methods', function () { }); }); - it('normalizes no callback into callback (cached throw)', function (done) { + it('normalizes no callback into callback (cached throw)', (done) => { const add = function (a, b) { @@ -1190,7 +1190,7 @@ describe('Methods', function () { }); }); - it('throws an error if unknown keys are present when making a server method using an object', function (done) { + it('throws an error if unknown keys are present when making a server method using an object', (done) => { const fn = function () { }; const server = new Hapi.Server(); diff --git a/test/payload.js b/test/payload.js index 784e22d7c..e65c385ad 100755 --- a/test/payload.js +++ b/test/payload.js @@ -26,9 +26,9 @@ const it = lab.it; const expect = Code.expect; -describe('payload', function () { +describe('payload', () => { - it('sets payload', function (done) { + it('sets payload', (done) => { const payload = '{"x":"1","y":"2","z":"3"}'; @@ -52,7 +52,7 @@ describe('payload', function () { }); }); - it('handles request socket error', function (done) { + it('handles request socket error', (done) => { const handler = function () { @@ -71,7 +71,7 @@ describe('payload', function () { }); }); - it('handles request socket close', function (done) { + it('handles request socket close', (done) => { const handler = function () { @@ -91,7 +91,7 @@ describe('payload', function () { server.inject({ method: 'POST', url: '/', payload: 'test', simulate: { close: true, end: false } }, function (res) { }); }); - it('handles aborted request', function (done) { + it('handles aborted request', (done) => { const handler = function (request, reply) { @@ -142,7 +142,7 @@ describe('payload', function () { }); }); - it('errors when payload too big', function (done) { + it('errors when payload too big', (done) => { const payload = '{"x":"1","y":"2","z":"3"}'; @@ -165,7 +165,7 @@ describe('payload', function () { }); }); - it('returns 400 with response when payload is not consumed', function (done) { + it('returns 400 with response when payload is not consumed', (done) => { const payload = new Buffer(10 * 1024 * 1024).toString(); @@ -195,7 +195,7 @@ describe('payload', function () { }); }); - it('peeks at unparsed data', function (done) { + it('peeks at unparsed data', (done) => { let data = null; const ext = function (request, reply) { @@ -206,7 +206,7 @@ describe('payload', function () { chunks.push(chunk); }); - request.once('finish', function () { + request.once('finish', () => { data = Buffer.concat(chunks); }); @@ -232,7 +232,7 @@ describe('payload', function () { }); }); - it('handles gzipped payload', function (done) { + it('handles gzipped payload', (done) => { const handler = function (request, reply) { @@ -266,7 +266,7 @@ describe('payload', function () { }); }); - it('saves a file after content decoding', function (done) { + it('saves a file after content decoding', (done) => { const path = Path.join(__dirname, './file/image.jpg'); const sourceContents = Fs.readFileSync(path); @@ -293,7 +293,7 @@ describe('payload', function () { }); }); - it('errors saving a file without parse', function (done) { + it('errors saving a file without parse', (done) => { const handler = function (request, reply) { }; @@ -307,7 +307,7 @@ describe('payload', function () { }); }); - it('sets parse mode when route methos is * and request is POST', function (done) { + it('sets parse mode when route methos is * and request is POST', (done) => { const handler = function (request, reply) { @@ -326,7 +326,7 @@ describe('payload', function () { }); }); - it('returns an error on unsupported mime type', function (done) { + it('returns an error on unsupported mime type', (done) => { const handler = function (request, reply) { @@ -362,7 +362,7 @@ describe('payload', function () { }); }); - it('ignores unsupported mime type', function (done) { + it('ignores unsupported mime type', (done) => { const handler = function (request, reply) { @@ -381,7 +381,7 @@ describe('payload', function () { }); }); - it('returns 200 on octet mime type', function (done) { + it('returns 200 on octet mime type', (done) => { const handler = function (request, reply) { @@ -400,7 +400,7 @@ describe('payload', function () { }); }); - it('returns 200 on text mime type', function (done) { + it('returns 200 on text mime type', (done) => { const textHandler = function (request, reply) { @@ -419,7 +419,7 @@ describe('payload', function () { }); }); - it('returns 200 on override mime type', function (done) { + it('returns 200 on override mime type', (done) => { const handler = function (request, reply) { @@ -438,7 +438,7 @@ describe('payload', function () { }); }); - it('returns 200 on text mime type when allowed', function (done) { + it('returns 200 on text mime type when allowed', (done) => { const textHandler = function (request, reply) { @@ -457,7 +457,7 @@ describe('payload', function () { }); }); - it('returns 415 on non text mime type when disallowed', function (done) { + it('returns 415 on non text mime type when disallowed', (done) => { const textHandler = function (request, reply) { @@ -475,7 +475,7 @@ describe('payload', function () { }); }); - it('returns 200 on text mime type when allowed (array)', function (done) { + it('returns 200 on text mime type when allowed (array)', (done) => { const textHandler = function (request, reply) { @@ -494,7 +494,7 @@ describe('payload', function () { }); }); - it('returns 415 on non text mime type when disallowed (array)', function (done) { + it('returns 415 on non text mime type when disallowed (array)', (done) => { const textHandler = function (request, reply) { @@ -512,7 +512,7 @@ describe('payload', function () { }); }); - it('parses application/x-www-form-urlencoded with arrays', function (done) { + it('parses application/x-www-form-urlencoded with arrays', (done) => { const server = new Hapi.Server(); server.connection(); @@ -534,7 +534,7 @@ describe('payload', function () { }); }); - it('returns parsed multipart data', function (done) { + it('returns parsed multipart data', (done) => { const multipartPayload = '--AaB03x\r\n' + @@ -592,7 +592,7 @@ describe('payload', function () { }); }); - it('times out when client request taking too long', function (done) { + it('times out when client request taking too long', (done) => { const handler = function (request, reply) { @@ -631,7 +631,7 @@ describe('payload', function () { }); }); - it('times out when client request taking too long (route override)', function (done) { + it('times out when client request taking too long (route override)', (done) => { const handler = function (request, reply) { @@ -670,7 +670,7 @@ describe('payload', function () { }); }); - it('returns payload when timeout is not triggered', function (done) { + it('returns payload when timeout is not triggered', (done) => { const handler = function (request, reply) { diff --git a/test/plugin.js b/test/plugin.js index fb0329aaa..feb0b148b 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -27,11 +27,11 @@ const it = lab.it; const expect = Code.expect; -describe('Plugin', function () { +describe('Plugin', () => { - describe('select()', function () { + describe('select()', () => { - it('creates a subset of connections for manipulation', function (done) { + it('creates a subset of connections for manipulation', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); @@ -134,7 +134,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin on selection inside a plugin', function (done) { + it('registers a plugin on selection inside a plugin', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a'] }); @@ -171,9 +171,9 @@ describe('Plugin', function () { }); }); - describe('register()', function () { + describe('register()', () => { - it('registers plugin with options', function (done) { + it('registers plugin with options', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); @@ -196,7 +196,7 @@ describe('Plugin', function () { }); }); - it('registers a required plugin', function (done) { + it('registers a required plugin', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); @@ -220,7 +220,7 @@ describe('Plugin', function () { }); }); - it('throws on bad plugin (missing attributes)', function (done) { + it('throws on bad plugin (missing attributes)', (done) => { const server = new Hapi.Server(); expect(function () { @@ -237,7 +237,7 @@ describe('Plugin', function () { done(); }); - it('throws on bad plugin (missing name)', function (done) { + it('throws on bad plugin (missing name)', (done) => { const register = function (srv, options, next) { @@ -255,7 +255,7 @@ describe('Plugin', function () { done(); }); - it('throws on bad plugin (empty pkg)', function (done) { + it('throws on bad plugin (empty pkg)', (done) => { const register = function (srv, options, next) { @@ -275,7 +275,7 @@ describe('Plugin', function () { done(); }); - it('throws when register is missing a callback function', function (done) { + it('throws when register is missing a callback function', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a', 'b'] }); @@ -297,7 +297,7 @@ describe('Plugin', function () { done(); }); - it('returns plugin error', function (done) { + it('returns plugin error', (done) => { const test = function (srv, options, next) { @@ -318,7 +318,7 @@ describe('Plugin', function () { }); }); - it('sets version to 0.0.0 if missing', function (done) { + it('sets version to 0.0.0 if missing', (done) => { const test = function (srv, options, next) { @@ -354,7 +354,7 @@ describe('Plugin', function () { }); }); - it('exposes plugin registration information', function (done) { + it('exposes plugin registration information', (done) => { const test = function (srv, options, next) { @@ -400,7 +400,7 @@ describe('Plugin', function () { }); }); - it('prevents plugin from multiple registrations', function (done) { + it('prevents plugin from multiple registrations', (done) => { const test = function (srv, options, next) { @@ -434,7 +434,7 @@ describe('Plugin', function () { }); }); - it('allows plugin multiple registrations (attributes)', function (done) { + it('allows plugin multiple registrations (attributes)', (done) => { const test = function (srv, options, next) { @@ -461,7 +461,7 @@ describe('Plugin', function () { }); }); - it('registers multiple plugins', function (done) { + it('registers multiple plugins', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -481,7 +481,7 @@ describe('Plugin', function () { }); }); - it('registers multiple plugins (verbose)', function (done) { + it('registers multiple plugins (verbose)', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -501,7 +501,7 @@ describe('Plugin', function () { }); }); - it('registers a child plugin', function (done) { + it('registers a child plugin', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -516,7 +516,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin with routes path prefix', function (done) { + it('registers a plugin with routes path prefix', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -532,7 +532,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin with routes path prefix (plugin options)', function (done) { + it('registers a plugin with routes path prefix (plugin options)', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -548,7 +548,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin with routes path prefix and plugin root route', function (done) { + it('registers a plugin with routes path prefix and plugin root route', (done) => { const test = function (srv, options, next) { @@ -580,7 +580,7 @@ describe('Plugin', function () { }); }); - it('ignores the type of the plugin value', function (done) { + it('ignores the type of the plugin value', (done) => { const a = function () { }; a.register = function (srv, options, next) { @@ -611,7 +611,7 @@ describe('Plugin', function () { }); }); - it('ignores unknown plugin properties', function (done) { + it('ignores unknown plugin properties', (done) => { const a = { register: function (srv, options, next) { @@ -640,7 +640,7 @@ describe('Plugin', function () { }); }); - it('ignores unknown plugin properties (with options)', function (done) { + it('ignores unknown plugin properties (with options)', (done) => { const a = { register: function (srv, options, next) { @@ -669,7 +669,7 @@ describe('Plugin', function () { }); }); - it('registers a child plugin with parent routes path prefix', function (done) { + it('registers a child plugin with parent routes path prefix', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -684,7 +684,7 @@ describe('Plugin', function () { }); }); - it('registers a child plugin with parent routes vhost prefix', function (done) { + it('registers a child plugin with parent routes vhost prefix', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -699,7 +699,7 @@ describe('Plugin', function () { }); }); - it('registers a child plugin with parent routes path prefix and inner register prefix', function (done) { + it('registers a child plugin with parent routes path prefix and inner register prefix', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -714,7 +714,7 @@ describe('Plugin', function () { }); }); - it('registers a child plugin with parent routes vhost prefix and inner register vhost', function (done) { + it('registers a child plugin with parent routes vhost prefix and inner register vhost', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -729,7 +729,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin with routes vhost', function (done) { + it('registers a plugin with routes vhost', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -749,7 +749,7 @@ describe('Plugin', function () { }); }); - it('registers a plugin with routes vhost (plugin options)', function (done) { + it('registers a plugin with routes vhost (plugin options)', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); @@ -769,7 +769,7 @@ describe('Plugin', function () { }); }); - it('registers plugins with pre-selected label', function (done) { + it('registers plugins with pre-selected label', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a'] }); @@ -810,7 +810,7 @@ describe('Plugin', function () { }); }); - it('registers plugins with pre-selected labels', function (done) { + it('registers plugins with pre-selected labels', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a'] }); @@ -860,7 +860,7 @@ describe('Plugin', function () { }); }); - it('registers plugins with pre-selected labels (plugin options)', function (done) { + it('registers plugins with pre-selected labels (plugin options)', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a'] }); @@ -910,7 +910,7 @@ describe('Plugin', function () { }); }); - it('sets multiple dependencies in one statement', function (done) { + it('sets multiple dependencies in one statement', (done) => { const a = function (srv, options, next) { @@ -958,7 +958,7 @@ describe('Plugin', function () { }); }); - it('sets multiple dependencies in attributes', function (done) { + it('sets multiple dependencies in attributes', (done) => { const a = function (srv, options, next) { @@ -1006,7 +1006,7 @@ describe('Plugin', function () { }); }); - it('sets multiple dependencies in multiple statements', function (done) { + it('sets multiple dependencies in multiple statements', (done) => { const a = function (srv, options, next) { @@ -1055,7 +1055,7 @@ describe('Plugin', function () { }); }); - it('sets multiple dependencies in multiple locations', function (done) { + it('sets multiple dependencies in multiple locations', (done) => { const a = function (srv, options, next) { @@ -1104,7 +1104,7 @@ describe('Plugin', function () { }); }); - it('errors when dependency loaded before connection was added', function (done) { + it('errors when dependency loaded before connection was added', (done) => { const a = function (srv, options, next) { @@ -1142,7 +1142,7 @@ describe('Plugin', function () { }); }); - it('set dependency on previously loaded connectionless plugin', function (done) { + it('set dependency on previously loaded connectionless plugin', (done) => { const a = function (srv, options, next) { @@ -1181,7 +1181,7 @@ describe('Plugin', function () { }); }); - it('allows multiple connectionless plugin', function (done) { + it('allows multiple connectionless plugin', (done) => { const a = function (srv, options, next) { @@ -1221,7 +1221,7 @@ describe('Plugin', function () { }); }); - it('register nested connectionless plugins', function (done) { + it('register nested connectionless plugins', (done) => { const a = function (srv, options, next) { @@ -1255,7 +1255,7 @@ describe('Plugin', function () { }); }); - it('throws when nested connectionless plugins select', function (done) { + it('throws when nested connectionless plugins select', (done) => { const a = function (srv, options, next) { @@ -1290,7 +1290,7 @@ describe('Plugin', function () { }); }); - it('register a plugin once per connection', function (done) { + it('register a plugin once per connection', (done) => { const a = function (srv, options, next) { @@ -1333,7 +1333,7 @@ describe('Plugin', function () { }); }); - it('register a plugin once per connection (skip empty selection)', function (done) { + it('register a plugin once per connection (skip empty selection)', (done) => { const a = function (srv, options, next) { @@ -1376,7 +1376,7 @@ describe('Plugin', function () { }); }); - it('register a connectionless plugin once (empty selection)', function (done) { + it('register a connectionless plugin once (empty selection)', (done) => { let count = 0; const b = function (srv, options, next) { @@ -1401,7 +1401,7 @@ describe('Plugin', function () { }); }); - it('register a plugin once per connection (no selection left)', function (done) { + it('register a plugin once per connection (no selection left)', (done) => { const a = function (srv, options, next) { @@ -1444,7 +1444,7 @@ describe('Plugin', function () { }); }); - it('register a plugin once (empty selection)', function (done) { + it('register a plugin once (empty selection)', (done) => { let count = 0; const b = function (srv, options, next) { @@ -1468,7 +1468,7 @@ describe('Plugin', function () { }); }); - it('register a connectionless plugin once', function (done) { + it('register a connectionless plugin once', (done) => { const a = function (srv, options, next) { @@ -1513,7 +1513,7 @@ describe('Plugin', function () { }); }); - it('register a connectionless plugin once (plugin attributes)', function (done) { + it('register a connectionless plugin once (plugin attributes)', (done) => { const a = function (srv, options, next) { @@ -1559,7 +1559,7 @@ describe('Plugin', function () { }); }); - it('register a connectionless plugin once (plugin options)', function (done) { + it('register a connectionless plugin once (plugin options)', (done) => { const a = function (srv, options, next) { @@ -1604,7 +1604,7 @@ describe('Plugin', function () { }); }); - it('register a connectionless plugin once (first time)', function (done) { + it('register a connectionless plugin once (first time)', (done) => { let count = 0; const b = function (srv, options, next) { @@ -1630,7 +1630,7 @@ describe('Plugin', function () { }); }); - it('throws when once used with plugin options', function (done) { + it('throws when once used with plugin options', (done) => { const a = function (srv, options, next) { @@ -1651,7 +1651,7 @@ describe('Plugin', function () { done(); }); - it('throws when dependencies is an object', function (done) { + it('throws when dependencies is an object', (done) => { const a = function (srv, options, next) { @@ -1667,12 +1667,12 @@ describe('Plugin', function () { expect(function () { - server.register(a, function () { }); + server.register(a, () => { }); }).to.throw(); done(); }); - it('throws when dependencies contain something else than a string', function (done) { + it('throws when dependencies contain something else than a string', (done) => { const a = function (srv, options, next) { @@ -1688,19 +1688,19 @@ describe('Plugin', function () { expect(function () { - server.register(a, function () { }); + server.register(a, () => { }); }).to.throw(); done(); }); - it('exposes server decorations to next register', function (done) { + it('exposes server decorations to next register', (done) => { const server = new Hapi.Server(); server.connection(); const a = function (srv, options, next) { - srv.decorate('server', 'a', function () { + srv.decorate('server', 'a', () => { return 'a'; }); @@ -1732,14 +1732,14 @@ describe('Plugin', function () { }); }); - it('exposes server decorations to dependency (dependency first)', function (done) { + it('exposes server decorations to dependency (dependency first)', (done) => { const server = new Hapi.Server(); server.connection(); const a = function (srv, options, next) { - srv.decorate('server', 'a', function () { + srv.decorate('server', 'a', () => { return 'a'; }); @@ -1776,14 +1776,14 @@ describe('Plugin', function () { }); }); - it('exposes server decorations to dependency (dependency second)', function (done) { + it('exposes server decorations to dependency (dependency second)', (done) => { const server = new Hapi.Server(); server.connection(); const a = function (srv, options, next) { - srv.decorate('server', 'a', function () { + srv.decorate('server', 'a', () => { return 'a'; }); @@ -1822,14 +1822,14 @@ describe('Plugin', function () { }); }); - it('exposes server decorations to next register when nested', function (done) { + it('exposes server decorations to next register when nested', (done) => { const server = new Hapi.Server(); server.connection(); const a = function (srv, options, next) { - srv.decorate('server', 'a', function () { + srv.decorate('server', 'a', () => { return 'a'; }); @@ -1866,9 +1866,9 @@ describe('Plugin', function () { }); }); - describe('auth', function () { + describe('auth', () => { - it('adds auth strategy via plugin', function (done) { + it('adds auth strategy via plugin', (done) => { const server = new Hapi.Server(); server.connection({ labels: 'a' }); @@ -1900,9 +1900,9 @@ describe('Plugin', function () { }); }); - describe('bind()', function () { + describe('bind()', () => { - it('sets plugin context', function (done) { + it('sets plugin context', (done) => { const test = function (srv, options, next) { @@ -1948,9 +1948,9 @@ describe('Plugin', function () { }); }); - describe('cache()', function () { + describe('cache()', () => { - it('provisions a server cache', function (done) { + it('provisions a server cache', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1970,7 +1970,7 @@ describe('Plugin', function () { }); }); - it('throws when missing segment', function (done) { + it('throws when missing segment', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1981,7 +1981,7 @@ describe('Plugin', function () { done(); }); - it('provisions a server cache with custom partition', function (done) { + it('provisions a server cache with custom partition', (done) => { const server = new Hapi.Server({ cache: { engine: CatboxMemory, partition: 'hapi-test-other' } }); server.connection(); @@ -2002,7 +2002,7 @@ describe('Plugin', function () { }); }); - it('throws when allocating an invalid cache segment', function (done) { + it('throws when allocating an invalid cache segment', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2014,7 +2014,7 @@ describe('Plugin', function () { done(); }); - it('allows allocating a cache segment with empty options', function (done) { + it('allows allocating a cache segment with empty options', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2026,7 +2026,7 @@ describe('Plugin', function () { done(); }); - it('allows reusing the same cache segment (server)', function (done) { + it('allows reusing the same cache segment (server)', (done) => { const server = new Hapi.Server({ cache: { engine: CatboxMemory, shared: true } }); server.connection(); @@ -2038,7 +2038,7 @@ describe('Plugin', function () { done(); }); - it('allows reusing the same cache segment (cache)', function (done) { + it('allows reusing the same cache segment (cache)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2050,7 +2050,7 @@ describe('Plugin', function () { done(); }); - it('uses plugin cache interface', function (done) { + it('uses plugin cache interface', (done) => { const test = function (srv, options, next) { @@ -2108,9 +2108,9 @@ describe('Plugin', function () { }); }); - describe('connection()', function () { + describe('connection()', () => { - it('returns a selection object within the same realm', function (done) { + it('returns a selection object within the same realm', (done) => { const plugin = function (srv, options, next) { @@ -2146,9 +2146,9 @@ describe('Plugin', function () { }); }); - describe('decorate()', function () { + describe('decorate()', () => { - it('decorates request', function (done) { + it('decorates request', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2175,7 +2175,7 @@ describe('Plugin', function () { }); }); - it('decorates reply', function (done) { + it('decorates reply', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2202,36 +2202,36 @@ describe('Plugin', function () { }); }); - it('throws on double reply decoration', function (done) { + it('throws on double reply decoration', (done) => { const server = new Hapi.Server(); server.connection(); - server.decorate('reply', 'success', function () { + server.decorate('reply', 'success', () => { return this.response({ status: 'ok' }); }); expect(function () { - server.decorate('reply', 'success', function () { }); + server.decorate('reply', 'success', () => { }); }).to.throw('Reply interface decoration already defined: success'); done(); }); - it('throws on internal conflict', function (done) { + it('throws on internal conflict', (done) => { const server = new Hapi.Server(); server.connection(); expect(function () { - server.decorate('reply', 'redirect', function () { }); + server.decorate('reply', 'redirect', () => { }); }).to.throw('Cannot override built-in reply interface decoration: redirect'); done(); }); - it('decorates server', function (done) { + it('decorates server', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2258,7 +2258,7 @@ describe('Plugin', function () { }); }); - it('throws on double server decoration', function (done) { + it('throws on double server decoration', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2277,51 +2277,51 @@ describe('Plugin', function () { expect(function () { - server.decorate('server', 'ok', function () { }); + server.decorate('server', 'ok', () => { }); }).to.throw('Server decoration already defined: ok'); done(); }); - it('throws on server decoration root conflict', function (done) { + it('throws on server decoration root conflict', (done) => { const server = new Hapi.Server(); server.connection(); expect(function () { - server.decorate('server', 'start', function () { }); + server.decorate('server', 'start', () => { }); }).to.throw('Cannot override the built-in server interface method: start'); done(); }); - it('throws on server decoration plugin conflict', function (done) { + it('throws on server decoration plugin conflict', (done) => { const server = new Hapi.Server(); server.connection(); expect(function () { - server.decorate('server', 'select', function () { }); + server.decorate('server', 'select', () => { }); }).to.throw('Cannot override the built-in server interface method: select'); done(); }); - it('throws on invalid decoration name', function (done) { + it('throws on invalid decoration name', (done) => { const server = new Hapi.Server(); server.connection(); expect(function () { - server.decorate('server', '_special', function () { }); + server.decorate('server', '_special', () => { }); }).to.throw('Property name cannot begin with an underscore: _special'); done(); }); }); - describe('dependency()', function () { + describe('dependency()', () => { - it('fails to register single plugin with dependencies', function (done) { + it('fails to register single plugin with dependencies', (done) => { const test = function (srv, options, next) { @@ -2346,7 +2346,7 @@ describe('Plugin', function () { }); }); - it('fails to register single plugin with dependencies (attributes)', function (done) { + it('fails to register single plugin with dependencies (attributes)', (done) => { const test = function (srv, options, next) { @@ -2371,7 +2371,7 @@ describe('Plugin', function () { }); }); - it('fails to register single plugin with dependencies (connectionless)', function (done) { + it('fails to register single plugin with dependencies (connectionless)', (done) => { const test = function (srv, options, next) { @@ -2397,7 +2397,7 @@ describe('Plugin', function () { }); }); - it('fails to register plugin with multiple dependencies (connectionless)', function (done) { + it('fails to register plugin with multiple dependencies (connectionless)', (done) => { const test = function (srv, options, next) { @@ -2433,7 +2433,7 @@ describe('Plugin', function () { }); }); - it('register plugin with multiple dependencies (connectionless)', function (done) { + it('register plugin with multiple dependencies (connectionless)', (done) => { const test = function (srv, options, next) { @@ -2468,7 +2468,7 @@ describe('Plugin', function () { }); }); - it('fails to register multiple plugins with dependencies', function (done) { + it('fails to register multiple plugins with dependencies', (done) => { const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); @@ -2483,7 +2483,7 @@ describe('Plugin', function () { }); }); - it('recognizes dependencies from peer plugins', function (done) { + it('recognizes dependencies from peer plugins', (done) => { const a = function (srv, options, next) { @@ -2522,7 +2522,7 @@ describe('Plugin', function () { }); }); - it('errors when missing inner dependencies', function (done) { + it('errors when missing inner dependencies', (done) => { const a = function (srv, options, next) { @@ -2556,7 +2556,7 @@ describe('Plugin', function () { }); }); - it('errors when missing inner dependencies (attributes)', function (done) { + it('errors when missing inner dependencies (attributes)', (done) => { const a = function (srv, options, next) { @@ -2591,13 +2591,13 @@ describe('Plugin', function () { }); }); - describe('events', function () { + describe('events', () => { - it('plugin event handlers receive more than 2 arguments when they exist', function (done) { + it('plugin event handlers receive more than 2 arguments when they exist', (done) => { const test = function (srv, options, next) { - srv.once('request-internal', function () { + srv.once('request-internal', () => { expect(arguments).to.have.length(3); done(); @@ -2615,11 +2615,11 @@ describe('Plugin', function () { server.register(test, function (err) { expect(err).to.not.exist(); - server.inject({ url: '/' }, function () { }); + server.inject({ url: '/' }, () => { }); }); }); - it('listens to events on selected connections', function (done) { + it('listens to events on selected connections', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a'] }); @@ -2633,12 +2633,12 @@ describe('Plugin', function () { let counter = 0; const test = function (srv, options, next) { - srv.select(['a', 'b']).on('test', function () { + srv.select(['a', 'b']).on('test', () => { ++counter; }); - srv.select(['a']).on('start', function () { + srv.select(['a']).on('start', () => { ++counter; }); @@ -2672,9 +2672,9 @@ describe('Plugin', function () { }); }); - describe('expose()', function () { + describe('expose()', () => { - it('exposes an api', function (done) { + it('exposes an api', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); @@ -2699,9 +2699,9 @@ describe('Plugin', function () { }); }); - describe('ext()', function () { + describe('ext()', () => { - it('extends onRequest point', function (done) { + it('extends onRequest point', (done) => { const test = function (srv, options, next) { @@ -2742,7 +2742,7 @@ describe('Plugin', function () { }); }); - it('adds multiple ext functions with simple dependencies', function (done) { + it('adds multiple ext functions with simple dependencies', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['a', 'b', '0'] }); @@ -2786,7 +2786,7 @@ describe('Plugin', function () { }); }); - it('adds multiple ext functions with complex dependencies', function (done) { + it('adds multiple ext functions with complex dependencies', (done) => { // Generate a plugin with a specific index and ext dependencies. @@ -2842,18 +2842,18 @@ describe('Plugin', function () { }); }); - it('throws when adding ext without connections', function (done) { + it('throws when adding ext without connections', (done) => { const server = new Hapi.Server(); expect(function () { - server.ext('onRequest', function () { }); + server.ext('onRequest', () => { }); }).to.throw('Cannot add ext without a connection'); done(); }); - it('binds server ext to context (options)', function (done) { + it('binds server ext to context (options)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2876,7 +2876,7 @@ describe('Plugin', function () { }); }); - it('binds server ext to context (realm)', function (done) { + it('binds server ext to context (realm)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2900,7 +2900,7 @@ describe('Plugin', function () { }); }); - it('extends server actions', function (done) { + it('extends server actions', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2944,7 +2944,7 @@ describe('Plugin', function () { }); }); - it('extends server actions (single call)', function (done) { + it('extends server actions (single call)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2999,7 +2999,7 @@ describe('Plugin', function () { }); }); - it('combine route extensions', function (done) { + it('combine route extensions', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3073,7 +3073,7 @@ describe('Plugin', function () { }); }); - it('calls method after plugin', function (done) { + it('calls method after plugin', (done) => { const x = function (srv, options, next) { @@ -3110,7 +3110,7 @@ describe('Plugin', function () { }); }); - it('calls method before start', function (done) { + it('calls method before start', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3130,7 +3130,7 @@ describe('Plugin', function () { }); }); - it('calls method before start even if plugin not registered', function (done) { + it('calls method before start even if plugin not registered', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3150,7 +3150,7 @@ describe('Plugin', function () { }); }); - it('fails to start server when after method fails', function (done) { + it('fails to start server when after method fails', (done) => { const test = function (srv, options, next) { @@ -3184,7 +3184,7 @@ describe('Plugin', function () { }); }); - it('errors when added after initialization', function (done) { + it('errors when added after initialization', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3193,7 +3193,7 @@ describe('Plugin', function () { expect(function () { - server.ext('onPreStart', function () { }); + server.ext('onPreStart', () => { }); }).to.throw('Cannot add onPreStart (after) extension after the server was initialized'); done(); @@ -3201,9 +3201,9 @@ describe('Plugin', function () { }); }); - describe('handler()', function () { + describe('handler()', () => { - it('add new handler', function (done) { + it('add new handler', (done) => { const test = function (srv, options1, next) { @@ -3243,7 +3243,7 @@ describe('Plugin', function () { }); }); - it('errors on duplicate handler', function (done) { + it('errors on duplicate handler', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -3251,12 +3251,12 @@ describe('Plugin', function () { expect(function () { - server.handler('file', function () { }); + server.handler('file', () => { }); }).to.throw('Handler name already exists: file'); done(); }); - it('errors on unknown handler', function (done) { + it('errors on unknown handler', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3268,7 +3268,7 @@ describe('Plugin', function () { done(); }); - it('errors on non-string name', function (done) { + it('errors on non-string name', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3280,7 +3280,7 @@ describe('Plugin', function () { done(); }); - it('errors on non-function handler', function (done) { + it('errors on non-function handler', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3293,9 +3293,9 @@ describe('Plugin', function () { }); }); - describe('log()', { parallel: false }, function () { + describe('log()', { parallel: false }, () => { - it('emits a log event', function (done) { + it('emits a log event', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3327,7 +3327,7 @@ describe('Plugin', function () { done(); }); - it('emits a log event and print to console', { parallel: false }, function (done) { + it('emits a log event and print to console', { parallel: false }, (done) => { const server = new Hapi.Server(); server.connection(); @@ -3350,7 +3350,7 @@ describe('Plugin', function () { server.log(['internal', 'implementation', 'error'], 'log event 1'); }); - it('outputs log data to debug console', function (done) { + it('outputs log data to debug console', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3368,7 +3368,7 @@ describe('Plugin', function () { server.log(['implementation'], { data: 1 }); }); - it('outputs log error data to debug console', function (done) { + it('outputs log error data to debug console', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3386,7 +3386,7 @@ describe('Plugin', function () { server.log(['implementation'], new Error('test')); }); - it('outputs log data to debug console without data', function (done) { + it('outputs log data to debug console without data', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3404,7 +3404,7 @@ describe('Plugin', function () { server.log(['implementation']); }); - it('does not output events when debug disabled', function (done) { + it('does not output events when debug disabled', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -3423,7 +3423,7 @@ describe('Plugin', function () { done(); }); - it('does not output events when debug.log disabled', function (done) { + it('does not output events when debug.log disabled', (done) => { const server = new Hapi.Server({ debug: { log: false } }); server.connection(); @@ -3442,7 +3442,7 @@ describe('Plugin', function () { done(); }); - it('does not output non-implementation events by default', function (done) { + it('does not output non-implementation events by default', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3461,7 +3461,7 @@ describe('Plugin', function () { done(); }); - it('emits server log events once', function (done) { + it('emits server log events once', (done) => { let pc = 0; const test = function (srv, options, next) { @@ -3498,9 +3498,9 @@ describe('Plugin', function () { }); }); - describe('lookup()', function () { + describe('lookup()', () => { - it('returns route based on id', function (done) { + it('returns route based on id', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3523,7 +3523,7 @@ describe('Plugin', function () { done(); }); - it('returns null on unknown route', function (done) { + it('returns null on unknown route', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3532,7 +3532,7 @@ describe('Plugin', function () { done(); }); - it('throws on missing id', function (done) { + it('throws on missing id', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3544,9 +3544,9 @@ describe('Plugin', function () { }); }); - describe('match()', function () { + describe('match()', () => { - it('returns route based on path', function (done) { + it('returns route based on path', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3622,7 +3622,7 @@ describe('Plugin', function () { done(); }); - it('throws on missing method', function (done) { + it('throws on missing method', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3633,7 +3633,7 @@ describe('Plugin', function () { done(); }); - it('throws on invalid method', function (done) { + it('throws on invalid method', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3644,7 +3644,7 @@ describe('Plugin', function () { done(); }); - it('throws on missing path', function (done) { + it('throws on missing path', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3655,7 +3655,7 @@ describe('Plugin', function () { done(); }); - it('throws on invalid path type', function (done) { + it('throws on invalid path type', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3666,7 +3666,7 @@ describe('Plugin', function () { done(); }); - it('throws on invalid path prefix', function (done) { + it('throws on invalid path prefix', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3677,7 +3677,7 @@ describe('Plugin', function () { done(); }); - it('throws on invalid path', function (done) { + it('throws on invalid path', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3699,7 +3699,7 @@ describe('Plugin', function () { done(); }); - it('throws on invalid host type', function (done) { + it('throws on invalid host type', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3711,9 +3711,9 @@ describe('Plugin', function () { }); }); - describe('method()', function () { + describe('method()', () => { - it('adds server method using arguments', function (done) { + it('adds server method using arguments', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3738,7 +3738,7 @@ describe('Plugin', function () { }); }); - it('adds server method with plugin bind', function (done) { + it('adds server method with plugin bind', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3768,7 +3768,7 @@ describe('Plugin', function () { }); }); - it('adds server method with method bind', function (done) { + it('adds server method with method bind', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3797,7 +3797,7 @@ describe('Plugin', function () { }); }); - it('adds server method with method and ext bind', function (done) { + it('adds server method with method and ext bind', (done) => { const server = new Hapi.Server(); server.connection(); @@ -3828,9 +3828,9 @@ describe('Plugin', function () { }); }); - describe('path()', function () { + describe('path()', () => { - it('sets local path for directory route handler', function (done) { + it('sets local path for directory route handler', (done) => { const test = function (srv, options, next) { @@ -3867,7 +3867,7 @@ describe('Plugin', function () { }); }); - it('throws when plugin sets undefined path', function (done) { + it('throws when plugin sets undefined path', (done) => { const test = function (srv, options, next) { @@ -3889,9 +3889,9 @@ describe('Plugin', function () { }); }); - describe('render()', function () { + describe('render()', () => { - it('renders view', function (done) { + it('renders view', (done) => { const server = new Hapi.Server(); server.register(Vision, Hoek.ignore); @@ -3910,9 +3910,9 @@ describe('Plugin', function () { }); }); - describe('state()', function () { + describe('state()', () => { - it('throws when adding state without connections', function (done) { + it('throws when adding state without connections', (done) => { const server = new Hapi.Server(); expect(function () { @@ -3924,9 +3924,9 @@ describe('Plugin', function () { }); }); - describe('views()', function () { + describe('views()', () => { - it('requires plugin with views', function (done) { + it('requires plugin with views', (done) => { const test = function (srv, options, next) { @@ -4077,7 +4077,7 @@ internals.plugins = { server.auth.strategy('basic', 'basic', 'required', { validateFunc: loadUser }); - server.auth.scheme('special', function () { + server.auth.scheme('special', () => { return { authenticate: function () { } }; }); diff --git a/test/protect.js b/test/protect.js index c7c8949c4..20666eb04 100755 --- a/test/protect.js +++ b/test/protect.js @@ -23,9 +23,9 @@ const it = lab.it; const expect = Code.expect; -describe('Protect', function () { +describe('Protect', () => { - it('does not handle errors when useDomains is false', function (done) { + it('does not handle errors when useDomains is false', (done) => { const server = new Hapi.Server({ useDomains: false, debug: false }); server.connection(); @@ -52,7 +52,7 @@ describe('Protect', function () { }); }); - it('catches error when handler throws after reply() is called', function (done) { + it('catches error when handler throws after reply() is called', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -74,7 +74,7 @@ describe('Protect', function () { }); }); - it('catches error when handler throws twice after reply() is called', function (done) { + it('catches error when handler throws twice after reply() is called', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -102,7 +102,7 @@ describe('Protect', function () { }); }); - it('catches errors thrown during request handling in non-request domain', function (done) { + it('catches errors thrown during request handling in non-request domain', (done) => { const Client = function () { @@ -158,7 +158,7 @@ describe('Protect', function () { }); }); - it('logs to console after request completed', function (done) { + it('logs to console after request completed', (done) => { const handler = function (request, reply) { diff --git a/test/reply.js b/test/reply.js index dcae52739..fa3b7b20d 100755 --- a/test/reply.js +++ b/test/reply.js @@ -25,9 +25,9 @@ const it = lab.it; const expect = Code.expect; -describe('Reply', function () { +describe('Reply', () => { - it('throws when reply called twice', function (done) { + it('throws when reply called twice', (done) => { const handler = function (request, reply) { @@ -44,7 +44,7 @@ describe('Reply', function () { }); }); - it('redirects from handler', function (done) { + it('redirects from handler', (done) => { const handler = function (request, reply) { @@ -62,9 +62,9 @@ describe('Reply', function () { }); }); - describe('interface()', function () { + describe('interface()', () => { - it('uses reply(null, result) for result', function (done) { + it('uses reply(null, result) for result', (done) => { const handler = function (request, reply) { @@ -82,7 +82,7 @@ describe('Reply', function () { }); }); - it('uses reply(null, err) for err', function (done) { + it('uses reply(null, err) for err', (done) => { const handler = function (request, reply) { @@ -99,7 +99,7 @@ describe('Reply', function () { }); }); - it('ignores result when err provided in reply(err, result)', function (done) { + it('ignores result when err provided in reply(err, result)', (done) => { const handler = function (request, reply) { @@ -117,9 +117,9 @@ describe('Reply', function () { }); }); - describe('response()', function () { + describe('response()', () => { - it('returns null', function (done) { + it('returns null', (done) => { const handler = function (request, reply) { @@ -139,7 +139,7 @@ describe('Reply', function () { }); }); - it('returns a buffer reply', function (done) { + it('returns a buffer reply', (done) => { const handler = function (request, reply) { @@ -159,7 +159,7 @@ describe('Reply', function () { }); }); - it('returns an object response', function (done) { + it('returns an object response', (done) => { const handler = function (request, reply) { @@ -178,7 +178,7 @@ describe('Reply', function () { }); }); - it('returns false', function (done) { + it('returns false', (done) => { const handler = function (request, reply) { @@ -196,7 +196,7 @@ describe('Reply', function () { }); }); - it('returns an error reply', function (done) { + it('returns an error reply', (done) => { const handler = function (request, reply) { @@ -215,7 +215,7 @@ describe('Reply', function () { }); }); - it('returns an empty reply', function (done) { + it('returns an empty reply', (done) => { const handler = function (request, reply) { @@ -235,7 +235,7 @@ describe('Reply', function () { }); }); - it('returns a stream reply', function (done) { + it('returns a stream reply', (done) => { const TestStream = function () { @@ -281,7 +281,7 @@ describe('Reply', function () { }); }); - it('errors on non-readable stream reply', function (done) { + it('errors on non-readable stream reply', (done) => { const streamHandler = function (request, reply) { @@ -332,7 +332,7 @@ describe('Reply', function () { }); }); - it('errors on an http client stream reply', function (done) { + it('errors on an http client stream reply', (done) => { const handler = function (request, reply) { @@ -361,7 +361,7 @@ describe('Reply', function () { }); }); - it('errors on objectMode stream reply', function (done) { + it('errors on objectMode stream reply', (done) => { const TestStream = function () { @@ -398,9 +398,9 @@ describe('Reply', function () { }); }); - describe('promises', function () { + describe('promises', () => { - it('returns a stream', function (done) { + it('returns a stream', (done) => { const TestStream = function () { @@ -440,7 +440,7 @@ describe('Reply', function () { }); }); - it('returns a buffer', function (done) { + it('returns a buffer', (done) => { const handler = function (request, reply) { @@ -462,9 +462,9 @@ describe('Reply', function () { }); }); - describe('hold()', function () { + describe('hold()', () => { - it('undo scheduled next tick in reply interface', function (done) { + it('undo scheduled next tick in reply interface', (done) => { const server = new Hapi.Server(); server.connection(); @@ -483,7 +483,7 @@ describe('Reply', function () { }); }); - it('sends reply after timed handler', function (done) { + it('sends reply after timed handler', (done) => { const server = new Hapi.Server(); server.connection(); @@ -507,9 +507,9 @@ describe('Reply', function () { }); }); - describe('close()', function () { + describe('close()', () => { - it('returns a reply with manual end', function (done) { + it('returns a reply with manual end', (done) => { const handler = function (request, reply) { @@ -528,7 +528,7 @@ describe('Reply', function () { }); }); - it('returns a reply with auto end', function (done) { + it('returns a reply with auto end', (done) => { const handler = function (request, reply) { @@ -547,9 +547,9 @@ describe('Reply', function () { }); }); - describe('continue()', function () { + describe('continue()', () => { - it('sets empty reply on continue in handler', function (done) { + it('sets empty reply on continue in handler', (done) => { const handler = function (request, reply) { @@ -569,7 +569,7 @@ describe('Reply', function () { }); }); - it('sets empty reply on continue in prerequisite', function (done) { + it('sets empty reply on continue in prerequisite', (done) => { const pre1 = function (request, reply) { diff --git a/test/request.js b/test/request.js index 301f6a52a..706d51f79 100755 --- a/test/request.js +++ b/test/request.js @@ -26,14 +26,14 @@ const it = lab.it; const expect = Code.expect; -describe('Request.Generator', function () { +describe('Request.Generator', () => { - it('decorates request multiple times', function (done) { + it('decorates request multiple times', (done) => { const server = new Hapi.Server(); server.connection(); - server.decorate('request', 'x2', function () { + server.decorate('request', 'x2', () => { return 2; }); @@ -58,9 +58,9 @@ describe('Request.Generator', function () { }); }); -describe('Request', function () { +describe('Request', () => { - it('sets client address', function (done) { + it('sets client address', (done) => { const server = new Hapi.Server(); server.connection(); @@ -91,7 +91,7 @@ describe('Request', function () { }); }); - it('sets referrer', function (done) { + it('sets referrer', (done) => { const server = new Hapi.Server(); server.connection(); @@ -111,7 +111,7 @@ describe('Request', function () { }); }); - it('sets referer', function (done) { + it('sets referer', (done) => { const server = new Hapi.Server(); server.connection(); @@ -131,7 +131,7 @@ describe('Request', function () { }); }); - it('sets headers', function (done) { + it('sets headers', (done) => { const handler = function (request, reply) { @@ -149,7 +149,7 @@ describe('Request', function () { }); }); - it('generates unique request id', function (done) { + it('generates unique request id', (done) => { const handler = function (request, reply) { @@ -175,9 +175,9 @@ describe('Request', function () { }); }); - describe('_execute()', function () { + describe('_execute()', () => { - it('returns 400 on invalid path', function (done) { + it('returns 400 on invalid path', (done) => { const server = new Hapi.Server(); server.connection(); @@ -188,7 +188,7 @@ describe('Request', function () { }); }); - it('returns error response on ext error', function (done) { + it('returns error response on ext error', (done) => { const handler = function (request, reply) { @@ -213,7 +213,7 @@ describe('Request', function () { }); }); - it('handles aborted requests', { parallel: false }, function (done) { + it('handles aborted requests', { parallel: false }, (done) => { const handler = function (request, reply) { @@ -246,7 +246,7 @@ describe('Request', function () { let disconnected = 0; server.ext('onRequest', function (request, reply) { - request.once('disconnect', function () { + request.once('disconnect', () => { ++disconnected; }); @@ -261,13 +261,13 @@ describe('Request', function () { let total = 2; const createConnection = function () { - const client = Net.connect(server.info.port, function () { + const client = Net.connect(server.info.port, () => { client.write('GET / HTTP/1.1\r\n\r\n'); client.write('GET / HTTP/1.1\r\n\r\n'); }); - client.on('data', function () { + client.on('data', () => { --total; client.destroy(); @@ -290,7 +290,7 @@ describe('Request', function () { }); }); - it('returns empty params array when none present', function (done) { + it('returns empty params array when none present', (done) => { const handler = function (request, reply) { @@ -308,7 +308,7 @@ describe('Request', function () { }); }); - it('returns empty params array when none present (not found)', function (done) { + it('returns empty params array when none present (not found)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -324,7 +324,7 @@ describe('Request', function () { }); }); - it('does not fail on abort', function (done) { + it('does not fail on abort', (done) => { let clientRequest; @@ -361,7 +361,7 @@ describe('Request', function () { }); }); - it('does not fail on abort (onPreHandler)', function (done) { + it('does not fail on abort (onPreHandler)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -397,7 +397,7 @@ describe('Request', function () { }); }); - it('does not fail on abort with ext', function (done) { + it('does not fail on abort with ext', (done) => { let clientRequest; @@ -419,7 +419,7 @@ describe('Request', function () { return reply.continue(); }); - server.on('tail', function () { + server.on('tail', () => { server.stop(done); }); @@ -439,7 +439,7 @@ describe('Request', function () { }); }); - it('returns not found on internal only route (external)', function (done) { + it('returns not found on internal only route (external)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -467,7 +467,7 @@ describe('Request', function () { }); }); - it('returns not found on internal only route (inject)', function (done) { + it('returns not found on internal only route (inject)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -490,7 +490,7 @@ describe('Request', function () { }); }); - it('allows internal only route (inject with allowInternals)', function (done) { + it('allows internal only route (inject with allowInternals)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -514,9 +514,9 @@ describe('Request', function () { }); }); - describe('_finalize()', function (done) { + describe('_finalize()', (done) => { - it('generate response event', function (done) { + it('generate response event', (done) => { const handler = function (request, reply) { @@ -536,7 +536,7 @@ describe('Request', function () { server.inject('/', function (res) { }); }); - it('closes response after server timeout', function (done) { + it('closes response after server timeout', (done) => { const handler = function (request, reply) { @@ -572,7 +572,7 @@ describe('Request', function () { }); }); - it('does not attempt to close error response after server timeout', function (done) { + it('does not attempt to close error response after server timeout', (done) => { const handler = function (request, reply) { @@ -597,7 +597,7 @@ describe('Request', function () { }); }); - it('emits request-error once', function (done) { + it('emits request-error once', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -631,7 +631,7 @@ describe('Request', function () { expect(res.result.message).to.equal('An internal server error occurred'); }); - server.once('response', function () { + server.once('response', () => { expect(errs).to.equal(1); expect(req.getLog('error')[1].tags).to.deep.equal(['internal', 'error']); @@ -639,7 +639,7 @@ describe('Request', function () { }); }); - it('emits request-error on implementation error', function (done) { + it('emits request-error on implementation error', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -661,7 +661,7 @@ describe('Request', function () { server.route({ method: 'GET', path: '/', handler: handler }); - server.once('response', function () { + server.once('response', () => { expect(errs).to.equal(1); expect(req.getLog('error')[0].tags).to.deep.equal(['internal', 'implementation', 'error']); @@ -676,7 +676,7 @@ describe('Request', function () { }); }); - it('does not emit request-error when error is replaced with valid response', function (done) { + it('does not emit request-error when error is replaced with valid response', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -705,7 +705,7 @@ describe('Request', function () { expect(res.result).to.equal('ok'); }); - server.once('response', function () { + server.once('response', () => { expect(errs).to.equal(0); done(); @@ -713,9 +713,9 @@ describe('Request', function () { }); }); - describe('tail()', function () { + describe('tail()', () => { - it('generates tail event', function (done) { + it('generates tail event', (done) => { const handler = function (request, reply) { @@ -735,7 +735,7 @@ describe('Request', function () { let result = null; - server.once('tail', function () { + server.once('tail', () => { expect(result).to.equal('Done'); done(); @@ -747,7 +747,7 @@ describe('Request', function () { }); }); - it('generates tail event without name', function (done) { + it('generates tail event without name', (done) => { const handler = function (request, reply) { @@ -760,7 +760,7 @@ describe('Request', function () { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.once('tail', function () { + server.once('tail', () => { done(); }); @@ -771,9 +771,9 @@ describe('Request', function () { }); }); - describe('setMethod()', function () { + describe('setMethod()', () => { - it('changes method with a lowercase version of the value passed in', function (done) { + it('changes method with a lowercase version of the value passed in', (done) => { const server = new Hapi.Server(); server.connection(); @@ -792,7 +792,7 @@ describe('Request', function () { }); }); - it('errors on missing method', function (done) { + it('errors on missing method', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -810,7 +810,7 @@ describe('Request', function () { }); }); - it('errors on invalid method type', function (done) { + it('errors on invalid method type', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -829,9 +829,9 @@ describe('Request', function () { }); }); - describe('setUrl()', function () { + describe('setUrl()', () => { - it('parses nested query string', function (done) { + it('parses nested query string', (done) => { const handler = function (request, reply) { @@ -849,7 +849,7 @@ describe('Request', function () { }); }); - it('sets url, path, and query', function (done) { + it('sets url, path, and query', (done) => { const url = 'http://localhost/page?param1=something'; const server = new Hapi.Server(); @@ -869,7 +869,7 @@ describe('Request', function () { }); }); - it('normalizes a path', function (done) { + it('normalizes a path', (done) => { const rawPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f%20%21%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f%40%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f%50%51%52%53%54%55%56%57%58%59%5a%5b%5c%5d%5e%5f%60%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f%70%71%72%73%74%75%76%77%78%79%7a%7b%7c%7d%7e%7f%80%81%82%83%84%85%86%87%88%89%8a%8b%8c%8d%8e%8f%90%91%92%93%94%95%96%97%98%99%9a%9b%9c%9d%9e%9f%a0%a1%a2%a3%a4%a5%a6%a7%a8%a9%aa%ab%ac%ad%ae%af%b0%b1%b2%b3%b4%b5%b6%b7%b8%b9%ba%bb%bc%bd%be%bf%c0%c1%c2%c3%c4%c5%c6%c7%c8%c9%ca%cb%cc%cd%ce%cf%d0%d1%d2%d3%d4%d5%d6%d7%d8%d9%da%db%dc%dd%de%df%e0%e1%e2%e3%e4%e5%e6%e7%e8%e9%ea%eb%ec%ed%ee%ef%f0%f1%f2%f3%f4%f5%f6%f7%f8%f9%fa%fb%fc%fd%fe%ff%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37%38%39%3A%3B%3C%3D%3E%3F%40%41%42%43%44%45%46%47%48%49%4A%4B%4C%4D%4E%4F%50%51%52%53%54%55%56%57%58%59%5A%5B%5C%5D%5E%5F%60%61%62%63%64%65%66%67%68%69%6A%6B%6C%6D%6E%6F%70%71%72%73%74%75%76%77%78%79%7A%7B%7C%7D%7E%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; const normPath = '/%0%1%2%3%4%5%6%7%8%9%a%b%c%d%e%f%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF%0%1%2%3%4%5%6%7%8%9%A%B%C%D%E%F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22%23$%25&\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF'; @@ -893,7 +893,7 @@ describe('Request', function () { }); }); - it('allows missing path', function (done) { + it('allows missing path', (done) => { const server = new Hapi.Server(); server.connection(); @@ -910,7 +910,7 @@ describe('Request', function () { }); }); - it('strips trailing slash', function (done) { + it('strips trailing slash', (done) => { const handler = function (request, reply) { @@ -927,7 +927,7 @@ describe('Request', function () { }); }); - it('does not strip trailing slash on /', function (done) { + it('does not strip trailing slash on /', (done) => { const handler = function (request, reply) { @@ -944,7 +944,7 @@ describe('Request', function () { }); }); - it('strips trailing slash with query', function (done) { + it('strips trailing slash with query', (done) => { const handler = function (request, reply) { @@ -961,7 +961,7 @@ describe('Request', function () { }); }); - it('accepts querystring parser options', function (done) { + it('accepts querystring parser options', (done) => { const url = 'http://localhost/page?a=1&b=1&c=1&d=1&e=1&f=1&g=1&h=1&i=1&j=1&k=1&l=1&m=1&n=1&o=1&p=1&q=1&r=1&s=1&t=1&u=1&v=1&w=1&x=1&y=1&z=1'; const qsParserOptions = { @@ -986,7 +986,7 @@ describe('Request', function () { }); }); - it('overrides qs settings', function (done) { + it('overrides qs settings', (done) => { const server = new Hapi.Server(); server.connection({ @@ -1016,9 +1016,9 @@ describe('Request', function () { }); }); - describe('log()', { parallel: false }, function () { + describe('log()', { parallel: false }, () => { - it('outputs log data to debug console', function (done) { + it('outputs log data to debug console', (done) => { const handler = function (request, reply) { @@ -1046,7 +1046,7 @@ describe('Request', function () { }); }); - it('emits a request event', function (done) { + it('emits a request event', (done) => { const handler = function (request, reply) { @@ -1073,7 +1073,7 @@ describe('Request', function () { }); }); - it('outputs log to debug console without data', function (done) { + it('outputs log to debug console without data', (done) => { const handler = function (request, reply) { @@ -1101,7 +1101,7 @@ describe('Request', function () { }); }); - it('outputs log to debug console with error data', function (done) { + it('outputs log to debug console with error data', (done) => { const handler = function (request, reply) { @@ -1129,7 +1129,7 @@ describe('Request', function () { }); }); - it('handles invalid log data object stringify', function (done) { + it('handles invalid log data object stringify', (done) => { const handler = function (request, reply) { @@ -1160,7 +1160,7 @@ describe('Request', function () { }); }); - it('adds a log event to the request', function (done) { + it('adds a log event to the request', (done) => { const handler = function (request, reply) { @@ -1186,7 +1186,7 @@ describe('Request', function () { }); }); - it('does not output events when debug disabled', function (done) { + it('does not output events when debug disabled', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1215,7 +1215,7 @@ describe('Request', function () { }); }); - it('does not output events when debug.request disabled', function (done) { + it('does not output events when debug.request disabled', (done) => { const server = new Hapi.Server({ debug: { request: false } }); server.connection(); @@ -1244,7 +1244,7 @@ describe('Request', function () { }); }); - it('does not output non-implementation events by default', function (done) { + it('does not output non-implementation events by default', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1274,9 +1274,9 @@ describe('Request', function () { }); }); - describe('_log()', { parallel: false }, function () { + describe('_log()', { parallel: false }, () => { - it('emits a request-internal event', function (done) { + it('emits a request-internal event', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1290,9 +1290,9 @@ describe('Request', function () { }); }); - describe('getLog()', function () { + describe('getLog()', () => { - it('returns the selected logs', function (done) { + it('returns the selected logs', (done) => { const handler = function (request, reply) { @@ -1314,9 +1314,9 @@ describe('Request', function () { }); }); - describe('_setResponse()', function () { + describe('_setResponse()', () => { - it('leaves the response open when the same response is set again', function (done) { + it('leaves the response open when the same response is set again', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1346,7 +1346,7 @@ describe('Request', function () { }); }); - it('leaves the response open when the same response source is set again', function (done) { + it('leaves the response open when the same response source is set again', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1377,9 +1377,9 @@ describe('Request', function () { }); }); - describe('timeout', { parallel: false }, function () { + describe('timeout', { parallel: false }, () => { - it('returns server error message when server taking too long', function (done) { + it('returns server error message when server taking too long', (done) => { const timeoutHandler = function (request, reply) { }; @@ -1397,7 +1397,7 @@ describe('Request', function () { }); }); - it('returns server error message when server timeout happens during request execution (and handler yields)', function (done) { + it('returns server error message when server timeout happens during request execution (and handler yields)', (done) => { const handler = function (request, reply) { @@ -1423,7 +1423,7 @@ describe('Request', function () { }); }); - it('returns server error message when server timeout is short and already occurs when request executes', function (done) { + it('returns server error message when server timeout is short and already occurs when request executes', (done) => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 2 } } }); @@ -1443,7 +1443,7 @@ describe('Request', function () { }); }); - it('handles server handler timeout with onPreResponse ext', function (done) { + it('handles server handler timeout with onPreResponse ext', (done) => { const handler = function (request, reply) { @@ -1465,7 +1465,7 @@ describe('Request', function () { }); }); - it('does not return an error response when server is slow but faster than timeout', function (done) { + it('does not return an error response when server is slow but faster than timeout', (done) => { const slowHandler = function (request, reply) { @@ -1488,7 +1488,7 @@ describe('Request', function () { }); }); - it('does not return an error when server is responding when the timeout occurs', function (done) { + it('does not return an error when server is responding when the timeout occurs', (done) => { let ended = false; const handler = function (request, reply) { @@ -1539,7 +1539,7 @@ describe('Request', function () { }); }); - it('does not return an error response when server is slower than timeout but response has started', function (done) { + it('does not return an error response when server is slower than timeout but response has started', (done) => { const streamHandler = function (request, reply) { @@ -1596,7 +1596,7 @@ describe('Request', function () { }); }); - it('does not return an error response when server takes less than timeout to respond', function (done) { + it('does not return an error response when server takes less than timeout to respond', (done) => { const fastHandler = function (request, reply) { @@ -1614,7 +1614,7 @@ describe('Request', function () { }); }); - it('handles race condition between equal client and server timeouts', function (done) { + it('handles race condition between equal client and server timeouts', (done) => { const timeoutHandler = function (request, reply) { }; diff --git a/test/response.js b/test/response.js index 236b51b1c..eceeb0c89 100755 --- a/test/response.js +++ b/test/response.js @@ -27,9 +27,9 @@ const it = lab.it; const expect = Code.expect; -describe('Response', function () { +describe('Response', () => { - it('returns a reply', function (done) { + it('returns a reply', (done) => { const handler = function (request, reply) { @@ -75,9 +75,9 @@ describe('Response', function () { }); }); - describe('_setSource()', function () { + describe('_setSource()', () => { - it('returns an empty string reply', function (done) { + it('returns an empty string reply', (done) => { const server = new Hapi.Server(); server.connection(); @@ -101,7 +101,7 @@ describe('Response', function () { }); }); - it('returns a null reply', function (done) { + it('returns a null reply', (done) => { const server = new Hapi.Server(); server.connection(); @@ -124,7 +124,7 @@ describe('Response', function () { }); }); - it('returns an undefined reply', function (done) { + it('returns an undefined reply', (done) => { const server = new Hapi.Server(); server.connection(); @@ -148,9 +148,9 @@ describe('Response', function () { }); }); - describe('header()', function () { + describe('header()', () => { - it('appends to set-cookie header', function (done) { + it('appends to set-cookie header', (done) => { const handler = function (request, reply) { @@ -168,7 +168,7 @@ describe('Response', function () { }); }); - it('sets null header', function (done) { + it('sets null header', (done) => { const handler = function (request, reply) { @@ -186,7 +186,7 @@ describe('Response', function () { }); }); - it('throws error on non-ascii value', function (done) { + it('throws error on non-ascii value', (done) => { let thrown = false; @@ -211,7 +211,7 @@ describe('Response', function () { }); }); - it('throws error on non-ascii value (header name)', function (done) { + it('throws error on non-ascii value (header name)', (done) => { let thrown = false; @@ -237,7 +237,7 @@ describe('Response', function () { }); }); - it('throws error on non-ascii value (buffer)', function (done) { + it('throws error on non-ascii value (buffer)', (done) => { let thrown = false; @@ -263,9 +263,9 @@ describe('Response', function () { }); }); - describe('created()', function () { + describe('created()', () => { - it('returns a stream reply (created)', function (done) { + it('returns a stream reply (created)', (done) => { const handler = function (request, reply) { @@ -286,7 +286,7 @@ describe('Response', function () { }); }); - it('returns error on created with GET', function (done) { + it('returns error on created with GET', (done) => { const handler = function (request, reply) { @@ -305,9 +305,9 @@ describe('Response', function () { }); }); - describe('state()', function () { + describe('state()', () => { - it('returns an error on bad cookie', function (done) { + it('returns an error on bad cookie', (done) => { const handler = function (request, reply) { @@ -329,9 +329,9 @@ describe('Response', function () { }); }); - describe('unstate()', function () { + describe('unstate()', () => { - it('allows options', function (done) { + it('allows options', (done) => { const handler = function (request, reply) { @@ -351,9 +351,9 @@ describe('Response', function () { }); }); - describe('vary()', function () { + describe('vary()', () => { - it('sets Vary header with single value', function (done) { + it('sets Vary header with single value', (done) => { const handler = function (request, reply) { @@ -373,7 +373,7 @@ describe('Response', function () { }); }); - it('sets Vary header with multiple values', function (done) { + it('sets Vary header with multiple values', (done) => { const handler = function (request, reply) { @@ -393,7 +393,7 @@ describe('Response', function () { }); }); - it('sets Vary header with *', function (done) { + it('sets Vary header with *', (done) => { const handler = function (request, reply) { @@ -413,7 +413,7 @@ describe('Response', function () { }); }); - it('leaves Vary header with * on additional values', function (done) { + it('leaves Vary header with * on additional values', (done) => { const handler = function (request, reply) { @@ -433,7 +433,7 @@ describe('Response', function () { }); }); - it('drops other Vary header values when set to *', function (done) { + it('drops other Vary header values when set to *', (done) => { const handler = function (request, reply) { @@ -453,7 +453,7 @@ describe('Response', function () { }); }); - it('sets Vary header with multiple similar and identical values', function (done) { + it('sets Vary header with multiple similar and identical values', (done) => { const handler = function (request, reply) { @@ -474,9 +474,9 @@ describe('Response', function () { }); }); - describe('etag()', function () { + describe('etag()', () => { - it('sets etag', function (done) { + it('sets etag', (done) => { const handler = function (request, reply) { @@ -494,7 +494,7 @@ describe('Response', function () { }); }); - it('sets weak etag', function (done) { + it('sets weak etag', (done) => { const handler = function (request, reply) { @@ -512,7 +512,7 @@ describe('Response', function () { }); }); - it('ignores varyEtag when etag header is removed', function (done) { + it('ignores varyEtag when etag header is removed', (done) => { const handler = function (request, reply) { @@ -531,7 +531,7 @@ describe('Response', function () { }); }); - it('leaves etag header when varyEtag is false', function (done) { + it('leaves etag header when varyEtag is false', (done) => { const handler = function (request, reply) { @@ -555,7 +555,7 @@ describe('Response', function () { }); }); - it('applies varyEtag when returning 304 due to if-modified-since match', function (done) { + it('applies varyEtag when returning 304 due to if-modified-since match', (done) => { const mdate = new Date().toUTCString(); @@ -576,9 +576,9 @@ describe('Response', function () { }); }); - describe('passThrough()', function () { + describe('passThrough()', () => { - it('passes stream headers and code through', function (done) { + it('passes stream headers and code through', (done) => { const TestStream = function () { @@ -618,7 +618,7 @@ describe('Response', function () { }); }); - it('excludes stream headers and code when passThrough is false', function (done) { + it('excludes stream headers and code when passThrough is false', (done) => { const TestStream = function () { @@ -658,7 +658,7 @@ describe('Response', function () { }); }); - it('ignores stream headers when empty', function (done) { + it('ignores stream headers when empty', (done) => { const TestStream = function () { @@ -698,7 +698,7 @@ describe('Response', function () { }); }); - it('retains local headers with stream headers pass-through', function (done) { + it('retains local headers with stream headers pass-through', (done) => { const TestStream = function () { @@ -738,9 +738,9 @@ describe('Response', function () { }); }); - describe('replacer()', function () { + describe('replacer()', () => { - it('errors when called on wrong type', function (done) { + it('errors when called on wrong type', (done) => { const handler = function (request, reply) { @@ -758,9 +758,9 @@ describe('Response', function () { }); }); - describe('spaces()', function () { + describe('spaces()', () => { - it('errors when called on wrong type', function (done) { + it('errors when called on wrong type', (done) => { const handler = function (request, reply) { @@ -778,9 +778,9 @@ describe('Response', function () { }); }); - describe('suffix()', function () { + describe('suffix()', () => { - it('errors when called on wrong type', function (done) { + it('errors when called on wrong type', (done) => { const handler = function (request, reply) { @@ -798,9 +798,9 @@ describe('Response', function () { }); }); - describe('type()', function () { + describe('type()', () => { - it('returns a file in the response with the correct headers using custom mime type', function (done) { + it('returns a file in the response with the correct headers using custom mime type', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -820,9 +820,9 @@ describe('Response', function () { }); }); - describe('redirect()', function () { + describe('redirect()', () => { - it('returns a redirection reply', function (done) { + it('returns a redirection reply', (done) => { const handler = function (request, reply) { @@ -842,7 +842,7 @@ describe('Response', function () { }); }); - it('returns a redirection reply using verbose call', function (done) { + it('returns a redirection reply using verbose call', (done) => { const handler = function (request, reply) { @@ -863,7 +863,7 @@ describe('Response', function () { }); }); - it('returns a 301 redirection reply', function (done) { + it('returns a 301 redirection reply', (done) => { const handler = function (request, reply) { @@ -881,7 +881,7 @@ describe('Response', function () { }); }); - it('returns a 302 redirection reply', function (done) { + it('returns a 302 redirection reply', (done) => { const handler = function (request, reply) { @@ -899,7 +899,7 @@ describe('Response', function () { }); }); - it('returns a 307 redirection reply', function (done) { + it('returns a 307 redirection reply', (done) => { const handler = function (request, reply) { @@ -917,7 +917,7 @@ describe('Response', function () { }); }); - it('returns a 308 redirection reply', function (done) { + it('returns a 308 redirection reply', (done) => { const handler = function (request, reply) { @@ -935,7 +935,7 @@ describe('Response', function () { }); }); - it('returns a 301 redirection reply (reveresed methods)', function (done) { + it('returns a 301 redirection reply (reveresed methods)', (done) => { const handler = function (request, reply) { @@ -953,7 +953,7 @@ describe('Response', function () { }); }); - it('returns a 302 redirection reply (reveresed methods)', function (done) { + it('returns a 302 redirection reply (reveresed methods)', (done) => { const handler = function (request, reply) { @@ -971,7 +971,7 @@ describe('Response', function () { }); }); - it('returns a 307 redirection reply (reveresed methods)', function (done) { + it('returns a 307 redirection reply (reveresed methods)', (done) => { const handler = function (request, reply) { @@ -989,7 +989,7 @@ describe('Response', function () { }); }); - it('returns a 308 redirection reply (reveresed methods)', function (done) { + it('returns a 308 redirection reply (reveresed methods)', (done) => { const handler = function (request, reply) { @@ -1007,7 +1007,7 @@ describe('Response', function () { }); }); - it('returns a 302 redirection reply (flip flop)', function (done) { + it('returns a 302 redirection reply (flip flop)', (done) => { const handler = function (request, reply) { @@ -1026,9 +1026,9 @@ describe('Response', function () { }); }); - describe('_prepare()', function () { + describe('_prepare()', () => { - it('handles promises that resolve', function (done) { + it('handles promises that resolve', (done) => { const handler = function (request, reply) { @@ -1047,7 +1047,7 @@ describe('Response', function () { }); }); - it('handles promises that resolve (object)', function (done) { + it('handles promises that resolve (object)', (done) => { const handler = function (request, reply) { @@ -1066,7 +1066,7 @@ describe('Response', function () { }); }); - it('handles promises that resolve (response object)', function (done) { + it('handles promises that resolve (response object)', (done) => { const handler = function (request, reply) { @@ -1085,7 +1085,7 @@ describe('Response', function () { }); }); - it('handles promises that reject', function (done) { + it('handles promises that reject', (done) => { const handler = function (request, reply) { @@ -1108,9 +1108,9 @@ describe('Response', function () { }); }); - describe('_marshal()', function () { + describe('_marshal()', () => { - it('emits request-error when view file for handler not found', function (done) { + it('emits request-error when view file for handler not found', (done) => { const server = new Hapi.Server({ debug: false }); server.register(Vision, Hoek.ignore); @@ -1139,9 +1139,9 @@ describe('Response', function () { }); }); - describe('_streamify()', function () { + describe('_streamify()', () => { - it('returns a formatted response', function (done) { + it('returns a formatted response', (done) => { const handler = function (request, reply) { @@ -1159,7 +1159,7 @@ describe('Response', function () { }); }); - it('returns a response with options', function (done) { + it('returns a response with options', (done) => { const handler = function (request, reply) { @@ -1178,7 +1178,7 @@ describe('Response', function () { }); }); - it('returns a response with options (different order)', function (done) { + it('returns a response with options (different order)', (done) => { const handler = function (request, reply) { @@ -1197,7 +1197,7 @@ describe('Response', function () { }); }); - it('captures object which cannot be stringify', function (done) { + it('captures object which cannot be stringify', (done) => { const handler = function (request, reply) { @@ -1218,9 +1218,9 @@ describe('Response', function () { }); }); - describe('_tap()', function () { + describe('_tap()', () => { - it('peeks into the response stream', function (done) { + it('peeks into the response stream', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1238,7 +1238,7 @@ describe('Response', function () { output += chunk.toString(); }); - response.once('finish', function () { + response.once('finish', () => { output += '!'; }); @@ -1253,9 +1253,9 @@ describe('Response', function () { }); }); - describe('_close()', function () { + describe('_close()', () => { - it('calls custom close processor', function (done) { + it('calls custom close processor', (done) => { let closed = false; const close = function (response) { diff --git a/test/route.js b/test/route.js index a5312c8db..98c12f389 100755 --- a/test/route.js +++ b/test/route.js @@ -23,9 +23,9 @@ const it = lab.it; const expect = Code.expect; -describe('Route', function () { +describe('Route', () => { - it('throws an error when a route is missing a path', function (done) { + it('throws an error when a route is missing a path', (done) => { expect(function () { @@ -36,7 +36,7 @@ describe('Route', function () { done(); }); - it('throws an error when a route is made without a connection', function (done) { + it('throws an error when a route is made without a connection', (done) => { expect(function () { @@ -46,7 +46,7 @@ describe('Route', function () { done(); }); - it('throws an error when a route is missing a method', function (done) { + it('throws an error when a route is missing a method', (done) => { expect(function () { @@ -57,7 +57,7 @@ describe('Route', function () { done(); }); - it('throws an error when a route has a malformed method name', function (done) { + it('throws an error when a route has a malformed method name', (done) => { expect(function () { @@ -68,7 +68,7 @@ describe('Route', function () { done(); }); - it('throws an error when a route uses the HEAD method', function (done) { + it('throws an error when a route uses the HEAD method', (done) => { expect(function () { @@ -79,7 +79,7 @@ describe('Route', function () { done(); }); - it('throws an error when a route is missing a handler', function (done) { + it('throws an error when a route is missing a handler', (done) => { expect(function () { @@ -90,7 +90,7 @@ describe('Route', function () { done(); }); - it('throws when handler is missing in config', function (done) { + it('throws when handler is missing in config', (done) => { const server = new Hapi.Server(); server.connection(); @@ -101,7 +101,7 @@ describe('Route', function () { done(); }); - it('throws when path has trailing slash and server set to strip', function (done) { + it('throws when path has trailing slash and server set to strip', (done) => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); @@ -112,7 +112,7 @@ describe('Route', function () { done(); }); - it('allows / when path has trailing slash and server set to strip', function (done) { + it('allows / when path has trailing slash and server set to strip', (done) => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); @@ -123,7 +123,7 @@ describe('Route', function () { done(); }); - it('sets route plugins and app settings', function (done) { + it('sets route plugins and app settings', (done) => { const handler = function (request, reply) { @@ -140,7 +140,7 @@ describe('Route', function () { }); }); - it('throws when validation is set without payload parsing', function (done) { + it('throws when validation is set without payload parsing', (done) => { const server = new Hapi.Server(); server.connection(); @@ -151,7 +151,7 @@ describe('Route', function () { done(); }); - it('throws when validation is set on GET', function (done) { + it('throws when validation is set on GET', (done) => { const server = new Hapi.Server(); server.connection(); @@ -162,7 +162,7 @@ describe('Route', function () { done(); }); - it('throws when payload parsing is set on GET', function (done) { + it('throws when payload parsing is set on GET', (done) => { const server = new Hapi.Server(); server.connection(); @@ -173,7 +173,7 @@ describe('Route', function () { done(); }); - it('ignores validation on * route when request is GET', function (done) { + it('ignores validation on * route when request is GET', (done) => { const handler = function (request, reply) { @@ -190,7 +190,7 @@ describe('Route', function () { }); }); - it('ignores default validation on GET', function (done) { + it('ignores default validation on GET', (done) => { const handler = function (request, reply) { @@ -207,7 +207,7 @@ describe('Route', function () { }); }); - it('shallow copies route config bind', function (done) { + it('shallow copies route config bind', (done) => { const server = new Hapi.Server(); server.connection(); @@ -237,7 +237,7 @@ describe('Route', function () { }); }); - it('shallow copies route config bind (server.bind())', function (done) { + it('shallow copies route config bind (server.bind())', (done) => { const server = new Hapi.Server(); server.connection(); @@ -268,7 +268,7 @@ describe('Route', function () { }); }); - it('shallow copies route config bind (connection defaults)', function (done) { + it('shallow copies route config bind (connection defaults)', (done) => { const server = new Hapi.Server(); const context = { key: 'is ' }; @@ -298,7 +298,7 @@ describe('Route', function () { }); }); - it('shallow copies route config bind (server defaults)', function (done) { + it('shallow copies route config bind (server defaults)', (done) => { const context = { key: 'is ' }; @@ -328,7 +328,7 @@ describe('Route', function () { }); }); - it('overrides server relativeTo', function (done) { + it('overrides server relativeTo', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -347,7 +347,7 @@ describe('Route', function () { }); }); - it('throws when server timeout is more then socket timeout', function (done) { + it('throws when server timeout is more then socket timeout', (done) => { const server = new Hapi.Server(); expect(function () { @@ -357,7 +357,7 @@ describe('Route', function () { done(); }); - it('throws when server timeout is more then socket timeout (node default)', function (done) { + it('throws when server timeout is more then socket timeout (node default)', (done) => { const server = new Hapi.Server(); expect(function () { @@ -367,7 +367,7 @@ describe('Route', function () { done(); }); - it('ignores large server timeout when socket timeout disabled', function (done) { + it('ignores large server timeout when socket timeout disabled', (done) => { const server = new Hapi.Server(); expect(function () { @@ -377,7 +377,7 @@ describe('Route', function () { done(); }); - it('overrides qs settings', function (done) { + it('overrides qs settings', (done) => { const server = new Hapi.Server(); server.connection(); @@ -404,9 +404,9 @@ describe('Route', function () { }); }); - describe('extensions', function () { + describe('extensions', () => { - it('combine connection extensions (route last)', function (done) { + it('combine connection extensions (route last)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -462,7 +462,7 @@ describe('Route', function () { }); }); - it('combine connection extensions (route first)', function (done) { + it('combine connection extensions (route first)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -519,7 +519,7 @@ describe('Route', function () { }); }); - it('combine connection extensions (route middle)', function (done) { + it('combine connection extensions (route middle)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -575,7 +575,7 @@ describe('Route', function () { }); }); - it('combine connection extensions (mixed sources)', function (done) { + it('combine connection extensions (mixed sources)', (done) => { const server = new Hapi.Server(); server.connection(); diff --git a/test/security.js b/test/security.js index d5a07f788..559e4d69c 100755 --- a/test/security.js +++ b/test/security.js @@ -21,9 +21,9 @@ const it = lab.it; const expect = Code.expect; -describe('security', function () { +describe('security', () => { - it('blocks response splitting through the request.create method', function (done) { + it('blocks response splitting through the request.create method', (done) => { const server = new Hapi.Server(); server.connection(); @@ -46,7 +46,7 @@ describe('security', function () { }); }); - it('prevents xss with invalid content types', function (done) { + it('prevents xss with invalid content types', (done) => { const handler = function (request, reply) { @@ -71,7 +71,7 @@ describe('security', function () { }); }); - it('prevents xss with invalid cookie values in the request', function (done) { + it('prevents xss with invalid cookie values in the request', (done) => { const handler = function (request, reply) { @@ -96,7 +96,7 @@ describe('security', function () { }); }); - it('prevents xss with invalid cookie name in the request', function (done) { + it('prevents xss with invalid cookie name in the request', (done) => { const handler = function (request, reply) { @@ -121,7 +121,7 @@ describe('security', function () { }); }); - it('prevents xss in path validation response message', function (done) { + it('prevents xss in path validation response message', (done) => { const server = new Hapi.Server(); server.connection(); @@ -149,7 +149,7 @@ describe('security', function () { }); }); - it('prevents xss in payload validation response message', function (done) { + it('prevents xss in payload validation response message', (done) => { const server = new Hapi.Server(); server.connection(); @@ -177,7 +177,7 @@ describe('security', function () { }); }); - it('prevents xss in query validation response message', function (done) { + it('prevents xss in query validation response message', (done) => { const server = new Hapi.Server(); server.connection(); diff --git a/test/server.js b/test/server.js index a0397961d..048fadcd2 100755 --- a/test/server.js +++ b/test/server.js @@ -21,9 +21,9 @@ const it = lab.it; const expect = Code.expect; -describe('Server', function () { +describe('Server', () => { - it('sets connections defaults', function (done) { + it('sets connections defaults', (done) => { const server = new Hapi.Server({ connections: { app: { message: 'test defaults' } } }); server.connection(); @@ -31,7 +31,7 @@ describe('Server', function () { done(); }); - it('overrides mime settings', function (done) { + it('overrides mime settings', (done) => { const options = { mime: { @@ -52,9 +52,9 @@ describe('Server', function () { done(); }); - describe('start()', function () { + describe('start()', () => { - it('starts and stops', function (done) { + it('starts and stops', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); @@ -65,12 +65,12 @@ describe('Server', function () { let started = 0; let stopped = 0; - server.on('start', function () { + server.on('start', () => { ++started; }); - server.on('stop', function () { + server.on('stop', () => { ++stopped; }); @@ -98,7 +98,7 @@ describe('Server', function () { }); }); - it('initializes, starts, and stops', function (done) { + it('initializes, starts, and stops', (done) => { const server = new Hapi.Server(); server.connection({ labels: ['s1', 'a', 'b'] }); @@ -109,12 +109,12 @@ describe('Server', function () { let started = 0; let stopped = 0; - server.on('start', function () { + server.on('start', () => { ++started; }); - server.on('stop', function () { + server.on('stop', () => { ++stopped; }); @@ -147,7 +147,7 @@ describe('Server', function () { }); }); - it('returns connection start error', function (done) { + it('returns connection start error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -172,7 +172,7 @@ describe('Server', function () { }); }); - it('returns onPostStart error', function (done) { + it('returns onPostStart error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -190,7 +190,7 @@ describe('Server', function () { }); }); - it('errors on bad cache start', function (done) { + it('errors on bad cache start', (done) => { const cache = { engine: { @@ -211,7 +211,7 @@ describe('Server', function () { }); }); - it('fails to start server without connections', function (done) { + it('fails to start server without connections', (done) => { const server = new Hapi.Server(); server.start(function (err) { @@ -222,7 +222,7 @@ describe('Server', function () { }); }); - it('fails to start server when registration incomplete', function (done) { + it('fails to start server when registration incomplete', (done) => { const plugin = function () { }; plugin.attributes = { name: 'plugin' }; @@ -238,7 +238,7 @@ describe('Server', function () { }); }); - it('fails to start when no callback is passed', function (done) { + it('fails to start when no callback is passed', (done) => { const server = new Hapi.Server(); @@ -249,7 +249,7 @@ describe('Server', function () { done(); }); - it('fails to initialize server when not stopped', function (done) { + it('fails to initialize server when not stopped', (done) => { const plugin = function () { }; plugin.attributes = { name: 'plugin' }; @@ -267,7 +267,7 @@ describe('Server', function () { }); }); - it('fails to start server when starting', function (done) { + it('fails to start server when starting', (done) => { const plugin = function () { }; plugin.attributes = { name: 'plugin' }; @@ -284,9 +284,9 @@ describe('Server', function () { }); }); - describe('stop()', function () { + describe('stop()', () => { - it('stops the cache', function (done) { + it('stops the cache', (done) => { const server = new Hapi.Server(); server.connection(); @@ -314,7 +314,7 @@ describe('Server', function () { }); }); - it('returns an extension error (onPreStop)', function (done) { + it('returns an extension error (onPreStop)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -334,7 +334,7 @@ describe('Server', function () { }); }); - it('returns an extension error (onPostStop)', function (done) { + it('returns an extension error (onPostStop)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -354,7 +354,7 @@ describe('Server', function () { }); }); - it('returns a connection stop error', function (done) { + it('returns a connection stop error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -374,7 +374,7 @@ describe('Server', function () { }); }); - it('errors when stopping a stopping server', function (done) { + it('errors when stopping a stopping server', (done) => { const server = new Hapi.Server(); server.connection(); @@ -389,9 +389,9 @@ describe('Server', function () { }); }); - describe('connection()', function () { + describe('connection()', () => { - it('returns a server with only the selected connection', function (done) { + it('returns a server with only the selected connection', (done) => { const server = new Hapi.Server(); const p1 = server.connection({ port: 1 }); @@ -405,7 +405,7 @@ describe('Server', function () { done(); }); - it('throws on invalid config', function (done) { + it('throws on invalid config', (done) => { const server = new Hapi.Server(); expect(function () { @@ -415,7 +415,7 @@ describe('Server', function () { done(); }); - it('combines configuration from server and connection (cors)', function (done) { + it('combines configuration from server and connection (cors)', (done) => { const server = new Hapi.Server({ connections: { routes: { cors: true } } }); server.connection({ routes: { cors: { origin: ['example.com'] } } }); @@ -423,7 +423,7 @@ describe('Server', function () { done(); }); - it('combines configuration from server and connection (security)', function (done) { + it('combines configuration from server and connection (security)', (done) => { const server = new Hapi.Server({ connections: { routes: { security: { hsts: 1, xss: false } } } }); server.connection({ routes: { security: { hsts: 2 } } }); @@ -433,7 +433,7 @@ describe('Server', function () { done(); }); - it('decorates and clears single connection shortcuts', function (done) { + it('decorates and clears single connection shortcuts', (done) => { const server = new Hapi.Server(); expect(server.info).to.not.exist(); @@ -446,9 +446,9 @@ describe('Server', function () { }); }); - describe('load', { parallel: false }, function () { + describe('load', { parallel: false }, () => { - it('measures loop delay', function (done) { + it('measures loop delay', (done) => { const server = new Hapi.Server({ load: { sampleInterval: 4 } }); server.connection(); diff --git a/test/state.js b/test/state.js index 94f1a3cc0..5eb78eb4f 100755 --- a/test/state.js +++ b/test/state.js @@ -20,9 +20,9 @@ const it = lab.it; const expect = Code.expect; -describe('state', function () { +describe('state', () => { - it('parses cookies', function (done) { + it('parses cookies', (done) => { const handler = function (request, reply) { @@ -40,7 +40,7 @@ describe('state', function () { }); }); - it('skips parsing cookies', function (done) { + it('skips parsing cookies', (done) => { const handler = function (request, reply) { @@ -58,7 +58,7 @@ describe('state', function () { }); }); - it('does not clear invalid cookie if cannot parse', function (done) { + it('does not clear invalid cookie if cannot parse', (done) => { const server = new Hapi.Server(); server.connection(); @@ -71,7 +71,7 @@ describe('state', function () { }); }); - it('ignores invalid cookies (state level config)', function (done) { + it('ignores invalid cookies (state level config)', (done) => { const handler = function (request, reply) { @@ -91,7 +91,7 @@ describe('state', function () { }); }); - it('ignores invalid cookies (header)', function (done) { + it('ignores invalid cookies (header)', (done) => { const handler = function (request, reply) { @@ -110,7 +110,7 @@ describe('state', function () { }); }); - it('logs invalid cookie (value)', function (done) { + it('logs invalid cookie (value)', (done) => { const handler = function (request, reply) { @@ -130,7 +130,7 @@ describe('state', function () { }); }); - it('clears invalid cookies (state level config)', function (done) { + it('clears invalid cookies (state level config)', (done) => { const handler = function (request, reply) { @@ -149,7 +149,7 @@ describe('state', function () { }); }); - it('sets cookie value automatically', function (done) { + it('sets cookie value automatically', (done) => { const handler = function (request, reply) { @@ -169,7 +169,7 @@ describe('state', function () { }); }); - it('appends handler set-cookie to server state', function (done) { + it('appends handler set-cookie to server state', (done) => { const handler = function (request, reply) { @@ -189,7 +189,7 @@ describe('state', function () { }); }); - it('sets cookie value automatically using function', function (done) { + it('sets cookie value automatically using function', (done) => { const present = function (request, next) { @@ -214,7 +214,7 @@ describe('state', function () { }); }); - it('fails to set cookie value automatically using function', function (done) { + it('fails to set cookie value automatically using function', (done) => { const present = function (request, next) { @@ -239,7 +239,7 @@ describe('state', function () { }); }); - it('sets cookie value with null ttl', function (done) { + it('sets cookie value with null ttl', (done) => { const handler = function (request, reply) { diff --git a/test/transmit.js b/test/transmit.js index 5795c2f15..03d2df0d3 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -31,11 +31,11 @@ const it = lab.it; const expect = Code.expect; -describe('transmission', function () { +describe('transmission', () => { - describe('marshal()', function () { + describe('marshal()', () => { - it('returns valid http date responses in last-modified header', function (done) { + it('returns valid http date responses in last-modified header', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -50,7 +50,7 @@ describe('transmission', function () { }); }); - it('returns 200 if if-modified-since is invalid', function (done) { + it('returns 200 if if-modified-since is invalid', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -64,7 +64,7 @@ describe('transmission', function () { }); }); - it('returns 200 if last-modified is invalid', function (done) { + it('returns 200 if last-modified is invalid', (done) => { const server = new Hapi.Server(); server.connection(); @@ -82,7 +82,7 @@ describe('transmission', function () { }); }); - it('closes file handlers when not reading file stream', { skip: process.platform === 'win32' }, function (done) { + it('closes file handlers when not reading file stream', { skip: process.platform === 'win32' }, (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -101,7 +101,7 @@ describe('transmission', function () { lsof += buffer.toString(); }); - cmd.stdout.on('end', function () { + cmd.stdout.on('end', () => { let count = 0; const lines = lsof.split('\n'); @@ -118,7 +118,7 @@ describe('transmission', function () { }); }); - it('closes file handlers when not using a manually open file stream', { skip: process.platform === 'win32' }, function (done) { + it('closes file handlers when not using a manually open file stream', { skip: process.platform === 'win32' }, (done) => { const server = new Hapi.Server(); server.connection(); @@ -142,7 +142,7 @@ describe('transmission', function () { lsof += buffer.toString(); }); - cmd.stdout.on('end', function () { + cmd.stdout.on('end', () => { let count = 0; const lines = lsof.split('\n'); @@ -159,7 +159,7 @@ describe('transmission', function () { }); }); - it('returns a 304 when the request has if-modified-since and the response has not been modified since (larger)', function (done) { + it('returns a 304 when the request has if-modified-since and the response has not been modified since (larger)', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -180,7 +180,7 @@ describe('transmission', function () { }); }); - it('returns a 304 when the request has if-modified-since and the response has not been modified since (equal)', function (done) { + it('returns a 304 when the request has if-modified-since and the response has not been modified since (equal)', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -200,7 +200,7 @@ describe('transmission', function () { }); }); - it('matches etag with content-encoding', function (done) { + it('matches etag with content-encoding', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -276,7 +276,7 @@ describe('transmission', function () { }); }); - it('returns 304 when manually set to 304', function (done) { + it('returns 304 when manually set to 304', (done) => { const server = new Hapi.Server(); server.connection(); @@ -295,7 +295,7 @@ describe('transmission', function () { }); }); - it('returns a stream reply with custom response headers', function (done) { + it('returns a stream reply with custom response headers', (done) => { const handler = function (request, reply) { @@ -333,7 +333,7 @@ describe('transmission', function () { }); }); - it('returns a stream reply with custom response status code', function (done) { + it('returns a stream reply with custom response status code', (done) => { const handler = function (request, reply) { @@ -370,7 +370,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP response', function (done) { + it('returns an JSONP response', (done) => { const handler = function (request, reply) { @@ -390,7 +390,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP response (no charset)', function (done) { + it('returns an JSONP response (no charset)', (done) => { const handler = function (request, reply) { @@ -410,7 +410,7 @@ describe('transmission', function () { }); }); - it('returns a X-Content-Type-Options: nosniff header on JSONP responses', function (done) { + it('returns a X-Content-Type-Options: nosniff header on JSONP responses', (done) => { const handler = function (request, reply) { @@ -429,7 +429,7 @@ describe('transmission', function () { }); }); - it('returns a normal response when JSONP enabled but not requested', function (done) { + it('returns a normal response when JSONP enabled but not requested', (done) => { const handler = function (request, reply) { @@ -447,7 +447,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP response with compression', function (done) { + it('returns an JSONP response with compression', (done) => { const handler = function (request, reply) { @@ -480,7 +480,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP response when response is a buffer', function (done) { + it('returns an JSONP response when response is a buffer', (done) => { const handler = function (request, reply) { @@ -499,7 +499,7 @@ describe('transmission', function () { }); }); - it('returns response on bad JSONP parameter', function (done) { + it('returns response on bad JSONP parameter', (done) => { const handler = function (request, reply) { @@ -518,7 +518,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP handler error', function (done) { + it('returns an JSONP handler error', (done) => { const handler = function (request, reply) { @@ -537,7 +537,7 @@ describe('transmission', function () { }); }); - it('returns an JSONP state error', function (done) { + it('returns an JSONP state error', (done) => { const handler = function (request, reply) { @@ -564,7 +564,7 @@ describe('transmission', function () { }); }); - it('sets caching headers', function (done) { + it('sets caching headers', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -580,9 +580,9 @@ describe('transmission', function () { }); }); - describe('transmit()', function () { + describe('transmit()', () => { - it('sends empty payload on 204', function (done) { + it('sends empty payload on 204', (done) => { const server = new Hapi.Server(); server.connection(); @@ -601,7 +601,7 @@ describe('transmission', function () { }); }); - it('sends 204 on empty payload', function (done) { + it('sends 204 on empty payload', (done) => { const server = new Hapi.Server(); server.connection({ routes: { response: { emptyStatusCode: 204 } } }); @@ -620,7 +620,7 @@ describe('transmission', function () { }); }); - it('does not send 204 for chunked transfer payloads', function (done) { + it('does not send 204 for chunked transfer payloads', (done) => { const server = new Hapi.Server(); server.connection({ routes: { response: { emptyStatusCode: 204 } } }); @@ -653,7 +653,7 @@ describe('transmission', function () { }); }); - it('skips compression on empty', function (done) { + it('skips compression on empty', (done) => { const server = new Hapi.Server(); server.connection(); @@ -673,7 +673,7 @@ describe('transmission', function () { }); }); - it('does not skip compression for chunked transfer payloads', function (done) { + it('does not skip compression for chunked transfer payloads', (done) => { const server = new Hapi.Server(); server.connection(); @@ -706,7 +706,7 @@ describe('transmission', function () { }); }); - it('sets vary header when accept-encoding is present but does not match', function (done) { + it('sets vary header when accept-encoding is present but does not match', (done) => { const server = new Hapi.Server(); server.connection(); @@ -725,7 +725,7 @@ describe('transmission', function () { }); }); - it('handles stream errors on the response after the response has been piped', function (done) { + it('handles stream errors on the response after the response has been piped', (done) => { const handler = function (request, reply) { @@ -768,7 +768,7 @@ describe('transmission', function () { }); }); - it('matches etag header list value', function (done) { + it('matches etag header list value', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -791,7 +791,7 @@ describe('transmission', function () { }); }); - it('changes etag when content encoding is used', function (done) { + it('changes etag when content encoding is used', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -819,7 +819,7 @@ describe('transmission', function () { }); }); - it('returns a gzipped file in the response when the request accepts gzip', function (done) { + it('returns a gzipped file in the response when the request accepts gzip', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -841,7 +841,7 @@ describe('transmission', function () { }); }); - it('returns a plain file when not compressible', function (done) { + it('returns a plain file when not compressible', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -863,7 +863,7 @@ describe('transmission', function () { }); }); - it('returns a plain file when compression disabled', function (done) { + it('returns a plain file when compression disabled', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -884,7 +884,7 @@ describe('transmission', function () { }); }); - it('returns a deflated file in the response when the request accepts deflate', function (done) { + it('returns a deflated file in the response when the request accepts deflate', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -906,7 +906,7 @@ describe('transmission', function () { }); }); - it('returns a gzipped stream reply without a content-length header when accept-encoding is gzip', function (done) { + it('returns a gzipped stream reply without a content-length header when accept-encoding is gzip', (done) => { const streamHandler = function (request, reply) { @@ -925,7 +925,7 @@ describe('transmission', function () { }); }); - it('returns a deflated stream reply without a content-length header when accept-encoding is deflate', function (done) { + it('returns a deflated stream reply without a content-length header when accept-encoding is deflate', (done) => { const streamHandler = function (request, reply) { @@ -944,7 +944,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a post request when accept-encoding: gzip is requested', function (done) { + it('returns a gzip response on a post request when accept-encoding: gzip is requested', (done) => { const data = '{"test":"true"}'; @@ -975,7 +975,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a get request when accept-encoding: gzip is requested', function (done) { + it('returns a gzip response on a get request when accept-encoding: gzip is requested', (done) => { const data = '{"test":"true"}'; @@ -1006,7 +1006,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a post request when accept-encoding: * is requested', function (done) { + it('returns a gzip response on a post request when accept-encoding: * is requested', (done) => { const data = '{"test":"true"}'; @@ -1034,7 +1034,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a get request when accept-encoding: * is requested', function (done) { + it('returns a gzip response on a get request when accept-encoding: * is requested', (done) => { const data = '{"test":"true"}'; @@ -1062,7 +1062,7 @@ describe('transmission', function () { }); }); - it('returns a deflate response on a post request when accept-encoding: deflate is requested', function (done) { + it('returns a deflate response on a post request when accept-encoding: deflate is requested', (done) => { const data = '{"test":"true"}'; const server = new Hapi.Server(); @@ -1092,7 +1092,7 @@ describe('transmission', function () { }); }); - it('returns a deflate response on a get request when accept-encoding: deflate is requested', function (done) { + it('returns a deflate response on a get request when accept-encoding: deflate is requested', (done) => { const data = '{"test":"true"}'; const server = new Hapi.Server(); @@ -1122,7 +1122,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a post request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', function (done) { + it('returns a gzip response on a post request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', (done) => { const data = '{"test":"true"}'; @@ -1153,7 +1153,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a get request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', function (done) { + it('returns a gzip response on a get request when accept-encoding: gzip;q=1, deflate;q=0.5 is requested', (done) => { const data = '{"test":"true"}'; @@ -1184,7 +1184,7 @@ describe('transmission', function () { }); }); - it('returns a deflate response on a post request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', function (done) { + it('returns a deflate response on a post request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', (done) => { const data = '{"test":"true"}'; @@ -1215,7 +1215,7 @@ describe('transmission', function () { }); }); - it('returns a deflate response on a get request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', function (done) { + it('returns a deflate response on a get request when accept-encoding: deflate;q=1, gzip;q=0.5 is requested', (done) => { const data = '{"test":"true"}'; @@ -1246,7 +1246,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', function (done) { + it('returns a gzip response on a post request when accept-encoding: deflate, gzip is requested', (done) => { const data = '{"test":"true"}'; @@ -1277,7 +1277,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response on a get request when accept-encoding: deflate, gzip is requested', function (done) { + it('returns a gzip response on a get request when accept-encoding: deflate, gzip is requested', (done) => { const data = '{"test":"true"}'; @@ -1308,7 +1308,7 @@ describe('transmission', function () { }); }); - it('returns an identity response on a post request when accept-encoding is missing', function (done) { + it('returns an identity response on a post request when accept-encoding is missing', (done) => { const data = '{"test":"true"}'; @@ -1336,7 +1336,7 @@ describe('transmission', function () { }); }); - it('returns an identity response on a get request when accept-encoding is missing', function (done) { + it('returns an identity response on a get request when accept-encoding is missing', (done) => { const data = '{"test":"true"}'; @@ -1366,7 +1366,7 @@ describe('transmission', function () { }); }); - it('returns a gzip response when forced by the handler', function (done) { + it('returns a gzip response when forced by the handler', (done) => { const data = '{"test":"true"}'; @@ -1398,7 +1398,7 @@ describe('transmission', function () { }); }); - it('does not open file stream on 304', function (done) { + it('does not open file stream on 304', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -1425,7 +1425,7 @@ describe('transmission', function () { }); }); - it('object listeners are maintained after transmission is complete', function (done) { + it('object listeners are maintained after transmission is complete', (done) => { const handler = function (request, reply) { @@ -1440,7 +1440,7 @@ describe('transmission', function () { server.ext('onPreResponse', function (request, reply) { response = request.response; - response.once('special', function () { + response.once('special', () => { done(); }); @@ -1454,7 +1454,7 @@ describe('transmission', function () { }); }); - it('stops processing the stream when the request closes', function (done) { + it('stops processing the stream when the request closes', (done) => { const ErrStream = function (request) { @@ -1500,7 +1500,7 @@ describe('transmission', function () { }); }); - it('does not truncate the response when stream finishes before response is done', function (done) { + it('does not truncate the response when stream finishes before response is done', (done) => { const chunkTimes = 10; const filePath = __dirname + '/response.js'; @@ -1545,7 +1545,7 @@ describe('transmission', function () { }); }); - it('does not truncate the response when stream finishes before response is done using https', function (done) { + it('does not truncate the response when stream finishes before response is done using https', (done) => { const chunkTimes = 10; const filePath = __dirname + '/response.js'; @@ -1597,7 +1597,7 @@ describe('transmission', function () { }); }); - it('does not leak stream data when request aborts before stream drains', function (done) { + it('does not leak stream data when request aborts before stream drains', (done) => { let destroyed = false; @@ -1624,7 +1624,7 @@ describe('transmission', function () { } }; - stream.once('end', function () { + stream.once('end', () => { server.stop(done); }); @@ -1653,7 +1653,7 @@ describe('transmission', function () { }); }); - it('does not leak classic stream data when passed to request and aborted', function (done) { + it('does not leak classic stream data when passed to request and aborted', (done) => { let destroyed = false; @@ -1698,7 +1698,7 @@ describe('transmission', function () { stream.resume(); - stream.once('end', function () { + stream.once('end', () => { server.stop(done); }); @@ -1727,7 +1727,7 @@ describe('transmission', function () { }); }); - it('does not leak stream data when request timeouts before stream drains', function (done) { + it('does not leak stream data when request timeouts before stream drains', (done) => { const handler = function (request, reply) { @@ -1746,7 +1746,7 @@ describe('transmission', function () { }, 10 * (count++)); // Must have back off here to hit the socket timeout }; - stream.once('end', function () { + stream.once('end', () => { server.stop(done); }); @@ -1770,7 +1770,7 @@ describe('transmission', function () { }); }); - it('does not leak stream data when request aborts before stream is returned', function (done) { + it('does not leak stream data when request aborts before stream is returned', (done) => { let clientRequest; @@ -1800,7 +1800,7 @@ describe('transmission', function () { } }; - stream.once('end', function () { + stream.once('end', () => { server.stop(done); }); @@ -1824,12 +1824,12 @@ describe('transmission', function () { port: server.info.port, method: 'GET' }); - clientRequest.on('error', function () { /* NOP */ }); + clientRequest.on('error', () => { /* NOP */ }); clientRequest.end(); }); }); - it('changes etag when content-encoding set manually', function (done) { + it('changes etag when content-encoding set manually', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1849,7 +1849,7 @@ describe('transmission', function () { }); }); - it('head request retains content-length header', function (done) { + it('head request retains content-length header', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1868,7 +1868,7 @@ describe('transmission', function () { }); }); - it('does not set accept-encoding multiple times', function (done) { + it('does not set accept-encoding multiple times', (done) => { const headersHandler = function (request, reply) { @@ -1907,7 +1907,7 @@ describe('transmission', function () { }); }); - describe('response range', function () { + describe('response range', () => { const fileStreamHandler = function (request, reply) { @@ -1915,7 +1915,7 @@ describe('transmission', function () { return reply(Fs.createReadStream(filePath)).bytes(Fs.statSync(filePath).size); }; - it('returns a subset of a fileStream (start)', function (done) { + it('returns a subset of a fileStream (start)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1932,7 +1932,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a fileStream (middle)', function (done) { + it('returns a subset of a fileStream (middle)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1949,7 +1949,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a fileStream (-to)', function (done) { + it('returns a subset of a fileStream (-to)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1966,7 +1966,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a fileStream (from-)', function (done) { + it('returns a subset of a fileStream (from-)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1983,7 +1983,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a fileStream (beyond end)', function (done) { + it('returns a subset of a fileStream (beyond end)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2000,7 +2000,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a fileStream (if-range)', function (done) { + it('returns a subset of a fileStream (if-range)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2023,7 +2023,7 @@ describe('transmission', function () { }); }); - it('returns 200 on incorrect if-range', function (done) { + it('returns 200 on incorrect if-range', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2036,7 +2036,7 @@ describe('transmission', function () { }); }); - it('returns 416 on invalid range (unit)', function (done) { + it('returns 416 on invalid range (unit)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2050,7 +2050,7 @@ describe('transmission', function () { }); }); - it('returns 416 on invalid range (inversed)', function (done) { + it('returns 416 on invalid range (inversed)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2064,7 +2064,7 @@ describe('transmission', function () { }); }); - it('returns 416 on invalid range (format)', function (done) { + it('returns 416 on invalid range (format)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2078,7 +2078,7 @@ describe('transmission', function () { }); }); - it('returns 416 on invalid range (empty range)', function (done) { + it('returns 416 on invalid range (empty range)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2092,7 +2092,7 @@ describe('transmission', function () { }); }); - it('returns 200 on multiple ranges', function (done) { + it('returns 200 on multiple ranges', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2106,7 +2106,7 @@ describe('transmission', function () { }); }); - it('returns a subset of a stream', function (done) { + it('returns a subset of a stream', (done) => { const TestStream = function () { @@ -2157,7 +2157,7 @@ describe('transmission', function () { }); }); - it('returns a consolidated range', function (done) { + it('returns a consolidated range', (done) => { const TestStream = function () { @@ -2209,7 +2209,7 @@ describe('transmission', function () { }); }); - it('skips undefined header values', function (done) { + it('skips undefined header values', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2229,9 +2229,9 @@ describe('transmission', function () { }); }); - describe('cache()', function () { + describe('cache()', () => { - it('sets max-age value (method and route)', function (done) { + it('sets max-age value (method and route)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2262,7 +2262,7 @@ describe('transmission', function () { }); }); - it('sets max-age value (expiresAt)', function (done) { + it('sets max-age value (expiresAt)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2285,7 +2285,7 @@ describe('transmission', function () { }); }); - it('returns no-cache on error', function (done) { + it('returns no-cache on error', (done) => { const handler = function (request, reply) { @@ -2302,7 +2302,7 @@ describe('transmission', function () { }); }); - it('sets cache-control on error with status override', function (done) { + it('sets cache-control on error with status override', (done) => { const handler = function (request, reply) { @@ -2319,7 +2319,7 @@ describe('transmission', function () { }); }); - it('does not return max-age value when route is not cached', function (done) { + it('does not return max-age value when route is not cached', (done) => { const server = new Hapi.Server(); server.connection(); @@ -2339,7 +2339,7 @@ describe('transmission', function () { }); }); - it('caches using non default cache', function (done) { + it('caches using non default cache', (done) => { const server = new Hapi.Server({ cache: { name: 'primary', engine: CatboxMemory } }); server.connection(); @@ -2375,7 +2375,7 @@ describe('transmission', function () { }); }); - it('leaves existing cache-control header', function (done) { + it('leaves existing cache-control header', (done) => { const handler = function (request, reply) { @@ -2395,7 +2395,7 @@ describe('transmission', function () { }); }); - it('sets cache-control header from ttl without policy', function (done) { + it('sets cache-control header from ttl without policy', (done) => { const handler = function (request, reply) { @@ -2413,7 +2413,7 @@ describe('transmission', function () { }); }); - it('leaves existing cache-control header (ttl)', function (done) { + it('leaves existing cache-control header (ttl)', (done) => { const handler = function (request, reply) { @@ -2432,7 +2432,7 @@ describe('transmission', function () { }); }); - it('includes caching header with 304', function (done) { + it('includes caching header with 304', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -2450,7 +2450,7 @@ describe('transmission', function () { }); }); - it('forbids caching on 304 if 200 is not included', function (done) { + it('forbids caching on 304 if 200 is not included', (done) => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); @@ -2469,9 +2469,9 @@ describe('transmission', function () { }); }); - describe('security()', function () { + describe('security()', () => { - it('does not set security headers by default', function (done) { + it('does not set security headers by default', (done) => { const handler = function (request, reply) { @@ -2495,7 +2495,7 @@ describe('transmission', function () { }); }); - it('returns default security headers when security is true', function (done) { + it('returns default security headers when security is true', (done) => { const handler = function (request, reply) { @@ -2519,7 +2519,7 @@ describe('transmission', function () { }); }); - it('does not set default security headers when the route sets security false', function (done) { + it('does not set default security headers when the route sets security false', (done) => { const handler = function (request, reply) { @@ -2548,7 +2548,7 @@ describe('transmission', function () { }); - it('does not return hsts header when secuirty.hsts is false', function (done) { + it('does not return hsts header when secuirty.hsts is false', (done) => { const handler = function (request, reply) { @@ -2573,7 +2573,7 @@ describe('transmission', function () { }); - it('returns only default hsts header when security.hsts is true', function (done) { + it('returns only default hsts header when security.hsts is true', (done) => { const handler = function (request, reply) { @@ -2593,7 +2593,7 @@ describe('transmission', function () { }); }); - it('returns correct hsts header when security.hsts is a number', function (done) { + it('returns correct hsts header when security.hsts is a number', (done) => { const handler = function (request, reply) { @@ -2613,7 +2613,7 @@ describe('transmission', function () { }); }); - it('returns correct hsts header when security.hsts is an object', function (done) { + it('returns correct hsts header when security.hsts is an object', (done) => { const handler = function (request, reply) { @@ -2633,7 +2633,7 @@ describe('transmission', function () { }); }); - it('returns the correct hsts header when security.hsts is an object only sepcifying maxAge', function (done) { + it('returns the correct hsts header when security.hsts is an object only sepcifying maxAge', (done) => { const handler = function (request, reply) { @@ -2653,7 +2653,7 @@ describe('transmission', function () { }); }); - it('returns correct hsts header when security.hsts is an object only specifying includeSubdomains', function (done) { + it('returns correct hsts header when security.hsts is an object only specifying includeSubdomains', (done) => { const handler = function (request, reply) { @@ -2673,7 +2673,7 @@ describe('transmission', function () { }); }); - it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains', function (done) { + it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains', (done) => { const handler = function (request, reply) { @@ -2693,7 +2693,7 @@ describe('transmission', function () { }); }); - it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains and preload', function (done) { + it('returns correct hsts header when security.hsts is an object only specifying includeSubDomains and preload', (done) => { const handler = function (request, reply) { @@ -2713,7 +2713,7 @@ describe('transmission', function () { }); }); - it('does not return the xframe header whe security.xframe is false', function (done) { + it('does not return the xframe header whe security.xframe is false', (done) => { const handler = function (request, reply) { @@ -2737,7 +2737,7 @@ describe('transmission', function () { }); }); - it('returns only default xframe header when security.xframe is true', function (done) { + it('returns only default xframe header when security.xframe is true', (done) => { const handler = function (request, reply) { @@ -2757,7 +2757,7 @@ describe('transmission', function () { }); }); - it('returns correct xframe header when security.xframe is a string', function (done) { + it('returns correct xframe header when security.xframe is a string', (done) => { const handler = function (request, reply) { @@ -2777,7 +2777,7 @@ describe('transmission', function () { }); }); - it('returns correct xframe header when security.xframe is an object', function (done) { + it('returns correct xframe header when security.xframe is an object', (done) => { const handler = function (request, reply) { @@ -2797,7 +2797,7 @@ describe('transmission', function () { }); }); - it('returns correct xframe header when security.xframe is an object', function (done) { + it('returns correct xframe header when security.xframe is an object', (done) => { const handler = function (request, reply) { @@ -2817,7 +2817,7 @@ describe('transmission', function () { }); }); - it('returns sameorigin xframe header when rule is allow-from but source is unspecified', function (done) { + it('returns sameorigin xframe header when rule is allow-from but source is unspecified', (done) => { const handler = function (request, reply) { @@ -2837,7 +2837,7 @@ describe('transmission', function () { }); }); - it('does not set x-download-options if noOpen is false', function (done) { + it('does not set x-download-options if noOpen is false', (done) => { const handler = function (request, reply) { @@ -2857,7 +2857,7 @@ describe('transmission', function () { }); }); - it('does not set x-content-type-options if noSniff is false', function (done) { + it('does not set x-content-type-options if noSniff is false', (done) => { const handler = function (request, reply) { @@ -2877,7 +2877,7 @@ describe('transmission', function () { }); }); - it('does not set the x-xss-protection header when security.xss is false', function (done) { + it('does not set the x-xss-protection header when security.xss is false', (done) => { const handler = function (request, reply) { @@ -2902,9 +2902,9 @@ describe('transmission', function () { }); }); - describe('content()', function () { + describe('content()', () => { - it('does not modify content-type header when charset manually set', function (done) { + it('does not modify content-type header when charset manually set', (done) => { const handler = function (request, reply) { @@ -2923,7 +2923,7 @@ describe('transmission', function () { }); }); - it('does not modify content-type header when charset is unset', function (done) { + it('does not modify content-type header when charset is unset', (done) => { const handler = function (request, reply) { @@ -2942,7 +2942,7 @@ describe('transmission', function () { }); }); - it('does not modify content-type header when charset is unset (default type)', function (done) { + it('does not modify content-type header when charset is unset (default type)', (done) => { const handler = function (request, reply) { diff --git a/test/validation.js b/test/validation.js index f8b28efcb..a08e7fccf 100755 --- a/test/validation.js +++ b/test/validation.js @@ -24,9 +24,9 @@ const it = lab.it; const expect = Code.expect; -describe('validation', function () { +describe('validation', () => { - it('validates valid input', function (done) { + it('validates valid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -53,7 +53,7 @@ describe('validation', function () { }); }); - it('validates both params and query', function (done) { + it('validates both params and query', (done) => { const server = new Hapi.Server(); server.connection(); @@ -84,7 +84,7 @@ describe('validation', function () { }); }); - it('validates valid input using context', function (done) { + it('validates valid input using context', (done) => { const server = new Hapi.Server(); server.connection(); @@ -126,7 +126,7 @@ describe('validation', function () { }); }); - it('validates valid input using auth context', function (done) { + it('validates valid input using auth context', (done) => { const server = new Hapi.Server(); server.connection(); @@ -186,7 +186,7 @@ describe('validation', function () { }); }); - it('fails valid input', function (done) { + it('fails valid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -213,7 +213,7 @@ describe('validation', function () { }); }); - it('validates valid input with validation options', function (done) { + it('validates valid input with validation options', (done) => { const server = new Hapi.Server(); server.connection({ routes: { validate: { options: { convert: false } } } }); @@ -240,7 +240,7 @@ describe('validation', function () { }); }); - it('allows any input when set to null', function (done) { + it('allows any input when set to null', (done) => { const server = new Hapi.Server(); server.connection(); @@ -265,7 +265,7 @@ describe('validation', function () { }); }); - it('validates using custom validation', function (done) { + it('validates using custom validation', (done) => { const server = new Hapi.Server(); server.connection(); @@ -299,7 +299,7 @@ describe('validation', function () { }); }); - it('catches error thrown in custom validation', function (done) { + it('catches error thrown in custom validation', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -327,7 +327,7 @@ describe('validation', function () { }); }); - it('casts input to desired type', function (done) { + it('casts input to desired type', (done) => { const server = new Hapi.Server(); server.connection(); @@ -355,7 +355,7 @@ describe('validation', function () { }); }); - it('uses original value before schema conversion', function (done) { + it('uses original value before schema conversion', (done) => { const server = new Hapi.Server(); server.connection(); @@ -383,7 +383,7 @@ describe('validation', function () { }); }); - it('invalidates forbidden input', function (done) { + it('invalidates forbidden input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -408,7 +408,7 @@ describe('validation', function () { }); }); - it('retains the validation error', function (done) { + it('retains the validation error', (done) => { const server = new Hapi.Server(); server.connection(); @@ -439,7 +439,7 @@ describe('validation', function () { }); }); - it('validates valid input (Object root)', function (done) { + it('validates valid input (Object root)', (done) => { const server = new Hapi.Server(); server.connection(); @@ -466,7 +466,7 @@ describe('validation', function () { }); }); - it('fails on invalid input', function (done) { + it('fails on invalid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -498,7 +498,7 @@ describe('validation', function () { }); }); - it('ignores invalid input', function (done) { + it('ignores invalid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -526,7 +526,7 @@ describe('validation', function () { }); }); - it('logs invalid input', function (done) { + it('logs invalid input', (done) => { const handler = function (request, reply) { @@ -558,7 +558,7 @@ describe('validation', function () { }); }); - it('replaces error with message on invalid input', function (done) { + it('replaces error with message on invalid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -590,7 +590,7 @@ describe('validation', function () { }); }); - it('catches error thrown in failAction', function (done) { + it('catches error thrown in failAction', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -621,7 +621,7 @@ describe('validation', function () { }); }); - it('customizes error on invalid input', function (done) { + it('customizes error on invalid input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -662,7 +662,7 @@ describe('validation', function () { }); }); - it('fails on invalid payload', function (done) { + it('fails on invalid payload', (done) => { const server = new Hapi.Server(); server.connection(); @@ -694,7 +694,7 @@ describe('validation', function () { }); }); - it('fails on text input', function (done) { + it('fails on text input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -721,7 +721,7 @@ describe('validation', function () { }); }); - it('fails on null input', function (done) { + it('fails on null input', (done) => { const server = new Hapi.Server(); server.connection(); @@ -749,7 +749,7 @@ describe('validation', function () { }); }); - it('fails on no payload', function (done) { + it('fails on no payload', (done) => { const server = new Hapi.Server(); server.connection(); @@ -781,7 +781,7 @@ describe('validation', function () { }); }); - it('samples responses', function (done) { + it('samples responses', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -820,7 +820,7 @@ describe('validation', function () { }); }); - it('validates response', function (done) { + it('validates response', (done) => { let i = 0; const handler = function (request, reply) { @@ -856,7 +856,7 @@ describe('validation', function () { }); }); - it('validates response with context', function (done) { + it('validates response with context', (done) => { const handler = function (request, reply) { @@ -892,7 +892,7 @@ describe('validation', function () { }); }); - it('validates error response', function (done) { + it('validates error response', (done) => { let i = 0; const handler = function (request, reply) { @@ -933,7 +933,7 @@ describe('validation', function () { }); }); - it('validates error response and ignore 200', function (done) { + it('validates error response and ignore 200', (done) => { let i = 0; const handler = function (request, reply) { @@ -984,7 +984,7 @@ describe('validation', function () { }); }); - it('validates and modifies response', function (done) { + it('validates and modifies response', (done) => { const handler = function (request, reply) { @@ -1015,7 +1015,7 @@ describe('validation', function () { }); }); - it('validates and modifies error response', function (done) { + it('validates and modifies error response', (done) => { const handler = function (request, reply) { @@ -1053,7 +1053,7 @@ describe('validation', function () { }); }); - it('validates empty response', function (done) { + it('validates empty response', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1080,7 +1080,7 @@ describe('validation', function () { }); }); - it('throws on sample with response modify', function (done) { + it('throws on sample with response modify', (done) => { const handler = function (request, reply) { @@ -1109,7 +1109,7 @@ describe('validation', function () { done(); }); - it('validates response using custom validation function', function (done) { + it('validates response using custom validation function', (done) => { let i = 0; const handler = function (request, reply) { @@ -1146,7 +1146,7 @@ describe('validation', function () { }); }); - it('catches error thrown by custom validation function', function (done) { + it('catches error thrown by custom validation function', (done) => { let i = 0; const handler = function (request, reply) { @@ -1177,7 +1177,7 @@ describe('validation', function () { }); }); - it('skips response validation when sample is zero', function (done) { + it('skips response validation when sample is zero', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1216,7 +1216,7 @@ describe('validation', function () { }); }); - it('does not delete the response object from the route when sample is 0', function (done) { + it('does not delete the response object from the route when sample is 0', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1246,7 +1246,7 @@ describe('validation', function () { }); }); - it('fails response validation with options', function (done) { + it('fails response validation with options', (done) => { const server = new Hapi.Server({ debug: false }); server.connection({ routes: { response: { options: { convert: false } } } }); @@ -1273,7 +1273,7 @@ describe('validation', function () { }); }); - it('skips response validation when schema is true', function (done) { + it('skips response validation when schema is true', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1298,7 +1298,7 @@ describe('validation', function () { }); }); - it('skips response validation when status is empty', function (done) { + it('skips response validation when status is empty', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1323,7 +1323,7 @@ describe('validation', function () { }); }); - it('forbids response when schema is false', function (done) { + it('forbids response when schema is false', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1348,7 +1348,7 @@ describe('validation', function () { }); }); - it('ignores error responses', function (done) { + it('ignores error responses', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1375,7 +1375,7 @@ describe('validation', function () { }); }); - it('errors on non-plain-object responses', function (done) { + it('errors on non-plain-object responses', (done) => { const server = new Hapi.Server({ debug: false }); server.register(Inert, Hoek.ignore); @@ -1403,7 +1403,7 @@ describe('validation', function () { }); }); - it('logs invalid responses', function (done) { + it('logs invalid responses', (done) => { const server = new Hapi.Server({ debug: false }); server.connection(); @@ -1438,7 +1438,7 @@ describe('validation', function () { }); }); - it('validates string response', function (done) { + it('validates string response', (done) => { let value = 'abcd'; const handler = function (request, reply) { @@ -1473,7 +1473,7 @@ describe('validation', function () { }); }); - it('validates boolean response', function (done) { + it('validates boolean response', (done) => { let value = 'abcd'; const handler = function (request, reply) { @@ -1509,7 +1509,7 @@ describe('validation', function () { }); }); - it('validates valid header', function (done) { + it('validates valid header', (done) => { const server = new Hapi.Server(); server.connection(); @@ -1546,7 +1546,7 @@ describe('validation', function () { }); }); - it('rejects invalid header', function (done) { + it('rejects invalid header', (done) => { const server = new Hapi.Server(); server.connection(); From ca3ee7e2e927dfba76267828ad85a6a5ce103fa8 Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Sun, 25 Oct 2015 02:37:27 -0700 Subject: [PATCH 0103/1139] Additional => conversions. For #2877 --- test/auth.js | 150 ++++----- test/connection.js | 285 +++++++++-------- test/cors.js | 66 ++-- test/handler.js | 176 ++++++----- test/methods.js | 201 ++++++------ test/payload.js | 96 +++--- test/plugin.js | 738 +++++++++++++++++++++++++-------------------- test/protect.js | 38 +-- test/reply.js | 58 ++-- test/request.js | 283 +++++++++-------- test/response.js | 108 +++---- test/route.js | 58 ++-- test/security.js | 14 +- test/server.js | 66 ++-- test/state.js | 24 +- test/transmit.js | 334 ++++++++++---------- test/validation.js | 128 ++++---- 17 files changed, 1498 insertions(+), 1325 deletions(-) diff --git a/test/auth.js b/test/auth.js index 40054802a..cc6303fcf 100755 --- a/test/auth.js +++ b/test/auth.js @@ -40,11 +40,11 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(401); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); done(); @@ -65,7 +65,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['cache-control']).to.equal('max-age=1, must-revalidate, private'); @@ -88,7 +88,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(500); done(); @@ -99,7 +99,7 @@ describe('authentication', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.auth.strategy('none'); }).to.throw('Authentication strategy none missing scheme'); @@ -113,11 +113,11 @@ describe('authentication', () => { server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: {} }, route: true }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(401); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); done(); @@ -170,11 +170,11 @@ describe('authentication', () => { } }); - server.inject('/view', function (res1) { + server.inject('/view', (res1) => { expect(res1.result).to.equal('

steve

'); - server.inject('/', function (res2) { + server.inject('/', (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result).to.equal('

xyz

'); @@ -203,11 +203,11 @@ describe('authentication', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(401); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); done(); @@ -229,11 +229,11 @@ describe('authentication', () => { server.auth.default({ strategy: 'default' }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(401); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); done(); @@ -247,7 +247,7 @@ describe('authentication', () => { server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); - expect(function () { + expect(() => { server.auth.default('default'); server.auth.default('default'); @@ -261,7 +261,7 @@ describe('authentication', () => { server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', { users: { steve: {} } }); - expect(function () { + expect(() => { server.auth.default({ mode: 'required' }); }).to.throw('Default authentication strategy missing strategy name'); @@ -279,7 +279,7 @@ describe('authentication', () => { server.auth.strategy('a', 'custom', { users: { steve: {} } }); server.auth.strategy('b', 'custom', { users: { steve: {} } }); - expect(function () { + expect(() => { server.route({ path: '/', @@ -315,7 +315,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.deep.equal({ @@ -343,12 +343,12 @@ describe('authentication', () => { }; server.route({ method: 'GET', path: '/', config: { handler: handler, auth: { mode: 'optional' } } }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.payload).to.equal('false'); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.payload).to.equal('true'); @@ -372,7 +372,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('required'); @@ -401,7 +401,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('second'); @@ -419,7 +419,7 @@ describe('authentication', () => { const doubleHandler = function (request, reply) { const options = { url: '/2', credentials: request.auth.credentials }; - server.inject(options, function (res) { + server.inject(options, (res) => { return reply(res.result); }); @@ -433,7 +433,7 @@ describe('authentication', () => { server.route({ method: 'GET', path: '/1', handler: doubleHandler }); server.route({ method: 'GET', path: '/2', handler: handler }); - server.inject({ url: '/1', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/1', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('steve'); @@ -451,7 +451,7 @@ describe('authentication', () => { const doubleHandler = function (request, reply) { const options = { url: '/2', credentials: request.auth.credentials, artifacts: '!' }; - server.inject(options, function (res) { + server.inject(options, (res) => { return reply(res.result); }); @@ -465,7 +465,7 @@ describe('authentication', () => { server.route({ method: 'GET', path: '/1', handler: doubleHandler }); server.route({ method: 'GET', path: '/2', handler: handler }); - server.inject({ url: '/1', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/1', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('steve!'); @@ -495,7 +495,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -522,7 +522,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -542,19 +542,19 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', 'try', { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.result.status).to.equal(false); expect(res1.result.error.message).to.equal('Missing authentication'); - server.inject({ url: '/', headers: { authorization: 'Custom john' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom john' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result.status).to.equal(false); expect(res2.result.error.message).to.equal('Missing credentials'); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res3) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res3) => { expect(res3.statusCode).to.equal(200); expect(res3.result.status).to.equal(true); @@ -578,7 +578,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom' } }, (res) => { expect(res.statusCode).to.equal(500); done(); @@ -598,14 +598,14 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: {} } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.on('request-internal', function (request, event, tags) { + server.on('request-internal', (request, event, tags) => { if (tags.auth) { done(); } }); - server.inject({ url: '/', headers: { authorization: 'Custom john' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom john' } }, (res) => { expect(res.statusCode).to.equal(401); }); @@ -624,7 +624,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { message: 'in a bottle' } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom message' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom message' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('in a bottle'); @@ -639,7 +639,7 @@ describe('authentication', () => { server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { steve: 'throw' } }); - server.once('request-error', function (request, err) { + server.once('request-error', (request, err) => { expect(err.message).to.equal('Uncaught error: Boom'); }); @@ -651,7 +651,7 @@ describe('authentication', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(500); done(); @@ -671,7 +671,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', 'try', { users: { message: 'in a bottle' } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom message' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom message' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('in a bottle'); @@ -701,7 +701,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -730,7 +730,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -759,7 +759,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -788,7 +788,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -815,7 +815,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/test', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/test', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -842,7 +842,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/test/admin', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/test/admin', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -869,7 +869,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/test', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/test', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -898,7 +898,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -927,7 +927,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -956,7 +956,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -985,7 +985,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -1017,7 +1017,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1044,7 +1044,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1071,7 +1071,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom client' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom client' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -1098,7 +1098,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom client' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom client' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1125,7 +1125,7 @@ describe('authentication', () => { } }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(403); done(); @@ -1136,7 +1136,7 @@ describe('authentication', () => { const server = new Hapi.Server(); server.connection(); - server.auth.scheme('test', function (srv, options) { + server.auth.scheme('test', (srv, options) => { return { authenticate: function (request, reply) { @@ -1158,14 +1158,14 @@ describe('authentication', () => { }); let result; - server.on('request-internal', function (request, event, tags) { + server.on('request-internal', (request, event, tags) => { if (tags.unauthenticated) { result = event.data; } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(result).to.equal(302); done(); @@ -1197,7 +1197,7 @@ describe('authentication', () => { artifacts: { bar: 'baz' } }; - server.inject(options, function (res) { + server.inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(res.result.bar).to.equal('baz'); @@ -1232,7 +1232,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1256,7 +1256,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1281,7 +1281,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1308,7 +1308,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom validPayload' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1321,7 +1321,7 @@ describe('authentication', () => { server.connection(); server.auth.scheme('custom', internals.implementation); server.auth.strategy('default', 'custom', true, { users: { validPayload: { payload: null } }, options: { payload: true } }); - expect(function () { + expect(() => { server.route({ method: 'POST', @@ -1351,7 +1351,7 @@ describe('authentication', () => { server.auth.scheme('custom', implementation); server.auth.strategy('default', 'custom', true, {}); - expect(function () { + expect(() => { server.route({ method: 'POST', @@ -1381,7 +1381,7 @@ describe('authentication', () => { server.auth.scheme('custom', implementation); server.auth.strategy('default', 'custom', true, {}); - expect(function () { + expect(() => { server.route({ method: 'POST', @@ -1413,7 +1413,7 @@ describe('authentication', () => { server.auth.scheme('custom2', internals.implementation); server.auth.strategy('default1', 'custom1', {}); server.auth.strategy('default2', 'custom2', {}); - expect(function () { + expect(() => { server.route({ method: 'POST', @@ -1450,7 +1450,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom skip' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom skip' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1478,7 +1478,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/' }, function (res) { + server.inject({ method: 'POST', url: '/' }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1505,7 +1505,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1532,7 +1532,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, (res) => { expect(res.statusCode).to.equal(401); done(); @@ -1559,7 +1559,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom optionalPayload' } }, (res) => { expect(res.statusCode).to.equal(401); done(); @@ -1586,7 +1586,7 @@ describe('authentication', () => { } }); - server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom invalidPayload' } }, function (res) { + server.inject({ method: 'POST', url: '/', headers: { authorization: 'Custom invalidPayload' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('Payload is invalid'); @@ -1610,7 +1610,7 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', true, { users: { steve: { response: Boom.internal() } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res) => { expect(res.statusCode).to.equal(500); done(); @@ -1624,7 +1624,7 @@ describe('authentication', () => { const handler = function (request, reply) { - request.server.auth.test('default', request, function (err, credentials) { + request.server.auth.test('default', request, (err, credentials) => { if (err) { return reply({ status: false }); @@ -1640,12 +1640,12 @@ describe('authentication', () => { server.auth.strategy('default', 'custom', { users: { steve: { name: 'steve' } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.result.status).to.equal(false); - server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, function (res2) { + server.inject({ url: '/', headers: { authorization: 'Custom steve' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result.status).to.equal(true); diff --git a/test/connection.js b/test/connection.js index 88f3b22bd..6d29c8511 100755 --- a/test/connection.js +++ b/test/connection.js @@ -38,7 +38,7 @@ describe('Connection', () => { it('allows null port and host', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ host: null, port: null }); }).to.not.throw(); @@ -56,7 +56,7 @@ describe('Connection', () => { it('throws when disabling autoListen and providing a port', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ port: 80, autoListen: false }); }).to.throw('Cannot specify port when autoListen is false'); @@ -67,7 +67,7 @@ describe('Connection', () => { const server = new Hapi.Server(); const port = Path.join(__dirname, 'hapi-server.socket'); - expect(function () { + expect(() => { server.connection({ port: port, autoListen: false }); }).to.throw('Cannot specify port when autoListen is false'); @@ -78,7 +78,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -96,7 +96,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost' }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(server.info.host).to.equal('no.such.domain.hapi'); @@ -110,7 +110,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection({ host: 'no.such.domain.hapi', address: 'localhost', uri: 'http://uri.example.com:8080' }); expect(server.info.uri).to.equal('http://uri.example.com:8080'); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(server.info.host).to.equal('no.such.domain.hapi'); @@ -123,7 +123,7 @@ describe('Connection', () => { it('throws on uri ending with /', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ uri: 'http://uri.example.com:8080/' }); }).to.throw(/Invalid connection options/); @@ -138,12 +138,12 @@ describe('Connection', () => { expect(server.connections[0].type).to.equal('socket'); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const absSocketPath = Path.resolve(port); expect(server.info.port).to.equal(absSocketPath); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); @@ -163,7 +163,7 @@ describe('Connection', () => { expect(server.connections[0].type).to.equal('socket'); - server.start(function (err) { + server.start((err) => { expect(server.info.port).to.equal(port); server.stop(done); @@ -195,10 +195,10 @@ describe('Connection', () => { server.connection({ listener: listener }); server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.get('http://localhost:' + server.info.port + '/', {}, function (err, res, body) { + Wreck.get('http://localhost:' + server.info.port + '/', {}, (err, res, body) => { expect(err).to.not.exist(); expect(body.toString()).to.equal('ok'); @@ -219,7 +219,7 @@ describe('Connection', () => { server.connection({ listener: listener, tls: true }); server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(server.info.protocol).to.equal('https'); @@ -241,10 +241,10 @@ describe('Connection', () => { listener.listen(0, 'localhost', () => { - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.get('http://localhost:' + server.info.port + '/', {}, function (err, res, body) { + Wreck.get('http://localhost:' + server.info.port + '/', {}, (err, res, body) => { expect(err).to.not.exist(); expect(body.toString()).to.equal('ok'); @@ -285,7 +285,7 @@ describe('Connection', () => { method: 'GET', path: '/', config: { handler: function (request, reply) { - setTimeout(function () { + setTimeout(() => { return reply('too late'); }, 70); @@ -293,10 +293,10 @@ describe('Connection', () => { } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.request('GET', 'http://localhost:' + server.info.port + '/', {}, function (err, res) { + Wreck.request('GET', 'http://localhost:' + server.info.port + '/', {}, (err, res) => { expect(err).to.exist(); expect(err.message).to.equal('Client request error: socket hang up'); @@ -316,7 +316,7 @@ describe('Connection', () => { server.connection({ routes: { timeout: { socket: false } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -329,9 +329,9 @@ describe('Connection', () => { return orig.apply(this, arguments); }; - Wreck.request('GET', 'http://localhost:' + server.info.port + '/', {}, function (err, res) { + Wreck.request('GET', 'http://localhost:' + server.info.port + '/', {}, (err, res) => { - Wreck.read(res, {}, function (err, payload) { + Wreck.read(res, {}, (err, payload) => { expect(err).to.not.exist(); expect(timeout).to.equal('gotcha'); @@ -347,7 +347,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); let expectedBoundAddress = '0.0.0.0'; @@ -371,7 +371,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection({ host: '0.0.0.0', port: 0, tls: tlsOptions }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(server.info.host).to.equal('0.0.0.0'); @@ -400,13 +400,13 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); done(); @@ -420,11 +420,11 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); server.connection({ port: server.info.port }); - server.start(function (err) { + server.start((err) => { expect(err).to.exist(); expect(err.message).to.match(/EADDRINUSE/); @@ -440,7 +440,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const socket1 = new Net.Socket(); @@ -452,15 +452,15 @@ describe('Connection', () => { socket2.connect(server.info.port, '127.0.0.1', () => { - server.listener.getConnections(function (err, count1) { + server.listener.getConnections((err, count1) => { expect(count1).to.be.greaterThan(0); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); - server.listener.getConnections(function (err, count2) { + server.listener.getConnections((err, count2) => { expect(count2).to.equal(0); done(); @@ -479,19 +479,19 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const socket1 = new Net.Socket(); const socket2 = new Net.Socket(); - socket1.once('error', function (err) { + socket1.once('error', (err) => { expect(err.errno).to.equal('ECONNRESET'); }); - socket2.once('error', function (err) { + socket2.once('error', (err) => { expect(err.errno).to.equal('ECONNRESET'); }); @@ -500,12 +500,12 @@ describe('Connection', () => { socket2.connect(server.info.port, server.connections[0].settings.host, () => { - server.listener.getConnections(function (err, count) { + server.listener.getConnections((err, count) => { expect(count).to.be.greaterThan(0); const timer = new Hoek.Bench(); - server.stop({ timeout: 20 }, function (err) { + server.stop({ timeout: 20 }, (err) => { expect(err).to.not.exist(); expect(timer.elapsed()).to.be.at.least(19); @@ -521,19 +521,19 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const socket1 = new Net.Socket(); const socket2 = new Net.Socket(); - socket1.once('error', function (err) { + socket1.once('error', (err) => { expect(err.errno).to.equal('ECONNRESET'); }); - socket2.once('error', function (err) { + socket2.once('error', (err) => { expect(err.errno).to.equal('ECONNRESET'); }); @@ -542,16 +542,16 @@ describe('Connection', () => { socket2.connect(server.info.port, server.connections[0].settings.host, () => { - server.listener.getConnections(function (err, count1) { + server.listener.getConnections((err, count1) => { expect(count1).to.be.greaterThan(0); const timer = new Hoek.Bench(); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); - server.listener.getConnections(function (err, count2) { + server.listener.getConnections((err, count2) => { expect(count2).to.equal(0); expect(timer.elapsed()).to.be.at.least(9); @@ -559,7 +559,7 @@ describe('Connection', () => { }); }); - setTimeout(function () { + setTimeout(() => { socket1.end(); socket2.end(); @@ -572,7 +572,7 @@ describe('Connection', () => { it('refuses to handle new incoming requests', (done) => { - const handler = function (request, reply) { + const handler = (request, reply) => { return reply('ok'); }; @@ -580,16 +580,16 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const agent = new Http.Agent({ keepAlive: true, maxSockets: 1 }); let err2; - Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, function (err1, res, body) { + Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, (err1, res, body) => { - server.stop(function (err3) { + server.stop((err3) => { expect(err3).to.not.exist(); expect(err1).to.not.exist(); @@ -600,7 +600,7 @@ describe('Connection', () => { }); }); - Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, function (err, res, body) { + Wreck.get('http://localhost:' + server.info.port + '/', { agent: agent }, (err, res, body) => { err2 = err; }); @@ -612,21 +612,21 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); const initial = server.listener.listeners('connection').length; - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(server.listener.listeners('connection').length).to.be.greaterThan(initial); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); expect(server.listener.listeners('connection').length).to.equal(initial); @@ -641,7 +641,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.stop(function (err) { + server.stop((err) => { server.stop(done); }); @@ -663,23 +663,23 @@ describe('Connection', () => { }; let logged = null; - server.once('log', function (event, tags) { + server.once('log', (event, tags) => { logged = (event.internal && tags.load && event.data); }); server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); - setImmediate(function () { + setImmediate(() => { - server.inject('/', function (res2) { + server.inject('/', (res2) => { expect(res2.statusCode).to.equal(503); expect(logged.rss > 10000).to.equal(true); @@ -709,7 +709,7 @@ describe('Connection', () => { credentials: { foo: 'bar' } }; - server.connections[0].inject(options, function (res) { + server.connections[0].inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(options.credentials).to.exist(); @@ -736,7 +736,7 @@ describe('Connection', () => { } }; - server.inject(options, function (res) { + server.inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(options.credentials).to.exist(); @@ -761,7 +761,7 @@ describe('Connection', () => { authority: 'something' }; - server.inject(options, function (res) { + server.inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('something'); @@ -786,7 +786,7 @@ describe('Connection', () => { authority: 'something' }; - server.inject(options, function (res) { + server.inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('something'); @@ -811,7 +811,7 @@ describe('Connection', () => { artifacts: { bar: 'baz' } }; - server.connections[0].inject(options, function (res) { + server.connections[0].inject(options, (res) => { expect(res.statusCode).to.equal(200); expect(res.result.bar).to.equal('baz'); @@ -832,7 +832,7 @@ describe('Connection', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.request.app.key).to.equal('value'); @@ -851,7 +851,7 @@ describe('Connection', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject({ url: '/', remoteAddress: '1.2.3.4' }, function (res) { + server.inject({ url: '/', remoteAddress: '1.2.3.4' }, (res) => { expect(res.statusCode).to.equal(200); expect(res.payload).to.equal('1.2.3.4'); @@ -870,7 +870,7 @@ describe('Connection', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.payload).to.equal('127.0.0.1'); @@ -891,7 +891,7 @@ describe('Connection', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('example.com:2080'); done(); @@ -1036,7 +1036,7 @@ describe('Connection', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('12'); done(); @@ -1047,11 +1047,13 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreHandler', function (request, reply) { + const preHandler = function (request, reply) { request.app.x = this.y; return reply.continue(); - }, { bind: { y: 42 } }); + }; + + server.ext('onPreHandler', preHandler, { bind: { y: 42 } }); const handler = function (request, reply) { @@ -1060,7 +1062,7 @@ describe('Connection', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(42); done(); @@ -1078,10 +1080,12 @@ describe('Connection', () => { path: __dirname + '/templates' }); - server.ext('onPreHandler', function (request, reply) { + const preHandler = function (request, reply) { return reply.view('test'); - }); + }; + + server.ext('onPreHandler', preHandler); const test = function (plugin, options, next) { @@ -1098,9 +1102,9 @@ describe('Connection', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { - server.inject('/view', function (res) { + server.inject('/view', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1112,12 +1116,14 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { return reply().redirect('/elsewhere'); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); expect(res.headers.location).to.equal('/elsewhere'); @@ -1129,12 +1135,14 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { return reply.redirect('/elsewhere'); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); expect(res.headers.location).to.equal('/elsewhere'); @@ -1148,12 +1156,14 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { return reply(Boom.badRequest('boom')); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(400); expect(res.result.message).to.equal('boom'); @@ -1165,10 +1175,12 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { return reply(null, Boom.badRequest('boom')); - }); + }; + + server.ext('onRequest', onRequest); const handler = function (request, reply) { @@ -1178,7 +1190,7 @@ describe('Connection', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.message).to.equal('boom'); done(); @@ -1196,10 +1208,12 @@ describe('Connection', () => { path: __dirname + '/templates' }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { return reply.view('test', { message: 'hola!' }); - }); + }; + + server.ext('onRequest', onRequest); const handler = function (request, reply) { @@ -1208,7 +1222,7 @@ describe('Connection', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.match(/
\r?\n

hola!<\/h1>\r?\n<\/div>\r?\n/); done(); @@ -1222,14 +1236,17 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreResponse', function (request, reply) { + + const preRequest = function (request, reply) { if (typeof request.response.source === 'string') { return reply(Boom.badRequest('boom')); } return reply.continue(); - }); + }; + + server.ext('onPreResponse', preRequest); server.route({ method: 'GET', @@ -1249,10 +1266,10 @@ describe('Connection', () => { } }); - server.inject({ method: 'GET', url: '/text' }, function (res1) { + server.inject({ method: 'GET', url: '/text' }, (res1) => { expect(res1.result.message).to.equal('boom'); - server.inject({ method: 'GET', url: '/obj' }, function (res2) { + server.inject({ method: 'GET', url: '/obj' }, (res2) => { expect(res2.result.status).to.equal('ok'); done(); @@ -1264,12 +1281,15 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreResponse', function (request, reply) { + + const preResponse = function (request, reply) { return reply(null, request.response.output.statusCode); - }); + }; + + server.ext('onPreResponse', preResponse); - server.inject({ method: 'GET', url: '/missing' }, function (res) { + server.inject({ method: 'GET', url: '/missing' }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal(404); @@ -1283,15 +1303,17 @@ describe('Connection', () => { server.register(Inert, Hoek.ignore); server.connection(); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { const response = request.response; return reply({ isBoom: response.isBoom }); - }); + }; + + server.ext('onPreResponse', preResponse); server.route({ method: 'GET', path: '/{path*}', handler: { directory: { path: './somewhere', listing: false, index: true } } }); - server.inject('/missing', function (res) { + server.inject('/missing', (res) => { expect(res.statusCode).to.equal(200); expect(res.result.isBoom).to.equal(true); @@ -1305,15 +1327,17 @@ describe('Connection', () => { server.register(Inert, Hoek.ignore); server.connection(); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { const response = request.response; return reply({ isBoom: response.isBoom }); - }); + }; + + server.ext('onPreResponse', preResponse); server.route({ method: 'GET', path: '/{path*}', handler: { file: './somewhere/something.txt' } }); - server.inject('/missing', function (res) { + server.inject('/missing', (res) => { expect(res.statusCode).to.equal(200); expect(res.result.isBoom).to.equal(true); @@ -1327,21 +1351,23 @@ describe('Connection', () => { server.register(Inert, Hoek.ignore); server.connection(); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply({ something: 'else' }); - }); + }; + + server.ext('onPreResponse', preResponse); server.route({ method: 'GET', path: '/{path*}', handler: { directory: { path: './' } } }); - server.inject('/package.json', function (res) { + server.inject('/package.json', (res) => { expect(res.statusCode).to.equal(200); expect(res.result.something).to.equal('else'); const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]); let lsof = ''; - cmd.stdout.on('data', function (buffer) { + cmd.stdout.on('data', (buffer) => { lsof += buffer.toString(); }); @@ -1366,17 +1392,22 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreResponse', function (request, reply) { + + const preResponse1 = function (request, reply) { request.response.source = request.response.source + '1'; return reply.continue(); - }); + }; + + server.ext('onPreResponse', preResponse1); - server.ext('onPreResponse', function (request, reply) { + const preResponse2 = function (request, reply) { request.response.source = request.response.source + '2'; return reply.continue(); - }); + }; + + server.ext('onPreResponse', preResponse2); const handler = function (request, reply) { @@ -1385,7 +1416,7 @@ describe('Connection', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'GET', url: '/' }, function (res) { + server.inject({ method: 'GET', url: '/' }, (res) => { expect(res.result).to.equal('012'); done(); @@ -1400,7 +1431,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection({ labels: 'a' }); - server.on('route', function (route, connection, srv) { + server.on('route', (route, connection, srv) => { expect(route.path).to.equal('/'); expect(connection.settings.labels).to.deep.equal(['a']); @@ -1428,7 +1459,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: '*', path: '/{p*}', handler: handler }); - server.inject({ method: 'GET', url: '/page' }, function (res) { + server.inject({ method: 'GET', url: '/page' }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('found'); @@ -1446,7 +1477,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'HEAD', url: '/' }, function (res) { + server.inject({ method: 'HEAD', url: '/' }, (res) => { expect(res.statusCode).to.equal(205); expect(res.headers['content-type']).to.contain('text/html'); @@ -1467,12 +1498,12 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); - server.inject({ method: 'HEAD', url: '/' }, function (res1) { + server.inject({ method: 'HEAD', url: '/' }, (res1) => { expect(res1.statusCode).to.equal(404); expect(res1.result).to.not.exist(); - server.inject({ method: 'HEAD', url: '/not-there' }, function (res2) { + server.inject({ method: 'HEAD', url: '/not-there' }, (res2) => { expect(res2.statusCode).to.equal(404); expect(res2.result).to.not.exist(); @@ -1493,26 +1524,26 @@ describe('Connection', () => { const config = { method: ['GET', 'PUT', 'POST', 'DELETE'], path: '/', handler: handler }; server.route(config); - server.inject({ method: 'HEAD', url: '/' }, function (res1) { + server.inject({ method: 'HEAD', url: '/' }, (res1) => { expect(res1.statusCode).to.equal(200); - server.inject({ method: 'GET', url: '/' }, function (res2) { + server.inject({ method: 'GET', url: '/' }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.payload).to.equal('get'); - server.inject({ method: 'PUT', url: '/' }, function (res3) { + server.inject({ method: 'PUT', url: '/' }, (res3) => { expect(res3.statusCode).to.equal(200); expect(res3.payload).to.equal('put'); - server.inject({ method: 'POST', url: '/' }, function (res4) { + server.inject({ method: 'POST', url: '/' }, (res4) => { expect(res4.statusCode).to.equal(200); expect(res4.payload).to.equal('post'); - server.inject({ method: 'DELETE', url: '/' }, function (res5) { + server.inject({ method: 'DELETE', url: '/' }, (res5) => { expect(res5.statusCode).to.equal(200); expect(res5.payload).to.equal('delete'); @@ -1563,7 +1594,7 @@ describe('Connection', () => { ]); const table = server.table()[0].table; - const paths = table.map(function (route) { + const paths = table.map((route) => { const obj = { method: route.method, @@ -1589,7 +1620,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: ['GET', 'PUT', 'POST', 'DELETE'], @@ -1613,7 +1644,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); - server.inject({ method: 'GET', url: '/nope' }, function (res) { + server.inject({ method: 'GET', url: '/nope' }, (res) => { expect(res.statusCode).to.equal(404); done(); @@ -1630,7 +1661,7 @@ describe('Connection', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/a/{p}', handler: handler }); - server.inject('/a/%', function (res) { + server.inject('/a/%', (res) => { expect(res.statusCode).to.equal(400); done(); diff --git a/test/cors.js b/test/cors.js index a7f9b95e5..3e0afcf9c 100755 --- a/test/cors.js +++ b/test/cors.js @@ -34,7 +34,7 @@ describe('CORS', () => { server.connection({ routes: { cors: false } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(404); done(); @@ -52,7 +52,7 @@ describe('CORS', () => { server.connection({ routes: { cors: true } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.headers['access-control-allow-origin']).to.equal('http://example.com/'); done(); @@ -70,7 +70,7 @@ describe('CORS', () => { server.connection(); server.route({ method: 'GET', path: '/x', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/x', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/x', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.headers['access-control-allow-origin']).to.equal('http://example.com/'); done(); @@ -89,13 +89,13 @@ describe('CORS', () => { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/b', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res2) => { expect(res2.statusCode).to.equal(404); expect(res2.result.message).to.equal('CORS is disabled for this route'); @@ -118,19 +118,19 @@ describe('CORS', () => { server.route({ method: 'GET', path: '/b', handler: handler, config: { cors: true } }); server.route({ method: 'GET', path: '/c', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result).to.be.null(); expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/c', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res3) { + server.inject({ method: 'OPTIONS', url: '/c', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res3) => { expect(res3.statusCode).to.equal(404); expect(res3.result.message).to.equal('CORS is disabled for this route'); @@ -153,7 +153,7 @@ describe('CORS', () => { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: true } }); server.route({ method: 'POST', path: '/a', handler: handler, config: { cors: true } }); - server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.be.null(); @@ -174,13 +174,13 @@ describe('CORS', () => { server.route({ method: 'GET', path: '/a', handler: handler, config: { cors: { origin: ['a'] } } }); server.route({ method: 'GET', path: '/b', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'a', 'access-control-request-method': 'GET' } }, function (res1) { + server.inject({ method: 'OPTIONS', url: '/a', headers: { origin: 'a', 'access-control-request-method': 'GET' } }, (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.result).to.be.null(); expect(res1.headers['access-control-allow-origin']).to.equal('a'); - server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'b', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/b', headers: { origin: 'b', 'access-control-request-method': 'GET' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result).to.be.null(); @@ -201,7 +201,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { credentials: true } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, (res) => { expect(res.result).to.equal(null); expect(res.headers['access-control-allow-credentials']).to.equal('true'); @@ -222,13 +222,13 @@ describe('CORS', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: true } }); - server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res1) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, (res1) => { expect(res1.result).to.exist(); expect(res1.result).to.equal('ok'); expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res2) => { expect(res2.result).to.be.null(); expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); @@ -248,7 +248,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://x.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); @@ -268,7 +268,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://test.example.com', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://test.example.com', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.payload.length).to.equal(0); @@ -288,7 +288,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { additionalExposedHeaders: ['xyz'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://example.com/' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); @@ -308,7 +308,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler, config: { cors: false } }); - server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://x.example.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('ok'); @@ -328,7 +328,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('Tada'); @@ -349,7 +349,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['*'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://www.example.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('Tada'); @@ -370,7 +370,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('Tada'); @@ -391,7 +391,7 @@ describe('CORS', () => { server.connection({ routes: { cors: { origin: ['http://test.example.com', 'http://www.example.com', 'http://*.b.com', 'http://*.a.com'] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, function (res) { + server.inject({ url: '/', headers: { origin: 'http://www.a.com' } }, (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('Tada'); @@ -412,12 +412,12 @@ describe('CORS', () => { server.connection({ routes: { cors: { exposedHeaders: [] } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res1) { + server.inject({ url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res1) => { expect(res1.headers['access-control-allow-origin']).to.equal('http://example.com/'); expect(res1.headers['access-control-expose-headers']).to.not.exist(); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res2) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res2) => { expect(res2.headers['access-control-allow-origin']).to.equal('http://example.com/'); expect(res2.headers['access-control-expose-headers']).to.not.exist(); @@ -456,7 +456,7 @@ describe('CORS', () => { handler: function (request, reply) { } }); - server.inject({ method: 'OPTIONS', url: '/', headers: { 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(404); expect(res.result.message).to.equal('Missing Origin header'); @@ -474,7 +474,7 @@ describe('CORS', () => { handler: function (request, reply) { } }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/' } }, (res) => { expect(res.statusCode).to.equal(404); expect(res.result.message).to.equal('Missing Access-Control-Request-Method header'); @@ -487,7 +487,7 @@ describe('CORS', () => { const server = new Hapi.Server(); server.connection({ routes: { cors: true } }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(404); done(); @@ -504,7 +504,7 @@ describe('CORS', () => { handler: function (request, reply) { } }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(404); expect(res.result.message).to.equal('Origin not allowed'); @@ -531,7 +531,7 @@ describe('CORS', () => { 'access-control-request-method': 'GET', 'access-control-request-headers': 'Authorization' } - }, function (res) { + }, (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['access-control-allow-headers']).to.equal('Accept,Authorization,Content-Type,If-None-Match'); @@ -558,7 +558,7 @@ describe('CORS', () => { 'access-control-request-method': 'GET', 'access-control-request-headers': 'authorization' } - }, function (res) { + }, (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['access-control-allow-headers']).to.equal('Accept,Authorization,Content-Type,If-None-Match'); @@ -585,7 +585,7 @@ describe('CORS', () => { 'access-control-request-method': 'GET', 'access-control-request-headers': 'X' } - }, function (res) { + }, (res) => { expect(res.statusCode).to.equal(404); expect(res.result.message).to.equal('Some headers are not allowed'); @@ -603,7 +603,7 @@ describe('CORS', () => { handler: function (request, reply) { } }); - server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, function (res) { + server.inject({ method: 'OPTIONS', url: '/', headers: { origin: 'http://example.com/', 'access-control-request-method': 'GET' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['access-control-allow-credentials']).to.equal('true'); @@ -627,7 +627,7 @@ describe('CORS', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['access-control-allow-origin']).to.not.exist(); diff --git a/test/handler.js b/test/handler.js index fa8d055e0..1646b97b6 100755 --- a/test/handler.js +++ b/test/handler.js @@ -42,7 +42,7 @@ describe('handler', () => { server.route({ method: 'GET', path: '/domain', handler: handler }); - server.inject('/domain', function (res) { + server.inject('/domain', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -53,7 +53,7 @@ describe('handler', () => { const handler = function (request) { - setImmediate(function () { + setImmediate(() => { not.here; }); @@ -62,7 +62,7 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.on('request-error', function (request, err) { + server.on('request-error', (request, err) => { expect(err.message).to.equal('Uncaught error: not is not defined'); done(); @@ -76,7 +76,7 @@ describe('handler', () => { expect(arguments[1]).to.equal('internal, implementation, error'); }; - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); }); @@ -103,7 +103,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(item.x); done(); @@ -124,7 +124,7 @@ describe('handler', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); done(); @@ -146,7 +146,7 @@ describe('handler', () => { server.route({ method: 'GET', path: '/file', handler: handler }); - server.inject('/file', function (res) { + server.inject('/file', (res) => { expect(res.statusCode).to.equal(499); expect(res.payload).to.contain('hapi'); @@ -175,7 +175,7 @@ describe('handler', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('

steve

'); done(); @@ -199,7 +199,7 @@ describe('handler', () => { const pre3 = function (request, reply) { - process.nextTick(function () { + process.nextTick(() => { return reply(' '); }); @@ -239,7 +239,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('Hello World!'); done(); @@ -272,7 +272,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('Hello'); done(); @@ -298,7 +298,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('Hello'); done(); @@ -319,7 +319,7 @@ describe('handler', () => { const pre3 = function (request, reply) { - process.nextTick(function () { + process.nextTick(() => { return reply(' ').takeover(); }); @@ -359,7 +359,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(' '); done(); @@ -397,7 +397,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.statusCode).to.equal(500); done(); @@ -429,7 +429,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(444); done(); @@ -468,7 +468,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.statusCode).to.equal(500); done(); @@ -480,10 +480,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -499,7 +501,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -511,10 +513,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user.get', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; + + server.method('user.get', method); server.route({ method: 'GET', @@ -530,7 +534,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -542,10 +546,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -564,7 +570,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -576,15 +582,19 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const user = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; - server.method('name', function (user, next) { + server.method('user', user); - return next(null, user.name); - }); + const name = function (obj, next) { + + return next(null, obj.name); + }; + + server.method('name', name); server.route({ method: 'GET', @@ -601,7 +611,7 @@ describe('handler', () => { } }); - server.inject('/user/5/name', function (res) { + server.inject('/user/5/name', (res) => { expect(res.result).to.equal('Bob'); done(); @@ -613,10 +623,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -632,7 +644,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -644,10 +656,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -663,7 +677,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -675,10 +689,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (next) { + const method = function (next) { return next(null, { name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -694,7 +710,7 @@ describe('handler', () => { } }); - server.inject('/user', function (res) { + server.inject('/user', (res) => { expect(res.result).to.deep.equal({ name: 'Bob' }); done(); @@ -706,10 +722,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (request, next) { + const method = function (request, next) { return next(null, { id: request.params.id, name: 'Bob' }); - }); + }; + + server.method('user', method); server.route({ method: 'GET', @@ -725,7 +743,7 @@ describe('handler', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -737,10 +755,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user.get', function (next) { + const method = function (next) { return next(null, { name: 'Bob' }); - }); + }; + + server.method('user.get', method); server.route({ method: 'GET', @@ -756,7 +776,7 @@ describe('handler', () => { } }); - server.inject('/user', function (res) { + server.inject('/user', (res) => { expect(res.result).to.deep.equal({ name: 'Bob' }); done(); @@ -837,7 +857,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(403); done(); @@ -868,7 +888,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -901,7 +921,7 @@ describe('handler', () => { }); let log = null; - server.on('request-internal', function (request, event, tags) { + server.on('request-internal', (request, event, tags) => { if (event.internal && tags.pre && @@ -911,7 +931,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(log).to.equal('before'); @@ -943,7 +963,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(item.x); done(); @@ -966,7 +986,7 @@ describe('handler', () => { }); let log = null; - server.on('request-internal', function (request, event, tags) { + server.on('request-internal', (request, event, tags) => { if (event.internal && tags.handler && @@ -976,7 +996,7 @@ describe('handler', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(403); expect(log.data.isBoom).to.equal(true); @@ -991,10 +1011,12 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob' }); - }, { cache: { expiresIn: 1000, generateTimeout: 10 } }); + }; + + server.method('user', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); server.route({ method: 'GET', @@ -1010,11 +1032,11 @@ describe('handler', () => { } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result[0].tags).to.deep.equal(['pre', 'method', 'user']); expect(res.result[0].internal).to.equal(true); @@ -1030,10 +1052,12 @@ describe('handler', () => { server.connection(); let gen = 0; - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: 'Bob', gen: gen++ }); - }, { cache: { expiresIn: 1000, generateTimeout: 10 } }); + }; + + server.method('user', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); server.route({ method: 'GET', @@ -1049,15 +1073,15 @@ describe('handler', () => { } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/user/5', function (res1) { + server.inject('/user/5', (res1) => { expect(res1.result).to.equal(0); - server.inject('/user/5', function (res2) { + server.inject('/user/5', (res2) => { expect(res2.result).to.equal(0); done(); @@ -1073,13 +1097,16 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - server.method('handler.get', function (request, reply) { + + const method = function (request, reply) { return reply(null, request.params.x + request.params.y).code(299); - }); + }; + + server.method('handler.get', method); server.route({ method: 'GET', path: '/{x}/{y}', handler: 'handler.get' }); - server.inject('/a/b', function (res) { + server.inject('/a/b', (res) => { expect(res.statusCode).to.equal(299); expect(res.result).to.equal('ab'); @@ -1104,7 +1131,7 @@ describe('handler', () => { server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.deep.equal({}); done(); @@ -1131,7 +1158,7 @@ describe('handler', () => { server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.deep.equal({ x: 1 }); done(); @@ -1161,7 +1188,7 @@ describe('handler', () => { server.connection(); server.handler('test', handler); server.route({ method: 'get', path: '/', handler: { test: 'value' } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.deep.equal({ x: 'get' }); done(); @@ -1182,7 +1209,7 @@ describe('handler', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.handler('test', handler); }).to.throw('Handler defaults property must be an object or function'); @@ -1197,10 +1224,13 @@ describe('handler', () => { const server = new Hapi.Server({ debug: false }); server.connection(); - server.ext('onRequest', function (request, next) { + + const onRequest = function (request, next) { a.b.c; - }); + }; + + server.ext('onRequest', onRequest); const handler = function (request, reply) { @@ -1209,7 +1239,7 @@ describe('handler', () => { server.route({ method: 'GET', path: '/domain', handler: handler }); - server.inject('/domain', function (res) { + server.inject('/domain', (res) => { expect(res.statusCode).to.equal(500); done(); diff --git a/test/methods.js b/test/methods.js index 1c38ac37b..71b6f46ae 100755 --- a/test/methods.js +++ b/test/methods.js @@ -34,7 +34,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add); - server.methods.add(1, 5, function (err, result) { + server.methods.add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -51,7 +51,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('_add', _add); - server.methods._add(1, 5, function (err, result) { + server.methods._add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -68,7 +68,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('$add', $add); - server.methods.$add(1, 5, function (err, result) { + server.methods.$add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -85,7 +85,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add_._that', _add); - server.methods.add_._that(1, 5, function (err, result) { + server.methods.add_._that(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -102,7 +102,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add$.$that', $add); - server.methods.add$.$that(1, 5, function (err, result) { + server.methods.add$.$that(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -135,7 +135,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { callback: false }); - server.methods.add(1, 5).then(function (result) { + server.methods.add(1, 5).then((result) => { expect(result).to.equal(6); done(); @@ -153,11 +153,11 @@ describe('Methods', () => { server.connection(); server.method('tools.add', add); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.tools.add(1, 5, function (err, result) { + server.methods.tools.add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -171,10 +171,12 @@ describe('Methods', () => { server.connection(); const context = { name: 'Bob' }; - server.method('user', function (id, next) { + const method = function (id, next) { return next(null, { id: id, name: this.name }); - }, { bind: context }); + }; + + server.method('user', method, { bind: context }); server.route({ method: 'GET', @@ -190,7 +192,7 @@ describe('Methods', () => { } }); - server.inject('/user/5', function (res) { + server.inject('/user/5', (res) => { expect(res.result).to.deep.equal({ id: '5', name: 'Bob' }); done(); @@ -214,14 +216,14 @@ describe('Methods', () => { server.method('tools.add', add); server.method('tools.sub', sub); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.tools.add(1, 5, function (err, result1) { + server.methods.tools.add(1, 5, (err, result1) => { expect(result1).to.equal(6); - server.methods.tools.sub(1, 5, function (err, result2) { + server.methods.tools.sub(1, 5, (err, result2) => { expect(result2).to.equal(-4); done(); @@ -239,7 +241,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('tools.add', add); - expect(function () { + expect(() => { server.method('tools.add', add); }).to.throw('Server method function name already exists: tools.add'); @@ -256,7 +258,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add); - expect(function () { + expect(() => { server.method('add.another', add); }).to.throw('Invalid segment another in reach path add.another'); @@ -276,15 +278,15 @@ describe('Methods', () => { server.connection(); server.method('test', method); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(1); done(); @@ -305,16 +307,16 @@ describe('Methods', () => { server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(err).to.not.exist(); expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(err).to.not.exist(); expect(result2.gen).to.equal(0); @@ -336,16 +338,16 @@ describe('Methods', () => { server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, callback: false }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(err).to.not.exist(); expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(err).to.not.exist(); expect(result2.gen).to.equal(0); @@ -373,21 +375,21 @@ describe('Methods', () => { server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, callback: false }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(err).to.not.exist(); expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(err).to.not.exist(); expect(result2.gen).to.equal(0); - server.methods.test(2, function (err, result3) { + server.methods.test(2, (err, result3) => { expect(err).to.exist(); expect(err.message).to.equal('boom'); @@ -416,15 +418,15 @@ describe('Methods', () => { server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, generateKey: generateKey }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(0); done(); @@ -450,11 +452,11 @@ describe('Methods', () => { server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, generateKey: generateKey }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result) { + server.methods.test(1, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('Invalid method key when invoking: test'); @@ -480,11 +482,11 @@ describe('Methods', () => { server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 }, generateKey: generateKey }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result) { + server.methods.test(1, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('Invalid method key when invoking: test'); @@ -505,15 +507,15 @@ describe('Methods', () => { server.connection(); server.method('test', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(0); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(1); done(); @@ -534,18 +536,18 @@ describe('Methods', () => { server.connection(); server.method('dropTest', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.dropTest(2, function (err, result1) { + server.methods.dropTest(2, (err, result1) => { expect(result1.gen).to.equal(0); - server.methods.dropTest.cache.drop(2, function (err) { + server.methods.dropTest.cache.drop(2, (err) => { expect(err).to.not.exist(); - server.methods.dropTest(2, function (err, result2) { + server.methods.dropTest(2, (err, result2) => { expect(result2.gen).to.equal(1); done(); @@ -567,11 +569,12 @@ describe('Methods', () => { server.connection(); server.method('dropErrTest', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.dropErrTest.cache.drop(function () { }, function (err) { + const invalid = () => { }; + server.methods.dropErrTest.cache.drop(invalid, (err) => { expect(err).to.exist(); done(); @@ -591,11 +594,11 @@ describe('Methods', () => { server.method('test', method, { cache: { generateTimeout: 10 } }); server.method('test2', method, { cache: { generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err) { + server.methods.test(1, (err) => { expect(err).to.not.exist(); expect(server.methods.test.cache.stats.gets).to.equal(1); @@ -607,7 +610,7 @@ describe('Methods', () => { it('throws an error when name is not a string', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method(0, () => { }); @@ -617,25 +620,25 @@ describe('Methods', () => { it('throws an error when name is invalid', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('0', () => { }); }).to.throw('Invalid name: 0'); - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('a..', () => { }); }).to.throw('Invalid name: a..'); - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('a.0', () => { }); }).to.throw('Invalid name: a.0'); - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('.a', () => { }); @@ -646,7 +649,7 @@ describe('Methods', () => { it('throws an error when method is not a function', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('user', 'function'); @@ -656,7 +659,7 @@ describe('Methods', () => { it('throws an error when options is not an object', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('user', () => { }, 'options'); @@ -666,7 +669,7 @@ describe('Methods', () => { it('throws an error when options.generateKey is not a function', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.method('user', () => { }, { generateKey: 'function' }); @@ -676,7 +679,7 @@ describe('Methods', () => { it('throws an error when options.cache is not valid', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server({ cache: CatboxMemory }); server.method('user', () => { }, { cache: { x: 'y', generateTimeout: 10 } }); @@ -687,7 +690,7 @@ describe('Methods', () => { it('throws an error when generateTimeout is not present', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.method('test', () => { }, { cache: {} }); }).to.throw('Method caching requires a timeout value in generateTimeout: test'); @@ -698,7 +701,7 @@ describe('Methods', () => { it('allows generateTimeout to be false', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.method('test', () => { }, { cache: { generateTimeout: false } }); }).to.not.throw(); @@ -716,7 +719,7 @@ describe('Methods', () => { }; server.method('user', method); - server.methods.user(4, function (err, result) { + server.methods.user(4, (err, result) => { expect(result.id).to.equal(4); done(); @@ -727,7 +730,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.connection(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); @@ -737,7 +740,7 @@ describe('Methods', () => { }; server.method('user', method, { cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.methods.user(4, 'something', function (err, result) { + server.methods.user(4, 'something', (err, result) => { expect(result.id).to.equal(4); expect(result.str).to.equal('something'); @@ -756,7 +759,7 @@ describe('Methods', () => { }; server.method('user', method); - server.methods.user(4, function (err, result) { + server.methods.user(4, (err, result) => { expect(err).to.exist(); done(); @@ -774,11 +777,11 @@ describe('Methods', () => { }; server.method('user', method); - server.methods.user(4, function (err, result1) { + server.methods.user(4, (err, result1) => { expect(result1.id).to.equal(4); expect(result1.gen).to.equal(1); - server.methods.user(4, function (err, result2) { + server.methods.user(4, (err, result2) => { expect(result2.id).to.equal(4); expect(result2.gen).to.equal(2); @@ -800,16 +803,16 @@ describe('Methods', () => { server.method('user', method, { cache: { expiresIn: 2000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); const id = Math.random(); - server.methods.user(id, function (err, result1) { + server.methods.user(id, (err, result1) => { expect(result1.id).to.equal(id); expect(result1.gen).to.equal(1); - server.methods.user(id, function (err, result2) { + server.methods.user(id, (err, result2) => { expect(result2.id).to.equal(id); expect(result2.gen).to.equal(1); @@ -827,7 +830,7 @@ describe('Methods', () => { let gen = 0; const method = function (id, next) { - setTimeout(function () { + setTimeout(() => { return next(null, { id: id, gen: ++gen }); }, 5); @@ -835,18 +838,18 @@ describe('Methods', () => { server.method('user', method, { cache: { expiresIn: 2000, generateTimeout: 3 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); const id = Math.random(); - server.methods.user(id, function (err, result1) { + server.methods.user(id, (err, result1) => { expect(err.output.statusCode).to.equal(503); - setTimeout(function () { + setTimeout(() => { - server.methods.user(id, function (err, result2) { + server.methods.user(id, (err, result2) => { expect(result2.id).to.equal(id); expect(result2.gen).to.equal(1); @@ -871,15 +874,15 @@ describe('Methods', () => { server.method('tos', method, { cache: { expiresIn: 2000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.tos(function (err, result1) { + server.methods.tos((err, result1) => { expect(result1.terms).to.equal(terms); expect(result1.gen).to.equal(0); - server.methods.tos(function (err, result2) { + server.methods.tos((err, result2) => { expect(result2.terms).to.equal(terms); expect(result2.gen).to.equal(0); @@ -900,17 +903,17 @@ describe('Methods', () => { }; server.method('user', method, { cache: { expiresIn: 2000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); const id1 = Math.random(); - server.methods.user(id1, function (err, result1) { + server.methods.user(id1, (err, result1) => { expect(result1.id).to.equal(id1); expect(result1.gen).to.equal(1); const id2 = Math.random(); - server.methods.user(id2, function (err, result2) { + server.methods.user(id2, (err, result2) => { expect(result2.id).to.equal(id2); expect(result2.gen).to.equal(2); @@ -932,15 +935,17 @@ describe('Methods', () => { server.method([{ name: 'user', method: method, options: { cache: { expiresIn: 2000, generateTimeout: 10 } } }]); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.user(1, function (err, result1) { + server.methods.user(1, (err, result1) => { expect(result1.id).to.equal(1); - server.methods.user(function () { }, function (err, result2) { + const invalid = function () { }; + + server.methods.user(invalid, (err, result2) => { expect(err).to.exist(); expect(err.message).to.equal('Invalid method key when invoking: user'); @@ -961,15 +966,15 @@ describe('Methods', () => { server.connection(); server.method('test', method, { bind: { gen: 7 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(7); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(8); done(); @@ -989,15 +994,15 @@ describe('Methods', () => { server.connection(); server.method('test', method, { bind: { gen: 7 }, cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(7); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(7); done(); @@ -1018,16 +1023,16 @@ describe('Methods', () => { server.connection(); server.method('test', method, { bind: bind, cache: { expiresIn: 1000, generateTimeout: 10 } }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.methods.test(1, function (err, result1) { + server.methods.test(1, (err, result1) => { expect(result1.gen).to.equal(7); expect(result1.bound).to.equal(true); - server.methods.test(1, function (err, result2) { + server.methods.test(1, (err, result2) => { expect(result2.gen).to.equal(7); done(); @@ -1076,7 +1081,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { callback: false }); - expect(function () { + expect(() => { server.methods.add(1, 5); }).to.throw('boom'); @@ -1093,7 +1098,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -1110,7 +1115,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('boom'); @@ -1128,7 +1133,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('boom'); @@ -1147,7 +1152,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(result).to.equal(6); done(); @@ -1164,7 +1169,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('boom'); @@ -1182,7 +1187,7 @@ describe('Methods', () => { const server = new Hapi.Server(); server.method('add', add, { cache: { expiresIn: 10, generateTimeout: 10 }, callback: false }); - server._methods._normalized.add(1, 5, function (err, result) { + server._methods._normalized.add(1, 5, (err, result) => { expect(err).to.exist(); expect(err.message).to.equal('boom'); @@ -1195,7 +1200,7 @@ describe('Methods', () => { const fn = function () { }; const server = new Hapi.Server(); - expect(function () { + expect(() => { server.method({ name: 'fn', diff --git a/test/payload.js b/test/payload.js index e65c385ad..39350c216 100755 --- a/test/payload.js +++ b/test/payload.js @@ -44,7 +44,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); - server.inject({ method: 'POST', url: '/', payload: payload }, function (res) { + server.inject({ method: 'POST', url: '/', payload: payload }, (res) => { expect(res.result).to.exist(); expect(res.result.x).to.equal('1'); @@ -63,7 +63,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); - server.inject({ method: 'POST', url: '/', payload: 'test', simulate: { error: true, end: false } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: 'test', simulate: { error: true, end: false } }, (res) => { expect(res.result).to.exist(); expect(res.result.statusCode).to.equal(500); @@ -82,13 +82,13 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); - server.once('response', function (request) { + server.once('response', (request) => { expect(request._isBailed).to.equal(true); done(); }); - server.inject({ method: 'POST', url: '/', payload: 'test', simulate: { close: true, end: false } }, function (res) { }); + server.inject({ method: 'POST', url: '/', payload: 'test', simulate: { close: true, end: false } }, (res) => { }); }); it('handles aborted request', (done) => { @@ -103,12 +103,12 @@ describe('payload', () => { server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { parse: false } } }); let message = null; - server.on('log', function (event, tags) { + server.on('log', (event, tags) => { message = event.data.message; }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -122,20 +122,20 @@ describe('payload', () => { } }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { }); req.write('Hello\n'); - req.on('error', function (err) { + req.on('error', (err) => { expect(message).to.equal('Parse Error'); expect(err.code).to.equal('ECONNRESET'); server.stop(done); }); - setTimeout(function () { + setTimeout(() => { req.abort(); }, 15); @@ -156,7 +156,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { maxBytes: 10 } } }); - server.inject({ method: 'POST', url: '/', payload: payload, headers: { 'content-length': payload.length } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: payload, headers: { 'content-length': payload.length } }, (res) => { expect(res.statusCode).to.equal(400); expect(res.result).to.exist(); @@ -178,13 +178,13 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { maxBytes: 1024 * 1024 } } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); const uri = 'http://localhost:' + server.info.port; - Wreck.post(uri, { payload: payload }, function (err, res, body) { + Wreck.post(uri, { payload: payload }, (err, res, body) => { expect(err).to.not.exist(); expect(res.statusCode).to.equal(400); @@ -201,7 +201,7 @@ describe('payload', () => { const ext = function (request, reply) { const chunks = []; - request.on('peek', function (chunk) { + request.on('peek', (chunk) => { chunks.push(chunk); }); @@ -225,7 +225,7 @@ describe('payload', () => { server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { parse: false } } }); const payload = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'; - server.inject({ method: 'POST', url: '/', payload: payload }, function (res) { + server.inject({ method: 'POST', url: '/', payload: payload }, (res) => { expect(res.result).to.equal(payload); done(); @@ -244,7 +244,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); - Zlib.gzip(JSON.stringify(message), function (err, buf) { + Zlib.gzip(JSON.stringify(message), (err, buf) => { const request = { method: 'POST', @@ -257,7 +257,7 @@ describe('payload', () => { payload: buf }; - server.inject(request, function (res) { + server.inject(request, (res) => { expect(res.result).to.exist(); expect(res.result).to.deep.equal(message); @@ -272,20 +272,20 @@ describe('payload', () => { const sourceContents = Fs.readFileSync(path); const stats = Fs.statSync(path); - Zlib.gzip(sourceContents, function (err, compressed) { + const handler = function (request, reply) { - const handler = function (request, reply) { + const receivedContents = Fs.readFileSync(request.payload.path); + Fs.unlinkSync(request.payload.path); + expect(receivedContents).to.deep.equal(sourceContents); + return reply(request.payload.bytes); + }; - const receivedContents = Fs.readFileSync(request.payload.path); - Fs.unlinkSync(request.payload.path); - expect(receivedContents).to.deep.equal(sourceContents); - return reply(request.payload.bytes); - }; + Zlib.gzip(sourceContents, (err, compressed) => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/file', config: { handler: handler, payload: { output: 'file' } } }); - server.inject({ method: 'POST', url: '/file', payload: compressed, headers: { 'content-encoding': 'gzip' } }, function (res) { + server.inject({ method: 'POST', url: '/file', payload: compressed, headers: { 'content-encoding': 'gzip' } }, (res) => { expect(res.result).to.equal(stats.size); done(); @@ -300,7 +300,7 @@ describe('payload', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'POST', path: '/file', config: { handler: handler, payload: { output: 'file', parse: false, uploads: '/a/b/c/d/not' } } }); - server.inject({ method: 'POST', url: '/file', payload: 'abcde' }, function (res) { + server.inject({ method: 'POST', url: '/file', payload: 'abcde' }, (res) => { expect(res.statusCode).to.equal(500); done(); @@ -318,7 +318,7 @@ describe('payload', () => { server.connection(); server.route({ method: '*', path: '/any', handler: handler }); - server.inject({ url: '/any', method: 'POST', payload: { key: '09876' } }, function (res) { + server.inject({ url: '/any', method: 'POST', payload: { key: '09876' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('09876'); @@ -337,7 +337,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -352,7 +352,7 @@ describe('payload', () => { } }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect(res.statusCode).to.equal(415); server.stop({ timeout: 1 }, done); @@ -373,7 +373,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', config: { handler: handler, payload: { failAction: 'ignore' } } }); - server.inject({ method: 'POST', url: '/', payload: 'testing123', headers: { 'content-type': 'application/unknown' } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: 'testing123', headers: { 'content-type': 'application/unknown' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.deep.equal(null); @@ -392,7 +392,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); - server.inject({ method: 'POST', url: '/', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('ok'); @@ -411,7 +411,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/text', config: { handler: textHandler } }); - server.inject({ method: 'POST', url: '/text', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, function (res) { + server.inject({ method: 'POST', url: '/text', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('testing123+456'); @@ -430,7 +430,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/override', config: { handler: handler, payload: { override: 'application/json' } } }); - server.inject({ method: 'POST', url: '/override', payload: '{"key":"cool"}', headers: { 'content-type': 'text/plain' } }, function (res) { + server.inject({ method: 'POST', url: '/override', payload: '{"key":"cool"}', headers: { 'content-type': 'text/plain' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('cool'); @@ -449,7 +449,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/textOnly', config: { handler: textHandler, payload: { allow: 'text/plain' } } }); - server.inject({ method: 'POST', url: '/textOnly', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, function (res) { + server.inject({ method: 'POST', url: '/textOnly', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('testing123+456'); @@ -468,7 +468,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/textOnly', config: { handler: textHandler, payload: { allow: 'text/plain' } } }); - server.inject({ method: 'POST', url: '/textOnly', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, function (res) { + server.inject({ method: 'POST', url: '/textOnly', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, (res) => { expect(res.statusCode).to.equal(415); done(); @@ -486,7 +486,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/textOnlyArray', config: { handler: textHandler, payload: { allow: ['text/plain'] } } }); - server.inject({ method: 'POST', url: '/textOnlyArray', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, function (res) { + server.inject({ method: 'POST', url: '/textOnlyArray', payload: 'testing123', headers: { 'content-type': 'text/plain' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('testing123+456'); @@ -505,7 +505,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/textOnlyArray', config: { handler: textHandler, payload: { allow: ['text/plain'] } } }); - server.inject({ method: 'POST', url: '/textOnlyArray', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, function (res) { + server.inject({ method: 'POST', url: '/textOnlyArray', payload: 'testing123', headers: { 'content-type': 'application/octet-stream' } }, (res) => { expect(res.statusCode).to.equal(415); done(); @@ -526,7 +526,7 @@ describe('payload', () => { } }); - server.inject({ method: 'POST', url: '/', payload: 'x[y]=1&x[z]=2', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: 'x[y]=1&x[z]=2', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('12'); @@ -581,7 +581,7 @@ describe('payload', () => { server.connection(); server.route({ method: 'POST', path: '/echo', config: { handler: handler } }); - server.inject({ method: 'POST', url: '/echo', payload: multipartPayload, headers: { 'content-type': 'multipart/form-data; boundary=AaB03x' } }, function (res) { + server.inject({ method: 'POST', url: '/echo', payload: multipartPayload, headers: { 'content-type': 'multipart/form-data; boundary=AaB03x' } }, (res) => { expect(Object.keys(res.result).length).to.equal(3); expect(res.result.field1).to.exist(); @@ -602,7 +602,7 @@ describe('payload', () => { const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/fast', config: { handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -614,17 +614,17 @@ describe('payload', () => { method: 'POST' }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect(res.statusCode).to.equal(408); expect(timer.elapsed()).to.be.at.least(45); server.stop({ timeout: 1 }, done); }); - req.on('error', function (err) { }); // Will error out, so don't allow error to escape test + req.on('error', (err) => { }); // Will error out, so don't allow error to escape test req.write('{}\n'); - setTimeout(function () { + setTimeout(() => { req.end(); }, 100); @@ -641,7 +641,7 @@ describe('payload', () => { const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: false } } }); server.route({ method: 'POST', path: '/fast', config: { payload: { timeout: 50 }, handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -653,17 +653,17 @@ describe('payload', () => { method: 'POST' }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect(res.statusCode).to.equal(408); expect(timer.elapsed()).to.be.at.least(45); server.stop({ timeout: 1 }, done); }); - req.on('error', function (err) { }); // Will error out, so don't allow error to escape test + req.on('error', (err) => { }); // Will error out, so don't allow error to escape test req.write('{}\n'); - setTimeout(function () { + setTimeout(() => { req.end(); }, 100); @@ -680,7 +680,7 @@ describe('payload', () => { const server = new Hapi.Server(); server.connection({ routes: { payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/fast', config: { handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -691,7 +691,7 @@ describe('payload', () => { method: 'POST' }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect(res.statusCode).to.equal(200); server.stop({ timeout: 1 }, done); diff --git a/test/plugin.js b/test/plugin.js index feb0b148b..453db5e0b 100755 --- a/test/plugin.js +++ b/test/plugin.js @@ -99,18 +99,18 @@ describe('Plugin', () => { }); memoryx.state('sid', { encoding: 'base64' }); - srv.method({ - name: 'testMethod', method: function (nxt) { + const method = function (nxt) { - return nxt(null, '123'); - }, options: { cache: { expiresIn: 1000, generateTimeout: 10 } } - }); + return nxt(null, '123'); + }; - srv.methods.testMethod(function (err, result1) { + srv.method({ name: 'testMethod', method: method, options: { cache: { expiresIn: 1000, generateTimeout: 10 } } }); + + srv.methods.testMethod((err, result1) => { expect(result1).to.equal('123'); - srv.methods.testMethod(function (err, result2) { + srv.methods.testMethod((err, result2) => { expect(result2).to.equal('123'); return next(); @@ -122,7 +122,7 @@ describe('Plugin', () => { name: 'plugin' }; - server.register(register, function (err) { + server.register(register, (err) => { expect(err).to.not.exist(); @@ -161,7 +161,7 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, { select: ['a', 'b'] }, function (err) { + server.register(test, { select: ['a', 'b'] }, (err) => { expect(err).to.not.exist(); expect(server.plugins.test.key1).to.equal(2); @@ -189,7 +189,7 @@ describe('Plugin', () => { name: 'test' }; - server.register({ register: test, options: { something: true } }, function (err) { + server.register({ register: test, options: { something: true } }, (err) => { expect(err).to.not.exist(); done(); @@ -213,7 +213,7 @@ describe('Plugin', () => { name: 'test' }; - server.register({ register: test, options: { something: true } }, function (err) { + server.register({ register: test, options: { something: true } }, (err) => { expect(err).to.not.exist(); done(); @@ -223,14 +223,14 @@ describe('Plugin', () => { it('throws on bad plugin (missing attributes)', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.register({ register: function (srv, options, next) { return next(); } - }, function (err) { }); + }, (err) => { }); }).to.throw(); @@ -247,9 +247,9 @@ describe('Plugin', () => { register.attributes = {}; const server = new Hapi.Server(); - expect(function () { + expect(() => { - server.register(register, function (err) { }); + server.register(register, (err) => { }); }).to.throw(); done(); @@ -267,9 +267,9 @@ describe('Plugin', () => { }; const server = new Hapi.Server(); - expect(function () { + expect(() => { - server.register(register, function (err) { }); + server.register(register, (err) => { }); }).to.throw(); done(); @@ -290,7 +290,7 @@ describe('Plugin', () => { name: 'test' }; - expect(function () { + expect(() => { server.register(test); }).to.throw('A callback function is required to register a plugin'); @@ -310,7 +310,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.exist(); expect(err.message).to.equal('from plugin'); @@ -342,11 +342,11 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); expect(server.connections[0].registrations.steve.version).to.equal('0.0.0'); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(require('../package.json').version); done(); @@ -383,7 +383,7 @@ describe('Plugin', () => { server.register({ register: test, options: { foo: 'bar' } - }, function (err) { + }, (err) => { expect(err).to.not.exist(); const bob = server.connections[0].registrations.bob; @@ -392,7 +392,7 @@ describe('Plugin', () => { expect(bob.version).to.equal('1.2.3'); expect(bob.attributes.multiple).to.be.true(); expect(bob.options.foo).to.equal('bar'); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(require('../package.json').version); done(); @@ -422,12 +422,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ host: 'example.com' }); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - expect(function () { + expect(() => { - server.register(test, function (err) { }); + server.register(test, (err) => { }); }).to.throw('Plugin test already registered in: http://example.com'); done(); @@ -449,10 +449,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); expect(server.app.x).to.equal(2); @@ -466,12 +466,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); let log = null; - server.once('log', function (event, tags) { + server.once('log', (event, tags) => { log = [event, tags]; }); - server.register([internals.plugins.test1, internals.plugins.test2], function (err) { + server.register([internals.plugins.test1, internals.plugins.test2], (err) => { expect(err).to.not.exist(); expect(internals.routesList(server)).to.deep.equal(['/test1', '/test2']); @@ -486,12 +486,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); let log = null; - server.once('log', function (event, tags) { + server.once('log', (event, tags) => { log = [event, tags]; }); - server.register([{ register: internals.plugins.test1 }, { register: internals.plugins.test2 }], function (err) { + server.register([{ register: internals.plugins.test1 }, { register: internals.plugins.test2 }], (err) => { expect(err).to.not.exist(); expect(internals.routesList(server)).to.deep.equal(['/test1', '/test2']); @@ -505,10 +505,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(internals.plugins.child, function (err) { + server.register(internals.plugins.child, (err) => { expect(err).to.not.exist(); - server.inject('/test1', function (res) { + server.inject('/test1', (res) => { expect(res.result).to.equal('testing123'); done(); @@ -520,11 +520,11 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(internals.plugins.test1, { routes: { prefix: '/xyz' } }, function (err) { + server.register(internals.plugins.test1, { routes: { prefix: '/xyz' } }, (err) => { expect(server.plugins.test1.prefix).to.equal('/xyz'); expect(err).to.not.exist(); - server.inject('/xyz/test1', function (res) { + server.inject('/xyz/test1', (res) => { expect(res.result).to.equal('testing123'); done(); @@ -536,11 +536,11 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register({ register: internals.plugins.test1, routes: { prefix: '/abc' } }, { routes: { prefix: '/xyz' } }, function (err) { + server.register({ register: internals.plugins.test1, routes: { prefix: '/abc' } }, { routes: { prefix: '/xyz' } }, (err) => { expect(server.plugins.test1.prefix).to.equal('/abc'); expect(err).to.not.exist(); - server.inject('/abc/test1', function (res) { + server.inject('/abc/test1', (res) => { expect(res.result).to.equal('testing123'); done(); @@ -569,10 +569,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(test, { routes: { prefix: '/xyz' } }, function (err) { + server.register(test, { routes: { prefix: '/xyz' } }, (err) => { expect(err).to.not.exist(); - server.inject('/xyz', function (res) { + server.inject('/xyz', (res) => { expect(res.result).to.equal('ok'); done(); @@ -600,10 +600,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(a, { routes: { prefix: '/xyz' } }, function (err) { + server.register(a, { routes: { prefix: '/xyz' } }, (err) => { expect(err).to.not.exist(); - server.inject('/xyz', function (res) { + server.inject('/xyz', (res) => { expect(res.result).to.equal('ok'); done(); @@ -633,7 +633,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { expect(err).to.not.exist(); done(); @@ -662,7 +662,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register({ register: a }, function (err) { + server.register({ register: a }, (err) => { expect(err).to.not.exist(); done(); @@ -673,10 +673,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(internals.plugins.child, { routes: { prefix: '/xyz' } }, function (err) { + server.register(internals.plugins.child, { routes: { prefix: '/xyz' } }, (err) => { expect(err).to.not.exist(); - server.inject('/xyz/test1', function (res) { + server.inject('/xyz/test1', (res) => { expect(res.result).to.equal('testing123'); done(); @@ -688,10 +688,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(internals.plugins.child, { routes: { vhost: 'example.com' } }, function (err) { + server.register(internals.plugins.child, { routes: { vhost: 'example.com' } }, (err) => { expect(err).to.not.exist(); - server.inject({ url: '/test1', headers: { host: 'example.com' } }, function (res) { + server.inject({ url: '/test1', headers: { host: 'example.com' } }, (res) => { expect(res.result).to.equal('testing123'); done(); @@ -703,10 +703,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register({ register: internals.plugins.child, options: { routes: { prefix: '/inner' } } }, { routes: { prefix: '/xyz' } }, function (err) { + server.register({ register: internals.plugins.child, options: { routes: { prefix: '/inner' } } }, { routes: { prefix: '/xyz' } }, (err) => { expect(err).to.not.exist(); - server.inject('/xyz/inner/test1', function (res) { + server.inject('/xyz/inner/test1', (res) => { expect(res.result).to.equal('testing123'); done(); @@ -718,10 +718,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register({ register: internals.plugins.child, options: { routes: { vhost: 'example.net' } } }, { routes: { vhost: 'example.com' } }, function (err) { + server.register({ register: internals.plugins.child, options: { routes: { vhost: 'example.net' } } }, { routes: { vhost: 'example.com' } }, (err) => { expect(err).to.not.exist(); - server.inject({ url: '/test1', headers: { host: 'example.com' } }, function (res) { + server.inject({ url: '/test1', headers: { host: 'example.com' } }, (res) => { expect(res.result).to.equal('testing123'); done(); @@ -733,14 +733,14 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register(internals.plugins.test1, { routes: { vhost: 'example.com' } }, function (err) { + server.register(internals.plugins.test1, { routes: { vhost: 'example.com' } }, (err) => { expect(err).to.not.exist(); - server.inject('/test1', function (res1) { + server.inject('/test1', (res1) => { expect(res1.statusCode).to.equal(404); - server.inject({ url: '/test1', headers: { host: 'example.com' } }, function (res2) { + server.inject({ url: '/test1', headers: { host: 'example.com' } }, (res2) => { expect(res2.result).to.equal('testing123'); done(); @@ -753,14 +753,14 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ labels: 'test' }); - server.register({ register: internals.plugins.test1, routes: { vhost: 'example.org' } }, { routes: { vhost: 'example.com' } }, function (err) { + server.register({ register: internals.plugins.test1, routes: { vhost: 'example.org' } }, { routes: { vhost: 'example.com' } }, (err) => { expect(err).to.not.exist(); - server.inject('/test1', function (res1) { + server.inject('/test1', (res1) => { expect(res1.statusCode).to.equal(404); - server.inject({ url: '/test1', headers: { host: 'example.org' } }, function (res2) { + server.inject({ url: '/test1', headers: { host: 'example.org' } }, (res2) => { expect(res2.result).to.equal('testing123'); done(); @@ -795,13 +795,13 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, { select: 'a' }, function (err) { + server.register(test, { select: 'a' }, (err) => { expect(err).to.not.exist(); - server1.inject('/', function (res1) { + server1.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); - server2.inject('/', function (res2) { + server2.inject('/', (res2) => { expect(res2.statusCode).to.equal(404); done(); @@ -839,18 +839,18 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, { select: ['a', 'c'] }, function (err) { + server.register(test, { select: ['a', 'c'] }, (err) => { expect(err).to.not.exist(); expect(server.plugins.test.super).to.equal('trooper'); - server1.inject('/', function (res1) { + server1.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); - server2.inject('/', function (res2) { + server2.inject('/', (res2) => { expect(res2.statusCode).to.equal(404); - server3.inject('/', function (res3) { + server3.inject('/', (res3) => { expect(res3.statusCode).to.equal(200); done(); @@ -889,18 +889,18 @@ describe('Plugin', () => { name: 'test' }; - server.register({ register: test, select: ['a', 'c'] }, { select: ['b'] }, function (err) { + server.register({ register: test, select: ['a', 'c'] }, { select: ['b'] }, (err) => { expect(err).to.not.exist(); expect(server.plugins.test.super).to.equal('trooper'); - server1.inject('/', function (res1) { + server1.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); - server2.inject('/', function (res2) { + server2.inject('/', (res2) => { expect(res2.statusCode).to.equal(404); - server3.inject('/', function (res3) { + server3.inject('/', (res3) => { expect(res3.statusCode).to.equal(200); done(); @@ -942,13 +942,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(c, function (err) { + server.register(c, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -990,13 +990,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(c, function (err) { + server.register(c, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1039,13 +1039,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(c, function (err) { + server.register(c, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1088,13 +1088,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(c, function (err) { + server.register(c, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1127,12 +1127,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin a missing dependency b in connection: ' + server.connections[1].info.uri); @@ -1167,12 +1167,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1207,12 +1207,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register([b, b], function (err) { + server.register([b, b], (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1225,7 +1225,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register(b, function (err) { + srv.register(b, (err) => { return next(); }); @@ -1248,7 +1248,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { expect(err).to.not.exist(); done(); @@ -1259,9 +1259,9 @@ describe('Plugin', () => { const a = function (srv, options, next) { - expect(function () { + expect(() => { - srv.register(b, { select: 'none' }, function (err) { }); + srv.register(b, { select: 'none' }, (err) => { }); }).to.throw('Cannot select inside a connectionless plugin'); return next(); }; @@ -1283,7 +1283,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { expect(err).to.not.exist(); done(); @@ -1294,7 +1294,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register(b, { once: true }, function (err) { + srv.register(b, { once: true }, (err) => { expect(err).to.not.exist(); return next(); @@ -1318,12 +1318,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(2); @@ -1337,7 +1337,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.select('none').register(b, { once: true }, function (err) { + srv.select('none').register(b, { once: true }, (err) => { expect(err).to.not.exist(); return next(); @@ -1362,11 +1362,11 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1393,7 +1393,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); server.connection(); - server.select('none').register(b, { once: true }, function (err) { + server.select('none').register(b, { once: true }, (err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1405,7 +1405,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register(b, { once: true }, function (err) { + srv.register(b, { once: true }, (err) => { expect(err).to.not.exist(); return next(); @@ -1430,11 +1430,11 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1460,7 +1460,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); server.connection(); - server.select('none').register(b, { once: true }, function (err) { + server.select('none').register(b, { once: true }, (err) => { expect(err).to.not.exist(); expect(count).to.equal(0); @@ -1472,7 +1472,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register(b, { once: true }, function (err) { + srv.register(b, { once: true }, (err) => { expect(err).to.not.exist(); return next(); @@ -1498,12 +1498,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1517,7 +1517,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register(b, function (err) { + srv.register(b, (err) => { expect(err).to.not.exist(); return next(); @@ -1544,12 +1544,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1563,7 +1563,7 @@ describe('Plugin', () => { const a = function (srv, options, next) { - srv.register({ register: b, once: true }, function (err) { + srv.register({ register: b, once: true }, (err) => { expect(err).to.not.exist(); return next(); @@ -1589,12 +1589,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(b, function (err) { + server.register(b, (err) => { server.connection(); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1622,7 +1622,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); server.connection(); - server.register(b, { once: true }, function (err) { + server.register(b, { once: true }, (err) => { expect(err).to.not.exist(); expect(count).to.equal(1); @@ -1643,9 +1643,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { - server.register({ register: a, options: {}, once: true }, function (err) { }); + server.register({ register: a, options: {}, once: true }, (err) => { }); }).to.throw(); done(); @@ -1665,7 +1665,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.register(a, () => { }); }).to.throw(); @@ -1686,7 +1686,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.register(a, () => { }); }).to.throw(); @@ -1721,10 +1721,10 @@ describe('Plugin', () => { name: 'b' }; - server.register([a, b], function (err) { + server.register([a, b], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1753,10 +1753,12 @@ describe('Plugin', () => { const b = function (srv, options, next) { - srv.dependency('a', function (srv2, next2) { + const after = function (srv2, next2) { return next2(typeof srv2.a === 'function' ? null : new Error('Missing decoration')); - }); + }; + + srv.dependency('a', after); return next(); }; @@ -1765,10 +1767,10 @@ describe('Plugin', () => { name: 'b' }; - server.register([a, b], function (err) { + server.register([a, b], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1798,11 +1800,13 @@ describe('Plugin', () => { const b = function (srv, options, next) { srv.realm.x = 1; - srv.dependency('a', function (srv2, next2) { + const after = function (srv2, next2) { expect(srv2.realm.x).to.equal(1); return next2(typeof srv2.a === 'function' ? null : new Error('Missing decoration')); - }); + }; + + srv.dependency('a', after); return next(); }; @@ -1811,10 +1815,10 @@ describe('Plugin', () => { name: 'b' }; - server.register([b, a], function (err) { + server.register([b, a], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1843,7 +1847,7 @@ describe('Plugin', () => { const b = function (srv, options, next) { - srv.register(a, function (err) { + srv.register(a, (err) => { expect(err).to.not.exist(); return next(typeof srv.a === 'function' ? null : new Error('Missing decoration')); @@ -1854,10 +1858,10 @@ describe('Plugin', () => { name: 'b' }; - server.register([b], function (err) { + server.register([b], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -1882,14 +1886,14 @@ describe('Plugin', () => { } }); - server.register(internals.plugins.auth, function (err) { + server.register(internals.plugins.auth, (err) => { expect(err).to.not.exist(); - server.select('a').inject('/', function (res1) { + server.select('a').inject('/', (res1) => { expect(res1.statusCode).to.equal(401); - server.select('a').inject({ method: 'GET', url: '/', headers: { authorization: 'Basic ' + (new Buffer('john:12345', 'utf8')).toString('base64') } }, function (res2) { + server.select('a').inject({ method: 'GET', url: '/', headers: { authorization: 'Basic ' + (new Buffer('john:12345', 'utf8')).toString('base64') } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.result).to.equal('authenticated!'); @@ -1922,10 +1926,12 @@ describe('Plugin', () => { } }); - srv.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply(request.response.source + this.suffix); - }); + }; + + srv.ext('onPreResponse', preResponse); return next(); }; @@ -1936,10 +1942,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('in context throughout'); done(); @@ -1955,13 +1961,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); const cache = server.cache({ segment: 'test', expiresIn: 1000 }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - cache.set('a', 'going in', 0, function (err) { + cache.set('a', 'going in', 0, (err) => { - cache.get('a', function (err, value, cached, report) { + cache.get('a', (err, value, cached, report) => { expect(value).to.equal('going in'); done(); @@ -1974,7 +1980,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.cache({ expiresIn: 1000 }); }).to.throw('Missing cache segment name'); @@ -1986,13 +1992,13 @@ describe('Plugin', () => { const server = new Hapi.Server({ cache: { engine: CatboxMemory, partition: 'hapi-test-other' } }); server.connection(); const cache = server.cache({ segment: 'test', expiresIn: 1000 }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - cache.set('a', 'going in', 0, function (err) { + cache.set('a', 'going in', 0, (err) => { - cache.get('a', function (err, value, cached, report) { + cache.get('a', (err, value, cached, report) => { expect(value).to.equal('going in'); expect(cache._cache.connection.settings.partition).to.equal('hapi-test-other'); @@ -2006,7 +2012,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.cache({ segment: 'a', expiresAt: '12:00', expiresIn: 1000 }); }).throws(); @@ -2018,7 +2024,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.cache({ segment: 'a' }); }).to.not.throw(); @@ -2030,7 +2036,7 @@ describe('Plugin', () => { const server = new Hapi.Server({ cache: { engine: CatboxMemory, shared: true } }); server.connection(); - expect(function () { + expect(() => { server.cache({ segment: 'a', expiresIn: 1000 }); server.cache({ segment: 'a', expiresIn: 1000 }); @@ -2042,7 +2048,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.cache({ segment: 'a', expiresIn: 1000 }); server.cache({ segment: 'a', expiresIn: 1000, shared: true }); @@ -2058,7 +2064,7 @@ describe('Plugin', () => { srv.expose({ get: function (key, callback) { - cache.get(key, function (err, value, cached, report) { + cache.get(key, (err, value, cached, report) => { callback(err, value); }); @@ -2078,23 +2084,23 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.plugins.test.set('a', '1', function (err) { + server.plugins.test.set('a', '1', (err) => { expect(err).to.not.exist(); - server.plugins.test.get('a', function (err, value1) { + server.plugins.test.get('a', (err, value1) => { expect(err).to.not.exist(); expect(value1).to.equal('1'); - setTimeout(function () { + setTimeout(() => { - server.plugins.test.get('a', function (err, value2) { + server.plugins.test.get('a', (err, value2) => { expect(err).to.not.exist(); expect(value2).to.equal(null); @@ -2134,10 +2140,10 @@ describe('Plugin', () => { }; const server = new Hapi.Server(); - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); - server.connections[0].inject('/', function (res) { + server.connections[0].inject('/', (res) => { expect(res.result).to.equal('context'); done(); @@ -2153,10 +2159,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.decorate('request', 'getId', function () { + const getId = function () { return this.id; - }); + }; + + server.decorate('request', 'getId', getId); server.route({ method: 'GET', @@ -2167,7 +2175,7 @@ describe('Plugin', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.match(/^.*\:.*\:.*\:.*\:.*$/); @@ -2180,10 +2188,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.decorate('reply', 'success', function () { + const success = function () { return this.response({ status: 'ok' }); - }); + }; + + server.decorate('reply', 'success', success); server.route({ method: 'GET', @@ -2194,7 +2204,7 @@ describe('Plugin', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result.status).to.equal('ok'); @@ -2212,7 +2222,7 @@ describe('Plugin', () => { return this.response({ status: 'ok' }); }); - expect(function () { + expect(() => { server.decorate('reply', 'success', () => { }); }).to.throw('Reply interface decoration already defined: success'); @@ -2224,7 +2234,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.decorate('reply', 'redirect', () => { }); }).to.throw('Cannot override built-in reply interface decoration: redirect'); @@ -2236,7 +2246,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.decorate('server', 'ok', function (path) { + const ok = function (path) { server.route({ method: 'GET', @@ -2246,11 +2256,13 @@ describe('Plugin', () => { return reply('ok'); } }); - }); + }; + + server.decorate('server', 'ok', ok); server.ok('/'); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('ok'); @@ -2263,7 +2275,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.decorate('server', 'ok', function (path) { + const ok = function (path) { server.route({ method: 'GET', @@ -2273,9 +2285,11 @@ describe('Plugin', () => { return reply('ok'); } }); - }); + }; + + server.decorate('server', 'ok', ok); - expect(function () { + expect(() => { server.decorate('server', 'ok', () => { }); }).to.throw('Server decoration already defined: ok'); @@ -2287,7 +2301,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.decorate('server', 'start', () => { }); }).to.throw('Cannot override the built-in server interface method: start'); @@ -2299,7 +2313,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.decorate('server', 'select', () => { }); }).to.throw('Cannot override the built-in server interface method: select'); @@ -2311,7 +2325,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.decorate('server', '_special', () => { }); }).to.throw('Property name cannot begin with an underscore: _special'); @@ -2335,9 +2349,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin test missing dependency none in connection: ' + server.info.uri); @@ -2360,9 +2374,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin test missing dependency none in connection: ' + server.info.uri); @@ -2386,9 +2400,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin test missing dependency none'); @@ -2422,9 +2436,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register([test, b], function (err) { + server.register([test, b], (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin test missing dependency none'); @@ -2458,9 +2472,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register([test, b], function (err) { + server.register([test, b], (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); done(); @@ -2472,9 +2486,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); - server.register([internals.plugins.deps1, internals.plugins.deps3], function (err) { + server.register([internals.plugins.deps1, internals.plugins.deps3], (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin deps1 missing dependency deps2 in connection: ' + server.info.uri); @@ -2515,7 +2529,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register([a, c], function (err) { + server.register([a, c], (err) => { expect(err).to.not.exist(); done(); @@ -2545,9 +2559,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin b missing dependency c in connection: ' + server.info.uri); @@ -2579,9 +2593,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection({ port: 80, host: 'localhost' }); - server.register(a, function (err) { + server.register(a, (err) => { - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); expect(err.message).to.equal('Plugin b missing dependency c in connection: ' + server.info.uri); @@ -2612,7 +2626,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); server.inject({ url: '/' }, () => { }); @@ -2650,18 +2664,18 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); server1.emit('test'); server2.emit('test'); server3.emit('test'); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); expect(counter).to.equal(3); @@ -2682,7 +2696,7 @@ describe('Plugin', () => { server.connection({ labels: ['s3', 'a', 'b', 'd', 'cache'] }); server.connection({ labels: ['s4', 'b', 'test', 'cache'] }); - server.register(internals.plugins.test1, function (err) { + server.register(internals.plugins.test1, (err) => { expect(err).to.not.exist(); @@ -2714,11 +2728,13 @@ describe('Plugin', () => { } }); - srv.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setUrl('/b'); return reply.continue(); - }); + }; + + srv.ext('onRequest', onRequest); return next(); }; @@ -2729,12 +2745,12 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); expect(internals.routesList(server)).to.deep.equal(['/b']); - server.inject('/a', function (res) { + server.inject('/a', (res) => { expect(res.result).to.equal('b'); done(); @@ -2758,24 +2774,24 @@ describe('Plugin', () => { server.select('1').route({ method: 'GET', path: '/', handler: handler }); server.select('2').route({ method: 'GET', path: '/', handler: handler }); - server.register([internals.plugins.deps1, internals.plugins.deps2, internals.plugins.deps3], function (err) { + server.register([internals.plugins.deps1, internals.plugins.deps2, internals.plugins.deps3], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(server.plugins.deps1.breaking).to.equal('bad'); - server.connections[0].inject('/', function (res1) { + server.connections[0].inject('/', (res1) => { expect(res1.result).to.equal('|2|1|'); - server.connections[1].inject('/', function (res2) { + server.connections[1].inject('/', (res2) => { expect(res2.result).to.equal('|3|1|'); - server.connections[2].inject('/', function (res3) { + server.connections[2].inject('/', (res3) => { expect(res3.result).to.equal('|3|2|'); done(); @@ -2794,12 +2810,14 @@ describe('Plugin', () => { const plugin = function (server, options, next) { - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.app.complexDeps = request.app.complexDeps || '|'; request.app.complexDeps += num + '|'; return reply.continue(); - }, deps); + }; + + server.ext('onRequest', onRequest, deps); next(); }; @@ -2825,15 +2843,15 @@ describe('Plugin', () => { pluginCurrier(1, { after: 'deps2' }), pluginCurrier(2), pluginCurrier(3, { before: ['deps1', 'deps2'] }) - ], function (err) { + ], (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('|3|2|1|'); done(); @@ -2845,7 +2863,7 @@ describe('Plugin', () => { it('throws when adding ext without connections', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.ext('onRequest', () => { }); }).to.throw('Cannot add ext without a connection'); @@ -2862,13 +2880,15 @@ describe('Plugin', () => { state: false }; - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { this.state = true; return next(); - }, { bind: bind }); + }; + + server.ext('onPreStart', preStart, { bind: bind }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(bind.state).to.be.true(); @@ -2886,13 +2906,15 @@ describe('Plugin', () => { }; server.bind(bind); - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { this.state = true; return next(); - }); + }; + + server.ext('onPreStart', preStart); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(bind.state).to.be.true(); @@ -2906,36 +2928,44 @@ describe('Plugin', () => { server.connection(); let result = ''; - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { result += '1'; return next(); - }); + }; + + server.ext('onPreStart', preStart); - server.ext('onPostStart', function (srv, next) { + const postStart = function (srv, next) { result += '2'; return next(); - }); + }; + + server.ext('onPostStart', postStart); - server.ext('onPreStop', function (srv, next) { + const preStop = function (srv, next) { result += '3'; return next(); - }); + }; + + server.ext('onPreStop', preStop); - server.ext('onPreStop', function (srv, next) { + const postStop = function (srv, next) { result += '4'; return next(); - }); + }; + + server.ext('onPostStop', postStop); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(result).to.equal('12'); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); expect(result).to.equal('1234'); @@ -2985,12 +3015,12 @@ describe('Plugin', () => { } ]); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); expect(result).to.equal('12'); - server.stop(function (err) { + server.stop((err) => { expect(err).to.not.exist(); expect(result).to.equal('1234'); @@ -3004,11 +3034,13 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreAuth', function (request, reply) { + const preAuth = function (request, reply) { request.app.x = '1'; return reply.continue(); - }); + }; + + server.ext('onPreAuth', preAuth); const plugin = function (srv, options, next) { @@ -3032,11 +3064,13 @@ describe('Plugin', () => { } }); - srv.ext('onPreAuth', function (request, reply) { + const preAuthSandbox = function (request, reply) { request.app.x += '3'; return reply.continue(); - }, { sandbox: 'plugin' }); + }; + + srv.ext('onPreAuth', preAuthSandbox, { sandbox: 'plugin' }); return next(); }; @@ -3045,7 +3079,7 @@ describe('Plugin', () => { name: 'test' }; - server.register(plugin, function (err) { + server.register(plugin, (err) => { expect(err).to.not.exist(); @@ -3060,11 +3094,11 @@ describe('Plugin', () => { } }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.result).to.equal('123'); - server.inject('/a', function (res2) { + server.inject('/a', (res2) => { expect(res2.result).to.equal('1'); done(); @@ -3091,17 +3125,19 @@ describe('Plugin', () => { expect(server.plugins.x).to.not.exist(); let called = false; - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { expect(srv.plugins.x.a).to.equal('b'); called = true; return next(); - }, { after: 'x' }); + }; + + server.ext('onPreStart', preStart, { after: 'x' }); - server.register(x, function (err) { + server.register(x, (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(called).to.be.true(); @@ -3116,13 +3152,15 @@ describe('Plugin', () => { server.connection(); let called = false; - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { called = true; return next(); - }); + }; + + server.ext('onPreStart', preStart); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(called).to.be.true(); @@ -3136,13 +3174,15 @@ describe('Plugin', () => { server.connection(); let called = false; - server.ext('onPreStart', function (srv, next) { + const preStart = function (srv, next) { called = true; return next(); - }, { after: 'x' }); + }; + + server.ext('onPreStart', preStart, { after: 'x' }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); expect(called).to.be.true(); @@ -3154,15 +3194,19 @@ describe('Plugin', () => { const test = function (srv, options, next) { - srv.ext('onPreStart', function (inner, finish) { + const preStart1 = function (inner, finish) { return finish(); - }); + }; + + srv.ext('onPreStart', preStart1); - srv.ext('onPreStart', function (inner, finish) { + const preStart2 = function (inner, finish) { return finish(new Error('Not in the mood')); - }); + }; + + srv.ext('onPreStart', preStart2); return next(); }; @@ -3173,10 +3217,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.exist(); done(); @@ -3189,9 +3233,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.initialize(function (err) { + server.initialize((err) => { - expect(function () { + expect(() => { server.ext('onPreStart', () => { }); }).to.throw('Cannot add onPreStart (after) extension after the server was initialized'); @@ -3207,13 +3251,15 @@ describe('Plugin', () => { const test = function (srv, options1, next) { - srv.handler('bar', function (route, options2) { + const handler = function (route, options2) { return function (request, reply) { return reply('success'); }; - }); + }; + + srv.handler('bar', handler); return next(); }; @@ -3224,7 +3270,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); server.route({ @@ -3235,7 +3281,7 @@ describe('Plugin', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('success'); done(); @@ -3249,7 +3295,7 @@ describe('Plugin', () => { server.register(Inert, Hoek.ignore); server.connection(); - expect(function () { + expect(() => { server.handler('file', () => { }); }).to.throw('Handler name already exists: file'); @@ -3261,7 +3307,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/', handler: { test: {} } }); }).to.throw('Unknown handler: test'); @@ -3273,7 +3319,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.handler(); }).to.throw('Invalid handler name'); @@ -3285,7 +3331,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.handler('foo', 'bar'); }).to.throw('Handler must be a function: foo'); @@ -3301,13 +3347,13 @@ describe('Plugin', () => { server.connection(); let count = 0; - server.once('log', function (event) { + server.once('log', (event) => { ++count; expect(event.data).to.equal('log event 1'); }); - server.once('log', function (event) { + server.once('log', (event) => { ++count; expect(event.data).to.equal('log event 1'); @@ -3315,7 +3361,7 @@ describe('Plugin', () => { server.log('1', 'log event 1', Date.now()); - server.once('log', function (event) { + server.once('log', (event) => { ++count; expect(event.data).to.equal('log event 2'); @@ -3332,7 +3378,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - server.once('log', function (event) { + server.once('log', (event) => { expect(event.data).to.equal('log event 1'); }); @@ -3466,7 +3512,7 @@ describe('Plugin', () => { let pc = 0; const test = function (srv, options, next) { - srv.on('log', function (event, tags) { + srv.on('log', (event, tags) => { ++pc; }); @@ -3482,12 +3528,12 @@ describe('Plugin', () => { server.connection(); let sc = 0; - server.on('log', function (event, tags) { + server.on('log', (event, tags) => { ++sc; }); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); server.log('test'); @@ -3536,7 +3582,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.lookup(); }).to.throw('Invalid route id: '); @@ -3626,7 +3672,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match(); }).to.throw('Invalid method: '); @@ -3637,7 +3683,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match(5); }).to.throw('Invalid method: 5'); @@ -3648,7 +3694,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match('get'); }).to.throw('Invalid path: '); @@ -3659,7 +3705,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match('get', 5); }).to.throw('Invalid path: 5'); @@ -3670,7 +3716,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match('get', '5'); }).to.throw('Invalid path: 5'); @@ -3692,7 +3738,7 @@ describe('Plugin', () => { } }); - expect(function () { + expect(() => { server.match('GET', '/%p'); }).to.throw('Invalid path: /%p'); @@ -3703,7 +3749,7 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.match('get', '/a', 5); }).to.throw('Invalid host: 5'); @@ -3720,10 +3766,12 @@ describe('Plugin', () => { const test = function (srv, options, next) { - srv.method('log', function (methodNext) { + const method = function (methodNext) { return methodNext(null); - }); + }; + + srv.method('log', method); return next(); }; @@ -3731,7 +3779,7 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); done(); @@ -3746,10 +3794,12 @@ describe('Plugin', () => { const test = function (srv, options, next) { srv.bind({ x: 1 }); - srv.method('log', function (methodNext) { + const method = function (methodNext) { return methodNext(null, this.x); - }); + }; + + srv.method('log', method); return next(); }; @@ -3757,10 +3807,10 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.methods.log(function (err, result) { + server.methods.log((err, result) => { expect(result).to.equal(1); done(); @@ -3775,10 +3825,12 @@ describe('Plugin', () => { const test = function (srv, options, next) { - srv.method('log', function (methodNext) { + const method = function (methodNext) { return methodNext(null, this.x); - }, { bind: { x: 2 } }); + }; + + srv.method('log', method, { bind: { x: 2 } }); return next(); }; @@ -3786,10 +3838,10 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.methods.log(function (err, result) { + server.methods.log((err, result) => { expect(result).to.equal(2); done(); @@ -3805,10 +3857,12 @@ describe('Plugin', () => { const test = function (srv, options, next) { srv.bind({ x: 1 }); - srv.method('log', function (methodNext) { + const method = function (methodNext) { return methodNext(null, this.x); - }, { bind: { x: 2 } }); + }; + + srv.method('log', method, { bind: { x: 2 } }); return next(); }; @@ -3816,10 +3870,10 @@ describe('Plugin', () => { name: 'test' }; - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.methods.log(function (err, result) { + server.methods.log((err, result) => { expect(result).to.equal(2); done(); @@ -3856,10 +3910,10 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.register(Inert, Hoek.ignore); server.connection({ routes: { files: { relativeTo: __dirname } } }); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.inject('/handler/package.json', function (res) { + server.inject('/handler/package.json', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -3881,9 +3935,9 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { - server.register(test, function (err) { }); + server.register(test, (err) => { }); }).to.throw('relativeTo must be a non-empty string'); done(); }); @@ -3901,7 +3955,7 @@ describe('Plugin', () => { path: __dirname + '/templates' }); - server.render('test', { title: 'test', message: 'Hapi' }, function (err, rendered, config) { + server.render('test', { title: 'test', message: 'Hapi' }, (err, rendered, config) => { expect(rendered).to.exist(); expect(rendered).to.contain('Hapi'); @@ -3915,7 +3969,7 @@ describe('Plugin', () => { it('throws when adding state without connections', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.state('sid', { encoding: 'base64' }); }).to.throw('Cannot add state without a connection'); @@ -3944,24 +3998,30 @@ describe('Plugin', () => { srv.route([ { - path: '/view', method: 'GET', handler: function (request, reply) { + path: '/view', + method: 'GET', + handler: function (request, reply) { return reply.view('test', { message: options.message }); } }, { - path: '/file', method: 'GET', handler: { file: './templates/plugin/test.html' } + path: '/file', + method: 'GET', + handler: { file: './templates/plugin/test.html' } } ]); - srv.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { if (request.path === '/ext') { return reply.view('test', { message: 'grabbed' }); } return reply.continue(); - }); + }; + + srv.ext('onRequest', onRequest); return next(); }; @@ -3973,18 +4033,18 @@ describe('Plugin', () => { const server = new Hapi.Server(); server.register([Inert, Vision], Hoek.ignore); server.connection(); - server.register({ register: test, options: { message: 'viewing it' } }, function (err) { + server.register({ register: test, options: { message: 'viewing it' } }, (err) => { expect(err).to.not.exist(); - server.inject('/view', function (res1) { + server.inject('/view', (res1) => { expect(res1.result).to.equal('

viewing it

'); - server.inject('/file', function (res2) { + server.inject('/file', (res2) => { expect(res2.result).to.equal('

{{message}}

'); - server.inject('/ext', function (res3) { + server.inject('/ext', (res3) => { expect(res3.result).to.equal('

grabbed

'); done(); @@ -4019,11 +4079,11 @@ internals.routesList = function (server, label) { internals.plugins = { auth: function (server, options, next) { - server.auth.scheme('basic', function (srv, authOptions) { + var scheme = function (srv, authOptions) { const settings = Hoek.clone(authOptions); - const scheme = { + return { authenticate: function (request, reply) { const req = request.raw.req; @@ -4052,7 +4112,7 @@ internals.plugins = { const username = credentialsParts[0]; const password = credentialsParts[1]; - settings.validateFunc(username, password, function (err, isValid, credentials) { + settings.validateFunc(username, password, (err, isValid, credentials) => { if (!isValid) { return reply(Boom.unauthorized('Bad username or password', 'Basic'), { credentials: credentials }); @@ -4062,9 +4122,9 @@ internals.plugins = { }); } }; + }; - return scheme; - }); + server.auth.scheme('basic', scheme); const loadUser = function (username, password, callback) { @@ -4096,20 +4156,24 @@ internals.plugins = { }, deps1: function (server, options, next) { - server.dependency('deps2', function (srv, nxt) { + const after = function (srv, nxt) { srv.expose('breaking', srv.plugins.deps2.breaking); return nxt(); - }); + }; + + server.dependency('deps2', after); const selection = server.select('a'); if (selection.connections.length) { - selection.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.app.deps = request.app.deps || '|'; request.app.deps += '1|'; return reply.continue(); - }, { after: 'deps3' }); + }; + + selection.ext('onRequest', onRequest, { after: 'deps3' }); } return next(); @@ -4118,12 +4182,14 @@ internals.plugins = { const selection = server.select('b'); if (selection.connections.length) { - selection.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.app.deps = request.app.deps || '|'; request.app.deps += '2|'; return reply.continue(); - }, { after: 'deps3', before: 'deps1' }); + }; + + selection.ext('onRequest', onRequest, { after: 'deps3', before: 'deps1' }); } server.expose('breaking', 'bad'); @@ -4134,12 +4200,14 @@ internals.plugins = { const selection = server.select('c'); if (selection.connections.length) { - selection.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.app.deps = request.app.deps || '|'; request.app.deps += '3|'; return reply.continue(); - }); + }; + + selection.ext('onRequest', onRequest); } return next(); @@ -4160,10 +4228,12 @@ internals.plugins = { } }); - server.expose('glue', function (a, b) { + const glue = function (a, b) { return a + b; - }); + }; + + server.expose('glue', glue); server.expose('prefix', server.realm.modifiers.route.prefix); diff --git a/test/protect.js b/test/protect.js index 20666eb04..b3d61e525 100755 --- a/test/protect.js +++ b/test/protect.js @@ -32,7 +32,7 @@ describe('Protect', () => { const handler = function (request, reply) { - process.nextTick(function () { + process.nextTick(() => { throw new Error('no domain'); }); @@ -40,15 +40,15 @@ describe('Protect', () => { server.route({ method: 'GET', path: '/', handler: handler }); const domain = Domain.createDomain(); - domain.once('error', function (err) { + domain.once('error', (err) => { expect(err.message).to.equal('no domain'); done(); }); - domain.run(function () { + domain.run(() => { - server.inject('/', function (res) { }); + server.inject('/', (res) => { }); }); }); @@ -60,14 +60,14 @@ describe('Protect', () => { const handler = function (request, reply) { reply('ok'); - process.nextTick(function () { + process.nextTick(() => { throw new Error('should not leave domain'); }); }; server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -83,19 +83,19 @@ describe('Protect', () => { reply('ok'); - process.nextTick(function () { + process.nextTick(() => { throw new Error('should not leave domain 1'); }); - process.nextTick(function () { + process.nextTick(() => { throw new Error('should not leave domain 2'); }); }; server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -113,19 +113,21 @@ describe('Protect', () => { const test = function (srv, options, next) { - srv.ext('onPreStart', function (plugin, afterNext) { + const preStart = function (plugin, afterNext) { const client = new Client(); // Created in the global domain plugin.bind({ client: client }); afterNext(); - }); + }; + + srv.ext('onPreStart', preStart); srv.route({ method: 'GET', path: '/', handler: function (request, reply) { - this.client.on('event', request.domain.bind(function () { + this.client.on('event', request.domain.bind(() => { throw new Error('boom'); // Caught by the global domain by default, not request domain })); @@ -143,14 +145,14 @@ describe('Protect', () => { const server = new Hapi.Server({ debug: false }); server.connection(); - server.register(test, function (err) { + server.register(test, (err) => { expect(err).to.not.exist(); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/', function (res) { + server.inject('/', (res) => { done(); }); @@ -163,7 +165,7 @@ describe('Protect', () => { const handler = function (request, reply) { reply('ok'); - setTimeout(function () { + setTimeout(() => { throw new Error('After done'); }, 10); @@ -172,14 +174,14 @@ describe('Protect', () => { const server = new Hapi.Server({ debug: false }); server.connection(); - server.on('log', function (event, tags) { + server.on('log', (event, tags) => { expect(tags.implementation).to.exist(); done(); }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); }); diff --git a/test/reply.js b/test/reply.js index fa3b7b20d..c147b3f1e 100755 --- a/test/reply.js +++ b/test/reply.js @@ -37,7 +37,7 @@ describe('Reply', () => { const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -54,7 +54,7 @@ describe('Reply', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); expect(res.headers.location).to.equal('/elsewhere'); @@ -74,7 +74,7 @@ describe('Reply', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('steve'); @@ -92,7 +92,7 @@ describe('Reply', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(400); done(); @@ -109,7 +109,7 @@ describe('Reply', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(400); done(); @@ -129,7 +129,7 @@ describe('Reply', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal(null); @@ -150,7 +150,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(299); expect(res.result).to.equal('Tada1'); @@ -170,7 +170,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('{\"a\":1,\"b\":2}'); expect(res.headers['content-length']).to.equal(13); @@ -189,7 +189,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('false'); done(); @@ -207,7 +207,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); expect(res.result).to.exist(); @@ -226,7 +226,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(299); expect(res.headers['content-length']).to.equal(0); @@ -265,13 +265,13 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); - server.inject('/stream', function (res1) { + server.inject('/stream', (res1) => { expect(res1.result).to.equal('xy'); expect(res1.statusCode).to.equal(200); expect(res1.headers['cache-control']).to.equal('max-age=2, must-revalidate'); - server.inject({ method: 'HEAD', url: '/stream' }, function (res2) { + server.inject({ method: 'HEAD', url: '/stream' }, (res2) => { expect(res2.result).to.equal(''); expect(res2.statusCode).to.equal(200); @@ -305,23 +305,23 @@ describe('Reply', () => { server.route({ method: 'GET', path: '/writable', handler: writableHandler }); let requestError; - server.on('request-error', function (request, err) { + server.on('request-error', (request, err) => { requestError = err; }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/stream', function (res1) { + server.inject('/stream', (res1) => { expect(res1.statusCode).to.equal(500); expect(requestError).to.exist(); expect(requestError.message).to.equal('Stream must have a streams2 readable interface'); requestError = undefined; - server.inject('/writable', function (res2) { + server.inject('/writable', (res2) => { expect(res2.statusCode).to.equal(500); expect(requestError).to.exist(); @@ -349,11 +349,11 @@ describe('Reply', () => { server.route({ method: 'GET', path: '/', handler: handler }); server.route({ method: 'GET', path: '/stream', handler: streamHandler }); - server.initialize(function (err) { + server.initialize((err) => { expect(err).to.not.exist(); - server.inject('/stream', function (res) { + server.inject('/stream', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -391,7 +391,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -432,7 +432,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/stream', config: { handler: handler, cache: { expiresIn: 9999 } } }); - server.inject('/stream', function (res) { + server.inject('/stream', (res) => { expect(res.result).to.equal('xy'); expect(res.statusCode).to.equal(299); @@ -451,7 +451,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(299); expect(res.result.toString()).to.equal('buffer content'); @@ -476,7 +476,7 @@ describe('Reply', () => { server.route({ method: 'GET', path: '/domain', handler: handler }); - server.inject('/domain', function (res) { + server.inject('/domain', (res) => { expect(res.result).to.equal('123'); done(); @@ -491,7 +491,7 @@ describe('Reply', () => { const handler = function (request, reply) { const response = reply('123').hold(); - setTimeout(function () { + setTimeout(() => { response.send(); }, 10); @@ -499,7 +499,7 @@ describe('Reply', () => { server.route({ method: 'GET', path: '/domain', handler: handler }); - server.inject('/domain', function (res) { + server.inject('/domain', (res) => { expect(res.result).to.equal('123'); done(); @@ -521,7 +521,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(''); done(); @@ -539,7 +539,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal(''); done(); @@ -560,7 +560,7 @@ describe('Reply', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal(null); @@ -609,7 +609,7 @@ describe('Reply', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.deep.equal({ diff --git a/test/request.js b/test/request.js index 706d51f79..941d30a82 100755 --- a/test/request.js +++ b/test/request.js @@ -49,7 +49,7 @@ describe('Request.Generator', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal(3); @@ -79,11 +79,11 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.get('http://localhost:' + server.info.port, function (err, res, body) { + Wreck.get('http://localhost:' + server.info.port, (err, res, body) => { expect(body.toString()).to.equal('ok'); server.stop(done); @@ -104,7 +104,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { referrer: 'http://site.com' } }, function (res) { + server.inject({ url: '/', headers: { referrer: 'http://site.com' } }, (res) => { expect(res.result).to.equal('ok'); done(); @@ -124,7 +124,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { referer: 'http://site.com' } }, function (res) { + server.inject({ url: '/', headers: { referer: 'http://site.com' } }, (res) => { expect(res.result).to.equal('ok'); done(); @@ -142,7 +142,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('shot'); done(); @@ -160,11 +160,11 @@ describe('Request', () => { server.connection(); server.connections[0]._requestCounter = { value: 10, min: 10, max: 11 }; server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { - server.inject('/', function (res2) { + server.inject('/', (res2) => { - server.inject('/', function (res3) { + server.inject('/', (res3) => { expect(res1.result).to.match(/10$/); expect(res2.result).to.match(/11$/); @@ -181,7 +181,7 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.inject('invalid', function (res) { + server.inject('invalid', (res) => { expect(res.statusCode).to.equal(400); done(); @@ -206,7 +206,7 @@ describe('Request', () => { server.ext('onPostHandler', ext); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.statusCode).to.equal(400); done(); @@ -244,7 +244,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); let disconnected = 0; - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.once('disconnect', () => { @@ -252,9 +252,11 @@ describe('Request', () => { }); return reply.continue(); - }); + }; - server.start(function (err) { + server.ext('onRequest', onRequest); + + server.start((err) => { expect(err).to.not.exist(); @@ -301,7 +303,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.deep.equal({}); done(); @@ -312,12 +314,14 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply(request.params); - }); + }; + + server.ext('onPreResponse', preResponse); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.deep.equal({}); done(); @@ -332,10 +336,10 @@ describe('Request', () => { clientRequest.abort(); - setTimeout(function () { + setTimeout(() => { reply(new Error('fail')); - setTimeout(function () { + setTimeout(() => { server.stop(done); }, 10); @@ -346,7 +350,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -368,21 +372,22 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: Hoek.ignore }); let clientRequest; - - server.ext('onPreHandler', function (request, reply) { + const preHandler = function (request, reply) { clientRequest.abort(); - setTimeout(function () { + setTimeout(() => { reply.continue(); - setTimeout(function () { + setTimeout(() => { server.stop(done); }, 10); }, 10); - }); + }; - server.start(function (err) { + server.ext('onPreHandler', preHandler); + + server.start((err) => { expect(err).to.not.exist(); @@ -404,7 +409,7 @@ describe('Request', () => { const handler = function (request, reply) { clientRequest.abort(); - setTimeout(function () { + setTimeout(() => { return reply(new Error('boom')); }, 10); @@ -414,17 +419,19 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply.continue(); - }); + }; + + server.ext('onPreResponse', preResponse); server.on('tail', () => { server.stop(done); }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -455,10 +462,10 @@ describe('Request', () => { } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.get('http://localhost:' + server.info.port, function (err, res, body) { + Wreck.get('http://localhost:' + server.info.port, (err, res, body) => { expect(res.statusCode).to.equal(404); expect(body.toString()).to.equal('{"statusCode":404,"error":"Not Found"}'); @@ -483,7 +490,7 @@ describe('Request', () => { } }); - server.inject('/some/route', function (res) { + server.inject('/some/route', (res) => { expect(res.statusCode).to.equal(404); done(); @@ -506,7 +513,7 @@ describe('Request', () => { } }); - server.inject({ url: '/some/route', allowInternals: true }, function (res) { + server.inject({ url: '/some/route', allowInternals: true }, (res) => { expect(res.statusCode).to.equal(200); done(); @@ -527,20 +534,20 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.once('response', function (request) { + server.once('response', (request) => { expect(request.info.responded).to.be.min(request.info.received); done(); }); - server.inject('/', function (res) { }); + server.inject('/', (res) => { }); }); it('closes response after server timeout', (done) => { const handler = function (request, reply) { - setTimeout(function () { + setTimeout(() => { const stream = new Stream.Readable(); stream._read = function (size) { @@ -566,7 +573,7 @@ describe('Request', () => { handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(503); }); @@ -576,7 +583,7 @@ describe('Request', () => { const handler = function (request, reply) { - setTimeout(function () { + setTimeout(() => { return reply(new Error('after')); }, 10); @@ -590,7 +597,7 @@ describe('Request', () => { handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(503); done(); @@ -604,7 +611,7 @@ describe('Request', () => { let errs = 0; let req = null; - server.on('request-error', function (request, err) { + server.on('request-error', (request, err) => { errs++; expect(err).to.exist(); @@ -612,10 +619,12 @@ describe('Request', () => { req = request; }); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply(new Error('boom2')); - }); + }; + + server.ext('onPreResponse', preResponse); const handler = function (request, reply) { @@ -624,7 +633,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); expect(res.result).to.exist(); @@ -646,7 +655,7 @@ describe('Request', () => { let errs = 0; let req = null; - server.on('request-error', function (request, err) { + server.on('request-error', (request, err) => { ++errs; expect(err).to.exist(); @@ -668,7 +677,7 @@ describe('Request', () => { done(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); expect(res.result).to.exist(); @@ -682,15 +691,17 @@ describe('Request', () => { server.connection(); let errs = 0; - server.on('request-error', function (request, err) { + server.on('request-error', (request, err) => { errs++; }); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply('ok'); - }); + }; + + server.ext('onPreResponse', preResponse); const handler = function (request, reply) { @@ -699,7 +710,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.equal('ok'); @@ -741,7 +752,7 @@ describe('Request', () => { done(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { result = res.result; }); @@ -765,7 +776,7 @@ describe('Request', () => { done(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { }); }); @@ -779,13 +790,15 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setMethod('POST'); return reply(request.method); - }); + }; - server.inject('/', function (res) { + server.ext('onRequest', onRequest); + + server.inject('/', (res) => { expect(res.payload).to.equal('post'); done(); @@ -798,12 +811,14 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setMethod(); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -816,12 +831,14 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setMethod(42); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -842,7 +859,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/?a[b]=5&d[ff]=ok', function (res) { + server.inject('/?a[b]=5&d[ff]=ok', (res) => { expect(res.result).to.deep.equal({ a: { b: '5' }, d: { ff: 'ok' } }); done(); @@ -856,13 +873,15 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setUrl(url); return reply([request.url.href, request.path, request.query.param1].join('|')); - }); + }; - server.inject('/', function (res) { + server.ext('onRequest', onRequest); + + server.inject('/', (res) => { expect(res.payload).to.equal(url + '|/page|something'); done(); @@ -880,13 +899,15 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: function (request, reply) { } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setUrl(url); return reply([request.url.href, request.path, request.query.param1].join('|')); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal(url + '|' + normPath + '|something'); done(); @@ -897,13 +918,15 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setUrl(''); return reply.continue(); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(400); done(); @@ -920,7 +943,7 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/test', handler: handler }); - server.inject('/test/', function (res) { + server.inject('/test/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -937,7 +960,7 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -954,7 +977,7 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); server.route({ method: 'GET', path: '/test', handler: handler }); - server.inject('/test/?a=b', function (res) { + server.inject('/test/?a=b', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -969,13 +992,15 @@ describe('Request', () => { }; const server = new Hapi.Server(); server.connection(); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { request.setUrl(url, null, qsParserOptions); return reply(request.query); - }); + }; - server.inject('/', function (res) { + server.ext('onRequest', onRequest); + + server.inject('/', (res) => { expect(res.result).to.deep.equal({ a: '1', b: '1', c: '1', d: '1', e: '1', f: '1', g: '1', h: '1', i: '1', @@ -1008,7 +1033,7 @@ describe('Request', () => { } }); - server.inject('/?a[0]=b&a[1]=c', function (res) { + server.inject('/?a[0]=b&a[1]=c', (res) => { expect(res.result).to.deep.equal({ a: { 0: 'b', 1: 'c' } }); done(); @@ -1040,7 +1065,7 @@ describe('Request', () => { done(); }; - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); }); @@ -1050,7 +1075,7 @@ describe('Request', () => { const handler = function (request, reply) { - server.on('request', function (req, event, tags) { + server.on('request', (req, event, tags) => { expect(event).to.contain(['request', 'timestamp', 'tags', 'data', 'internal']); expect(event.data).to.equal('data'); @@ -1066,7 +1091,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1095,7 +1120,7 @@ describe('Request', () => { done(); }; - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); }); @@ -1123,7 +1148,7 @@ describe('Request', () => { done(); }; - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); }); @@ -1154,7 +1179,7 @@ describe('Request', () => { done(); }; - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); }); @@ -1179,7 +1204,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('2|4|4|0|7|true'); done(); @@ -1206,7 +1231,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { console.error('nothing'); expect(i).to.equal(1); @@ -1235,7 +1260,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { console.error('nothing'); expect(i).to.equal(1); @@ -1264,7 +1289,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { console.error('nothing'); expect(i).to.equal(1); @@ -1280,13 +1305,13 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.once('request-internal', function (request, event, tags) { + server.once('request-internal', (request, event, tags) => { expect(tags.received).to.be.true(); done(); }); - server.inject('/', function (res) { }); + server.inject('/', (res) => { }); }); }); @@ -1306,7 +1331,7 @@ describe('Request', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('2|1|1|2|1|3'); done(); @@ -1320,10 +1345,12 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPostHandler', function (request, reply) { + const postHandler = function (request, reply) { return reply(request.response); - }); + }; + + server.ext('onPostHandler', postHandler); const handler = function (request, reply) { @@ -1339,7 +1366,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('value'); done(); @@ -1350,10 +1377,12 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection(); - server.ext('onPostHandler', function (request, reply) { + const postHandler = function (request, reply) { return reply(request.response.source); - }); + }; + + server.ext('onPostHandler', postHandler); const handler = function (request, reply) { @@ -1369,7 +1398,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('value'); done(); @@ -1389,7 +1418,7 @@ describe('Request', () => { const timer = new Hoek.Bench(); - server.inject('/timeout', function (res) { + server.inject('/timeout', (res) => { expect(res.statusCode).to.equal(503); expect(timer.elapsed()).to.be.at.least(45); @@ -1401,7 +1430,7 @@ describe('Request', () => { const handler = function (request, reply) { - setTimeout(function () { + setTimeout(() => { return reply(); }, 20); @@ -1411,12 +1440,14 @@ describe('Request', () => { server.connection({ routes: { timeout: { server: 10 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.ext('onPostHandler', function (request, reply) { + const postHandler = function (request, reply) { return reply.continue(); - }); + }; + + server.ext('onPostHandler', postHandler); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(503); done(); @@ -1428,15 +1459,17 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 2 } } }); server.route({ method: 'GET', path: '/', config: { handler: function () { } } }); - server.ext('onRequest', function (request, reply) { + const onRequest = function (request, reply) { - setTimeout(function () { + setTimeout(() => { return reply.continue(); }, 10); - }); + }; + + server.ext('onRequest', onRequest); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(503); done(); @@ -1453,12 +1486,14 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 10 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.ext('onPreResponse', function (request, reply) { + const preResponse = function (request, reply) { return reply.continue(); - }); + }; + + server.ext('onPreResponse', preResponse); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(503); done(); @@ -1469,7 +1504,7 @@ describe('Request', () => { const slowHandler = function (request, reply) { - setTimeout(function () { + setTimeout(() => { return reply('Slow'); }, 30); @@ -1480,7 +1515,7 @@ describe('Request', () => { server.route({ method: 'GET', path: '/slow', config: { handler: slowHandler } }); const timer = new Hoek.Bench(); - server.inject('/slow', function (res) { + server.inject('/slow', (res) => { expect(timer.elapsed()).to.be.at.least(20); expect(res.statusCode).to.equal(200); @@ -1511,7 +1546,7 @@ describe('Request', () => { self.push('Hello'); - setTimeout(function () { + setTimeout(() => { self.push(null); ended = true; @@ -1526,10 +1561,10 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 100 } } }); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); - Wreck.get(server.info.uri, {}, function (err, res, payload) { + Wreck.get(server.info.uri, {}, (err, res, payload) => { expect(ended).to.be.true(); expect(timer.elapsed()).to.be.at.least(150); @@ -1559,12 +1594,12 @@ describe('Request', () => { } this.isDone = true; - setTimeout(function () { + setTimeout(() => { self.push('Hello'); }, 30); - setTimeout(function () { + setTimeout(() => { self.push(null); }, 60); @@ -1576,7 +1611,7 @@ describe('Request', () => { const server = new Hapi.Server(); server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/stream', config: { handler: streamHandler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -1587,7 +1622,7 @@ describe('Request', () => { method: 'GET' }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect(res.statusCode).to.equal(200); server.stop({ timeout: 1 }, done); @@ -1607,7 +1642,7 @@ describe('Request', () => { server.connection({ routes: { timeout: { server: 50 } } }); server.route({ method: 'GET', path: '/fast', config: { handler: fastHandler } }); - server.inject('/fast', function (res) { + server.inject('/fast', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -1622,7 +1657,7 @@ describe('Request', () => { server.connection({ routes: { timeout: { server: 50 }, payload: { timeout: 50 } } }); server.route({ method: 'POST', path: '/timeout', config: { handler: timeoutHandler } }); - server.start(function (err) { + server.start((err) => { expect(err).to.not.exist(); @@ -1634,19 +1669,19 @@ describe('Request', () => { method: 'POST' }; - const req = Http.request(options, function (res) { + const req = Http.request(options, (res) => { expect([503, 408]).to.contain(res.statusCode); expect(timer.elapsed()).to.be.at.least(45); server.stop({ timeout: 1 }, done); }); - req.on('error', function (err) { + req.on('error', (err) => { }); req.write('\n'); - setTimeout(function () { + setTimeout(() => { req.end(); }, 100); diff --git a/test/response.js b/test/response.js index eceeb0c89..a89ad463e 100755 --- a/test/response.js +++ b/test/response.js @@ -61,7 +61,7 @@ describe('Response', () => { return reply.continue(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.result).to.exist(); @@ -90,7 +90,7 @@ describe('Response', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['content-length']).to.equal(0); @@ -114,7 +114,7 @@ describe('Response', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['content-length']).to.equal(0); @@ -137,7 +137,7 @@ describe('Response', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['content-length']).to.equal(0); @@ -160,7 +160,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['set-cookie']).to.deep.equal(['A', 'B']); @@ -178,7 +178,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers['set-cookie']).to.not.exist(); @@ -204,7 +204,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(thrown).to.equal(true); done(); @@ -230,7 +230,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(thrown).to.equal(true); done(); @@ -255,7 +255,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(thrown).to.equal(true); done(); @@ -276,7 +276,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'POST', path: '/', handler: handler }); - server.inject({ method: 'POST', url: '/' }, function (res) { + server.inject({ method: 'POST', url: '/' }, (res) => { expect(res.result).to.deep.equal({ a: 1 }); expect(res.statusCode).to.equal(201); @@ -297,7 +297,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -318,7 +318,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.exist(); expect(res.statusCode).to.equal(500); @@ -364,7 +364,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -384,7 +384,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -404,7 +404,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -424,7 +424,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -444,7 +444,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -464,7 +464,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); expect(res.statusCode).to.equal(200); @@ -486,7 +486,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers.etag).to.equal('"abc"'); @@ -504,7 +504,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers.etag).to.equal('W/"abc"'); @@ -523,7 +523,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); expect(res.headers.etag).to.not.exist(); @@ -541,12 +541,12 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.statusCode).to.equal(200); expect(res1.headers.etag).to.equal('"abc"'); - server.inject({ url: '/', headers: { 'if-none-match': '"abc-gzip"', 'accept-encoding': 'gzip' } }, function (res2) { + server.inject({ url: '/', headers: { 'if-none-match': '"abc-gzip"', 'accept-encoding': 'gzip' } }, (res2) => { expect(res2.statusCode).to.equal(200); expect(res2.headers.etag).to.equal('"abc"'); @@ -567,7 +567,7 @@ describe('Response', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject({ url: '/', headers: { 'if-modified-since': mdate, 'accept-encoding': 'gzip' } }, function (res) { + server.inject({ url: '/', headers: { 'if-modified-since': mdate, 'accept-encoding': 'gzip' } }, (res) => { expect(res.statusCode).to.equal(304); expect(res.headers.etag).to.equal('"abc-gzip"'); @@ -609,7 +609,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('x'); expect(res.statusCode).to.equal(299); @@ -649,7 +649,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('x'); expect(res.statusCode).to.equal(200); @@ -689,7 +689,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('x'); expect(res.statusCode).to.equal(299); @@ -728,7 +728,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('x'); expect(res.headers.xcustom).to.equal('other value'); @@ -750,7 +750,7 @@ describe('Response', () => { const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -770,7 +770,7 @@ describe('Response', () => { const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -790,7 +790,7 @@ describe('Response', () => { const server = new Hapi.Server({ debug: false }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -812,7 +812,7 @@ describe('Response', () => { server.route({ method: 'GET', path: '/file', handler: handler }); - server.inject('/file', function (res) { + server.inject('/file', (res) => { expect(res.headers['content-type']).to.equal('application/example'); done(); @@ -833,7 +833,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('http://example.org/', function (res) { + server.inject('http://example.org/', (res) => { expect(res.result).to.exist(); expect(res.headers.location).to.equal('/example'); @@ -853,7 +853,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.exist(); expect(res.result).to.equal('We moved!'); @@ -874,7 +874,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(301); done(); @@ -892,7 +892,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); done(); @@ -910,7 +910,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(307); done(); @@ -928,7 +928,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(308); done(); @@ -946,7 +946,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(301); done(); @@ -964,7 +964,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); done(); @@ -982,7 +982,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(307); done(); @@ -1000,7 +1000,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(308); done(); @@ -1018,7 +1018,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(302); done(); @@ -1039,7 +1039,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('promised response'); expect(res.statusCode).to.equal(201); @@ -1058,7 +1058,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.status).to.equal('ok'); expect(res.statusCode).to.equal(201); @@ -1077,7 +1077,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.status).to.equal('ok'); expect(res.statusCode).to.equal(201); @@ -1099,7 +1099,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result.message).to.equal('this is not allowed!'); expect(res.statusCode).to.equal(403); @@ -1130,7 +1130,7 @@ describe('Response', () => { server.route({ method: 'GET', path: '/{param}', handler: { view: 'noview' } }); - server.inject('/hello', function (res) { + server.inject('/hello', (res) => { expect(res.statusCode).to.equal(500); expect(res.result).to.exist(); @@ -1152,7 +1152,7 @@ describe('Response', () => { server.connection({ routes: { json: { replacer: ['a'], space: 4, suffix: '\n' } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('{\n \"a\": 1\n}\n'); done(); @@ -1170,7 +1170,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('{\n \"a\": 1\n}\n'); expect(res.headers['content-type']).to.equal('application/x-test'); @@ -1189,7 +1189,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.payload).to.equal('{\n \"a\": 1\n}\n'); expect(res.headers['content-type']).to.equal('application/x-test'); @@ -1210,7 +1210,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(500); done(); @@ -1245,7 +1245,7 @@ describe('Response', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(output).to.equal('1234567890!'); done(); @@ -1272,7 +1272,7 @@ describe('Response', () => { server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(closed).to.be.true(); done(); diff --git a/test/route.js b/test/route.js index 98c12f389..5bd22c23d 100755 --- a/test/route.js +++ b/test/route.js @@ -27,7 +27,7 @@ describe('Route', () => { it('throws an error when a route is missing a path', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.connection(); @@ -38,7 +38,7 @@ describe('Route', () => { it('throws an error when a route is made without a connection', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.route({ method: 'GET', path: '/dork', handler: function () { } }); @@ -48,7 +48,7 @@ describe('Route', () => { it('throws an error when a route is missing a method', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.connection(); @@ -59,7 +59,7 @@ describe('Route', () => { it('throws an error when a route has a malformed method name', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.connection(); @@ -70,7 +70,7 @@ describe('Route', () => { it('throws an error when a route uses the HEAD method', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.connection(); @@ -81,7 +81,7 @@ describe('Route', () => { it('throws an error when a route is missing a handler', (done) => { - expect(function () { + expect(() => { const server = new Hapi.Server(); server.connection(); @@ -94,7 +94,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/', config: {} }); }).to.throw('Missing or undefined handler: GET /'); @@ -105,7 +105,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/test/', handler: function () { } }); }).to.throw('Path cannot end with a trailing slash when connection configured to strip: GET /test/'); @@ -116,7 +116,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection({ router: { stripTrailingSlash: true } }); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/', handler: function () { } }); }).to.not.throw(); @@ -133,7 +133,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: 'GET', path: '/', config: { handler: handler, app: { x: 'o' }, plugins: { x: { y: 'k' } } } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('ok'); done(); @@ -144,7 +144,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: 'POST', path: '/', handler: function () { }, config: { validate: { payload: {} }, payload: { parse: false } } }); }).to.throw('Route payload must be set to \'parse\' when payload validation enabled: POST /'); @@ -155,7 +155,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/', handler: function () { }, config: { validate: { payload: {} } } }); }).to.throw('Cannot validate HEAD or GET requests: /'); @@ -166,7 +166,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); - expect(function () { + expect(() => { server.route({ method: 'GET', path: '/', handler: function () { }, config: { payload: { parse: true } } }); }).to.throw('Cannot set payload settings on HEAD or GET request: /'); @@ -183,7 +183,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection(); server.route({ method: '*', path: '/', handler: handler, config: { validate: { payload: { a: Joi.required() } } } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -200,7 +200,7 @@ describe('Route', () => { const server = new Hapi.Server(); server.connection({ routes: { validate: { payload: { a: Joi.required() } } } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.statusCode).to.equal(200); done(); @@ -229,7 +229,7 @@ describe('Route', () => { }; server.route({ method: 'GET', path: '/', handler: handler, config: { bind: context } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('is true'); expect(count).to.equal(0); @@ -260,7 +260,7 @@ describe('Route', () => { server.bind(context); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('is true'); expect(count).to.equal(0); @@ -290,7 +290,7 @@ describe('Route', () => { server.connection({ routes: { bind: context } }); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('is true'); expect(count).to.equal(0); @@ -320,7 +320,7 @@ describe('Route', () => { const server = new Hapi.Server({ connections: { routes: { bind: context } } }); server.connection(); server.route({ method: 'GET', path: '/', handler: handler }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('is true'); expect(count).to.equal(0); @@ -340,7 +340,7 @@ describe('Route', () => { server.route({ method: 'GET', path: '/file', handler: handler, config: { files: { relativeTo: __dirname } } }); - server.inject('/file', function (res) { + server.inject('/file', (res) => { expect(res.payload).to.contain('hapi'); done(); @@ -350,7 +350,7 @@ describe('Route', () => { it('throws when server timeout is more then socket timeout', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ routes: { timeout: { server: 60000, socket: 12000 } } }); }).to.throw('Server timeout must be shorter than socket timeout: /{p*}'); @@ -360,7 +360,7 @@ describe('Route', () => { it('throws when server timeout is more then socket timeout (node default)', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ routes: { timeout: { server: 6000000 } } }); }).to.throw('Server timeout must be shorter than socket timeout: /{p*}'); @@ -370,7 +370,7 @@ describe('Route', () => { it('ignores large server timeout when socket timeout disabled', (done) => { const server = new Hapi.Server(); - expect(function () { + expect(() => { server.connection({ routes: { timeout: { server: 6000000, socket: false } } }); }).to.not.throw(); @@ -397,7 +397,7 @@ describe('Route', () => { } }); - server.inject({ method: 'POST', url: '/', payload: 'a[0]=b&a[1]=c', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, function (res) { + server.inject({ method: 'POST', url: '/', payload: 'a[0]=b&a[1]=c', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, (res) => { expect(res.result).to.deep.equal({ a: { 0: 'b', 1: 'c' } }); done(); @@ -455,7 +455,7 @@ describe('Route', () => { } }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('123456'); done(); @@ -512,7 +512,7 @@ describe('Route', () => { return reply.continue(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('123456'); done(); @@ -568,7 +568,7 @@ describe('Route', () => { return reply.continue(); }); - server.inject('/', function (res) { + server.inject('/', (res) => { expect(res.result).to.equal('123456'); done(); @@ -623,11 +623,11 @@ describe('Route', () => { } }); - server.inject('/', function (res1) { + server.inject('/', (res1) => { expect(res1.result).to.equal('123'); - server.inject('/a', function (res2) { + server.inject('/a', (res2) => { expect(res2.result).to.equal('13'); done(); diff --git a/test/security.js b/test/security.js index 559e4d69c..9314074c6 100755 --- a/test/security.js +++ b/test/security.js @@ -39,7 +39,7 @@ describe('security', () => { method: 'POST', url: '/item', payload: '{"name": "foobar\r\nContent-Length: \r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 19\r\n\r\nShazam"}', headers: { 'Content-Type': 'application/json' } - }, function (res) { + }, (res) => { expect(res.statusCode).to.equal(400); done(); @@ -64,7 +64,7 @@ describe('security', () => { payload: '{"something":"something"}', headers: { 'content-type': ';' } }, - function (res) { + (res) => { expect(res.result.message).to.not.contain('script'); done(); @@ -89,7 +89,7 @@ describe('security', () => { payload: '{"something":"something"}', headers: { cookie: 'encoded="";' } }, - function (res) { + (res) => { expect(res.result.message).to.not.contain('=value;' } }, - function (res) { + (res) => { expect(res.result.message).to.not.contain('":"other"}', headers: { 'content-type': 'application/json' } }, - function (res) { + (res) => { expect(res.result.message).to.not.contain('=value' }, - function (res) { + (res) => { expect(res.result.message).to.not.contain(';' } - }, - (res) => { - - expect(res.result.message).to.not.contain('script'); - done(); }); + + expect(res.result.message).to.not.contain('script'); }); - it('prevents xss with invalid cookie values in the request', (done) => { + it('prevents xss with invalid cookie values in the request', async () => { const handler = function (request, reply) { @@ -80,20 +73,17 @@ describe('security', () => { server.state('encoded', { encoding: 'iron' }); server.route({ method: 'POST', path: '/', handler }); - server.inject({ + const res = await server.inject({ method: 'POST', url: '/', payload: '{"something":"something"}', headers: { cookie: 'encoded="";' } - }, - (res) => { - - expect(res.result.message).to.not.contain('=value;' } - }, - (res) => { - - expect(res.result.message).to.not.contain('":"other"}', headers: { 'content-type': 'application/json' } - }, - (res) => { - - expect(res.result.message).to.not.contain('=value' - }, - (res) => { - - expect(res.result.message).to.not.contain('":"other"}', - headers: { 'content-type': 'application/json' } - }); - - expect(res.result.message).to.not.contain('=value' - }); - - expect(res.result.message).to.not.contain('