From 8a18f9f9123b2bdea154df06526df5847ac163b5 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Wed, 6 May 2015 23:07:51 -0400 Subject: [PATCH] Fix comparing media types with quoted values fixes #36 --- HISTORY.md | 1 + lib/mediaType.js | 10 ++++++++-- test/mediaType.js | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 113fec7..a165bab 100644 --- a/HISTORY.md +++ b/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 diff --git a/lib/mediaType.js b/lib/mediaType.js index 6740797..2edb7b6 100644 --- a/lib/mediaType.js +++ b/lib/mediaType.js @@ -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) { diff --git a/test/mediaType.js b/test/mediaType.js index beea6b2..b76832b 100644 --- a/test/mediaType.js +++ b/test/mediaType.js @@ -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'],