| OLD | NEW |
| (Empty) |
| 1 if (self.importScripts) { | |
| 2 importScripts('/resources/testharness.js'); | |
| 3 importScripts('../resources/test-helpers.js'); | |
| 4 } | |
| 5 | |
| 6 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 7 return cache.match('not-present-in-the-cache') | |
| 8 .then(function(result) { | |
| 9 assert_equals(result, undefined, | |
| 10 'Cache.match failures should resolve with undefined.'); | |
| 11 }); | |
| 12 }, 'Cache.match with no matching entries'); | |
| 13 | |
| 14 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 15 return cache.match(entries.a.request.url) | |
| 16 .then(function(result) { | |
| 17 assert_response_equals(result, entries.a.response, | |
| 18 'Cache.match should match by URL.'); | |
| 19 }); | |
| 20 }, 'Cache.match with URL'); | |
| 21 | |
| 22 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 23 return cache.match(entries.a.request) | |
| 24 .then(function(result) { | |
| 25 assert_response_equals(result, entries.a.response, | |
| 26 'Cache.match should match by Request.'); | |
| 27 }); | |
| 28 }, 'Cache.match with Request'); | |
| 29 | |
| 30 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 31 var alt_response = new Response('', {status: 201}); | |
| 32 | |
| 33 return self.caches.open('second_matching_cache') | |
| 34 .then(function(cache) { | |
| 35 return cache.put(entries.a.request, alt_response.clone()); | |
| 36 }) | |
| 37 .then(function() { | |
| 38 return cache.match(entries.a.request) | |
| 39 }) | |
| 40 .then(function(result) { | |
| 41 assert_response_equals( | |
| 42 result, entries.a.response, | |
| 43 'Cache.match should match the first cache.'); | |
| 44 }); | |
| 45 }, 'Cache.match with multiple cache hits'); | |
| 46 | |
| 47 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 48 return cache.match(new Request(entries.a.request.url)) | |
| 49 .then(function(result) { | |
| 50 assert_response_equals(result, entries.a.response, | |
| 51 'Cache.match should match by Request.'); | |
| 52 }); | |
| 53 }, 'Cache.match with new Request'); | |
| 54 | |
| 55 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 56 return cache.match(new Request(entries.a.request.url, {method: 'HEAD'})) | |
| 57 .then(function(result) { | |
| 58 assert_equals(result, undefined, | |
| 59 'Cache.match should not match HEAD Request.'); | |
| 60 }); | |
| 61 }, 'Cache.match with HEAD'); | |
| 62 | |
| 63 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 64 return cache.match(entries.a.request, | |
| 65 {ignoreSearch: true}) | |
| 66 .then(function(result) { | |
| 67 assert_response_in_array( | |
| 68 result, | |
| 69 [ | |
| 70 entries.a.response, | |
| 71 entries.a_with_query.response | |
| 72 ], | |
| 73 'Cache.match with ignoreSearch should ignore the ' + | |
| 74 'search parameters of cached request.'); | |
| 75 }); | |
| 76 }, | |
| 77 'Cache.match with ignoreSearch option (request with no search ' + | |
| 78 'parameters)'); | |
| 79 | |
| 80 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 81 return cache.match(entries.a_with_query.request, | |
| 82 {ignoreSearch: true}) | |
| 83 .then(function(result) { | |
| 84 assert_response_in_array( | |
| 85 result, | |
| 86 [ | |
| 87 entries.a.response, | |
| 88 entries.a_with_query.response | |
| 89 ], | |
| 90 'Cache.match with ignoreSearch should ignore the ' + | |
| 91 'search parameters of request.'); | |
| 92 }); | |
| 93 }, | |
| 94 'Cache.match with ignoreSearch option (request with search parameter)'); | |
| 95 | |
| 96 cache_test(function(cache) { | |
| 97 var request = new Request('http://example.com/'); | |
| 98 var head_request = new Request('http://example.com/', {method: 'HEAD'}); | |
| 99 var response = new Response('foo'); | |
| 100 return cache.put(request.clone(), response.clone()) | |
| 101 .then(function() { | |
| 102 return cache.match(head_request.clone()); | |
| 103 }) | |
| 104 .then(function(result) { | |
| 105 assert_equals( | |
| 106 result, undefined, | |
| 107 'Cache.match should resolve as undefined with a ' + | |
| 108 'mismatched method.'); | |
| 109 return cache.match(head_request.clone(), | |
| 110 {ignoreMethod: true}); | |
| 111 }) | |
| 112 .then(function(result) { | |
| 113 assert_response_equals( | |
| 114 result, response, | |
| 115 'Cache.match with ignoreMethod should ignore the ' + | |
| 116 'method of request.'); | |
| 117 }); | |
| 118 }, 'Cache.match supports ignoreMethod'); | |
| 119 | |
| 120 cache_test(function(cache) { | |
| 121 var vary_request = new Request('http://example.com/c', | |
| 122 {headers: {'Cookies': 'is-for-cookie'}}); | |
| 123 var vary_response = new Response('', {headers: {'Vary': 'Cookies'}}); | |
| 124 var mismatched_vary_request = new Request('http://example.com/c'); | |
| 125 | |
| 126 return cache.put(vary_request.clone(), vary_response.clone()) | |
| 127 .then(function() { | |
| 128 return cache.match(mismatched_vary_request.clone()); | |
| 129 }) | |
| 130 .then(function(result) { | |
| 131 assert_equals( | |
| 132 result, undefined, | |
| 133 'Cache.match should resolve as undefined with a ' + | |
| 134 'mismatched vary.'); | |
| 135 return cache.match(mismatched_vary_request.clone(), | |
| 136 {ignoreVary: true}); | |
| 137 }) | |
| 138 .then(function(result) { | |
| 139 assert_response_equals( | |
| 140 result, vary_response, | |
| 141 'Cache.match with ignoreVary should ignore the ' + | |
| 142 'vary of request.'); | |
| 143 }); | |
| 144 }, 'Cache.match supports ignoreVary'); | |
| 145 | |
| 146 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 147 return cache.match(entries.cat.request.url + '#mouse') | |
| 148 .then(function(result) { | |
| 149 assert_response_equals(result, entries.cat.response, | |
| 150 'Cache.match should ignore URL fragment.'); | |
| 151 }); | |
| 152 }, 'Cache.match with URL containing fragment'); | |
| 153 | |
| 154 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 155 return cache.match('http') | |
| 156 .then(function(result) { | |
| 157 assert_equals( | |
| 158 result, undefined, | |
| 159 'Cache.match should treat query as a URL and not ' + | |
| 160 'just a string fragment.'); | |
| 161 }); | |
| 162 }, 'Cache.match with string fragment "http" as query'); | |
| 163 | |
| 164 prepopulated_cache_test(vary_entries, function(cache, entries) { | |
| 165 return cache.match('http://example.com/c') | |
| 166 .then(function(result) { | |
| 167 assert_response_in_array( | |
| 168 result, | |
| 169 [ | |
| 170 entries.vary_cookie_absent.response | |
| 171 ], | |
| 172 'Cache.match should honor "Vary" header.'); | |
| 173 }); | |
| 174 }, 'Cache.match with responses containing "Vary" header'); | |
| 175 | |
| 176 cache_test(function(cache) { | |
| 177 var request = new Request('http://example.com'); | |
| 178 var response; | |
| 179 var request_url = new URL('../resources/simple.txt', location.href).href; | |
| 180 return fetch(request_url) | |
| 181 .then(function(fetch_result) { | |
| 182 response = fetch_result; | |
| 183 assert_equals( | |
| 184 response.url, request_url, | |
| 185 '[https://fetch.spec.whatwg.org/#dom-response-url] ' + | |
| 186 'Reponse.url should return the URL of the response.'); | |
| 187 return cache.put(request, response.clone()); | |
| 188 }) | |
| 189 .then(function() { | |
| 190 return cache.match(request.url); | |
| 191 }) | |
| 192 .then(function(result) { | |
| 193 assert_response_equals( | |
| 194 result, response, | |
| 195 'Cache.match should return a Response object that has the same ' + | |
| 196 'properties as the stored response.'); | |
| 197 return cache.match(response.url); | |
| 198 }) | |
| 199 .then(function(result) { | |
| 200 assert_equals( | |
| 201 result, undefined, | |
| 202 'Cache.match should not match cache entry based on response URL.'); | |
| 203 }); | |
| 204 }, 'Cache.match with Request and Response objects with different URLs'); | |
| 205 | |
| 206 cache_test(function(cache) { | |
| 207 var request_url = new URL('../resources/simple.txt', location.href).href; | |
| 208 return fetch(request_url) | |
| 209 .then(function(fetch_result) { | |
| 210 return cache.put(new Request(request_url), fetch_result); | |
| 211 }) | |
| 212 .then(function() { | |
| 213 return cache.match(request_url); | |
| 214 }) | |
| 215 .then(function(result) { | |
| 216 return result.text(); | |
| 217 }) | |
| 218 .then(function(body_text) { | |
| 219 assert_equals(body_text, 'a simple text file\n', | |
| 220 'Cache.match should return a Response object with a ' + | |
| 221 'valid body.'); | |
| 222 }) | |
| 223 .then(function() { | |
| 224 return cache.match(request_url); | |
| 225 }) | |
| 226 .then(function(result) { | |
| 227 return result.text(); | |
| 228 }) | |
| 229 .then(function(body_text) { | |
| 230 assert_equals(body_text, 'a simple text file\n', | |
| 231 'Cache.match should return a Response object with a ' + | |
| 232 'valid body each time it is called.'); | |
| 233 }); | |
| 234 }, 'Cache.match invoked multiple times for the same Request/Response'); | |
| 235 | |
| 236 cache_test(function(cache) { | |
| 237 var request_url = new URL('../resources/simple.txt', location.href).href; | |
| 238 return fetch(request_url) | |
| 239 .then(function(fetch_result) { | |
| 240 return cache.put(new Request(request_url), fetch_result); | |
| 241 }) | |
| 242 .then(function() { | |
| 243 return cache.match(request_url); | |
| 244 }) | |
| 245 .then(function(result) { | |
| 246 return result.blob(); | |
| 247 }) | |
| 248 .then(function(blob) { | |
| 249 sliced = blob.slice(2,8); | |
| 250 | |
| 251 return new Promise(function (resolve, reject) { | |
| 252 reader = new FileReader(); | |
| 253 reader.onloadend = function(event) { | |
| 254 resolve(event.target.result); | |
| 255 } | |
| 256 reader.readAsText(sliced); | |
| 257 }); | |
| 258 }) | |
| 259 .then(function(text) { | |
| 260 assert_equals(text, 'simple', | |
| 261 'A Response blob returned by Cache.match should be ' + | |
| 262 'sliceable.' ); | |
| 263 }); | |
| 264 }, 'Cache.match blob should be sliceable'); | |
| 265 | |
| 266 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 267 var request = new Request(entries.a.request.clone(), {method: 'POST'}); | |
| 268 return cache.match(request) | |
| 269 .then(function(result) { | |
| 270 assert_equals(result, undefined, | |
| 271 'Cache.match should not find a match'); | |
| 272 }); | |
| 273 }, 'Cache.match with POST Request'); | |
| 274 | |
| 275 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 276 var response = entries.non_2xx_response.response; | |
| 277 return cache.match(entries.non_2xx_response.request.url) | |
| 278 .then(function(result) { | |
| 279 assert_response_equals( | |
| 280 result, entries.non_2xx_response.response, | |
| 281 'Cache.match should return a Response object that has the ' + | |
| 282 'same properties as a stored non-2xx response.'); | |
| 283 }); | |
| 284 }, 'Cache.match with a non-2xx Response'); | |
| 285 | |
| 286 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 287 var response = entries.error_response.response; | |
| 288 return cache.match(entries.error_response.request.url) | |
| 289 .then(function(result) { | |
| 290 assert_response_equals( | |
| 291 result, entries.error_response.response, | |
| 292 'Cache.match should return a Response object that has the ' + | |
| 293 'same properties as a stored network error response.'); | |
| 294 }); | |
| 295 }, 'Cache.match with a network error Response'); | |
| 296 | |
| 297 done(); | |
| OLD | NEW |