From c459788dda7c48c77c22b88030b49f7aab5c80b9 Mon Sep 17 00:00:00 2001 From: Christopher De Cairos Date: Wed, 25 May 2016 22:21:21 -0400 Subject: [PATCH 1/2] 1. Update dependencies 2. Fix breaks assiciated with dependency upgrades 3. Fix code style --- .travis.yml | 7 +-- examples/batch.js | 14 +++--- lib/batch.js | 122 ++++++++++++++++++++++++++-------------------- lib/index.js | 10 ++-- package.json | 19 +++----- test/batch.js | 13 +++-- 6 files changed, 100 insertions(+), 85 deletions(-) diff --git a/.travis.yml b/.travis.yml index f502178..1a51bc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: node_js +sudo: false + node_js: - - 0.10 - - 0.12 - - iojs + - 4 + - 6 diff --git a/examples/batch.js b/examples/batch.js index 26730da..ea3d372 100755 --- a/examples/batch.js +++ b/examples/batch.js @@ -1,11 +1,12 @@ +'use strict'; // Load modules -var Hapi = require('hapi'); +const Hapi = require('hapi'); // Declare internals -var internals = {}; +const internals = {}; /** @@ -53,7 +54,7 @@ internals.requestBatch = function (request, reply) { method: 'POST', url: '/batch', payload: '{ "requests": [{ "method": "get", "path": "/profile" }, { "method": "get", "path": "/item" }, { "method": "get", "path": "/item/$1.id" }] }' - }, function (res) { + }, (res) => { reply(res.result); }); @@ -72,16 +73,13 @@ internals.main = function () { { method: 'GET', path: '/request', handler: internals.requestBatch } ]); - internals.http.register(require('../'), function (err) { + internals.http.register(require('../'), (err) => { if (err) { console.log(err); } else { - internals.http.start(function () { - - console.log('Server started.'); - }); + internals.http.start(() => console.log('Server started.')); } }); }; diff --git a/lib/batch.js b/lib/batch.js index f713f89..c9e07b0 100644 --- a/lib/batch.js +++ b/lib/batch.js @@ -1,36 +1,44 @@ +'use strict'; + // Load modules -var Url = require('url'); -var Async = require('async'); -var Boom = require('boom'); -var Traverse = require('traverse'); -var Hoek = require('hoek'); -var Joi = require('joi'); +const Url = require('url'); +const Async = require('async'); +const Boom = require('boom'); +const Traverse = require('traverse'); +const Hoek = require('hoek'); +const Joi = require('joi'); // Declare internals -var internals = {}; +const internals = {}; module.exports.config = function (settings) { return { handler: function (request, reply) { - var resultsData = { + const resultsData = { results: [], resultsMap: [] }; - var requests = []; - var requestRegex = /(?:\/)(?:\$(\d)+\.)?([^\/\$]*)/g; // /project/$1.project/tasks, does not allow using array responses + const requests = []; + const requestRegex = /(?:\/)(?:\$(\d)+\.)?([^\/\$]*)/g; // /project/$1.project/tasks, does not allow using array responses - var payloads = []; - var payloadRegex = /^\$(\d+)(?:\.([^\s\$]*))?/; + const payloads = []; + const payloadRegex = /^\$(\d+)(?:\.([^\s\$]*))?/; // Validate requests - var errorMessage = null; - var parseRequest = function ($0, $1, $2) { + let errorMessage = null; + let i = 0; + let requestParts; + let result; + + const il = request.payload.requests.length; + + const parseRequest = function ($0, $1, $2) { if ($1) { if ($1 < i) { @@ -46,12 +54,12 @@ module.exports.config = function (settings) { return ''; }; - for (var i = 0, il = request.payload.requests.length; i < il; ++i) { + for ( ; i < il; ++i) { // Break into parts - var requestParts = []; - var result = request.payload.requests[i].path.replace(requestRegex, parseRequest); + requestParts = []; + result = request.payload.requests[i].path.replace(requestRegex, parseRequest); // Make sure entire string was processed (empty) @@ -63,8 +71,8 @@ module.exports.config = function (settings) { break; } - var payload = request.payload.requests[i].payload; - var payloadParts = internals.parsePayload(payloadRegex, payload); + const payload = request.payload.requests[i].payload; + const payloadParts = internals.parsePayload(payloadRegex, payload); payloads.push(payloadParts || []); } @@ -95,20 +103,23 @@ module.exports.config = function (settings) { internals.process = function (request, requests, payloads, resultsData, reply) { - var fnsParallel = []; - var fnsSerial = []; + const fnsParallel = []; + const fnsSerial = []; - var callBatch = function (pos, requestParts, payloadParts) { + const callBatch = function (pos, requestParts, payloadParts) { - return function (callback) { + return (callback) => { internals.batch(request, resultsData, pos, requestParts, payloadParts, callback); }; }; - for (var i = 0, il = requests.length; i < il; ++i) { - var requestParts = requests[i]; - var payloadParts = payloads[i]; + let i = 0; + const il = requests.length; + + for ( ; i < il; ++i) { + const requestParts = requests[i]; + const payloadParts = payloads[i]; if (internals.hasRefPart(requestParts) || payloadParts.length) { fnsSerial.push(callBatch(i, requestParts, payloadParts)); @@ -119,15 +130,15 @@ internals.process = function (request, requests, payloads, resultsData, reply) { } Async.series([ - function (callback) { + (callback) => { Async.parallel(fnsParallel, callback); }, - function (callback) { + (callback) => { Async.series(fnsSerial, callback); } - ], function (err) { + ], (err) => { if (err) { reply(err); @@ -141,7 +152,9 @@ internals.process = function (request, requests, payloads, resultsData, reply) { internals.hasRefPart = function (parts) { - for (var i = 0, il = parts.length; i < il; ++i) { + let i = 0; + const il = parts.length; + for ( ; i < il; ++i) { if (parts[i].type === 'ref') { return true; } @@ -152,17 +165,19 @@ internals.hasRefPart = function (parts) { internals.buildPath = function (resultsData, pos, parts) { - var path = ''; - var error = null; + let path = ''; + let error = null; + let i = 0; + const il = parts.length; - for (var i = 0, il = parts.length; i < il; ++i) { + for ( ; i < il; ++i) { path += '/'; if (parts[i].type === 'ref') { - var ref = resultsData.resultsMap[parts[i].index]; + const ref = resultsData.resultsMap[parts[i].index]; if (ref) { - var value = Hoek.reach(ref, parts[i].value); + const value = Hoek.reach(ref, parts[i].value); if (value !== null && value !== undefined) { @@ -194,7 +209,7 @@ internals.buildPath = function (resultsData, pos, parts) { internals.parsePayload = function (re, obj) { - var payloadParts = []; + const payloadParts = []; if (!obj) { return null; @@ -203,7 +218,7 @@ internals.parsePayload = function (re, obj) { Traverse(obj).forEach(function (value) { if (typeof value === 'string') { - var match = value.match(re); + const match = value.match(re); if (match) { payloadParts.push({ path: this.path, @@ -219,10 +234,10 @@ internals.parsePayload = function (re, obj) { internals.evalResults = function (results, index, path) { - var result = results[index]; + let result = results[index]; if (path) { - result = Hoek.reach(result, path); + result = Hoek.reach(result, path) || {}; } return result; @@ -230,13 +245,16 @@ internals.evalResults = function (results, index, path) { internals.buildPayload = function (payload, resultsData, parts) { - for (var i = 0, il = parts.length; i < il; ++i) { + let i = 0; + const il = parts.length; + for ( ; i < il; ++i) { - var result = internals.evalResults(resultsData.resultsMap, parts[i].resultIndex, parts[i].resultPath); + const result = internals.evalResults(resultsData.resultsMap, parts[i].resultIndex, parts[i].resultPath); if (parts[i].path.length) { Traverse(payload).set(parts[i].path, result); - } else { + } + else { payload = result; } } @@ -246,7 +264,7 @@ internals.buildPayload = function (payload, resultsData, parts) { internals.batch = function (batchRequest, resultsData, pos, requestParts, payloadParts, callback) { - var path = internals.buildPath(resultsData, pos, requestParts); + const path = internals.buildPath(resultsData, pos, requestParts); if (path instanceof Error) { resultsData.results[pos] = path; @@ -257,7 +275,7 @@ internals.batch = function (batchRequest, resultsData, pos, requestParts, payloa batchRequest.payload.requests[pos].path = path; if (payloadParts && payloadParts.length) { - var payload = internals.buildPayload( + const payload = internals.buildPayload( batchRequest.payload.requests[pos].payload, resultsData, payloadParts @@ -267,14 +285,14 @@ internals.batch = function (batchRequest, resultsData, pos, requestParts, payloa batchRequest.payload.requests[pos].payload = payload; } - internals.dispatch(batchRequest, batchRequest.payload.requests[pos], function (data) { + internals.dispatch(batchRequest, batchRequest.payload.requests[pos], (data) => { // If redirection if (('' + data.statusCode).indexOf('3') === 0) { batchRequest.payload.requests[pos].path = data.headers.location; - internals.dispatch(batchRequest, batchRequest.payload.requests[pos], function (batchData) { + internals.dispatch(batchRequest, batchRequest.payload.requests[pos], (batchData) => { - var batchResult = batchData.result; + const batchResult = batchData.result; resultsData.results[pos] = batchResult; resultsData.resultsMap[pos] = batchResult; @@ -283,7 +301,7 @@ internals.batch = function (batchRequest, resultsData, pos, requestParts, payloa return; } - var result = data.result; + const result = data.result; resultsData.results[pos] = result; resultsData.resultsMap[pos] = result; callback(null, result); @@ -293,18 +311,18 @@ internals.batch = function (batchRequest, resultsData, pos, requestParts, payloa internals.dispatch = function (batchRequest, request, callback) { - var path = request.path; + let path = request.path; if (request.query) { - var urlObject = { + const urlObject = { pathname: request.path, query: request.query }; path = Url.format(urlObject); } - var body = (request.payload !== null && request.payload !== undefined ? JSON.stringify(request.payload) : null); // payload can be '' or 0 - var injectOptions = { + const body = (request.payload !== null && request.payload !== undefined ? JSON.stringify(request.payload) : null); // payload can be '' or 0 + const injectOptions = { url: path, method: request.method, headers: batchRequest.headers, diff --git a/lib/index.js b/lib/index.js index 8884982..265908b 100755 --- a/lib/index.js +++ b/lib/index.js @@ -1,12 +1,14 @@ +'use strict'; + // Load modules -var Hoek = require('hoek'); -var Batch = require('./batch'); +const Hoek = require('hoek'); +const Batch = require('./batch'); // Declare internals -var internals = { +const internals = { defaults: { batchEndpoint: '/batch', description: 'Batch endpoint', @@ -18,7 +20,7 @@ var internals = { exports.register = function (server, options, next) { - var settings = Hoek.applyToDefaults(internals.defaults, options); + const settings = Hoek.applyToDefaults(internals.defaults, options); server.route({ method: 'POST', diff --git a/package.json b/package.json index b00ed71..b926e7b 100644 --- a/package.json +++ b/package.json @@ -12,20 +12,17 @@ "engines": { "node": ">=0.10.32" }, - "peerDependencies": { - "hapi": ">=2.x.x" - }, "dependencies": { - "async": "1.3.x", - "boom": "2.x.x", - "hoek": "2.x.x", - "joi": "^6.10.1", - "traverse": "^0.6.6" + "async": "1.x.x", + "boom": "3.x.x", + "hoek": "4.x.x", + "joi": "8.x.x", + "traverse": "0.6.x" }, "devDependencies": { - "code": "1.x.x", - "hapi": "8.x.x", - "lab": "6.x.x", + "code": "2.x.x", + "hapi": "13.x.x", + "lab": "10.x.x", "sinon": "1.x.x" }, "scripts": { diff --git a/test/batch.js b/test/batch.js index f2c6bd5..1db1345 100644 --- a/test/batch.js +++ b/test/batch.js @@ -151,7 +151,6 @@ describe('Batch', function () { }; var echoHandler = function (request, reply) { - return reply(request.payload); }; @@ -200,7 +199,7 @@ describe('Batch', function () { return callback(res.result); }; - server.inject({ + server.connections[0].inject({ method: 'post', url: '/batch', payload: payload @@ -465,7 +464,7 @@ describe('Batch', function () { expect(res.length).to.equal(2); expect(res[0]).to.deep.equal({ a: 1 }); - expect(res[1]).to.deep.equal({}); + expect(res[1]).to.deep.equal(null); done(); }); }); @@ -627,7 +626,7 @@ describe('Batch', function () { makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', function (res) { expect(res.length).to.equal(1); - expect(res[0]).to.deep.equal({}); + expect(res[0]).to.deep.equal(null); done(); }); }); @@ -693,14 +692,14 @@ describe('Batch', function () { }); }); - it('returns an undefined property when a nonexistent path is set in the payload', function (done) { + it('returns an empty object when a nonexistent path is set in the payload', function (done) { makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"foo": "$0.foo"}} ] }', function (res) { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); expect(res[0].name).to.equal('Active Item'); - expect(res[1].foo).to.be.undefined(); + expect(res[1].foo).to.be.empty(); done(); }); @@ -714,7 +713,7 @@ describe('Batch', function () { makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', function (res) { expect(res.length).to.equal(1); - expect(res[0]).to.deep.equal({}); + expect(res[0]).to.deep.equal(null); done(); }); From 32fabe86d4f0d76b682dea206b6dfc8a9a2f34e8 Mon Sep 17 00:00:00 2001 From: Christopher De Cairos Date: Wed, 25 May 2016 22:32:12 -0400 Subject: [PATCH 2/2] Fix test file style --- examples/batch.js | 1 + test/batch.js | 239 +++++++++++++++++++++++----------------------- test/plugin.js | 70 +++++++------- 3 files changed, 158 insertions(+), 152 deletions(-) diff --git a/examples/batch.js b/examples/batch.js index ea3d372..958f1ff 100755 --- a/examples/batch.js +++ b/examples/batch.js @@ -1,4 +1,5 @@ 'use strict'; + // Load modules const Hapi = require('hapi'); diff --git a/test/batch.js b/test/batch.js index 1db1345..28cfc58 100644 --- a/test/batch.js +++ b/test/batch.js @@ -1,34 +1,36 @@ +'use strict'; + // Load modules -var Async = require('async'); -var Bassmaster = require('../'); -var Code = require('code'); -var Hapi = require('hapi'); -var Lab = require('lab'); -var Sinon = require('sinon'); +const Async = require('async'); +const Bassmaster = require('../'); +const Code = require('code'); +const Hapi = require('hapi'); +const Lab = require('lab'); +const Sinon = require('sinon'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var describe = lab.describe; -var it = lab.it; -var before = lab.before; -var expect = Code.expect; +const lab = exports.lab = Lab.script(); +const describe = lab.describe; +const it = lab.it; +const before = lab.before; +const expect = Code.expect; -describe('Batch', function () { +describe('Batch', () => { - var server = null; + let server = null; - var profileHandler = function (request, reply) { + const profileHandler = function (request, reply) { - var id = request.query.id || 'fa0dbda9b1b'; + const id = request.query.id || 'fa0dbda9b1b'; return reply({ 'id': id, @@ -36,7 +38,7 @@ describe('Batch', function () { }); }; - var activeItemHandler = function (request, reply) { + const activeItemHandler = function (request, reply) { return reply({ 'id': '55cf687663', @@ -44,7 +46,7 @@ describe('Batch', function () { }); }; - var itemHandler = function (request, reply) { + const itemHandler = function (request, reply) { return reply({ 'id': request.params.id, @@ -52,7 +54,7 @@ describe('Batch', function () { }); }; - var item2Handler = function (request, reply) { + const item2Handler = function (request, reply) { return reply({ 'id': request.params.id || 'mystery-guest', @@ -60,7 +62,7 @@ describe('Batch', function () { }); }; - var arrayHandler = function (request, reply) { + const arrayHandler = function (request, reply) { return reply({ 'id': '55cf687663', @@ -69,7 +71,7 @@ describe('Batch', function () { }); }; - var zeroIntegerHandler = function (request, reply) { + const zeroIntegerHandler = function (request, reply) { return reply({ 'id': 0, @@ -77,7 +79,7 @@ describe('Batch', function () { }); }; - var integerHandler = function (request, reply) { + const integerHandler = function (request, reply) { return reply({ 'id': 123, @@ -85,7 +87,7 @@ describe('Batch', function () { }); }; - var integerItemHandler = function (request, reply) { + const integerItemHandler = function (request, reply) { return reply({ 'id': request.params.id, @@ -93,7 +95,7 @@ describe('Batch', function () { }); }; - var badCharHandler = function (request, reply) { + const badCharHandler = function (request, reply) { return reply({ 'id': 'test', @@ -102,59 +104,60 @@ describe('Batch', function () { }); }; - var badValueHandler = function (request, reply) { + const badValueHandler = function (request, reply) { return reply(null); }; - var redirectHandler = function (request, reply) { + const redirectHandler = function (request, reply) { return reply().redirect('/profile'); }; - var fetch1 = function (request, next) { + const fetch1 = function (request, next) { next('Hello'); }; - var fetch2 = function (request, next) { + const fetch2 = function (request, next) { next(request.pre.m1 + request.pre.m3 + request.pre.m4); }; - var fetch3 = function (request, next) { + const fetch3 = function (request, next) { - process.nextTick(function () { + process.nextTick(() => { next(' '); }); }; - var fetch4 = function (request, next) { + const fetch4 = function (request, next) { next('World'); }; - var fetch5 = function (request, next) { + const fetch5 = function (request, next) { next(request.pre.m2 + '!'); }; - var getFetch = function (request, reply) { + const getFetch = function (request, reply) { return reply(request.pre.m5 + '\n'); }; - var errorHandler = function (request, reply) { + const errorHandler = function (request, reply) { return reply(new Error('myerror')); }; - var echoHandler = function (request, reply) { + const echoHandler = function (request, reply) { + return reply(request.payload); }; - var setupServer = function (done) { + const setupServer = function (done) { server = new Hapi.Server(); server.connection(); @@ -192,9 +195,9 @@ describe('Batch', function () { server.register(Bassmaster, done); }; - var makeRequest = function (payload, callback) { + const makeRequest = function (payload, callback) { - var next = function (res) { + const next = function (res) { return callback(res.result); }; @@ -208,9 +211,9 @@ describe('Batch', function () { before(setupServer); - it('shows single response when making request for single endpoint', function (done) { + it('shows single response when making request for single endpoint', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/profile" }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/profile" }] }', (res) => { expect(res[0].id).to.equal('fa0dbda9b1b'); expect(res[0].name).to.equal('John Doe'); @@ -219,9 +222,9 @@ describe('Batch', function () { }); }); - it('supports redirect', function (done) { + it('supports redirect', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/redirect" }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/redirect" }] }', (res) => { expect(res[0].id).to.equal('fa0dbda9b1b'); expect(res[0].name).to.equal('John Doe'); @@ -230,9 +233,9 @@ describe('Batch', function () { }); }); - it('supports query string in the request', function (done) { + it('supports query string in the request', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/profile?id=someid" }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/profile?id=someid" }] }', (res) => { expect(res[0].id).to.equal('someid'); expect(res[0].name).to.equal('John Doe'); @@ -241,9 +244,9 @@ describe('Batch', function () { }); }); - it('supports non alphanum characters in the request', function (done) { + it('supports non alphanum characters in the request', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/item/item-_^~&-end" }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/item/item-_^~&-end" }] }', (res) => { expect(res[0].id).to.equal('item-_^~&-end'); expect(res[0].name).to.equal('Item'); @@ -252,9 +255,9 @@ describe('Batch', function () { }); }); - it('shows two ordered responses when requesting two endpoints', function (done) { + it('shows two ordered responses when requesting two endpoints', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/profile"}, {"method": "get", "path": "/item"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/profile"}, {"method": "get", "path": "/item"}] }', (res) => { expect(res[0].id).to.equal('fa0dbda9b1b'); expect(res[0].name).to.equal('John Doe'); @@ -265,9 +268,9 @@ describe('Batch', function () { }); }); - it('shows two ordered responses when requesting two endpoints (with optional path param)', function (done) { + it('shows two ordered responses when requesting two endpoints (with optional path param)', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/item2/john"}, {"method": "get", "path": "/item2/"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/item2/john"}, {"method": "get", "path": "/item2/"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('john'); @@ -276,9 +279,9 @@ describe('Batch', function () { }); }); - it('handles a large number of batch requests in parallel', function (done) { + it('handles a large number of batch requests in parallel', (done) => { - var requestBody = '{ "requests": [{"method": "get", "path": "/profile"},' + + const requestBody = '{ "requests": [{"method": "get", "path": "/profile"},' + '{"method": "get", "path": "/item"},' + '{"method": "get", "path": "/profile"},' + '{"method": "get", "path": "/item"},' + @@ -360,8 +363,8 @@ describe('Batch', function () { '{"method": "get", "path": "/fetch"}' + '] }'; - var asyncSpy = Sinon.spy(Async, 'parallel'); - makeRequest(requestBody, function (res) { + const asyncSpy = Sinon.spy(Async, 'parallel'); + makeRequest(requestBody, (res) => { expect(res[0].id).to.equal('fa0dbda9b1b'); expect(res[0].name).to.equal('John Doe'); @@ -373,9 +376,9 @@ describe('Batch', function () { }); }); - it('supports piping a response into the next request', function (done) { + it('supports piping a response into the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.id"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.id"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -386,9 +389,9 @@ describe('Batch', function () { }); }); - it('supports piping integer response into the next request', function (done) { + it('supports piping integer response into the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/int"}, {"method": "get", "path": "/int/$0.id"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/int"}, {"method": "get", "path": "/int/$0.id"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal(123); @@ -399,9 +402,9 @@ describe('Batch', function () { }); }); - it('supports piping a zero integer response into the next request', function (done) { + it('supports piping a zero integer response into the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/zero"}, {"method": "get", "path": "/int/$0.id"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/zero"}, {"method": "get", "path": "/int/$0.id"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal(0); @@ -412,9 +415,9 @@ describe('Batch', function () { }); }); - it('supports posting multiple requests', function (done) { + it('supports posting multiple requests', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":{"a":2}}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":{"a":2}}] }', (res) => { expect(res.length).to.equal(2); expect(res[0]).to.deep.equal({ a: 1 }); @@ -423,9 +426,9 @@ describe('Batch', function () { }); }); - it('supports sending multiple PUTs requests', function (done) { + it('supports sending multiple PUTs requests', (done) => { - makeRequest('{ "requests": [ {"method": "put", "path": "/echo", "payload":{"a":1}}, {"method": "put", "path": "/echo", "payload":{"a":2}}] }', function (res) { + makeRequest('{ "requests": [ {"method": "put", "path": "/echo", "payload":{"a":1}}, {"method": "put", "path": "/echo", "payload":{"a":2}}] }', (res) => { expect(res.length).to.equal(2); expect(res[0]).to.deep.equal({ a: 1 }); @@ -434,9 +437,9 @@ describe('Batch', function () { }); }); - it('supports piping a response from post into the next get request', function (done) { + it('supports piping a response from post into the next get request', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": {"id":"55cf687663"}}, {"method": "get", "path": "/item/$0.id"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": {"id":"55cf687663"}}, {"method": "get", "path": "/item/$0.id"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -446,9 +449,9 @@ describe('Batch', function () { }); }); - it('supports piping a nested response value from post into the next get request', function (done) { + it('supports piping a nested response value from post into the next get request', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.id"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.id"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].data.id).to.equal('44cf687663'); @@ -458,9 +461,9 @@ describe('Batch', function () { }); }); - it('handles null payloads gracefully', function (done) { + it('handles null payloads gracefully', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":null}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload":{"a":1}}, {"method": "post", "path": "/echo", "payload":null}] }', (res) => { expect(res.length).to.equal(2); expect(res[0]).to.deep.equal({ a: 1 }); @@ -469,9 +472,9 @@ describe('Batch', function () { }); }); - it('includes errors when they occur in the request', function (done) { + it('includes errors when they occur in the request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/nothere"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "get", "path": "/nothere"}] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -481,9 +484,9 @@ describe('Batch', function () { }); }); - it('bad requests return the correct error', function (done) { + it('bad requests return the correct error', (done) => { - makeRequest('{ "blah": "test" }', function (res) { + makeRequest('{ "blah": "test" }', (res) => { expect(res.statusCode).to.equal(400); done(); @@ -491,117 +494,117 @@ describe('Batch', function () { }); - it('handles empty payload', function (done) { + it('handles empty payload', (done) => { - makeRequest(null, function (res) { + makeRequest(null, (res) => { expect(res.statusCode).to.equal(400); done(); }); }); - it('handles payload request not array', function (done) { + it('handles payload request not array', (done) => { - makeRequest('{ "requests": {"method": "get", "path": "/$1"} }', function (res) { + makeRequest('{ "requests": {"method": "get", "path": "/$1"} }', (res) => { expect(res.statusCode).to.equal(400); done(); }); }); - it('handles bad paths in requests array', function (done) { + it('handles bad paths in requests array', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/$1"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/$1"}] }', (res) => { expect(res.statusCode).to.equal(400); done(); }); }); - it('handles errors in the requested handlers', function (done) { + it('handles errors in the requested handlers', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/error"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/error"}] }', (res) => { expect(res[0].statusCode).to.equal(500); done(); }); }); - it('an out of bounds reference returns an error', function (done) { + it('an out of bounds reference returns an error', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$1.id"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$1.id"}] }', (res) => { expect(res.error).to.equal('Bad Request'); done(); }); }); - it('a non-existant reference returns an internal error', function (done) { + it('a non-existant reference returns an internal error', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.nothere"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.nothere"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('a non-existant & nested reference returns an internal error', function (done) { + it('a non-existant & nested reference returns an internal error', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.not.here"}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "payload": { "data": {"id":"44cf687663"}}}, {"method": "get", "path": "/item/$0.data.not.here"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('handles a bad character in the reference value', function (done) { + it('handles a bad character in the reference value', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.invalidChar"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.invalidChar"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('handles a null value in the reference value', function (done) { + it('handles a null value in the reference value', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.null"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/badchar"}, {"method": "get", "path": "/item/$0.null"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('cannot use invalid character to request reference', function (done) { + it('cannot use invalid character to request reference', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$:.name"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$:.name"}] }', (res) => { expect(res.statusCode).to.equal(400); done(); }); }); - it('handles missing reference', function (done) { + it('handles missing reference', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$0.name"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/badvalue"}, {"method": "get", "path": "/item/$0.name"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('handles error when getting reference value', function (done) { + it('handles error when getting reference value', (done) => { - makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.1"}] }', function (res) { + makeRequest('{ "requests": [{"method": "get", "path": "/item"}, {"method": "get", "path": "/item/$0.1"}] }', (res) => { expect(res.statusCode).to.equal(500); done(); }); }); - it('supports an optional query object', function (done) { + it('supports an optional query object', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "someid" } }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "someid" } }] }', (res) => { expect(res[0].id).to.equal('someid'); expect(res[0].name).to.equal('John Doe'); @@ -610,9 +613,9 @@ describe('Batch', function () { }); }); - it('supports alphanum characters in the query', function (done) { + it('supports alphanum characters in the query', (done) => { - makeRequest('{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "item-_^~&-end" } }] }', function (res) { + makeRequest('{ "requests": [{ "method": "get", "path": "/profile", "query": { "id": "item-_^~&-end" } }] }', (res) => { expect(res[0].id).to.equal('item-_^~&-end'); expect(res[0].name).to.equal('John Doe'); @@ -621,9 +624,9 @@ describe('Batch', function () { }); }); - it('handles null queries gracefully', function (done) { + it('handles null queries gracefully', (done) => { - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', (res) => { expect(res.length).to.equal(1); expect(res[0]).to.deep.equal(null); @@ -631,9 +634,9 @@ describe('Batch', function () { }); }); - it('supports piping a whole payload to the next request', function (done) { + it('supports piping a whole payload to the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0"} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0"} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -644,9 +647,9 @@ describe('Batch', function () { }); }); - it('supports piping a partial payload to the next request', function (done) { + it('supports piping a partial payload to the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.name"} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.name"} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -656,9 +659,9 @@ describe('Batch', function () { }); }); - it('supports piping a partial payload from a nested array to the next request', function (done) { + it('supports piping a partial payload from a nested array to the next request', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/array"}, {"method": "post", "path": "/echo", "payload":"$0.items.1"} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/array"}, {"method": "post", "path": "/echo", "payload":"$0.items.1"} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -668,9 +671,9 @@ describe('Batch', function () { }); }); - it('returns an empty object when a non-existent path is set at the root of the payload', function (done) { + it('returns an empty object when a non-existent path is set at the root of the payload', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.foo"} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":"$0.foo"} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -680,9 +683,9 @@ describe('Batch', function () { }); }); - it('sets a nested reference in the payload', function (done) { + it('sets a nested reference in the payload', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"name2": "$0.name"}} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"name2": "$0.name"}} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -692,9 +695,9 @@ describe('Batch', function () { }); }); - it('returns an empty object when a nonexistent path is set in the payload', function (done) { + it('returns an empty object when a nonexistent path is set in the payload', (done) => { - makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"foo": "$0.foo"}} ] }', function (res) { + makeRequest('{ "requests": [ {"method": "get", "path": "/item"}, {"method": "post", "path": "/echo", "payload":{"foo": "$0.foo"}} ] }', (res) => { expect(res.length).to.equal(2); expect(res[0].id).to.equal('55cf687663'); @@ -705,12 +708,12 @@ describe('Batch', function () { }); }); - it('works with multiple connections', function (done) { + it('works with multiple connections', (done) => { // Add a connection to the server server.connection({ port: 8000, host: 'localhost', labels: ['test'] }); - makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', function (res) { + makeRequest('{ "requests": [ {"method": "post", "path": "/echo", "query": null}] }', (res) => { expect(res.length).to.equal(1); expect(res[0]).to.deep.equal(null); diff --git a/test/plugin.js b/test/plugin.js index bb51274..feb1657 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -1,103 +1,105 @@ +'use strict'; + // Load modules -var Code = require('code'); -var Bassmaster = require('../'); -var Lab = require('lab'); -var Hapi = require('hapi'); +const Code = require('code'); +const Bassmaster = require('../'); +const Lab = require('lab'); +const Hapi = require('hapi'); // 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('bassmaster', function () { +describe('bassmaster', () => { - it('can be added as a plugin to hapi', function (done) { + it('can be added as a plugin to hapi', (done) => { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - server.register({ register: Bassmaster }, function (err) { + server.register({ register: Bassmaster }, (err) => { expect(err).to.not.exist(); done(); }); }); - it('can be given a custom route url', function (done) { + it('can be given a custom route url', (done) => { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - server.register({ register: Bassmaster, options: { batchEndpoint: '/custom' } }, function (err) { + server.register({ register: Bassmaster, options: { batchEndpoint: '/custom' } }, (err) => { expect(err).to.not.exist(); - var path = server.connections[0].table()[0].path; + const path = server.connections[0].table()[0].path; expect(path).to.equal('/custom'); done(); }); }); - it('can be given a custom description', function (done) { + it('can be given a custom description', (done) => { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - server.register({ register: Bassmaster, options: { description: 'customDescription' } }, function (err) { + server.register({ register: Bassmaster, options: { description: 'customDescription' } }, (err) => { expect(err).to.not.exist(); - var description = server.connections[0].table()[0].settings.description; + const description = server.connections[0].table()[0].settings.description; expect(description).to.equal('customDescription'); done(); }); }); - it('can be given an authentication strategy', function (done) { + it('can be given an authentication strategy', (done) => { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - var mockScheme = { - authenticate: function () { + const mockScheme = { + authenticate: () => { return null; }, - payload: function () { + payload: () => { return null; }, - response: function () { + response: () => { return null; } }; - server.auth.scheme('mockScheme', function () { + server.auth.scheme('mockScheme', () => { return mockScheme; }); server.auth.strategy('mockStrategy', 'mockScheme'); - server.register({ register: Bassmaster, options: { auth: 'mockStrategy' } }, function (err) { + server.register({ register: Bassmaster, options: { auth: 'mockStrategy' } }, (err) => { expect(err).to.not.exist(); - var auth = server.connections[0].table()[0].settings.auth.strategies[0]; + const auth = server.connections[0].table()[0].settings.auth.strategies[0]; expect(auth).to.equal('mockStrategy'); done(); }); }); - it('can be given custom tags', function (done) { + it('can be given custom tags', (done) => { - var server = new Hapi.Server(); + const server = new Hapi.Server(); server.connection(); - server.register({ register: Bassmaster, options: { tags: ['custom', 'tags'] } }, function (err) { + server.register({ register: Bassmaster, options: { tags: ['custom', 'tags'] } }, (err) => { expect(err).to.not.exist(); - var tags = server.connections[0].table()[0].settings.tags; + const tags = server.connections[0].table()[0].settings.tags; expect(tags).to.deep.equal(['custom', 'tags']); done(); });