From ce845cd979fc302ea4a462ffb21826e1dee0283e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Tue, 31 May 2016 16:33:51 +0100 Subject: [PATCH] Parsing test --- index.js | 6 +++--- test/responsetest.js | 12 ++++++++++++ test/varytest.js | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d4dbf55..2a71924 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,9 @@ function parseCacheControl(header) { // TODO: When there is more than one value present for a given directive (e.g., two Expires header fields, multiple Cache-Control: max-age directives), // the directive's value is considered invalid. Caches are encouraged to consider responses that have invalid freshness information to be stale - const parts = header.split(/\s*,\s*/); // TODO: lame parsing + const parts = header.trim().split(/\s*,\s*/); // TODO: lame parsing for(const part of parts) { - const [k,v] = part.split(/\s*=\s*/); + const [k,v] = part.split(/\s*=\s*/, 2); cc[k] = (v === undefined) ? true : v.replace(/^"|"$/g, ''); // TODO: lame unquoting } @@ -128,7 +128,7 @@ CachePolicy.prototype = { return false; } - const fields = this._resHeaders.vary.toLowerCase().split(/\s*,\s*/); + const fields = this._resHeaders.vary.trim().toLowerCase().split(/\s*,\s*/); for(const name of fields) { if (req.headers[name] !== this._reqHeaders[name]) return false; } diff --git a/test/responsetest.js b/test/responsetest.js index 7c0884e..ee6ec62 100644 --- a/test/responsetest.js +++ b/test/responsetest.js @@ -17,6 +17,18 @@ describe('Response headers', function() { assert.equal(cache.maxAge(), 999999); }); + it('weird syntax', function() { + const cache = new CachePolicy(req, {headers:{'cache-control': ',,,,max-age = 456 ,'}}); + assert(!cache.stale()); + assert.equal(cache.maxAge(), 456); + }); + + it('quoted syntax', function() { + const cache = new CachePolicy(req, {headers:{'cache-control': ' max-age = "678" '}}); + assert(!cache.stale()); + assert.equal(cache.maxAge(), 678); + }); + it('cache with expires', function() { const cache = new CachePolicy(req, {headers:{ 'date': new Date().toGMTString(), diff --git a/test/varytest.js b/test/varytest.js index a23ac68..9d5cfb2 100644 --- a/test/varytest.js +++ b/test/varytest.js @@ -55,6 +55,14 @@ describe('Vary', function() { assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'bad'}})); }); + it('Whitespace is OK', function() { + const policy = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':' weather , sun '}}); + + assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}})); + assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}})); + assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining'}})); + }); + it('Order is irrelevant', function() { const policy1 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}}); const policy2 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'sun, weather'}});