Browse Source

Parsing test

master
Kornel Lesiński 9 years ago
parent
commit
ce845cd979
  1. 6
      index.js
  2. 12
      test/responsetest.js
  3. 8
      test/varytest.js

6
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), // 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 // 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) { 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 cc[k] = (v === undefined) ? true : v.replace(/^"|"$/g, ''); // TODO: lame unquoting
} }
@ -128,7 +128,7 @@ CachePolicy.prototype = {
return false; 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) { for(const name of fields) {
if (req.headers[name] !== this._reqHeaders[name]) return false; if (req.headers[name] !== this._reqHeaders[name]) return false;
} }

12
test/responsetest.js

@ -17,6 +17,18 @@ describe('Response headers', function() {
assert.equal(cache.maxAge(), 999999); 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() { it('cache with expires', function() {
const cache = new CachePolicy(req, {headers:{ const cache = new CachePolicy(req, {headers:{
'date': new Date().toGMTString(), 'date': new Date().toGMTString(),

8
test/varytest.js

@ -55,6 +55,14 @@ describe('Vary', function() {
assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'bad'}})); 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() { it('Order is irrelevant', function() {
const policy1 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}}); 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'}}); const policy2 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'sun, weather'}});

Loading…
Cancel
Save