summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/http-cache-semantics/test/varytest.js
blob: 9d5cfb23256094547b446b97cbedef89b4c659b7 (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
'use strict';

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

describe('Vary', function() {
    it('Basic', function() {
        const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather'}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
    });

    it("* doesn't match", function() {
        const policy = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'max-age=5','vary':'*'}});

        assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'ok'}}));
    });

    it("* is stale", function() {
        const policy1 = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'public,max-age=99', 'vary':'*'}});
        const policy2 = new CachePolicy({headers:{'weather': 'ok'}}, {headers:{'cache-control':'public,max-age=99', 'vary':'weather'}});

        assert(policy1.stale());
        assert(!policy2.stale());
    });

    it('Values are case-sensitive', function() {
        const policy = new CachePolicy({headers:{'weather': 'BAD'}}, {headers:{'cache-control':'max-age=5','vary':'Weather'}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'BAD'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
    });

    it('Irrelevant headers ignored', function() {
        const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'moon-phase'}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'bad'}}));
        assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'moon-phase': 'full'}}));
    });

    it('Absence is meaningful', function() {
        const policy = new CachePolicy({headers:{'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'moon-phase, weather'}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'moon-phase': ''}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{}}));
    });

    it('All values must match', function() {
        const policy = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'bad'}}));
    });

    it('Whitespace is OK', function() {
        const policy = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'    weather       ,     sun     '}});

        assert(policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'weather': 'nice'}}));
        assert(!policy.satisfiesWithoutRevalidation({headers:{'sun': 'shining'}}));
    });

    it('Order is irrelevant', function() {
        const policy1 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'weather, sun'}});
        const policy2 = new CachePolicy({headers:{'sun': 'shining', 'weather': 'nice'}}, {headers:{'cache-control':'max-age=5','vary':'sun, weather'}});

        assert(policy1.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'sun': 'shining'}}));
        assert(policy1.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
        assert(policy2.satisfiesWithoutRevalidation({headers:{'weather': 'nice', 'sun': 'shining'}}));
        assert(policy2.satisfiesWithoutRevalidation({headers:{'sun': 'shining', 'weather': 'nice'}}));
    });
});