1 | 'use strict';
|
2 |
|
3 | const assert = require('assert');
|
4 | const CachePolicy = require('..');
|
5 |
|
6 | describe('Vary', function() {
|
7 | it('Basic', function() {
|
8 | const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather'}});
|
9 |
|
10 | assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
|
11 | assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
|
12 | });
|
13 |
|
14 | it("* doesn't match", function() {
|
15 | const policy = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'max-age=5','vary':'*'}});
|
16 |
|
17 | assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'ok'}}));
|
18 | });
|
19 |
|
20 | it("* is stale", function() {
|
21 | const policy1 = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'public,max-age=99', 'vary':'*'}});
|
22 | const policy2 = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'public,max-age=99', 'vary':'weather'}});
|
23 |
|
24 | assert(policy1.stale());
|
25 | assert(!policy2.stale());
|
26 | });
|
27 |
|
28 | it('Values are case-sensitive', function() {
|
29 | const policy = new CachePolicy({headers:{'weather': 'BAD'}}, {headers:{'cache-control':'max-age=5','vary':'Weather'}});
|
30 |
|
31 | assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'BAD'}}));
|
32 | assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
|
33 | });
|
34 |
|
35 | it('Irrelevant headers ignored', function() {
|
36 | const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'moon-phase'}});
|
37 |
|
38 | assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
|
39 | assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining'}}));
|
40 | assert(!policy.satisfiesWithoutRevalidation({headers:{'moon-phase': 'full'}}));
|
41 | });
|
42 |
|
43 | it('Absence is meaningful', function() {
|
44 | const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'moon-phase, weather'}});
|
45 |
|
46 | assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
|
47 | assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'moon-phase': ''}}));
|
48 | assert(!policy.satisfiesWithoutRevalidation({headers:{}}));
|
49 | });
|
50 |
|
51 | it('All values must match', function() {
|
52 | const policy = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}});
|
53 |
|
54 | assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
|
55 | assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'bad'}}));
|
56 | });
|
57 |
|
58 | it('Whitespace is OK', function() {
|
59 | const policy = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':' weather , sun '}});
|
60 |
|
61 | assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
|
62 | assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
|
63 | assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining'}}));
|
64 | });
|
65 |
|
66 | it('Order is irrelevant', function() {
|
67 | const policy1 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}});
|
68 | const policy2 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'sun, weather'}});
|
69 |
|
70 | assert(policy1.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'sun': 'shining'}}));
|
71 | assert(policy1.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
|
72 | assert(policy2.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'sun': 'shining'}}));
|
73 | assert(policy2.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
|
74 | });
|
75 | });
|