summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-profile/node_modules/make-fetch-happen/node_modules/http-cache-semantics/test/revalidatetest.js
blob: 1f905ec27cbc4c0f9263644e3ab6eb74d8796cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';

const assert = require('assert');
const CachePolicy = require('..');

const simpleRequest = {
    method:'GET',
    headers:{
      host:'www.w3c.org',
      connection: 'close',
      'x-custom': 'yes',
    },
    url:'/Protocols/rfc2616/rfc2616-sec14.html',
};
function simpleRequestBut(overrides) {
    return Object.assign({}, simpleRequest, overrides);
}

const cacheableResponse = {headers:{'cache-control':'max-age=111'}};
const etaggedResponse = {headers:Object.assign({'etag':'"123456789"'},cacheableResponse.headers)};
const lastModifiedResponse = {headers:Object.assign({'last-modified':'Tue, 15 Nov 1994 12:45:26 GMT'},cacheableResponse.headers)};
const multiValidatorResponse = {headers:Object.assign({},etaggedResponse.headers,lastModifiedResponse.headers)};
const alwaysVariableResponse = {headers:Object.assign({'vary':'*'},cacheableResponse.headers)};

function assertHeadersPassed(headers) {
     assert.strictEqual(headers.connection, undefined);
     assert.strictEqual(headers['x-custom'], 'yes');
}
function assertNoValidators(headers) {
     assert.strictEqual(headers['if-none-match'], undefined);
     assert.strictEqual(headers['if-modified-since'], undefined);
}

describe('Can be revalidated?', function() {
    it('ok if method changes to HEAD', function(){
         const cache = new CachePolicy(simpleRequest,etaggedResponse);
         const headers = cache.revalidationHeaders(simpleRequestBut({method:'HEAD'}));
         assertHeadersPassed(headers);
         assert.equal(headers['if-none-match'], '"123456789"');
    });

    it('not if method mismatch (other than HEAD)', function(){
        const cache = new CachePolicy(simpleRequest,etaggedResponse);
        const incomingRequest = simpleRequestBut({method:'POST'});
        const headers = cache.revalidationHeaders(incomingRequest);
        assertHeadersPassed(headers);
        assertNoValidators(headers);
    });

    it('not if url mismatch', function(){
        const cache = new CachePolicy(simpleRequest,etaggedResponse);
        const incomingRequest = simpleRequestBut({url:'/yomomma'});
        const headers = cache.revalidationHeaders(incomingRequest);
        assertHeadersPassed(headers);
        assertNoValidators(headers);
    });

    it('not if host mismatch', function(){
        const cache = new CachePolicy(simpleRequest,etaggedResponse);
        const incomingRequest = simpleRequestBut({headers:{host:'www.w4c.org'}});
        const headers = cache.revalidationHeaders(incomingRequest);
        assertNoValidators(headers);
        assert.strictEqual(headers['x-custom'], undefined);
    });

    it('not if vary fields prevent', function(){
        const cache = new CachePolicy(simpleRequest,alwaysVariableResponse);
        const headers = cache.revalidationHeaders(simpleRequest);
        assertHeadersPassed(headers);
        assertNoValidators(headers);
    });

    it('when entity tag validator is present', function() {
        const cache = new CachePolicy(simpleRequest, etaggedResponse);
        const headers = cache.revalidationHeaders(simpleRequest);
        assertHeadersPassed(headers);
        assert.equal(headers['if-none-match'], '"123456789"');
    });

    it('skips weak validtors on post', function() {
        const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
        const cache = new CachePolicy(postReq, multiValidatorResponse);
        const headers = cache.revalidationHeaders(postReq);
        assert.equal(headers['if-none-match'], '"strong", "123456789"');
        assert.strictEqual(undefined, headers['if-modified-since']);
    });

    it('skips weak validtors on post 2', function() {
        const postReq = simpleRequestBut({method:'POST', headers:{'if-none-match': 'W/"weak"'}});
        const cache = new CachePolicy(postReq, lastModifiedResponse);
        const headers = cache.revalidationHeaders(postReq);
        assert.strictEqual(undefined, headers['if-none-match']);
        assert.strictEqual(undefined, headers['if-modified-since']);
    });

    it('merges validtors', function() {
        const postReq = simpleRequestBut({headers:{'if-none-match': 'W/"weak", "strong", W/"weak2"'}});
        const cache = new CachePolicy(postReq, multiValidatorResponse);
        const headers = cache.revalidationHeaders(postReq);
        assert.equal(headers['if-none-match'], 'W/"weak", "strong", W/"weak2", "123456789"');
        assert.equal('Tue, 15 Nov 1994 12:45:26 GMT', headers['if-modified-since']);
    });

    it('when last-modified validator is present', function() {
        const cache = new CachePolicy(simpleRequest, lastModifiedResponse);
        const headers = cache.revalidationHeaders(simpleRequest);
        assertHeadersPassed(headers);
        assert.equal(headers['if-modified-since'], 'Tue, 15 Nov 1994 12:45:26 GMT');
    });

    it('not without validators', function() {
        const cache = new CachePolicy(simpleRequest, cacheableResponse);
        const headers = cache.revalidationHeaders(simpleRequest);
        assertHeadersPassed(headers);
        assertNoValidators(headers);
    })

});

describe('Validation request', function(){
    it('removes warnings', function() {
      const cache = new CachePolicy({headers:{}}, {headers:{
          "warning": "199 test danger",
      }});

      assert.strictEqual(undefined, cache.responseHeaders().warning);
    });

    it('must contain any etag', function(){
        const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
        const expected = multiValidatorResponse.headers.etag;
        const actual = cache.revalidationHeaders(simpleRequest)['if-none-match'];
        assert.equal(actual,expected);
    });

    it('merges etags', function(){
        const cache = new CachePolicy(simpleRequest, etaggedResponse);
        const expected = `"foo", "bar", ${etaggedResponse.headers.etag}`;
        const headers = cache.revalidationHeaders(simpleRequestBut({headers:{
            host:'www.w3c.org',
            'if-none-match': '"foo", "bar"',
        }}));
        assert.equal(headers['if-none-match'],expected);
    });

    it('should send the Last-Modified value', function(){
        const cache = new CachePolicy(simpleRequest,multiValidatorResponse);
        const expected = multiValidatorResponse.headers['last-modified'];
        const actual = cache.revalidationHeaders(simpleRequest)['if-modified-since'];
        assert.equal(actual,expected);
    });

    it('should not send the Last-Modified value for POST', function(){
        const postReq = {method:'POST', headers:{'if-modified-since':'yesterday'}};
        const cache = new CachePolicy(postReq, lastModifiedResponse);
        const actual = cache.revalidationHeaders(postReq)['if-modified-since'];
        assert.equal(actual, undefined);
    });

    it('should not send the Last-Modified value for range requests', function(){
        const rangeReq = {method:'GET', headers:{'accept-ranges':'1-3', 'if-modified-since':'yesterday'}};
        const cache = new CachePolicy(rangeReq, lastModifiedResponse);
        const actual = cache.revalidationHeaders(rangeReq)['if-modified-since'];
        assert.equal(actual, undefined);
    });
});