Skip to content

Commit

Permalink
Fix comparing media types with quoted values
Browse files Browse the repository at this point in the history
fixes #36
  • Loading branch information
dougwilson committed May 7, 2015
1 parent 4f40e4f commit 8a18f9f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
@@ -1,6 +1,7 @@
unreleased
==========

* Fix comparing media types with quoted values
* Fix splitting media types with quoted commas

0.5.1 / 2015-02-14
Expand Down
10 changes: 8 additions & 2 deletions lib/mediaType.js
Expand Up @@ -32,8 +32,14 @@ function parseMediaType(s, i) {
params = match[3].split(';').map(function(s) {
return s.trim().split('=');
}).reduce(function (set, p) {
set[p[0]] = p[1];
return set
var name = p[0];
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) {
Expand Down
12 changes: 12 additions & 0 deletions test/mediaType.js
Expand Up @@ -334,6 +334,18 @@ describe('negotiator.mediaTypes(array)', function () {
))
})

whenAccept('text/html;level=1;foo="bar"', function () {
it('should accept text/html;level=1;foo=bar', mediaTypesNegotiated(
['text/html;level=1;foo=bar'],
['text/html;level=1;foo=bar']
))

it('should accept text/html;level=1;foo="bar"', mediaTypesNegotiated(
['text/html;level=1;foo="bar"'],
['text/html;level=1;foo="bar"']
))
})

whenAccept('text/html;level=2', function () {
it('should not accept text/html;level=1', mediaTypesNegotiated(
['text/html;level=1'],
Expand Down

0 comments on commit 8a18f9f

Please sign in to comment.