summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/cache-storage/script-tests/cache-put.js
blob: bcc8541ad406377c9caaf2dcbae20e536a2ff696 (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
if (self.importScripts) {
    importScripts('/resources/testharness.js');
    importScripts('../resources/testharness-helpers.js');
    importScripts('../resources/test-helpers.js');
}

var test_url = 'https://example.com/foo';
var test_body = 'Hello world!';

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) {
    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 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.');
        });
  }, 'Cache.put called twice 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) {
    return assert_promise_rejects(
      cache.put(new Request(test_url), 'Hello world!'),
      new TypeError(),
      'Cache.put should only accept a Response object as the response.');
  }, 'Cache.put with an invalid response');

cache_test(function(cache) {
    return assert_promise_rejects(
      cache.put(new Request('file:///etc/passwd'),
                new Response(test_body)),
      new TypeError(),
      '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) {
    var request = new Request('http://example.com/foo', { method: 'HEAD' });
    return assert_promise_rejects(
      cache.put(request, new Response(test_body)),
      new TypeError(),
      'Cache.put should throw a TypeError for non-GET requests.');
  }, 'Cache.put with a non-GET request');

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

cache_test(function(cache) {
    var request = new Request(test_url, {method: 'POST', body: test_body});
    return assert_promise_rejects(
      cache.put(request, new Response(test_body)),
      new TypeError(),
      '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(new TypeError(), () => response.body.getReader());
      });
  }, 'getReader() after Cache.put');

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

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

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');

done();