Browse Source

Tests

master
Kornel Lesiński 9 years ago
committed by Kornel Lesiński
parent
commit
681e140d5f
  1. 56
      test/responsetest.js

56
test/cachetest.js → test/responsetest.js

@ -3,20 +3,22 @@
const assert = require('assert'); const assert = require('assert');
const CachePolicy = require('..'); const CachePolicy = require('..');
describe('Cache', function() { const req = {method:'GET', headers:{}};
describe('Response headers', function() {
it('simple miss', function() { it('simple miss', function() {
const cache = new CachePolicy({}, {headers:{}}); const cache = new CachePolicy(req, {headers:{}});
assert(!cache.isFresh()); assert(!cache.isFresh());
}); });
it('simple hit', function() { it('simple hit', function() {
const cache = new CachePolicy({}, {headers:{'cache-control': 'public, max-age=999999'}}); const cache = new CachePolicy(req, {headers:{'cache-control': 'public, max-age=999999'}});
assert(cache.isFresh()); assert(cache.isFresh());
assert.equal(cache.maxAge(), 999999); assert.equal(cache.maxAge(), 999999);
}); });
it('cache with expires', function() { it('cache with expires', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'date': new Date().toGMTString(), 'date': new Date().toGMTString(),
'expires': new Date(Date.now() + 2000).toGMTString(), 'expires': new Date(Date.now() + 2000).toGMTString(),
}}); }});
@ -25,7 +27,7 @@ describe('Cache', function() {
}); });
it('cache expires no date', function() { it('cache expires no date', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'cache-control': 'public', 'cache-control': 'public',
'expires': new Date(Date.now()+3600*1000).toGMTString(), 'expires': new Date(Date.now()+3600*1000).toGMTString(),
}}); }});
@ -35,7 +37,7 @@ describe('Cache', function() {
}); });
it('cache old files', function() { it('cache old files', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'date': new Date().toGMTString(), 'date': new Date().toGMTString(),
'last-modified': 'Mon, 07 Mar 2016 11:52:56 GMT', 'last-modified': 'Mon, 07 Mar 2016 11:52:56 GMT',
}}); }});
@ -44,7 +46,7 @@ describe('Cache', function() {
}); });
it('pragma: no-cache', function() { it('pragma: no-cache', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'pragma': 'no-cache', 'pragma': 'no-cache',
'last-modified': 'Mon, 07 Mar 2016 11:52:56 GMT', 'last-modified': 'Mon, 07 Mar 2016 11:52:56 GMT',
}}); }});
@ -52,7 +54,7 @@ describe('Cache', function() {
}); });
it('no-store', function() { it('no-store', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'cache-control': 'no-store, public, max-age=1', 'cache-control': 'no-store, public, max-age=1',
}}); }});
assert(!cache.isFresh()); assert(!cache.isFresh());
@ -63,11 +65,11 @@ describe('Cache', function() {
const privateHeader = { const privateHeader = {
'cache-control': 'private, max-age=1234', 'cache-control': 'private, max-age=1234',
}; };
const proxyCache = new CachePolicy({}, {headers:privateHeader}); const proxyCache = new CachePolicy(req, {headers:privateHeader});
assert(!proxyCache.isFresh()); assert(!proxyCache.isFresh());
assert.equal(0, proxyCache.maxAge()); assert.equal(0, proxyCache.maxAge());
const uaCache = new CachePolicy({}, {headers:privateHeader}, {shared:false}); const uaCache = new CachePolicy(req, {headers:privateHeader}, {shared:false});
assert(uaCache.isFresh()); assert(uaCache.isFresh());
assert.equal(1234, uaCache.maxAge()); assert.equal(1234, uaCache.maxAge());
}); });
@ -77,11 +79,11 @@ describe('Cache', function() {
'set-cookie': 'foo=bar', 'set-cookie': 'foo=bar',
'cache-control': 'max-age=99', 'cache-control': 'max-age=99',
}; };
const proxyCache = new CachePolicy({}, {headers:cookieHeader}, {shared:true}); const proxyCache = new CachePolicy(req, {headers:cookieHeader}, {shared:true});
assert(!proxyCache.isFresh()); assert(!proxyCache.isFresh());
assert.equal(0, proxyCache.maxAge()); assert.equal(0, proxyCache.maxAge());
const uaCache = new CachePolicy({}, {headers:cookieHeader}, {shared:false}); const uaCache = new CachePolicy(req, {headers:cookieHeader}, {shared:false});
assert(uaCache.isFresh()); assert(uaCache.isFresh());
assert.equal(99, uaCache.maxAge()); assert.equal(99, uaCache.maxAge());
}); });
@ -91,21 +93,39 @@ describe('Cache', function() {
'set-cookie': 'foo=bar', 'set-cookie': 'foo=bar',
'cache-control': 'max-age=5, public', 'cache-control': 'max-age=5, public',
}; };
const proxyCache = new CachePolicy({}, {headers:cookieHeader}, {shared:true}); const proxyCache = new CachePolicy(req, {headers:cookieHeader}, {shared:true});
assert(proxyCache.isFresh()); assert(proxyCache.isFresh());
assert.equal(5, proxyCache.maxAge()); assert.equal(5, proxyCache.maxAge());
}); });
it('miss max-age=0', function() { it('miss max-age=0', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'cache-control': 'public, max-age=0', 'cache-control': 'public, max-age=0',
}}); }});
assert(!cache.isFresh()); assert(!cache.isFresh());
assert.equal(0, cache.maxAge()); assert.equal(0, cache.maxAge());
}); });
it('cacheable 301', function() {
const cache = new CachePolicy(req, {
status: 301,
headers:{
'last-modified': 'Mon, 07 Mar 2016 11:52:56 GMT',
}});
assert(cache.isFresh());
});
it('cacheable 303', function() {
const cache = new CachePolicy(req, {
status: 303,
headers:{
'cache-control': 'max-age=1000',
}});
assert(cache.isFresh());
});
it('expired expires cached with max-age', function() { it('expired expires cached with max-age', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'cache-control': 'public, max-age=9999', 'cache-control': 'public, max-age=9999',
'expires': 'Sat, 07 May 2016 15:35:18 GMT', 'expires': 'Sat, 07 May 2016 15:35:18 GMT',
}}); }});
@ -118,17 +138,17 @@ describe('Cache', function() {
'cache-control': 'public, s-maxage=9999', 'cache-control': 'public, s-maxage=9999',
'expires': 'Sat, 07 May 2016 15:35:18 GMT', 'expires': 'Sat, 07 May 2016 15:35:18 GMT',
}; };
const proxyCache = new CachePolicy({}, {headers:sMaxAgeHeaders}); const proxyCache = new CachePolicy(req, {headers:sMaxAgeHeaders});
assert(proxyCache.isFresh()); assert(proxyCache.isFresh());
assert.equal(9999, proxyCache.maxAge()); assert.equal(9999, proxyCache.maxAge());
const uaCache = new CachePolicy({}, {headers:sMaxAgeHeaders}, {shared:false}); const uaCache = new CachePolicy(req, {headers:sMaxAgeHeaders}, {shared:false});
assert(!uaCache.isFresh()); assert(!uaCache.isFresh());
assert.equal(0, uaCache.maxAge()); assert.equal(0, uaCache.maxAge());
}); });
it('max-age wins over future expires', function() { it('max-age wins over future expires', function() {
const cache = new CachePolicy({}, {headers:{ const cache = new CachePolicy(req, {headers:{
'cache-control': 'public, max-age=333', 'cache-control': 'public, max-age=333',
'expires': new Date(Date.now()+3600*1000).toGMTString(), 'expires': new Date(Date.now()+3600*1000).toGMTString(),
}}); }});
Loading…
Cancel
Save