Skip to content

Commit

Permalink
Upping Mocha version; Removing components/:name@* routes; removing un…
Browse files Browse the repository at this point in the history
…used response function
  • Loading branch information
jonwinton committed Aug 31, 2017
1 parent 0c2f158 commit d5fb6d7
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 113 deletions.
8 changes: 4 additions & 4 deletions lib/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
38 changes: 0 additions & 38 deletions lib/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
55 changes: 0 additions & 55 deletions lib/responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
14 changes: 0 additions & 14 deletions lib/routes/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion lib/services/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit d5fb6d7

Please sign in to comment.