Skip to content

Commit

Permalink
Treat Accept extensions differently from media type parameters
Browse files Browse the repository at this point in the history
This parses Accept extensions into a separate object from the other
parameters, which then isn’t used at all for negotiation (since no
extensions have been defined, that I know of) and isn’t returned with
the other parameters when the user asks for detailed media types.
  • Loading branch information
ethanresnick committed Jun 15, 2015
1 parent ee8b3c3 commit eb73790
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
43 changes: 24 additions & 19 deletions lib/mediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,36 @@ function parseMediaType(s, i) {
subtype = match[2],
full = "" + type + "/" + subtype,
params = {},
q = 1;
ext = {},
q = 1,
foundQ = false;

if (match[3]) {
params = match[3].split(';').map(function(s) {
return s.trim().split('=');
}).reduce(function (set, p) {
var name = p[0].toLowerCase();
var value = p[1];

set[name] = value && value[0] === '"' && value[value.length - 1] === '"'
? value.substr(1, value.length - 2)
: value;

return set;
}, params);

if (params.q != null) {
q = parseFloat(params.q);
delete params.q;
}
match[3].split(';').forEach(function(s) {
var nameVal = s.trim().split('=');
var name = nameVal[0].toLowerCase();
var value = nameVal[1];

value = value && value[0] === '"' && value[value.length - 1] === '"'
? value.substr(1, value.length - 2)
: value;

if(name === "q") {
q = parseFloat(value);
foundQ = true;
}

else {
(foundQ ? ext : params)[name] = value;
}
});
}

return {
type: type,
subtype: subtype,
params: params,
ext: ext,
q: q,
i: i,
full: full
Expand Down Expand Up @@ -139,7 +143,8 @@ function preferredMediaTypes(accept, provided, options) {
return {
type: "" + spec.type + '/' + spec.subtype,
parameters: spec.params,
q: spec.q
q: spec.q,
extensions: spec.ext
};
};

Expand Down
8 changes: 4 additions & 4 deletions test/mediaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ describe('negotiator.mediaTypes()', function () {
})

describe('negotiator.mediaTypes(_, {detailed: true})', function() {
whenAccept('text/html;LEVEL=1, application/json;q=0.5', function () {
whenAccept('text/html;LEVEL=1, application/json;q=0.5;fizz=buzz', function () {
it('should return more-detailed spec objects', function () {
assert.deepEqual(
this.negotiator.mediaTypes(undefined, {detailed: true}),
[
{"type": "text/html", "parameters": {"level": "1"}, "q": 1},
{"type": "application/json", "parameters": {}, "q": 0.5},
{"type": "text/html", "parameters": {"level": "1"}, "q": 1, "extensions": {}},
{"type": "application/json", "parameters": {}, "q": 0.5, "extensions": {"fizz": "buzz"}}
]
);
});
Expand All @@ -214,7 +214,7 @@ describe('negotiator.mediaTypes(_, {detailed: true})', function() {

assert.deepEqual(
this.negotiator.mediaTypes(["text/html; level=1"], {detailed: true}),
[{"type": "text/html", "parameters": {"level": "1"}, "q": 1}]
[{"type": "text/html", "parameters": {"level": "1"}, "q": 1, "extensions": {}}]
);
});
});
Expand Down

0 comments on commit eb73790

Please sign in to comment.