OLD | NEW |
---|---|
(Empty) | |
1 importScripts('worker-testharness.js'); | |
2 importScripts('/resources/testharness-helpers.js'); | |
3 | |
4 // | |
5 // Variations: | |
6 // - ignoreSearch : Ignores search parameter. | |
7 // - prefixMatch : only matches a prefix of the URL. | |
8 // - ignoreVary : ignores a 'Vary' header if there is one. | |
9 var simple_entries = { | |
10 a: { | |
11 request: new Request('http://example.com/a'), | |
12 response: new Response('') | |
13 }, | |
14 | |
15 b: { | |
16 request: new Request('http://example.com/b'), | |
17 response: new Response('') | |
18 }, | |
19 | |
20 a_with_query: { | |
21 request: new Request('http://example.com/a?q=r'), | |
22 response: new Response('') | |
23 }, | |
24 | |
25 A: { | |
26 request: new Request('http://example.com/A'), | |
27 response: new Response('') | |
28 }, | |
29 | |
30 a_https: { | |
31 request: new Request('https://example.com/a'), | |
32 response: new Response('') | |
33 }, | |
34 | |
35 a_org: { | |
36 request: new Request('http://example.org/a'), | |
37 response: new Response('') | |
38 }, | |
39 | |
40 cat: { | |
41 request: new Request('http://example.com/cat'), | |
42 response: new Response('') | |
43 }, | |
44 | |
45 cat_with_fragment: { | |
46 request: new Request('http://example.com/cat#mouse'), | |
47 response: new Response('') | |
48 }, | |
49 | |
50 cat_in_the_hat: { | |
51 request: new Request('http://example.com/cat/in/the/hat'), | |
52 response: new Response('') | |
53 } | |
54 }; | |
55 | |
56 var vary_entries = { | |
57 no_vary_header: { | |
58 request: new Request('http://example.com/c'), | |
59 response: new Response('') | |
60 }, | |
61 | |
62 vary_cookie_is_cookie: { | |
63 request: new Request('http://example.com/c', | |
64 {headers: {'Cookies': 'is-for-cookie'}}), | |
65 response: new Response('', | |
66 {headers: {'Vary': 'Cookies'}}) | |
67 }, | |
68 | |
69 vary_cookie_is_good: { | |
70 request: new Request('http://example.com/c', | |
71 {headers: {'Cookies': 'is-good-enough-for-me'}}), | |
72 response: new Response('', | |
73 {headers: {'Vary': 'Cookies'}}) | |
74 }, | |
75 | |
76 vary_cookie_absent: { | |
77 request: new Request('http://example.com/c'), | |
78 response: new Response('', | |
79 {headers: {'Vary': 'Cookies'}}) | |
80 }, | |
81 | |
82 vary_wildcard: { | |
83 request: new Request('http://example.com/c', | |
84 {headers: {'Cookies': 'x', 'X-Key': '1'}}), | |
85 response: new Response('', | |
86 {headers: {'Vary': '*'}}) | |
87 } | |
88 }; | |
89 | |
90 promise_test(function(t) { | |
91 return create_populated_cache(t, simple_entries) | |
92 .then(function(cache) { | |
93 return cache.matchAll(simple_entries.a.request.url); | |
94 }) | |
95 .then(function(result) { | |
96 assert_array_objects_equals(result, [simple_entries.a.response], | |
97 'Cache.matchAll should match by URL.'); | |
98 }); | |
99 }, 'Cache.matchAll with URL'); | |
100 | |
101 promise_test(function(t) { | |
102 return create_populated_cache(t, simple_entries) | |
103 .then(function(cache) { | |
104 return cache.match(simple_entries.a.request.url); | |
105 }) | |
106 .then(function(result) { | |
107 assert_object_equals(result, simple_entries.a.response, | |
108 'Cache.match should match by URL.'); | |
109 }); | |
110 }, 'Cache.match with URL'); | |
111 | |
112 promise_test(function(t) { | |
113 return create_populated_cache(t, simple_entries) | |
114 .then(function(cache) { | |
115 return cache.matchAll(simple_entries.a.request); | |
116 }) | |
117 .then(function(result) { | |
118 assert_array_objects_equals( | |
119 result, [simple_entries.a.response], | |
120 'Cache.matchAll should match by Request.'); | |
121 }); | |
122 }, 'Cache.matchAll with Request'); | |
123 | |
124 promise_test(function(t) { | |
125 return create_populated_cache(t, simple_entries) | |
126 .then(function(cache) { | |
127 return cache.match(simple_entries.a.request); | |
128 }) | |
129 .then(function(result) { | |
130 assert_object_equals(result, simple_entries.a.response, | |
131 'Cache.match should match by Request.'); | |
132 }); | |
133 }, 'Cache.match with Request'); | |
134 | |
135 promise_test(function(t) { | |
136 return create_populated_cache(t, simple_entries) | |
137 .then(function(cache) { | |
138 return cache.matchAll(new Request(simple_entries.a.request.url)); | |
139 }) | |
140 .then(function(result) { | |
141 assert_array_objects_equals( | |
142 result, [simple_entries.a.response], | |
143 'Cache.matchAll should match by Request.'); | |
144 }); | |
145 }, 'Cache.matchAll with new Request'); | |
146 | |
147 promise_test(function(t) { | |
148 return create_populated_cache(t, simple_entries) | |
149 .then(function(cache) { | |
150 return cache.match(new Request(simple_entries.a.request.url)); | |
151 }) | |
152 .then(function(result) { | |
153 assert_object_equals(result, simple_entries.a.response, | |
154 'Cache.match should match by Request.'); | |
155 }); | |
156 }, 'Cache.match with new Request'); | |
157 | |
158 promise_test(function(t) { | |
159 return create_populated_cache(t, simple_entries) | |
160 .then(function(cache) { | |
161 return cache.matchAll(simple_entries.a.request, | |
162 {ignoreSearch: true}); | |
163 }) | |
164 .then(function(result) { | |
165 assert_array_equivalent( | |
166 result, | |
167 [ | |
168 simple_entries.a.response, | |
169 simple_entries.a_with_query.response | |
170 ], | |
171 'Cache.matchAll with ignoreSearch should ignore the ' + | |
172 'search parameters of cached request.'); | |
173 }); | |
174 }, | |
175 'Cache.matchAll with ignoreSearch option (request with no search ' + | |
176 'parameters)'); | |
177 | |
178 promise_test(function(t) { | |
179 return create_populated_cache(t, simple_entries) | |
180 .then(function(cache) { | |
181 return cache.matchAll(simple_entries.a_with_query.request, | |
182 {ignoreSearch: true}); | |
183 }) | |
184 .then(function(result) { | |
185 assert_array_equivalent( | |
186 result, | |
187 [ | |
188 simple_entries.a.response, | |
189 simple_entries.a_with_query.response | |
190 ], | |
191 'Cache.matchAll with ignoreSearch should ignore the ' + | |
192 'search parameters of request.'); | |
193 }); | |
194 }, | |
195 'Cache.matchAll with ignoreSearch option (request with search parameter)'); | |
196 | |
197 promise_test(function(t) { | |
198 return create_populated_cache(t, simple_entries) | |
199 .then(function(cache) { | |
200 return cache.matchAll(simple_entries.cat.request); | |
201 }) | |
202 .then(function(result) { | |
203 assert_array_equivalent( | |
204 result, | |
205 [ | |
206 simple_entries.cat.response, | |
207 simple_entries.cat_with_fragment.response | |
208 ], | |
209 'Cache.matchAll should ignore URL hash.'); | |
210 }); | |
211 }, 'Cache.matchAll with request containing hash'); | |
212 | |
213 promise_test(function(t) { | |
214 return create_populated_cache(t, simple_entries) | |
215 .then(function(cache) { | |
216 return cache.matchAll('http'); | |
217 }) | |
218 .then(function(result) { | |
219 assert_array_equivalent( | |
220 result, [], | |
221 'Cache.matchAll should treat query as a URL and not ' + | |
222 'just a string fragment.'); | |
223 }); | |
224 }, 'Cache.matchAll with string fragment \'http\' as query'); | |
jsbell
2014/10/20 18:20:41
Fine as-is, but also feel free to use " here to av
asanka
2014/10/22 22:35:31
Done.
| |
225 | |
226 promise_test(function(t) { | |
227 return create_populated_cache(t, simple_entries) | |
228 .then(function(cache) { | |
229 return cache.matchAll('http://example.com/cat', | |
230 {prefixMatch: true}); | |
231 }) | |
232 .then(function(result) { | |
233 assert_array_equivalent( | |
234 result, | |
235 [ | |
236 simple_entries.cat.response, | |
237 simple_entries.cat_with_fragment.response, | |
238 simple_entries.cat_in_the_hat.response | |
239 ], | |
240 'Cache.matchAll should honor prefixMatch.'); | |
241 }); | |
242 }, 'Cache.matchAll with prefixMatch option'); | |
243 | |
244 promise_test(function(t) { | |
245 return create_populated_cache(t, simple_entries) | |
246 .then(function(cache) { | |
247 return cache.matchAll('http://example.com/cat/', | |
248 {prefixMatch: true}); | |
249 }) | |
250 .then(function(result) { | |
251 assert_array_equivalent( | |
252 result, [simple_entries.cat_in_the_hat.response], | |
253 'Cache.matchAll should honor prefixMatch.'); | |
254 }); | |
255 }, 'Cache.matchAll with prefixMatch option'); | |
256 | |
257 promise_test(function(t) { | |
258 var cache; | |
259 return create_populated_cache(t, vary_entries) | |
260 .then(function(populated_cache) { | |
261 cache = populated_cache; | |
262 return cache.matchAll('http://example.com/c'); | |
263 }) | |
264 .then(function(result) { | |
265 assert_array_equivalent( | |
266 result, | |
267 [ | |
268 vary_entries.no_vary_header.response, | |
269 vary_entries.vary_wildcard.response, | |
270 vary_entries.vary_cookie_absent.response | |
271 ], | |
272 'Cache.matchAll should exclude matches if a vary header is ' + | |
273 'missing in the query request, but is present in the cached ' + | |
274 'request.'); | |
275 }) | |
276 | |
277 .then(function() { | |
278 return cache.matchAll( | |
279 new Request('http://example.com/c', | |
280 {headers: {'Cookies': 'none-of-the-above'}})); | |
281 }) | |
282 .then(function(result) { | |
283 assert_array_equivalent( | |
284 result, | |
285 [ | |
286 vary_entries.no_vary_header.response, | |
287 vary_entries.vary_wildcard.response | |
288 ], | |
289 'Cache.matchAll should exclude matches if a vary header is ' + | |
290 'missing in the cached request, but is present in the query ' + | |
291 'request.'); | |
292 }) | |
293 | |
294 .then(function() { | |
295 return cache.matchAll( | |
296 new Request('http://example.com/c', | |
297 {headers: {'Cookies': 'is-for-cookie'}})); | |
298 }) | |
299 .then(function(result) { | |
300 assert_array_equivalent( | |
301 result, | |
302 [vary_entries.vary_cookie_is_cookie.response], | |
303 'Cache.matchAll should match the entire header if a vary header ' + | |
304 'is present in both the query and cached requests.'); | |
305 }); | |
306 }, 'Cache.matchAll with responses containing \'Vary\' header'); | |
307 | |
308 promise_test(function(t) { | |
309 return create_populated_cache(t, vary_entries) | |
310 .then(function(cache) { | |
311 return cache.match('http://example.com/c'); | |
312 }) | |
313 .then(function(result) { | |
314 assert_object_in_array( | |
315 result, | |
316 [ | |
317 vary_entries.no_vary_header.response, | |
318 vary_entries.vary_wildcard.response, | |
319 vary_entries.vary_cookie_absent.response | |
320 ], | |
321 'Cache.match should honor \'Vary\' header.'); | |
322 }); | |
323 }, 'Cache.match with responses containing \'Vary\' header'); | |
324 | |
325 promise_test(function(t) { | |
326 return create_populated_cache(t, vary_entries) | |
327 .then(function(cache) { | |
328 return cache.matchAll('http://example.com/c', | |
329 {ignoreVary: true}); | |
330 }) | |
331 .then(function(result) { | |
332 assert_array_equivalent( | |
333 result, | |
334 [ | |
335 vary_entries.no_vary_header.response, | |
336 vary_entries.vary_cookie_is_cookie.response, | |
337 vary_entries.vary_cookie_is_good.response, | |
338 vary_entries.vary_cookie_absent.response, | |
339 vary_entries.vary_wildcard.response | |
340 ], | |
341 'Cache.matchAll should honor \'ignoreVary\' parameter.'); | |
342 }); | |
343 }, 'Cache.matchAll with \'ignoreVary\' parameter'); | |
344 | |
345 // Helpers --- | |
346 | |
347 // Create a new Cache and populate it with requests from |entries|. The latter | |
348 // is expected to be an Object mapping arbitrary keys to objects of the form | |
349 // {request: <Request object>, response: <Response object>}. | |
350 // | |
351 // The return value is a Promise that resolves to the populated Cache. The | |
352 // lifetime of the Cache is bound to the duration of |test|. | |
353 function create_populated_cache(test, entries) { | |
354 return create_temporary_cache(test) | |
355 .then(function(cache) { | |
356 return Promise.all(Object.keys(entries).map(function(k) { | |
357 return cache.put(entries[k].request, entries[k].response); | |
358 })) | |
359 .then(function() { | |
360 return Promise.resolve(cache); | |
jsbell
2014/10/20 18:20:41
You should just be able to return `cache` here.
asanka
2014/10/22 22:35:32
Done.
| |
361 }); | |
362 }); | |
363 } | |
OLD | NEW |