UNPKG

7.25 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert');
4const CachePolicy = require('..');
5
6const simpleRequest = {
7 method:'GET',
8 headers:{
9 host:'www.w3c.org',
10 connection: 'close',
11 'x-custom': 'yes',
12 },
13 url:'/Protocols/rfc2616/rfc2616-sec14.html',
14};
15function simpleRequestBut(overrides) {
16 return Object.assign({}, simpleRequest, overrides);
17}
18
19const cacheableResponse = {headers:{'cache-control':'max-age=111'}};
20const etaggedResponse = {headers:Object.assign({'etag':'"123456789"'},cacheableResponse.headers)};
21const lastModifiedResponse = {headers:Object.assign({'last-modified':'Tue, 15 Nov 1994 12:45:26 GMT'},cacheableResponse.headers)};
22const multiValidatorResponse = {headers:Object.assign({},etaggedResponse.headers,lastModifiedResponse.headers)};
23const alwaysVariableResponse = {headers:Object.assign({'vary':'*'},cacheableResponse.headers)};
24
25function assertHeadersPassed(headers) {
26 assert.strictEqual(headers.connection, undefined);
27 assert.strictEqual(headers['x-custom'], 'yes');
28}
29function assertNoValidators(headers) {
30 assert.strictEqual(headers['if-none-match'], undefined);
31 assert.strictEqual(headers['if-modified-since'], undefined);
32}
33
34describe('Can be revalidated?', function() {
35 it('ok if method changes to HEAD', function(){
36 const cache = new CachePolicy(simpleRequest,etaggedResponse);
37 const headers = cache.revalidationHeaders(simpleRequestBut({method:'HEAD'}));
38 assertHeadersPassed(headers);
39 assert.equal(headers['if-none-match'], '"123456789"');
40 });
41
42 it('not if method mismatch (other than HEAD)', function(){
43 const cache = new CachePolicy(simpleRequest,etaggedResponse);
44 const incomingRequest = simpleRequestBut({method:'POST'});
45 const headers = cache.revalidationHeaders(incomingRequest);
46 assertHeadersPassed(headers);
47 assertNoValidators(headers);
48 });
49
50 it('not if url mismatch', function(){
51 const cache = new CachePolicy(simpleRequest,etaggedResponse);
52 const incomingRequest = simpleRequestBut({url:'/yomomma'});
53 const headers = cache.revalidationHeaders(incomingRequest);
54 assertHeadersPassed(headers);
55 assertNoValidators(headers);
56 });
57
58 it('not if host mismatch', function(){
59 const cache = new CachePolicy(simpleRequest,etaggedResponse);
60 const incomingRequest = simpleRequestBut({headers:{host:'www.w4c.org'}});
61 const headers = cache.revalidationHeaders(incomingRequest);
62 assertNoValidators(headers);
63 assert.strictEqual(headers['x-custom'], undefined);
64 });
65
66 it('not if vary fields prevent', function(){
67 const cache = new CachePolicy(simpleRequest,alwaysVariableResponse);
68 const headers = cache.revalidationHeaders(simpleRequest);
69 assertHeadersPassed(headers);
70 assertNoValidators(headers);
71 });
72
73 it('when entity tag validator is present', function() {
74 const cache = new CachePolicy(simpleRequest, etaggedResponse);
75 const headers = cache.revalidationHeaders(simpleRequest);
76 assertHeadersPassed(headers);
77 assert.equal(headers['if-none-match'], '"123456789"');
78 });
79
80 it('skips weak validtors on post', function() {
81 const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
82 const cache = new CachePolicy(postReq, multiValidatorResponse);
83 const headers = cache.revalidationHeaders(postReq);
84 assert.equal(headers['if-none-match'], '"strong", "123456789"');
85 assert.strictEqual(undefined, headers['if-modified-since']);
86 });
87
88 it('skips weak validtors on post 2', function() {
89 const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak"'}});
90 const cache = new CachePolicy(postReq, lastModifiedResponse);
91 const headers = cache.revalidationHeaders(postReq);
92 assert.strictEqual(undefined, headers['if-none-match']);
93 assert.strictEqual(undefined, headers['if-modified-since']);
94 });
95
96 it('merges validtors', function() {
97 const postReq = simpleRequestBut({headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
98 const cache = new CachePolicy(postReq, multiValidatorResponse);
99 const headers = cache.revalidationHeaders(postReq);
100 assert.equal(headers['if-none-match'], 'W/"weak", "strong", W/"weak2", "123456789"');
101 assert.equal('Tue, 15 Nov 1994 12:45:26 GMT', headers['if-modified-since']);
102 });
103
104 it('when last-modified validator is present', function() {
105 const cache = new CachePolicy(simpleRequest, lastModifiedResponse);
106 const headers = cache.revalidationHeaders(simpleRequest);
107 assertHeadersPassed(headers);
108 assert.equal(headers['if-modified-since'], 'Tue, 15 Nov 1994 12:45:26 GMT');
109 });
110
111 it('not without validators', function() {
112 const cache = new CachePolicy(simpleRequest, cacheableResponse);
113 const headers = cache.revalidationHeaders(simpleRequest);
114 assertHeadersPassed(headers);
115 assertNoValidators(headers);
116 })
117
118});
119
120describe('Validation request', function(){
121 it('removes warnings', function() {
122 const cache = new CachePolicy({headers:{}}, {headers:{
123 "warning": "199 test danger",
124 }});
125
126 assert.strictEqual(undefined, cache.responseHeaders().warning);
127 });
128
129 it('must contain any etag', function(){
130 const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
131 const expected = multiValidatorResponse.headers.etag;
132 const actual = cache.revalidationHeaders(simpleRequest)['if-none-match'];
133 assert.equal(actual,expected);
134 });
135
136 it('merges etags', function(){
137 const cache = new CachePolicy(simpleRequest, etaggedResponse);
138 const expected = `"foo", "bar", ${etaggedResponse.headers.etag}`;
139 const headers = cache.revalidationHeaders(simpleRequestBut({headers:{
140 host:'www.w3c.org',
141 'if-none-match': '"foo", "bar"',
142 }}));
143 assert.equal(headers['if-none-match'],expected);
144 });
145
146 it('should send the Last-Modified value', function(){
147 const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
148 const expected = multiValidatorResponse.headers['last-modified'];
149 const actual = cache.revalidationHeaders(simpleRequest)['if-modified-since'];
150 assert.equal(actual,expected);
151 });
152
153 it('should not send the Last-Modified value for POST', function(){
154 const postReq = {method:'POST', headers:{'if-modified-since':'yesterday'}};
155 const cache = new CachePolicy(postReq, lastModifiedResponse);
156 const actual = cache.revalidationHeaders(postReq)['if-modified-since'];
157 assert.equal(actual, undefined);
158 });
159
160 it('should not send the Last-Modified value for range requests', function(){
161 const rangeReq = {method:'GET', headers:{'accept-ranges':'1-3', 'if-modified-since':'yesterday'}};
162 const cache = new CachePolicy(rangeReq, lastModifiedResponse);
163 const actual = cache.revalidationHeaders(rangeReq)['if-modified-since'];
164 assert.equal(actual, undefined);
165 });
166});