From d5fb6d7c3e88c6c32e31b4e4e8a2a729f3a02989 Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Thu, 31 Aug 2017 10:58:37 -0400 Subject: [PATCH] Upping Mocha version; Removing components/:name@* routes; removing unused response function --- lib/render.test.js | 8 +++--- lib/responses.js | 38 --------------------------- lib/responses.test.js | 55 ---------------------------------------- lib/routes/components.js | 14 ---------- lib/services/db.test.js | 2 +- package.json | 2 +- 6 files changed, 6 insertions(+), 113 deletions(-) diff --git a/lib/render.test.js b/lib/render.test.js index 93ea3daa..589df23e 100644 --- a/lib/render.test.js +++ b/lib/render.test.js @@ -122,7 +122,7 @@ describe(_.startCase(filename), function () { sandbox.stub(lib, 'renderUri').returns(Promise.resolve({type: 'html', output: 'some html'})); - return lib(res.req, res, _.noop) + lib(res.req, res, _.noop) .then(function () { sinon.assert.calledOnce(res.send); sinon.assert.calledOnce(res.type); @@ -134,7 +134,7 @@ describe(_.startCase(filename), function () { sandbox.stub(lib, 'renderUri').returns(Promise.resolve()); - return lib(res.req, res, _.noop) + lib(res.req, res, _.noop) .then(function () { expect(res.send.calledOnce).to.be.false; done(); @@ -149,7 +149,7 @@ describe(_.startCase(filename), function () { error.name = 'NotFoundError'; sandbox.stub(lib, 'renderUri').returns(Promise.reject(error)); - return lib(res.req, res, next) + lib(res.req, res, next) .then(function () { sinon.assert.calledOnce(next); done(); @@ -163,7 +163,7 @@ describe(_.startCase(filename), function () { sandbox.stub(lib, 'renderUri').returns(Promise.reject(error)); - return lib(res.req, res, next) + lib(res.req, res, next) .then(function () { sinon.assert.calledOnce(next); done(); diff --git a/lib/responses.js b/lib/responses.js index 32aef7d9..df4df467 100644 --- a/lib/responses.js +++ b/lib/responses.js @@ -280,43 +280,6 @@ function notFound(err, res) { sendDefaultResponseForCode(code, message, res); } -/** - * @param {{extensions: [string]}} options - * @returns {function} - */ -function onlyAcceptExtensions(options) { - const extensions = options.extensions; - - if (!extensions) { - throw new Error('Missing extensions option'); - } - - return function (req, res, next) { - let type, isType, - ext = !!req.params.ext && req.params.ext.toLowerCase(), - accepts = req.headers.accept || extensions[ext], - isAll = !!accepts && accepts.indexOf('*/*') !== -1; - - if (!ext) { - throw new Error('Missing required extension parameter'); - } - - if (!extensions[ext]) { - return notFound(res); - } - - type = extensions[ext]; - isType = !!accepts && accepts.indexOf(type) !== -1; - - if (!isAll && !isType) { - notAcceptable({accept: [type]})(req, res, next); - } else { - req.headers.accept = type; - next(); - } - }; -} - /** * All server errors should look like this. * @@ -627,7 +590,6 @@ module.exports.denyTrailingSlashOnId = denyTrailingSlashOnId; // nice 400 about module.exports.denyReferenceAtRoot = denyReferenceAtRoot; // nice 400 about _ref at root of object module.exports.handleError = handleError; // 404 or 500 based on exception module.exports.acceptJSONOnly = acceptJSONOnly; // 406 on non-JSON body -module.exports.onlyAcceptExtensions = onlyAcceptExtensions; // nice 404 for unsupported extensions module.exports.expectText = expectText; module.exports.expectJSON = expectJSON; module.exports.expectResponseType = expectResponseType; diff --git a/lib/responses.test.js b/lib/responses.test.js index 2e1df6fb..bbc1e532 100644 --- a/lib/responses.test.js +++ b/lib/responses.test.js @@ -226,61 +226,6 @@ describe(_.startCase(filename), function () { }); }); - describe('onlyAcceptExtensions', function (done) { - const fn = lib[this.title]; - - it('throws when not given extensions', function () { - expect(function () { - fn({}); - }).to.throw('Missing extensions option'); - }); - - it('throws when no extensions', function () { - const req = createMockReq(), - res = createMockRes({formatter: 'json'}), - routeFn = fn({extensions: {a: 'b'}}); - - expect(function () { - routeFn(req, res, done); - }).to.throw('Missing required extension parameter'); - }); - - it('blocks when client does not accept data', function (done) { - const req = createMockReq(), - res = createMockRes({formatter: 'json'}), - routeFn = fn({extensions: {a: 'b'}}); - - req.params = {ext: 'a'}; - expectStatus(res, 404); - routeFn(req, res, done); - }); - - it('blocks when client passes unknown extension with 404', function (done) { - const req = createMockReq(), - res = createMockRes({formatter: 'json'}), - routeFn = fn({extensions: {a: 'b'}}); - - req.params = {ext: 'c'}; - expectStatus(res, 404); - res.send = function (result) { - expect(result).to.deep.equal({ message: 'Not Found', code: 404 }); - done(); - }; - routeFn(req, res); - }); - - it('blocks when client passes known extension with unknown accepts header', function (done) { - const req = createMockReq(), - res = createMockRes({formatter: 'json'}), - routeFn = fn({extensions: {a: 'b'}}); - - req.params = {ext: 'a'}; - req.headers.accepts = 'c'; - expectStatus(res, 406); - routeFn(req, res, done); - }); - }); - describe('methodNotAllowed', function () { const fn = lib[this.title]; diff --git a/lib/routes/components.js b/lib/routes/components.js index 7ed5795f..3eae7982 100644 --- a/lib/routes/components.js +++ b/lib/routes/components.js @@ -221,20 +221,6 @@ function routes(router) { router.all('/:name*', validation.componentMustExist); router.get('/:name.:ext', route.getExtension); - // router.all('/:name@:version', responses.acceptJSONOnly); - // router.all('/:name@:version', responses.methodNotAllowed({ - // allow: ['get', 'put'] - // })); - // router.all('/:name@:version', responses.denyTrailingSlashOnId); - // router.get('/:name@:version', route.get); - // router.put('/:name@:version', responses.denyReferenceAtRoot); - // - // router.put('/:name@:version', route.put); - // router.put('/:name@:version', withAuthLevel(authLevels.WRITE)); - - -// router.put('/:name.:ext', responses.onlyAcceptExtensions({extensions: acceptedExtensions})); - router.all('/:name', responses.acceptJSONOnly); router.all('/:name', responses.methodNotAllowed({ allow: ['get', 'put', 'delete'] diff --git a/lib/services/db.test.js b/lib/services/db.test.js index 6fec620c..5a6c9d96 100644 --- a/lib/services/db.test.js +++ b/lib/services/db.test.js @@ -250,7 +250,7 @@ describe(_.startCase(filename), function () { onEnd = () => done(), onError = done; - return lib.put('1', '2') + lib.put('1', '2') .then(()=>{ const pipe = fn({ json: false diff --git a/package.json b/package.json index 36463515..442c6bb0 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "coveralls": "^2.11.2", "eslint": "^2.7.0", "istanbul": "^0.4.0", - "mocha": "^2.2.0", + "mocha": "^3.5.0", "sinon": "^1.14.0", "supertest": "^3.0.0" },