Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(740)

Side by Side Diff: LayoutTests/http/tests/serviceworker/resources/cache-match-worker.js

Issue 425413002: [ServiceWorker] Tests for Cache (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 simple_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
54 var vary_entries = {
55 no_vary_header: {
56 request: new Request('http://example.com/c'),
57 response: new Response('')
58 },
59
60 vary_cookie_is_cookie: {
61 request: new Request('http://example.com/c',
62 {headers: {'Cookies': 'is-for-cookie'}}),
63 response: new Response('',
64 {headers: {'Vary': 'Cookies'}})
65 },
66
67 vary_cookie_is_good: {
68 request: new Request('http://example.com/c',
69 {headers: {'Cookies': 'is-good-enough-for-me'}}),
70 response: new Response('',
71 {headers: {'Vary': 'Cookies'}})
72 },
73
74 vary_cookie_absent: {
75 request: new Request('http://example.com/c'),
76 response: new Response('',
77 {headers: {'Vary': 'Cookies'}})
78 },
79
80 vary_wildcard: {
81 request: new Request('http://example.com/c',
82 {headers: {'Cookies': 'x', 'X-Key': '1'}}),
83 response: new Response('',
84 {headers: {'Vary': '*'}})
85 }
86 };
87
88 promise_test(function(t) {
89 return create_populated_cache(t, simple_entries)
90 .then(function(cache) {
91 return cache.matchAll(simple_entries.a.request.url);
92 })
93 .then(function(result) {
94 assert_array_equals(result, [simple_entries.a.response],
95 'Cache.matchAll should match by URL.');
96 });
97 }, 'Cache.matchAll with URL');
98
99 promise_test(function(t) {
100 return create_populated_cache(t, simple_entries)
101 .then(function(cache) {
102 return cache.match(simple_entries.a.request.url);
103 })
104 .then(function(result) {
105 assert_equals(result, simple_entries.a.response,
106 'Cache.match should match by URL.');
107 });
108 }, 'Cache.match with URL');
109
110 promise_test(function(t) {
111 return create_populated_cache(t, simple_entries)
112 .then(function(cache) {
113 return cache.matchAll(simple_entries.a.request);
114 })
115 .then(function(result) {
116 assert_array_equals(result, [simple_entries.a.response],
117 'Cache.matchAll should match by Request.');
118 });
119 }, 'Cache.matchAll with Request');
120
121 promise_test(function(t) {
122 return create_populated_cache(t, simple_entries)
123 .then(function(cache) {
124 return cache.match(simple_entries.a.request);
125 })
126 .then(function(result) {
127 assert_equals(result, simple_entries.a.response,
128 'Cache.match should match by Request.');
129 });
130 }, 'Cache.match with Request');
131
132 promise_test(function(t) {
133 return create_populated_cache(t, simple_entries)
134 .then(function(cache) {
135 return cache.matchAll(new Request(simple_entries.a.request.url));
136 })
137 .then(function(result) {
138 assert_array_equals(result, [simple_entries.a.response],
139 'Cache.matchAll should match by Request.');
140 });
141 }, 'Cache.matchAll with new Request');
142
143 promise_test(function(t) {
144 return create_populated_cache(t, simple_entries)
145 .then(function(cache) {
146 return cache.match(new Request(simple_entries.a.request.url));
147 })
148 .then(function(result) {
149 assert_equals(result, simple_entries.a.response,
150 'Cache.match should match by Request.');
151 });
152 }, 'Cache.match with new Request');
153
154 promise_test(function(t) {
155 return create_populated_cache(t, simple_entries)
156 .then(function(cache) {
157 return cache.matchAll(simple_entries.a.request,
158 {ignoreSearch: true});
159 })
160 .then(function(result) {
161 assert_array_equivalent(
162 result,
163 [
164 simple_entries.a.response,
165 simple_entries.a_with_query.response
166 ],
167 'Cache.matchAll with ignoreSearch should ignore the ' +
168 'search parameters of cached request.');
169 });
170 },
171 'Cache.matchAll with ignoreSearch option (request with no search ' +
172 'parameters)');
173
174 promise_test(function(t) {
175 return create_populated_cache(t, simple_entries)
176 .then(function(cache) {
177 return cache.matchAll(simple_entries.a_with_query.request,
178 {ignoreSearch: true});
179 })
180 .then(function(result) {
181 assert_array_equivalent(
182 result,
183 [
184 simple_entries.a.response,
185 simple_entries.a_with_query.response
186 ],
187 'Cache.matchAll with ignoreSearch should ignore the ' +
188 'search parameters of request.');
189 });
190 },
191 'Cache.matchAll with ignoreSearch option (request with search parameter)');
192
193 promise_test(function(t) {
194 return create_populated_cache(t, simple_entries)
195 .then(function(cache) {
196 return cache.matchAll(simple_entries.cat.request);
197 })
198 .then(function(result) {
199 assert_array_equivalent(
200 result,
201 [
202 simple_entries.cat.response,
203 simple_entries.cat_with_fragment.response
204 ],
205 'Cache.matchAll should ignore URL hash.');
206 });
207 }, 'Cache.matchAll with request containing hash');
208
209 promise_test(function(t) {
210 return create_populated_cache(t, simple_entries)
211 .then(function(cache) {
212 return cache.matchAll('http');
213 })
214 .then(function(result) {
215 assert_array_equivalent(
216 result, [],
217 'Cache.matchAll should treat query as a URL and not ' +
218 'just a string fragment.');
219 });
220 }, 'Cache.matchAll with string fragment \'http\' as query');
221
222 promise_test(function(t) {
223 return create_populated_cache(t, simple_entries)
224 .then(function(cache) {
225 return cache.matchAll('http://example.com/cat',
226 {prefixMatch: true});
227 })
228 .then(function(result) {
229 assert_array_equivalent(
230 result,
231 [
232 simple_entries.cat.response,
233 simple_entries.cat_with_fragment.response,
234 simple_entries.cat_in_the_hat.response
235 ],
236 'Cache.matchAll should honor prefixMatch.');
237 });
238 }, 'Cache.matchAll with prefixMatch option');
239
240 promise_test(function(t) {
241 return create_populated_cache(t, simple_entries)
242 .then(function(cache) {
243 return cache.matchAll('http://example.com/cat/',
244 {prefixMatch: true});
245 })
246 .then(function(result) {
247 assert_array_equivalent(
248 result, [simple_entries.cat_in_the_hat.response],
249 'Cache.matchAll should honor prefixMatch.');
250 });
251 }, 'Cache.matchAll with prefixMatch option');
252
253 promise_test(function(t) {
254 var cache;
255 return create_populated_cache(t, vary_entries)
256 .then(function(populated_cache) {
257 cache = populated_cache;
258 return cache.matchAll('http://example.com/c');
259 })
260 .then(function(result) {
261 assert_array_equivalent(
262 result,
263 [
264 vary_entries.no_vary_header.response,
265 vary_entries.vary_wildcard.response,
266 vary_entries.vary_cookie_absent.response
267 ],
268 'Cache.matchAll should exclude matches if a vary header is ' +
269 'missing in the query request, but is present in the cached ' +
270 'request.');
271 })
272
273 .then(function() {
274 return cache.matchAll(
275 new Request('http://example.com/c',
276 {headers: {'Cookies': 'none-of-the-above'}}));
277 })
278 .then(function(result) {
279 assert_array_equivalent(
280 result,
281 [
282 vary_entries.no_vary_header.response,
283 vary_entries.vary_wildcard.response
284 ],
285 'Cache.matchAll should exclude matches if a vary header is ' +
286 'missing in the cached request, but is present in the query ' +
287 'request.');
288 })
289
290 .then(function() {
291 return cache.matchAll(
292 new Request('http://example.com/c',
293 {headers: {'Cookies': 'is-for-cookie'}}));
294 })
295 .then(function(result) {
296 assert_array_equals(
297 result,
298 [vary_entries.vary_cookie_is_cookie.response],
299 'Cache.matchAll should match the entire header if a vary header ' +
300 'is present in both the query and cached requests.');
301 });
302 }, 'Cache.matchAll with responses containing \'Vary\' header');
303
304 promise_test(function(t) {
305 return create_populated_cache(t, vary_entries)
306 .then(function(cache) {
307 return cache.match('http://example.com/c');
308 })
309 .then(function(result) {
310 assert_in_array(
311 result,
312 [
313 vary_entries.no_vary_header.response,
314 vary_entries.vary_wildcard.response,
315 vary_entries.vary_cookie_absent.response
316 ],
317 'Cache.match should honor \'Vary\' header.');
318 });
319 }, 'Cache.match with responses containing \'Vary\' header');
320
321 promise_test(function(t) {
322 return create_populated_cache(t, vary_entries)
323 .then(function(cache) {
324 return cache.matchAll('http://example.com/c',
325 {ignoreVary: true});
326 })
327 .then(function(result) {
328 assert_array_equivalent(
329 result,
330 [
331 vary_entries.no_vary_header.response,
332 vary_entries.vary_cookie_is_cookie.response,
333 vary_entries.vary_cookie_is_good.response,
334 vary_entries.vary_cookie_absent.response,
335 vary_entries.vary_wildcard.response
336 ],
337 'Cache.matchAll should honor \'ignoreVary\' parameter.');
338 });
339 }, 'Cache.matchAll with \'ignoreVary\' parameter');
340
341 // Helpers ---
342
343 // Create a new Cache and populate it with requests from |entries|. The latter
344 // is expected to be an Object mapping arbitrary keys to objects of the form
345 // {request: <Request object>, response: <Response object>}.
346 //
347 // The return value is a Promise that resolves to the populated Cache. The
348 // lifetime of the Cache is bound to the duration of |test|.
349 function create_populated_cache(test, entries) {
350 var cache;
351 return create_temporary_cache(test)
352 .then(function(result) {
353 cache = result;
354 return Promise.all(Object.keys(entries).map(function(k) {
355 return cache.put(entries[k].request, entries[k].response);
356 }));
357 })
358 .then(function() {
359 return Promise.resolve(cache);
360 });
361 }
362
363 // Assert that the two arrays |actual| and |expected| contain the same set of
364 // elements ignoring order. This is necessary because the result array returned
365 // by Cache.matchAll() does not guarantee any specific ordering.
366 //
367 // |expected| is assumed to not contain any duplicates.
368 function assert_array_equivalent(actual, expected, description) {
369 assert_true(Array.isArray(actual), description);
370 assert_equals(actual.length, expected.length, description);
371 expected.forEach(function(expected_element) {
372 // assert_in_array treats the first argument as being 'actual', and the
373 // second as being 'expected array'. We are switching them around becaus
374 // we want to be resilient against the |actual| array containing
375 // duplicates.
376 assert_in_array(expected_element, actual, description);
377 });
378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698