summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/service-workers/cache-storage/cache-put.https.any.js
blob: dbf2650a75a5ff051f222ccb736b14f65d98f1b9 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// META: title=Cache.put
// META: global=window,worker
// META: script=/common/get-host-info.sub.js
// META: script=./resources/test-helpers.js
// META: timeout=long

var test_url = 'https://example.com/foo';
var test_body = 'Hello world!';
const { REMOTE_HOST } = get_host_info();

cache_test(function(cache) {
    var request = new Request(test_url);
    var response = new Response(test_body);
    return cache.put(request, response)
      .then(function(result) {
          assert_equals(result, undefined,
                        'Cache.put should resolve with undefined on success.');
        });
  }, 'Cache.put called with simple Request and Response');

cache_test(function(cache) {
    var test_url = new URL('./resources/simple.txt', location.href).href;
    var request = new Request(test_url);
    var response;
    return fetch(test_url)
      .then(function(fetch_result) {
          response = fetch_result.clone();
          return cache.put(request, fetch_result);
        })
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_response_equals(result, response,
                                 'Cache.put should update the cache with ' +
                                 'new request and response.');
          return result.text();
        })
      .then(function(body) {
          assert_equals(body, 'a simple text file\n',
                        'Cache.put should store response body.');
        });
  }, 'Cache.put called with Request and Response from fetch()');

cache_test(function(cache) {
    var request = new Request(test_url);
    var response = new Response(test_body);
    assert_false(request.bodyUsed,
                 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
                 'Request.bodyUsed should be initially false.');
    return cache.put(request, response)
      .then(function() {
        assert_false(request.bodyUsed,
                     'Cache.put should not mark empty request\'s body used');
      });
  }, 'Cache.put with Request without a body');

cache_test(function(cache) {
    var request = new Request(test_url);
    var response = new Response();
    assert_false(response.bodyUsed,
                 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
                 'Response.bodyUsed should be initially false.');
    return cache.put(request, response)
      .then(function() {
        assert_false(response.bodyUsed,
                     'Cache.put should not mark empty response\'s body used');
      });
  }, 'Cache.put with Response without a body');

cache_test(function(cache) {
    var request = new Request(test_url);
    var response = new Response(test_body);
    return cache.put(request, response.clone())
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_response_equals(result, response,
                                 'Cache.put should update the cache with ' +
                                 'new Request and Response.');
        });
  }, 'Cache.put with a Response containing an empty URL');

cache_test(function(cache) {
    var request = new Request(test_url);
    var response = new Response('', {
        status: 200,
        headers: [['Content-Type', 'text/plain']]
      });
    return cache.put(request, response)
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_equals(result.status, 200, 'Cache.put should store status.');
          assert_equals(result.headers.get('Content-Type'), 'text/plain',
                        'Cache.put should store headers.');
          return result.text();
        })
      .then(function(body) {
          assert_equals(body, '',
                        'Cache.put should store response body.');
        });
  }, 'Cache.put with an empty response body');

cache_test(function(cache, test) {
    var request = new Request(test_url);
    var response = new Response('', {
        status: 206,
        headers: [['Content-Type', 'text/plain']]
      });

    return promise_rejects_js(
      test,
      TypeError,
      cache.put(request, response),
      'Cache.put should reject 206 Responses with a TypeError.');
  }, 'Cache.put with synthetic 206 response');

cache_test(function(cache, test) {
    var test_url = new URL('./resources/fetch-status.py?status=206', location.href).href;
    var request = new Request(test_url);
    var response;
    return fetch(test_url)
      .then(function(fetch_result) {
          assert_equals(fetch_result.status, 206,
                        'Test framework error: The status code should be 206.');
          response = fetch_result.clone();
          return promise_rejects_js(test, TypeError, cache.put(request, fetch_result));
        });
  }, 'Cache.put with HTTP 206 response');

cache_test(function(cache, test) {
    // We need to jump through some hoops to allow the test to perform opaque
    // response filtering, but bypass the ORB safelist check. This is
    // done, by forcing the MIME type retrieval to fail and the
    // validation of partial first response to succeed.
    var pipe = "status(206)|header(Content-Type,)|header(Content-Range, bytes 0-1/41)|slice(null, 1)";
    var test_url = new URL(`./resources/blank.html?pipe=${pipe}`, location.href);
    test_url.hostname = REMOTE_HOST;
    var request = new Request(test_url.href, { mode: 'no-cors' });
    var response;
    return fetch(request)
      .then(function(fetch_result) {
          assert_equals(fetch_result.type, 'opaque',
              'Test framework error: The response type should be opaque.');
          assert_equals(fetch_result.status, 0,
              'Test framework error: The status code should be 0 for an ' +
              ' opaque-filtered response. This is actually HTTP 206.');
          response = fetch_result.clone();
          return cache.put(request, fetch_result);
        })
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_not_equals(result, undefined,
              'Cache.put should store an entry for the opaque response');
        });
  }, 'Cache.put with opaque-filtered HTTP 206 response');

cache_test(function(cache) {
    var test_url = new URL('./resources/fetch-status.py?status=500', location.href).href;
    var request = new Request(test_url);
    var response;
    return fetch(test_url)
      .then(function(fetch_result) {
          assert_equals(fetch_result.status, 500,
                        'Test framework error: The status code should be 500.');
          response = fetch_result.clone();
          return cache.put(request, fetch_result);
        })
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_response_equals(result, response,
                                 'Cache.put should update the cache with ' +
                                 'new request and response.');
          return result.text();
        })
      .then(function(body) {
          assert_equals(body, '',
                        'Cache.put should store response body.');
        });
  }, 'Cache.put with HTTP 500 response');

cache_test(function(cache) {
    var alternate_response_body = 'New body';
    var alternate_response = new Response(alternate_response_body,
                                          { statusText: 'New status' });
    return cache.put(new Request(test_url),
                     new Response('Old body', { statusText: 'Old status' }))
      .then(function() {
          return cache.put(new Request(test_url), alternate_response.clone());
        })
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_response_equals(result, alternate_response,
                                 'Cache.put should replace existing ' +
                                 'response with new response.');
          return result.text();
        })
      .then(function(body) {
          assert_equals(body, alternate_response_body,
                        'Cache put should store new response body.');
        });
  }, 'Cache.put called twice with matching Requests and different Responses');

cache_test(function(cache) {
    var first_url = test_url;
    var second_url = first_url + '#(O_o)';
    var third_url = first_url + '#fragment';
    var alternate_response_body = 'New body';
    var alternate_response = new Response(alternate_response_body,
                                          { statusText: 'New status' });
    return cache.put(new Request(first_url),
                     new Response('Old body', { statusText: 'Old status' }))
      .then(function() {
          return cache.put(new Request(second_url), alternate_response.clone());
        })
      .then(function() {
          return cache.match(test_url);
        })
      .then(function(result) {
          assert_response_equals(result, alternate_response,
                                 'Cache.put should replace existing ' +
                                 'response with new response.');
          return result.text();
        })
      .then(function(body) {
          assert_equals(body, alternate_response_body,
                        'Cache put should store new response body.');
        })
      .then(function() {
          return cache.put(new Request(third_url), alternate_response.clone());
        })
      .then(function() {
          return cache.keys();
        })
      .then(function(results) {
          // Should match urls (without fragments or with different ones) to the
          // same cache key. However, result.url should be the latest url used.
          assert_equals(results[0].url, third_url);
          return;
        });
}, 'Cache.put called multiple times with request URLs that differ only by a fragment');

cache_test(function(cache) {
    var url = 'http://example.com/foo';
    return cache.put(url, new Response('some body'))
      .then(function() { return cache.match(url); })
      .then(function(response) { return response.text(); })
      .then(function(body) {
          assert_equals(body, 'some body',
                        'Cache.put should accept a string as request.');
        });
  }, 'Cache.put with a string request');

cache_test(function(cache, test) {
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(new Request(test_url), 'Hello world!'),
      'Cache.put should only accept a Response object as the response.');
  }, 'Cache.put with an invalid response');

cache_test(function(cache, test) {
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(new Request('file:///etc/passwd'),
                new Response(test_body)),
      'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.');
  }, 'Cache.put with a non-HTTP/HTTPS request');

cache_test(function(cache) {
    var response = new Response(test_body);
    return cache.put(new Request('relative-url'), response.clone())
      .then(function() {
          return cache.match(new URL('relative-url', location.href).href);
        })
      .then(function(result) {
          assert_response_equals(result, response,
                                 'Cache.put should accept a relative URL ' +
                                 'as the request.');
        });
  }, 'Cache.put with a relative URL');

cache_test(function(cache, test) {
    var request = new Request('http://example.com/foo', { method: 'HEAD' });
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(request, new Response(test_body)),
      'Cache.put should throw a TypeError for non-GET requests.');
  }, 'Cache.put with a non-GET request');

cache_test(function(cache, test) {
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(new Request(test_url), null),
      'Cache.put should throw a TypeError for a null response.');
  }, 'Cache.put with a null response');

cache_test(function(cache, test) {
    var request = new Request(test_url, {method: 'POST', body: test_body});
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(request, new Response(test_body)),
      'Cache.put should throw a TypeError for a POST request.');
  }, 'Cache.put with a POST request');

cache_test(function(cache) {
    var response = new Response(test_body);
    assert_false(response.bodyUsed,
                 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +
                 'Response.bodyUsed should be initially false.');
    return response.text().then(function() {
      assert_true(
        response.bodyUsed,
        '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' +
          'The text() method should make the body disturbed.');
      var request = new Request(test_url);
      return cache.put(request, response).then(() => {
          assert_unreached('cache.put should be rejected');
        }, () => {});
    });
  }, 'Cache.put with a used response body');

cache_test(function(cache) {
    var response = new Response(test_body);
    return cache.put(new Request(test_url), response)
      .then(function() {
          assert_throws_js(TypeError, () => response.body.getReader());
      });
  }, 'getReader() after Cache.put');

cache_test(function(cache, test) {
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(new Request(test_url),
                new Response(test_body, { headers: { VARY: '*' }})),
      'Cache.put should reject VARY:* Responses with a TypeError.');
  }, 'Cache.put with a VARY:* Response');

cache_test(function(cache, test) {
    return promise_rejects_js(
      test,
      TypeError,
      cache.put(new Request(test_url),
                new Response(test_body,
                             { headers: { VARY: 'Accept-Language,*' }})),
      'Cache.put should reject Responses with an embedded VARY:* with a ' +
      'TypeError.');
  }, 'Cache.put with an embedded VARY:* Response');

cache_test(async function(cache, test) {
    const url = new URL('./resources/vary.py?vary=*',
        get_host_info().HTTPS_REMOTE_ORIGIN + self.location.pathname);
    const request = new Request(url, { mode: 'no-cors' });
    const response = await fetch(request);
    assert_equals(response.type, 'opaque');
    await cache.put(request, response);
  }, 'Cache.put with a VARY:* opaque response should not reject');

cache_test(function(cache) {
    var url = 'foo.html';
    var redirectURL = 'http://example.com/foo-bar.html';
    var redirectResponse = Response.redirect(redirectURL);
    assert_equals(redirectResponse.headers.get('Location'), redirectURL,
                  'Response.redirect() should set Location header.');
    return cache.put(url, redirectResponse.clone())
      .then(function() {
          return cache.match(url);
        })
      .then(function(response) {
          assert_response_equals(response, redirectResponse,
                                 'Redirect response is reproduced by the Cache API');
          assert_equals(response.headers.get('Location'), redirectURL,
                        'Location header is preserved by Cache API.');
        });
  }, 'Cache.put should store Response.redirect() correctly');

cache_test(async (cache) => {
    var request = new Request(test_url);
    var response = new Response(new Blob([test_body]));
    await cache.put(request, response);
    var cachedResponse = await cache.match(request);
    assert_equals(await cachedResponse.text(), test_body);
  }, 'Cache.put called with simple Request and blob Response');

cache_test(async (cache) => {
  var formData = new FormData();
  formData.append("name", "value");

  var request = new Request(test_url);
  var response = new Response(formData);
  await cache.put(request, response);
  var cachedResponse = await cache.match(request);
  var cachedResponseText = await cachedResponse.text();
  assert_true(cachedResponseText.indexOf("name=\"name\"\r\n\r\nvalue") !== -1);
}, 'Cache.put called with simple Request and form data Response');

done();