Skip to content

Commit eb73790

Browse files
committed
Treat Accept extensions differently from media type parameters
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.
1 parent ee8b3c3 commit eb73790

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

lib/mediaType.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,36 @@ function parseMediaType(s, i) {
3434
subtype = match[2],
3535
full = "" + type + "/" + subtype,
3636
params = {},
37-
q = 1;
37+
ext = {},
38+
q = 1,
39+
foundQ = false;
3840

3941
if (match[3]) {
40-
params = match[3].split(';').map(function(s) {
41-
return s.trim().split('=');
42-
}).reduce(function (set, p) {
43-
var name = p[0].toLowerCase();
44-
var value = p[1];
45-
46-
set[name] = value && value[0] === '"' && value[value.length - 1] === '"'
47-
? value.substr(1, value.length - 2)
48-
: value;
49-
50-
return set;
51-
}, params);
52-
53-
if (params.q != null) {
54-
q = parseFloat(params.q);
55-
delete params.q;
56-
}
42+
match[3].split(';').forEach(function(s) {
43+
var nameVal = s.trim().split('=');
44+
var name = nameVal[0].toLowerCase();
45+
var value = nameVal[1];
46+
47+
value = value && value[0] === '"' && value[value.length - 1] === '"'
48+
? value.substr(1, value.length - 2)
49+
: value;
50+
51+
if(name === "q") {
52+
q = parseFloat(value);
53+
foundQ = true;
54+
}
55+
56+
else {
57+
(foundQ ? ext : params)[name] = value;
58+
}
59+
});
5760
}
5861

5962
return {
6063
type: type,
6164
subtype: subtype,
6265
params: params,
66+
ext: ext,
6367
q: q,
6468
i: i,
6569
full: full
@@ -139,7 +143,8 @@ function preferredMediaTypes(accept, provided, options) {
139143
return {
140144
type: "" + spec.type + '/' + spec.subtype,
141145
parameters: spec.params,
142-
q: spec.q
146+
q: spec.q,
147+
extensions: spec.ext
143148
};
144149
};
145150

test/mediaType.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ describe('negotiator.mediaTypes()', function () {
195195
})
196196

197197
describe('negotiator.mediaTypes(_, {detailed: true})', function() {
198-
whenAccept('text/html;LEVEL=1, application/json;q=0.5', function () {
198+
whenAccept('text/html;LEVEL=1, application/json;q=0.5;fizz=buzz', function () {
199199
it('should return more-detailed spec objects', function () {
200200
assert.deepEqual(
201201
this.negotiator.mediaTypes(undefined, {detailed: true}),
202202
[
203-
{"type": "text/html", "parameters": {"level": "1"}, "q": 1},
204-
{"type": "application/json", "parameters": {}, "q": 0.5},
203+
{"type": "text/html", "parameters": {"level": "1"}, "q": 1, "extensions": {}},
204+
{"type": "application/json", "parameters": {}, "q": 0.5, "extensions": {"fizz": "buzz"}}
205205
]
206206
);
207207
});
@@ -214,7 +214,7 @@ describe('negotiator.mediaTypes(_, {detailed: true})', function() {
214214

215215
assert.deepEqual(
216216
this.negotiator.mediaTypes(["text/html; level=1"], {detailed: true}),
217-
[{"type": "text/html", "parameters": {"level": "1"}, "q": 1}]
217+
[{"type": "text/html", "parameters": {"level": "1"}, "q": 1, "extensions": {}}]
218218
);
219219
});
220220
});

0 commit comments

Comments
 (0)