OLD | NEW |
1 importScripts('worker-testharness.js'); | 1 importScripts('worker-testharness.js'); |
2 importScripts('/resources/testharness-helpers.js'); | 2 importScripts('/resources/testharness-helpers.js'); |
3 | 3 |
4 var test_url = 'https://example.com/foo'; | 4 var test_url = 'https://example.com/foo'; |
5 var test_response_body = 'Hello world!'; | 5 var test_body = 'Hello world!'; |
6 | 6 |
7 cache_test(function(cache) { | 7 cache_test(function(cache) { |
8 var request = new Request(test_url); | 8 var request = new Request(test_url); |
9 var response = new Response(test_response_body); | 9 var response = new Response(test_body); |
10 return cache.put(request, response) | 10 return cache.put(request, response) |
11 .then(function(result) { | 11 .then(function(result) { |
12 assert_equals(result, undefined, | 12 assert_equals(result, undefined, |
13 'Cache.put should resolve with undefined on success.'); | 13 'Cache.put should resolve with undefined on success.'); |
14 }); | 14 }); |
15 }, 'Cache.put called with simple Request and Response'); | 15 }, 'Cache.put called with simple Request and Response'); |
16 | 16 |
17 cache_test(function(cache) { | 17 cache_test(function(cache) { |
18 var test_url = new URL('simple.txt', location.href).href; | 18 var test_url = new URL('simple.txt', location.href).href; |
19 var request = new Request(test_url); | 19 var request = new Request(test_url); |
(...skipping 12 matching lines...) Expand all Loading... |
32 'new request and response.'); | 32 'new request and response.'); |
33 return result.text(); | 33 return result.text(); |
34 }) | 34 }) |
35 .then(function(body) { | 35 .then(function(body) { |
36 assert_equals(body, 'a simple text file\n', | 36 assert_equals(body, 'a simple text file\n', |
37 'Cache.put should store response body.'); | 37 'Cache.put should store response body.'); |
38 }); | 38 }); |
39 }, 'Cache.put called with Request and Response from fetch()'); | 39 }, 'Cache.put called with Request and Response from fetch()'); |
40 | 40 |
41 cache_test(function(cache) { | 41 cache_test(function(cache) { |
| 42 var request = new Request(test_url); |
| 43 var response = new Response(test_body); |
| 44 assert_false(request.bodyUsed, |
| 45 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + |
| 46 'Request.bodyUsed should be initially false.'); |
| 47 return cache.put(request, response) |
| 48 .then(function() { |
| 49 assert_false(request.bodyUsed, |
| 50 'Cache.put should not mark empty request\'s body used'); |
| 51 }); |
| 52 }, 'Cache.put with Request without a body'); |
| 53 |
| 54 cache_test(function(cache) { |
42 var request = new Request(test_url, { | 55 var request = new Request(test_url, { |
43 method: 'GET', | 56 method: 'GET', |
44 body: 'Hello' | 57 body: 'Hello' |
45 }); | 58 }); |
46 var response = new Response(test_response_body); | 59 var response = new Response(test_body); |
47 assert_false(request.bodyUsed, | 60 assert_false(request.bodyUsed, |
48 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | 61 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + |
49 'Request.bodyUsed should be initially false.'); | 62 'Request.bodyUsed should be initially false.'); |
50 return cache.put(request, response) | 63 return cache.put(request, response.clone()) |
51 .then(function() { | 64 .then(function() { |
52 assert_false(request.bodyUsed, | 65 assert_true(request.bodyUsed, |
53 'Cache.put should not consume Request body.'); | 66 'Cache.put should consume Request body.'); |
54 }) | 67 }) |
55 .then(function() { | 68 .then(function() { |
56 return cache.match(request); | 69 return cache.match(request); |
57 }) | 70 }) |
58 .then(function(result) { | 71 .then(function(result) { |
59 assert_object_equals(result, response, | 72 assert_object_equals(result, response, |
60 'Cache.put should store response body.'); | 73 'Cache.put should store response body.'); |
61 }); | 74 }); |
62 }, 'Cache.put with Request containing a body'); | 75 }, 'Cache.put with Request containing a body'); |
63 | 76 |
64 cache_test(function(cache) { | 77 cache_test(function(cache) { |
65 var request = new Request(test_url); | 78 var request = new Request(test_url); |
66 var response = new Response(test_response_body); | 79 var response = new Response(test_body); |
67 return cache.put(request, response) | 80 return cache.put(request, response.clone()) |
68 .then(function() { | 81 .then(function() { |
69 return cache.match(test_url); | 82 return cache.match(test_url); |
70 }) | 83 }) |
71 .then(function(result) { | 84 .then(function(result) { |
72 assert_object_equals(result, response, | 85 assert_object_equals(result, response, |
73 'Cache.put should update the cache with ' + | 86 'Cache.put should update the cache with ' + |
74 'new Request and Response.'); | 87 'new Request and Response.'); |
75 }); | 88 }); |
76 }, 'Cache.put with a Response containing an empty URL'); | 89 }, 'Cache.put with a Response containing an empty URL'); |
77 | 90 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 }); | 136 }); |
124 }, 'Cache.put with HTTP 500 response'); | 137 }, 'Cache.put with HTTP 500 response'); |
125 | 138 |
126 cache_test(function(cache) { | 139 cache_test(function(cache) { |
127 var alternate_response_body = 'New body'; | 140 var alternate_response_body = 'New body'; |
128 var alternate_response = new Response(alternate_response_body, | 141 var alternate_response = new Response(alternate_response_body, |
129 { statusText: 'New status' }); | 142 { statusText: 'New status' }); |
130 return cache.put(new Request(test_url), | 143 return cache.put(new Request(test_url), |
131 new Response('Old body', { statusText: 'Old status' })) | 144 new Response('Old body', { statusText: 'Old status' })) |
132 .then(function() { | 145 .then(function() { |
133 return cache.put(new Request(test_url), alternate_response); | 146 return cache.put(new Request(test_url), alternate_response.clone()); |
134 }) | 147 }) |
135 .then(function() { | 148 .then(function() { |
136 return cache.match(test_url); | 149 return cache.match(test_url); |
137 }) | 150 }) |
138 .then(function(result) { | 151 .then(function(result) { |
139 assert_object_equals(result, alternate_response, | 152 assert_object_equals(result, alternate_response, |
140 'Cache.put should replace existing ' + | 153 'Cache.put should replace existing ' + |
141 'response with new response.'); | 154 'response with new response.'); |
142 return result.text(); | 155 return result.text(); |
143 }) | 156 }) |
144 .then(function(body) { | 157 .then(function(body) { |
145 assert_equals(body, alternate_response_body, | 158 assert_equals(body, alternate_response_body, |
146 'Cache put should store new response body.'); | 159 'Cache put should store new response body.'); |
147 }); | 160 }); |
148 }, 'Cache.put called twice with matching Requests and different Responses'); | 161 }, 'Cache.put called twice with matching Requests and different Responses'); |
149 | 162 |
150 cache_test(function(cache) { | 163 cache_test(function(cache) { |
151 var first_url = test_url; | 164 var first_url = test_url; |
152 var second_url = first_url + '#(O_o)'; | 165 var second_url = first_url + '#(O_o)'; |
153 var alternate_response_body = 'New body'; | 166 var alternate_response_body = 'New body'; |
154 var alternate_response = new Response(alternate_response_body, | 167 var alternate_response = new Response(alternate_response_body, |
155 { statusText: 'New status' }); | 168 { statusText: 'New status' }); |
156 return cache.put(new Request(first_url), | 169 return cache.put(new Request(first_url), |
157 new Response('Old body', { statusText: 'Old status' })) | 170 new Response('Old body', { statusText: 'Old status' })) |
158 .then(function() { | 171 .then(function() { |
159 return cache.put(new Request(second_url), alternate_response); | 172 return cache.put(new Request(second_url), alternate_response.clone()); |
160 }) | 173 }) |
161 .then(function() { | 174 .then(function() { |
162 return cache.match(test_url); | 175 return cache.match(test_url); |
163 }) | 176 }) |
164 .then(function(result) { | 177 .then(function(result) { |
165 assert_object_equals(result, alternate_response, | 178 assert_object_equals(result, alternate_response, |
166 'Cache.put should replace existing ' + | 179 'Cache.put should replace existing ' + |
167 'response with new response.'); | 180 'response with new response.'); |
168 return result.text(); | 181 return result.text(); |
169 }) | 182 }) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 cache_test(function(cache) { | 236 cache_test(function(cache) { |
224 return assert_promise_rejects( | 237 return assert_promise_rejects( |
225 cache.put(new Request(test_url), 'Hello world!'), | 238 cache.put(new Request(test_url), 'Hello world!'), |
226 new TypeError(), | 239 new TypeError(), |
227 'Cache.put should only accept a Response object as the response.'); | 240 'Cache.put should only accept a Response object as the response.'); |
228 }, 'Cache.put with an invalid response'); | 241 }, 'Cache.put with an invalid response'); |
229 | 242 |
230 cache_test(function(cache) { | 243 cache_test(function(cache) { |
231 return assert_promise_rejects( | 244 return assert_promise_rejects( |
232 cache.put(new Request('file:///etc/passwd'), | 245 cache.put(new Request('file:///etc/passwd'), |
233 new Response(test_response_body)), | 246 new Response(test_body)), |
234 new TypeError(), | 247 new TypeError(), |
235 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); | 248 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); |
236 }, 'Cache.put with a non-HTTP/HTTPS request'); | 249 }, 'Cache.put with a non-HTTP/HTTPS request'); |
237 | 250 |
238 cache_test(function(cache) { | 251 cache_test(function(cache) { |
239 var response = new Response(test_response_body); | 252 var response = new Response(test_body); |
240 return cache.put(new Request('relative-url'), response) | 253 return cache.put(new Request('relative-url'), response.clone()) |
241 .then(function() { | 254 .then(function() { |
242 return cache.match(new URL('relative-url', location.href).href); | 255 return cache.match(new URL('relative-url', location.href).href); |
243 }) | 256 }) |
244 .then(function(result) { | 257 .then(function(result) { |
245 assert_object_equals(result, response, | 258 assert_object_equals(result, response, |
246 'Cache.put should accept a relative URL ' + | 259 'Cache.put should accept a relative URL ' + |
247 'as the request.'); | 260 'as the request.'); |
248 }); | 261 }); |
249 }, 'Cache.put with a relative URL'); | 262 }, 'Cache.put with a relative URL'); |
250 | 263 |
251 cache_test(function(cache) { | 264 cache_test(function(cache) { |
252 var request = new Request('http://example.com/foo', { method: 'HEAD' }); | 265 var request = new Request('http://example.com/foo', { method: 'HEAD' }); |
253 return assert_promise_rejects( | 266 return assert_promise_rejects( |
254 cache.put(request, new Response(test_response_body)), | 267 cache.put(request, new Response(test_body)), |
255 new TypeError(), | 268 new TypeError(), |
256 'Cache.put should throw a TypeError for non-GET requests.'); | 269 'Cache.put should throw a TypeError for non-GET requests.'); |
257 }, 'Cache.put with a non-GET request'); | 270 }, 'Cache.put with a non-GET request'); |
258 | 271 |
259 cache_test(function(cache) { | 272 cache_test(function(cache) { |
260 return assert_promise_rejects( | 273 return assert_promise_rejects( |
261 cache.put(new Request(test_url), null), | 274 cache.put(new Request(test_url), null), |
262 new TypeError(), | 275 new TypeError(), |
263 'Cache.put should throw a TypeError for an empty response.'); | 276 'Cache.put should throw a TypeError for an empty response.'); |
264 }, 'Cache.put with an empty response'); | 277 }, 'Cache.put with an empty response'); |
| 278 |
| 279 cache_test(function(cache) { |
| 280 var request = new Request(test_url, {body: test_body}); |
| 281 assert_false(request.bodyUsed, |
| 282 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + |
| 283 'Request.bodyUsed should be initially false.'); |
| 284 var copy = new Request(request); |
| 285 assert_true(request.bodyUsed, |
| 286 '[https://fetch.spec.whatwg.org/#dom-request] ' + |
| 287 'Request constructor should set input\'s used flag.'); |
| 288 return assert_promise_rejects( |
| 289 cache.put(request, new Response(test_body)), |
| 290 new TypeError(), |
| 291 'Cache.put should throw a TypeError for a request with used body.'); |
| 292 }, 'Cache.put with a used request body'); |
| 293 |
| 294 cache_test(function(cache) { |
| 295 var response = new Response(test_body); |
| 296 assert_false(response.bodyUsed, |
| 297 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + |
| 298 'Response.bodyUsed should be initially false.'); |
| 299 response.text().then(function() { |
| 300 assert_true( |
| 301 response.bodyUsed, |
| 302 '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' + |
| 303 'The text() method should consume the body of the response.'); |
| 304 return assert_promise_rejects( |
| 305 cache.put(new Request(test_url), response), |
| 306 new TypeError(), |
| 307 'Cache.put should throw a TypeError for a response with used body.'); |
| 308 }); |
| 309 }, 'Cache.put with a used response body'); |
OLD | NEW |