1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const CachePolicy = require('..');
|
5 |
|
6 | const publicCacheableResponse = {headers:{'cache-control': 'public, max-age=222'}};
|
7 | const cacheableResponse = {headers:{'cache-control': 'max-age=111'}};
|
8 |
|
9 | describe('Request properties', function() {
|
10 | it('No store kills cache', function() {
|
11 | const cache = new CachePolicy({method:'GET',headers:{'cache-control':'no-store'}}, publicCacheableResponse);
|
12 | assert(cache.stale());
|
13 | assert(!cache.storable());
|
14 | });
|
15 |
|
16 | it('POST not cacheable by default', function() {
|
17 | const cache = new CachePolicy({method:'POST',headers:{}}, {headers:{'cache-control': 'public'}});
|
18 | assert(cache.stale());
|
19 | assert(!cache.storable());
|
20 | });
|
21 |
|
22 | it('POST cacheable explicitly', function() {
|
23 | const cache = new CachePolicy({method:'POST',headers:{}}, publicCacheableResponse);
|
24 | assert(!cache.stale());
|
25 | assert(cache.storable());
|
26 | });
|
27 |
|
28 | it('Public cacheable auth is OK', function() {
|
29 | const cache = new CachePolicy({method:'GET',headers:{'authorization': 'test'}}, publicCacheableResponse);
|
30 | assert(!cache.stale());
|
31 | assert(cache.storable());
|
32 | });
|
33 |
|
34 | it('Proxy cacheable auth is OK', function() {
|
35 | const cache = new CachePolicy({method:'GET',headers:{'authorization': 'test'}}, {headers:{'cache-control':'max-age=0,s-maxage=12'}});
|
36 | assert(!cache.stale());
|
37 | assert(cache.storable());
|
38 |
|
39 | const cache2 = CachePolicy.fromObject(JSON.parse(JSON.stringify(cache.toObject())));
|
40 | assert(cache2 instanceof CachePolicy);
|
41 | assert(!cache2.stale());
|
42 | assert(cache2.storable());
|
43 | });
|
44 |
|
45 | it('Private auth is OK', function() {
|
46 | const cache = new CachePolicy({method:'GET',headers:{'authorization': 'test'}}, cacheableResponse, {shared:false});
|
47 | assert(!cache.stale());
|
48 | assert(cache.storable());
|
49 | });
|
50 |
|
51 | it('Revalidated auth is OK', function() {
|
52 | const cache = new CachePolicy({headers:{'authorization': 'test'}}, {headers:{'cache-control':'max-age=88,must-revalidate'}});
|
53 | assert(cache.storable());
|
54 | });
|
55 |
|
56 | it('Auth prevents caching by default', function() {
|
57 | const cache = new CachePolicy({method:'GET',headers:{'authorization': 'test'}}, cacheableResponse);
|
58 | assert(cache.stale());
|
59 | assert(!cache.storable());
|
60 | });
|
61 | });
|