Skip to content

Commit

Permalink
Merge pull request #2 from elliotttf/relaxed-accept
Browse files Browse the repository at this point in the history
Relaxed accept header handling
  • Loading branch information
elliotttf committed Oct 14, 2015
2 parents 78d9491 + db85f32 commit 198c119
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
27 changes: 24 additions & 3 deletions lib/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var _ = require('lodash');
var contentType = require('content-type');
var createError = require('http-errors');

var acceptRE = /(.+".+"[^,]*|[^,]+)/;

module.exports = function (supportedExt) {
supportedExt = supportedExt || [];
if (!Array.isArray(supportedExt)) {
Expand Down Expand Up @@ -48,10 +50,29 @@ module.exports = function (supportedExt) {

try {
var accept = req.get('accept');
if (!accept) {
return next(createError(406));
if (accept) {
var matches = accept.match(acceptRE);
matches.shift();
var errs = [];
var count = 0;
matches.forEach(function (val) {
if (val.indexOf('application/vnd.api+json') !== -1) {
count++;
try {
setCT(resCT, val);
}
catch (e) {
errs.push(e);
}
}
});

// If all instances of the JSON API accept header are modified, throw a
// 406 error.
if (count && count === errs.length) {
throw createError(406);
}
}
setCT(resCT, accept);

var reqCT = req.get('content-type');
if (reqCT) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-headers",
"version": "1.0.1",
"version": "1.0.2",
"description": "Provide a middleware to validate JSON API headers on a request",
"main": "lib/headers.js",
"scripts": {
Expand Down
38 changes: 32 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,47 @@ module.exports = {

test.done();
},
emptyExtensions: function (test) {
emptyHeaders: function (test) {
test.expect(1);
var tHeaders = headers();

testHeaders(
tHeaders,
'application/vnd.api+json',
undefined,
undefined,
function (key, val) {
test.equal(val, 'application/vnd.api+json');
},
test.done
);
},
invalidParameters: function (test) {
undefinedAccept: function (test) {
test.expect(1);
var tHeaders = headers();
testHeaders(
tHeaders,
'*/*; charset=test',
undefined,
function (key, val) {
test.equal(val, 'application/vnd.api+json');
},
test.done
);
},
mixexValidAccept: function (test) {
test.expect(1);
var tHeaders = headers();
testHeaders(
tHeaders,
'application/vnd.api+json,application/vnd.api+json; charset=test',
undefined,
function (key, val) {
test.equal(val, 'application/vnd.api+json');
},
test.done
);
},
invalidAccept: function (test) {
test.expect(1);
var tHeaders = headers();
testHeaders(
Expand All @@ -48,12 +74,12 @@ module.exports = {
undefined,
test.done,
function (err) {
test.equal(err.status, 415, 'Unexpected error code.');
test.equal(err.status, 406, 'Unexpected error code.');
test.done();
}
);
},
invalidAccept: function (test) {
invalidParameters: function (test) {
test.expect(1);
var tHeaders = headers();
testHeaders(
Expand All @@ -62,7 +88,7 @@ module.exports = {
'application/vnd.api+json; charset=test',
test.done,
function (err) {
test.equal(err.status, 406, 'Unexpected error code.');
test.equal(err.status, 415, 'Unexpected error code.');
test.done();
}
);
Expand Down

0 comments on commit 198c119

Please sign in to comment.