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