OLD | NEW |
(Empty) | |
| 1 importScripts('worker-test-harness.js'); |
| 2 // |
| 3 // Variations: |
| 4 // - ignoreSearch : Ignores search parameter. |
| 5 // - prefixMatch : only matches a prefix of the URL. |
| 6 // - ignoreVary : ignores a 'Vary' header if there is one. |
| 7 var entries = { |
| 8 a: { |
| 9 request: new Request('http://example.com/a'), |
| 10 response: new Response('') |
| 11 }, |
| 12 |
| 13 b: { |
| 14 request: new Request('http://example.com/b'), |
| 15 response: new Response('') |
| 16 }, |
| 17 |
| 18 a_with_query: { |
| 19 request: new Request('http://example.com/a?q=r'), |
| 20 response: new Response('') |
| 21 }, |
| 22 |
| 23 A: { |
| 24 request: new Request('http://example.com/A'), |
| 25 response: new Response('') |
| 26 }, |
| 27 |
| 28 a_https: { |
| 29 request: new Request('https://example.com/a'), |
| 30 response: new Response('') |
| 31 }, |
| 32 |
| 33 a_org: { |
| 34 request: new Request('http://example.org/a'), |
| 35 response: new Response('') |
| 36 }, |
| 37 |
| 38 cat: { |
| 39 request: new Request('http://example.com/cat'), |
| 40 response: new Response('') |
| 41 }, |
| 42 |
| 43 cat_with_fragment: { |
| 44 request: new Request('http://example.com/cat#mouse'), |
| 45 response: new Response('') |
| 46 }, |
| 47 |
| 48 cat_in_the_hat: { |
| 49 request: new Request('http://example.com/cat/in/the/hat'), |
| 50 response: new Response('') |
| 51 }, |
| 52 |
| 53 c_is_for_cookie: { |
| 54 request: new Request('http://example.com/c', |
| 55 {headers: {'Cookies': 'is-for-cookie'}}), |
| 56 response: new Response('', |
| 57 {headers: {'Vary': 'Cookies'}}) |
| 58 }, |
| 59 |
| 60 c_is_for_cake: { |
| 61 request: new Request('http://example.com/c', |
| 62 {headers: {'Cookies': 'is-for-cake'}}), |
| 63 response: new Response('', |
| 64 {headers: {'Vary': 'Cookies'}}) |
| 65 }, |
| 66 |
| 67 c_x_key_is_1: { |
| 68 request: new Request('http://example.com/c', |
| 69 {headers: {'Cookies': 'x', 'X-Key': '1'}}), |
| 70 response: new Response('', |
| 71 {headers: {'Vary': '*'}}) |
| 72 }, |
| 73 |
| 74 c_y_key_is_1: { |
| 75 request: new Request('http://example.com/c', |
| 76 {headers: {'Cookies': 'y', 'X-Key': '1'}}), |
| 77 response: new Response('', |
| 78 {headers: {'Vary': 'Cookies,X-Key'}}) |
| 79 }, |
| 80 |
| 81 c_y_key_is_2: { |
| 82 request: new Request('http://example.com/c', |
| 83 {headers: {'Cookies': 'y', 'X-Key': '2'}}), |
| 84 response: new Response('', |
| 85 {headers: {'Vary': 'Cookies,X-Key'}}) |
| 86 } |
| 87 }; |
| 88 |
| 89 promise_test(function(t) { |
| 90 var cache; |
| 91 return create_temporary_cache(t) |
| 92 .then(function(result) { |
| 93 cache = result; |
| 94 return put_all_requests_in_cache(cache); |
| 95 }) |
| 96 |
| 97 .then(function() { |
| 98 |
| 99 promise_test(function() { |
| 100 return cache.matchAll(entries.a.request.url) |
| 101 .then(function(result) { |
| 102 assert_array_equals(result, [entries.a.response], |
| 103 'Cache.matchAll should match by URL.'); |
| 104 }); |
| 105 }, 'Cache.matchAll with URL'); |
| 106 |
| 107 promise_test(function() { |
| 108 return cache.matchAll(entries.a.request) |
| 109 .then(function(result) { |
| 110 assert_array_equals(result, [entries.a.response], |
| 111 'Cache.matchAll should match by ' + |
| 112 'Request.'); |
| 113 }); |
| 114 }, 'Cache.matchAll with Request'); |
| 115 |
| 116 promise_test(function() { |
| 117 return cache.matchAll(new Request(entries.a.request.url)) |
| 118 .then(function(result) { |
| 119 assert_array_equals(result, [entries.a.response], |
| 120 'Cache.matchAll should match by ' + |
| 121 'Request.'); |
| 122 }); |
| 123 }, 'Cache.matchAll with new Request'); |
| 124 |
| 125 promise_test(function() { |
| 126 return cache.match(entries.a.request) |
| 127 .then(function(result) { |
| 128 assert_equals(result, entries.a.response, |
| 129 'Cache.match should match by Request.'); |
| 130 }); |
| 131 }, 'Cache.match with Request'); |
| 132 |
| 133 promise_test(function() { |
| 134 return cache.matchAll(entries.a.request, { ignoreSearch: true }) |
| 135 .then(function(result) { |
| 136 assert_array_equivalent( |
| 137 result, |
| 138 [ |
| 139 entries.a.response, |
| 140 entries.a_with_query.response |
| 141 ], |
| 142 'Cache.matchAll with ignoreSearch should ignore the ' + |
| 143 'search parameters of cached request.'); |
| 144 }); |
| 145 }, 'Cache.matchAll with ignoreSearch option ' + |
| 146 '(request with no search parameters)'); |
| 147 |
| 148 promise_test(function() { |
| 149 return cache.matchAll(entries.a_with_query.request, |
| 150 { ignoreSearch: true }) |
| 151 .then(function(result) { |
| 152 assert_array_equivalent( |
| 153 result, |
| 154 [ |
| 155 entries.a.response, |
| 156 entries.a_with_query.response |
| 157 ], |
| 158 'Cache.matchAll with ignoreSearch should ignore the ' + |
| 159 'search parameters of request.'); |
| 160 }); |
| 161 }, 'Cache.matchAll with ignoreSearch option ' + |
| 162 '(request with search parameter)'); |
| 163 |
| 164 promise_test(function() { |
| 165 return cache.matchAll(entries.cat.request) |
| 166 .then(function(result) { |
| 167 assert_array_equivalent( |
| 168 result, |
| 169 [ |
| 170 entries.cat.response, |
| 171 entries.cat_with_fragment.response |
| 172 ], |
| 173 'Cache.matchAll should ignore URL hash.'); |
| 174 }); |
| 175 }, 'Cache.matchAll with request containing hash'); |
| 176 |
| 177 promise_test(function() { |
| 178 return cache.matchAll('http') |
| 179 .then(function(result) { |
| 180 assert_array_equivalent( |
| 181 result, [], |
| 182 'Cache.matchAll should treat query as a URL and not ' + |
| 183 'just a string fragment.'); |
| 184 }); |
| 185 }, 'Cache.matchAll with string fragment \'http\' as query'); |
| 186 |
| 187 promise_test(function() { |
| 188 return cache.matchAll('http://example.com/cat', |
| 189 { prefixMatch: true }) |
| 190 .then(function(result) { |
| 191 assert_array_equivalent( |
| 192 result, |
| 193 [ |
| 194 entries.cat.response, |
| 195 entries.cat_with_fragment.response, |
| 196 entries.cat_in_the_hat.response |
| 197 ], |
| 198 'Cache.matchAll should honor prefixMatch.'); |
| 199 }); |
| 200 }, 'Cache.matchAll with prefixMatch option'); |
| 201 |
| 202 promise_test(function() { |
| 203 return cache.matchAll('http://example.com/cat/', |
| 204 { prefixMatch: true }) |
| 205 .then(function(result) { |
| 206 assert_array_equivalent( |
| 207 result, [entries.cat_in_the_hat.response], |
| 208 'Cache.matchAll should honor prefixMatch.'); |
| 209 }); |
| 210 }, 'Cache.matchAll with prefixMatch option'); |
| 211 }); |
| 212 }, 'Cache.match'); |
| 213 |
| 214 // Helpers --- |
| 215 |
| 216 function put_all_requests_in_cache(cache) { |
| 217 return Promise.all(Object.keys(entries).map(function(k) { |
| 218 return cache.put(entries[k].request, entries[k].response); |
| 219 })); |
| 220 } |
| 221 |
| 222 // Assert that the two arrays |actual| and |expected| contain the same set of |
| 223 // elements ignoring order. This is necessary because the result array returned |
| 224 // by Cache.matchAll() does not guarantee any specific ordering. |
| 225 // |
| 226 // |expected| is assumed to not contain any duplicates. |
| 227 function assert_array_equivalent(actual, expected, description) { |
| 228 assert_true(Array.isArray(actual), description); |
| 229 assert_equals(actual.length, expected.length, description); |
| 230 expected.forEach(function(element) { |
| 231 assert_in_array(element, actual, description); |
| 232 }); |
| 233 } |
OLD | NEW |