| OLD | NEW |
| (Empty) |
| 1 if (self.importScripts) { | |
| 2 importScripts('/resources/testharness.js'); | |
| 3 importScripts('../resources/test-helpers.js'); | |
| 4 } | |
| 5 | |
| 6 var test_url = 'https://example.com/foo'; | |
| 7 var test_body = 'Hello world!'; | |
| 8 | |
| 9 cache_test(function(cache) { | |
| 10 var request = new Request(test_url); | |
| 11 var response = new Response(test_body); | |
| 12 return cache.put(request, response) | |
| 13 .then(function(result) { | |
| 14 assert_equals(result, undefined, | |
| 15 'Cache.put should resolve with undefined on success.'); | |
| 16 }); | |
| 17 }, 'Cache.put called with simple Request and Response'); | |
| 18 | |
| 19 cache_test(function(cache) { | |
| 20 var test_url = new URL('../resources/simple.txt', location.href).href; | |
| 21 var request = new Request(test_url); | |
| 22 var response; | |
| 23 return fetch(test_url) | |
| 24 .then(function(fetch_result) { | |
| 25 response = fetch_result.clone(); | |
| 26 return cache.put(request, fetch_result); | |
| 27 }) | |
| 28 .then(function() { | |
| 29 return cache.match(test_url); | |
| 30 }) | |
| 31 .then(function(result) { | |
| 32 assert_response_equals(result, response, | |
| 33 'Cache.put should update the cache with ' + | |
| 34 'new request and response.'); | |
| 35 return result.text(); | |
| 36 }) | |
| 37 .then(function(body) { | |
| 38 assert_equals(body, 'a simple text file\n', | |
| 39 'Cache.put should store response body.'); | |
| 40 }); | |
| 41 }, 'Cache.put called with Request and Response from fetch()'); | |
| 42 | |
| 43 cache_test(function(cache) { | |
| 44 var request = new Request(test_url); | |
| 45 var response = new Response(test_body); | |
| 46 assert_false(request.bodyUsed, | |
| 47 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | |
| 48 'Request.bodyUsed should be initially false.'); | |
| 49 return cache.put(request, response) | |
| 50 .then(function() { | |
| 51 assert_false(request.bodyUsed, | |
| 52 'Cache.put should not mark empty request\'s body used'); | |
| 53 }); | |
| 54 }, 'Cache.put with Request without a body'); | |
| 55 | |
| 56 cache_test(function(cache) { | |
| 57 var request = new Request(test_url); | |
| 58 var response = new Response(); | |
| 59 assert_false(response.bodyUsed, | |
| 60 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | |
| 61 'Response.bodyUsed should be initially false.'); | |
| 62 return cache.put(request, response) | |
| 63 .then(function() { | |
| 64 assert_false(response.bodyUsed, | |
| 65 'Cache.put should not mark empty response\'s body used'); | |
| 66 }); | |
| 67 }, 'Cache.put with Response without a body'); | |
| 68 | |
| 69 cache_test(function(cache) { | |
| 70 var request = new Request(test_url); | |
| 71 var response = new Response(test_body); | |
| 72 return cache.put(request, response.clone()) | |
| 73 .then(function() { | |
| 74 return cache.match(test_url); | |
| 75 }) | |
| 76 .then(function(result) { | |
| 77 assert_response_equals(result, response, | |
| 78 'Cache.put should update the cache with ' + | |
| 79 'new Request and Response.'); | |
| 80 }); | |
| 81 }, 'Cache.put with a Response containing an empty URL'); | |
| 82 | |
| 83 cache_test(function(cache) { | |
| 84 var request = new Request(test_url); | |
| 85 var response = new Response('', { | |
| 86 status: 200, | |
| 87 headers: [['Content-Type', 'text/plain']] | |
| 88 }); | |
| 89 return cache.put(request, response) | |
| 90 .then(function() { | |
| 91 return cache.match(test_url); | |
| 92 }) | |
| 93 .then(function(result) { | |
| 94 assert_equals(result.status, 200, 'Cache.put should store status.'); | |
| 95 assert_equals(result.headers.get('Content-Type'), 'text/plain', | |
| 96 'Cache.put should store headers.'); | |
| 97 return result.text(); | |
| 98 }) | |
| 99 .then(function(body) { | |
| 100 assert_equals(body, '', | |
| 101 'Cache.put should store response body.'); | |
| 102 }); | |
| 103 }, 'Cache.put with an empty response body'); | |
| 104 | |
| 105 cache_test(function(cache) { | |
| 106 var test_url = new URL('../resources/fetch-status.php?status=500', location.
href).href; | |
| 107 var request = new Request(test_url); | |
| 108 var response; | |
| 109 return fetch(test_url) | |
| 110 .then(function(fetch_result) { | |
| 111 assert_equals(fetch_result.status, 500, | |
| 112 'Test framework error: The status code should be 500.'); | |
| 113 response = fetch_result.clone(); | |
| 114 return cache.put(request, fetch_result); | |
| 115 }) | |
| 116 .then(function() { | |
| 117 return cache.match(test_url); | |
| 118 }) | |
| 119 .then(function(result) { | |
| 120 assert_response_equals(result, response, | |
| 121 'Cache.put should update the cache with ' + | |
| 122 'new request and response.'); | |
| 123 return result.text(); | |
| 124 }) | |
| 125 .then(function(body) { | |
| 126 assert_equals(body, '', | |
| 127 'Cache.put should store response body.'); | |
| 128 }); | |
| 129 }, 'Cache.put with HTTP 500 response'); | |
| 130 | |
| 131 cache_test(function(cache, test) { | |
| 132 var test_url = new URL('../resources/fetch-status.php?status=206', location.
href).href; | |
| 133 var request = new Request(test_url); | |
| 134 var response; | |
| 135 return fetch(test_url) | |
| 136 .then(function(fetch_result) { | |
| 137 assert_equals(fetch_result.status, 206, | |
| 138 'Test framework error: The status code should be 206.'); | |
| 139 response = fetch_result.clone(); | |
| 140 return promise_rejects(test, new TypeError, cache.put(request, fetch_r
esult)); | |
| 141 }); | |
| 142 }, 'Cache.put with HTTP 206 response'); | |
| 143 | |
| 144 cache_test(function(cache) { | |
| 145 var alternate_response_body = 'New body'; | |
| 146 var alternate_response = new Response(alternate_response_body, | |
| 147 { statusText: 'New status' }); | |
| 148 return cache.put(new Request(test_url), | |
| 149 new Response('Old body', { statusText: 'Old status' })) | |
| 150 .then(function() { | |
| 151 return cache.put(new Request(test_url), alternate_response.clone()); | |
| 152 }) | |
| 153 .then(function() { | |
| 154 return cache.match(test_url); | |
| 155 }) | |
| 156 .then(function(result) { | |
| 157 assert_response_equals(result, alternate_response, | |
| 158 'Cache.put should replace existing ' + | |
| 159 'response with new response.'); | |
| 160 return result.text(); | |
| 161 }) | |
| 162 .then(function(body) { | |
| 163 assert_equals(body, alternate_response_body, | |
| 164 'Cache put should store new response body.'); | |
| 165 }); | |
| 166 }, 'Cache.put called twice with matching Requests and different Responses'); | |
| 167 | |
| 168 cache_test(function(cache) { | |
| 169 var first_url = test_url; | |
| 170 var second_url = first_url + '#(O_o)'; | |
| 171 var alternate_response_body = 'New body'; | |
| 172 var alternate_response = new Response(alternate_response_body, | |
| 173 { statusText: 'New status' }); | |
| 174 return cache.put(new Request(first_url), | |
| 175 new Response('Old body', { statusText: 'Old status' })) | |
| 176 .then(function() { | |
| 177 return cache.put(new Request(second_url), alternate_response.clone()); | |
| 178 }) | |
| 179 .then(function() { | |
| 180 return cache.match(test_url); | |
| 181 }) | |
| 182 .then(function(result) { | |
| 183 assert_response_equals(result, alternate_response, | |
| 184 'Cache.put should replace existing ' + | |
| 185 'response with new response.'); | |
| 186 return result.text(); | |
| 187 }) | |
| 188 .then(function(body) { | |
| 189 assert_equals(body, alternate_response_body, | |
| 190 'Cache put should store new response body.'); | |
| 191 }); | |
| 192 }, 'Cache.put called twice with request URLs that differ only by a fragment'); | |
| 193 | |
| 194 cache_test(function(cache) { | |
| 195 var url = 'http://example.com/foo'; | |
| 196 return cache.put(url, new Response('some body')) | |
| 197 .then(function() { return cache.match(url); }) | |
| 198 .then(function(response) { return response.text(); }) | |
| 199 .then(function(body) { | |
| 200 assert_equals(body, 'some body', | |
| 201 'Cache.put should accept a string as request.'); | |
| 202 }); | |
| 203 }, 'Cache.put with a string request'); | |
| 204 | |
| 205 cache_test(function(cache, test) { | |
| 206 return promise_rejects( | |
| 207 test, | |
| 208 new TypeError(), | |
| 209 cache.put(new Request(test_url), 'Hello world!'), | |
| 210 'Cache.put should only accept a Response object as the response.'); | |
| 211 }, 'Cache.put with an invalid response'); | |
| 212 | |
| 213 cache_test(function(cache, test) { | |
| 214 return promise_rejects( | |
| 215 test, | |
| 216 new TypeError(), | |
| 217 cache.put(new Request('file:///etc/passwd'), | |
| 218 new Response(test_body)), | |
| 219 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); | |
| 220 }, 'Cache.put with a non-HTTP/HTTPS request'); | |
| 221 | |
| 222 cache_test(function(cache) { | |
| 223 var response = new Response(test_body); | |
| 224 return cache.put(new Request('relative-url'), response.clone()) | |
| 225 .then(function() { | |
| 226 return cache.match(new URL('relative-url', location.href).href); | |
| 227 }) | |
| 228 .then(function(result) { | |
| 229 assert_response_equals(result, response, | |
| 230 'Cache.put should accept a relative URL ' + | |
| 231 'as the request.'); | |
| 232 }); | |
| 233 }, 'Cache.put with a relative URL'); | |
| 234 | |
| 235 cache_test(function(cache, test) { | |
| 236 var request = new Request('http://example.com/foo', { method: 'HEAD' }); | |
| 237 return promise_rejects( | |
| 238 test, | |
| 239 new TypeError(), | |
| 240 cache.put(request, new Response(test_body)), | |
| 241 'Cache.put should throw a TypeError for non-GET requests.'); | |
| 242 }, 'Cache.put with a non-GET request'); | |
| 243 | |
| 244 cache_test(function(cache, test) { | |
| 245 return promise_rejects( | |
| 246 test, | |
| 247 new TypeError(), | |
| 248 cache.put(new Request(test_url), null), | |
| 249 'Cache.put should throw a TypeError for a null response.'); | |
| 250 }, 'Cache.put with a null response'); | |
| 251 | |
| 252 cache_test(function(cache, test) { | |
| 253 var request = new Request(test_url, {method: 'POST', body: test_body}); | |
| 254 return promise_rejects( | |
| 255 test, | |
| 256 new TypeError(), | |
| 257 cache.put(request, new Response(test_body)), | |
| 258 'Cache.put should throw a TypeError for a POST request.'); | |
| 259 }, 'Cache.put with a POST request'); | |
| 260 | |
| 261 cache_test(function(cache) { | |
| 262 var response = new Response(test_body); | |
| 263 assert_false(response.bodyUsed, | |
| 264 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | |
| 265 'Response.bodyUsed should be initially false.'); | |
| 266 return response.text().then(function() { | |
| 267 assert_true( | |
| 268 response.bodyUsed, | |
| 269 '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' + | |
| 270 'The text() method should make the body disturbed.'); | |
| 271 var request = new Request(test_url); | |
| 272 return cache.put(request, response).then(() => { | |
| 273 assert_unreached('cache.put should be rejected'); | |
| 274 }, () => {}); | |
| 275 }); | |
| 276 }, 'Cache.put with a used response body'); | |
| 277 | |
| 278 cache_test(function(cache) { | |
| 279 var response = new Response(test_body); | |
| 280 return cache.put(new Request(test_url), response) | |
| 281 .then(function() { | |
| 282 assert_throws(new TypeError(), () => response.body.getReader()); | |
| 283 }); | |
| 284 }, 'getReader() after Cache.put'); | |
| 285 | |
| 286 cache_test(function(cache, test) { | |
| 287 return promise_rejects( | |
| 288 test, | |
| 289 new TypeError(), | |
| 290 cache.put(new Request(test_url), | |
| 291 new Response(test_body, { headers: { VARY: '*' }})), | |
| 292 'Cache.put should reject VARY:* Responses with a TypeError.'); | |
| 293 }, 'Cache.put with a VARY:* Response'); | |
| 294 | |
| 295 cache_test(function(cache, test) { | |
| 296 return promise_rejects( | |
| 297 test, | |
| 298 new TypeError(), | |
| 299 cache.put(new Request(test_url), | |
| 300 new Response(test_body, | |
| 301 { headers: { VARY: 'Accept-Language,*' }})), | |
| 302 'Cache.put should reject Responses with an embedded VARY:* with a ' + | |
| 303 'TypeError.'); | |
| 304 }, 'Cache.put with an embedded VARY:* Response'); | |
| 305 | |
| 306 done(); | |
| OLD | NEW |