Browse Source

Full test coverage

master
Kornel Lesiński 9 years ago
committed by Kornel Lesiński
parent
commit
90ab2420ed
  1. 47
      test/responsetest.js
  2. 64
      test/satisfytest.js

47
test/responsetest.js

@ -48,6 +48,53 @@ describe('Response headers', function() {
assert(cache.maxAge() < 3605);
});
it('Ages', function() {
let now = 1000;
class TimeTravellingPolicy extends CachePolicy {
now() {
return now;
}
}
const cache = new TimeTravellingPolicy(req, {headers:{
'cache-control':'max-age=100',
'age': '50',
}});
assert(cache.storable());
assert(!cache.stale());
now += 48*1000;
assert(!cache.stale());
now += 5*1000;
assert(cache.stale());
});
it('Age can make stale', function() {
const cache = new CachePolicy(req, {headers:{
'cache-control':'max-age=100',
'age': '101',
}});
assert(cache.stale());
assert(cache.storable());
});
it('Age not always stale', function() {
const cache = new CachePolicy(req, {headers:{
'cache-control':'max-age=20',
'age': '15',
}});
assert(!cache.stale());
assert(cache.storable());
});
it('Bogus age ignored', function() {
const cache = new CachePolicy(req, {headers:{
'cache-control':'max-age=20',
'age': 'golden',
}});
assert(!cache.stale());
assert(cache.storable());
});
it('cache old files', function() {
const cache = new CachePolicy(req, {headers:{
'date': new Date().toGMTString(),

64
test/satisfytest.js

@ -0,0 +1,64 @@
'use strict';
const assert = require('assert');
const CachePolicy = require('..');
describe('Satisfies', function() {
it('when URLs match', function() {
const policy = new CachePolicy({url:'/',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(policy.satisfiesWithoutRevalidation({url:'/',headers:{}}));
});
it('when expires is present', function() {
const policy = new CachePolicy({headers:{}}, {status:302,headers:{'expires':new Date(Date.now()+2000).toGMTString()}});
assert(policy.satisfiesWithoutRevalidation({headers:{}}));
});
it('not when URLs mismatch', function() {
const policy = new CachePolicy({url:'/foo',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(!policy.satisfiesWithoutRevalidation({url:'/foo?bar',headers:{}}));
});
it('when methods match', function() {
const policy = new CachePolicy({method:'GET',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(policy.satisfiesWithoutRevalidation({method:'GET',headers:{}}));
});
it('not when hosts mismatch', function() {
const policy = new CachePolicy({headers:{'host':'foo'}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(policy.satisfiesWithoutRevalidation({headers:{'host':'foo'}}));
assert(!policy.satisfiesWithoutRevalidation({headers:{'host':'foofoo'}}));
});
it('when methods match HEAD', function() {
const policy = new CachePolicy({method:'HEAD',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(policy.satisfiesWithoutRevalidation({method:'HEAD',headers:{}}));
});
it('not when methods mismatch', function() {
const policy = new CachePolicy({method:'POST',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(!policy.satisfiesWithoutRevalidation({method:'GET',headers:{}}));
});
it('not when methods mismatch HEAD', function() {
const policy = new CachePolicy({method:'HEAD',headers:{}}, {status:200,headers:{'cache-control':'max-age=2'}});
assert(!policy.satisfiesWithoutRevalidation({method:'GET',headers:{}}));
});
it('not when proxy revalidating', function() {
const policy = new CachePolicy({headers:{}}, {status:200,headers:{'cache-control':'max-age=2, proxy-revalidate '}});
assert(!policy.satisfiesWithoutRevalidation({headers:{}}));
});
it('when not a proxy revalidating', function() {
const policy = new CachePolicy({headers:{}}, {status:200,headers:{'cache-control':'max-age=2, proxy-revalidate '}}, {shared:false});
assert(policy.satisfiesWithoutRevalidation({headers:{}}));
});
it('not when no-cache requesting', function() {
const policy = new CachePolicy({headers:{}}, {headers:{'cache-control':'max-age=2'}});
assert(policy.satisfiesWithoutRevalidation({headers:{'cache-control':'fine'}}));
assert(!policy.satisfiesWithoutRevalidation({headers:{'cache-control':'no-cache'}}));
assert(!policy.satisfiesWithoutRevalidation({headers:{'pragma':'no-cache'}}));
});
});
Loading…
Cancel
Save