summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/cache-storage/cache-storage-match.https.any.js
blob: 0c31b726294b14b1b849ecbeb369d23a59d126d8 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// META: title=CacheStorage.match
// META: global=window,worker
// META: script=./resources/test-helpers.js
// META: timeout=long

(function() {
  var next_index = 1;

  // Returns a transaction (request, response, and url) for a unique URL.
  function create_unique_transaction(test) {
    var uniquifier = String(next_index++);
    var url = 'http://example.com/' + uniquifier;

    return {
      request: new Request(url),
      response: new Response('hello'),
      url: url
    };
  }

  self.create_unique_transaction = create_unique_transaction;
})();

cache_test(function(cache) {
    var transaction = create_unique_transaction();

    return cache.put(transaction.request.clone(), transaction.response.clone())
      .then(function() {
          return self.caches.match(transaction.request);
        })
      .then(function(response) {
          assert_response_equals(response, transaction.response,
                                 'The response should not have changed.');
        });
}, 'CacheStorageMatch with no cache name provided');

cache_test(function(cache) {
    var transaction = create_unique_transaction();

    var test_cache_list = ['a', 'b', 'c'];
    return cache.put(transaction.request.clone(), transaction.response.clone())
      .then(function() {
          return Promise.all(test_cache_list.map(function(key) {
              return self.caches.open(key);
            }));
        })
      .then(function() {
          return self.caches.match(transaction.request);
        })
      .then(function(response) {
          assert_response_equals(response, transaction.response,
                                 'The response should not have changed.');
        });
}, 'CacheStorageMatch from one of many caches');

promise_test(function(test) {
    var transaction = create_unique_transaction();

    var test_cache_list = ['x', 'y', 'z'];
    return Promise.all(test_cache_list.map(function(key) {
        return self.caches.open(key);
      }))
      .then(function() { return self.caches.open('x'); })
      .then(function(cache) {
          return cache.put(transaction.request.clone(),
                           transaction.response.clone());
        })
      .then(function() {
          return self.caches.match(transaction.request, {cacheName: 'x'});
        })
      .then(function(response) {
          assert_response_equals(response, transaction.response,
                                 'The response should not have changed.');
        })
      .then(function() {
          return self.caches.match(transaction.request, {cacheName: 'y'});
        })
      .then(function(response) {
          assert_equals(response, undefined,
                        'Cache y should not have a response for the request.');
        });
}, 'CacheStorageMatch from one of many caches by name');

cache_test(function(cache) {
    var transaction = create_unique_transaction();
    return cache.put(transaction.url, transaction.response.clone())
      .then(function() {
          return self.caches.match(transaction.request);
        })
      .then(function(response) {
          assert_response_equals(response, transaction.response,
                                 'The response should not have changed.');
        });
}, 'CacheStorageMatch a string request');

cache_test(function(cache) {
    var transaction = create_unique_transaction();
    return cache.put(transaction.request.clone(), transaction.response.clone())
      .then(function() {
          return self.caches.match(new Request(transaction.request.url,
                                              {method: 'HEAD'}));
        })
      .then(function(response) {
          assert_equals(response, undefined,
                        'A HEAD request should not be matched');
        });
}, 'CacheStorageMatch a HEAD request');

promise_test(function(test) {
    var transaction = create_unique_transaction();
    return self.caches.match(transaction.request)
      .then(function(response) {
          assert_equals(response, undefined,
                        'The response should not be found.');
        });
}, 'CacheStorageMatch with no cached entry');

promise_test(function(test) {
    var transaction = create_unique_transaction();
    return self.caches.delete('foo')
      .then(function() {
          return self.caches.has('foo');
        })
      .then(function(has_foo) {
          assert_false(has_foo, "The cache should not exist.");
          return self.caches.match(transaction.request, {cacheName: 'foo'});
        })
      .then(function(response) {
          assert_equals(response, undefined,
                        'The match with bad cache name should resolve to ' +
                        'undefined.');
          return self.caches.has('foo');
        })
      .then(function(has_foo) {
          assert_false(has_foo, "The cache should still not exist.");
        });
}, 'CacheStorageMatch with no caches available but name provided');

cache_test(function(cache) {
    var transaction = create_unique_transaction();

    return self.caches.delete('')
      .then(function() {
          return self.caches.has('');
        })
      .then(function(has_cache) {
          assert_false(has_cache, "The cache should not exist.");
          return cache.put(transaction.request, transaction.response.clone());
        })
      .then(function() {
          return self.caches.match(transaction.request, {cacheName: ''});
        })
      .then(function(response) {
          assert_equals(response, undefined,
                        'The response should not be found.');
          return self.caches.open('');
        })
      .then(function(cache) {
          return cache.put(transaction.request, transaction.response);
        })
      .then(function() {
          return self.caches.match(transaction.request, {cacheName: ''});
        })
      .then(function(response) {
          assert_response_equals(response, transaction.response,
                                 'The response should be matched.');
          return self.caches.delete('');
        });
}, 'CacheStorageMatch with empty cache name provided');

cache_test(function(cache) {
    var request = new Request('http://example.com/?foo');
    var no_query_request = new Request('http://example.com/');
    var response = new Response('foo');
    return cache.put(request.clone(), response.clone())
      .then(function() {
          return self.caches.match(no_query_request.clone());
        })
      .then(function(result) {
          assert_equals(
            result, undefined,
            'CacheStorageMatch should resolve as undefined with a ' +
            'mismatched query.');
          return self.caches.match(no_query_request.clone(),
                                   {ignoreSearch: true});
        })
      .then(function(result) {
          assert_response_equals(
            result, response,
            'CacheStorageMatch with ignoreSearch should ignore the ' +
            'query of the request.');
        });
  }, 'CacheStorageMatch supports ignoreSearch');

cache_test(function(cache) {
    var request = new Request('http://example.com/');
    var head_request = new Request('http://example.com/', {method: 'HEAD'});
    var response = new Response('foo');
    return cache.put(request.clone(), response.clone())
      .then(function() {
          return self.caches.match(head_request.clone());
        })
      .then(function(result) {
          assert_equals(
            result, undefined,
            'CacheStorageMatch should resolve as undefined with a ' +
            'mismatched method.');
          return self.caches.match(head_request.clone(),
                                   {ignoreMethod: true});
        })
      .then(function(result) {
          assert_response_equals(
            result, response,
            'CacheStorageMatch with ignoreMethod should ignore the ' +
            'method of request.');
        });
  }, 'Cache.match supports ignoreMethod');

cache_test(function(cache) {
    var vary_request = new Request('http://example.com/c',
                                   {headers: {'Cookies': 'is-for-cookie'}});
    var vary_response = new Response('', {headers: {'Vary': 'Cookies'}});
    var mismatched_vary_request = new Request('http://example.com/c');

    return cache.put(vary_request.clone(), vary_response.clone())
      .then(function() {
          return self.caches.match(mismatched_vary_request.clone());
        })
      .then(function(result) {
          assert_equals(
            result, undefined,
            'CacheStorageMatch should resolve as undefined with a ' +
            ' mismatched vary.');
          return self.caches.match(mismatched_vary_request.clone(),
                                   {ignoreVary: true});
        })
      .then(function(result) {
          assert_response_equals(
            result, vary_response,
            'CacheStorageMatch with ignoreVary should ignore the ' +
            'vary of request.');
        });
  }, 'CacheStorageMatch supports ignoreVary');

done();