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

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: Enable more tests 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-testharness.js');
2 importScripts('/resources/testharness-helpers.js');
3
4 // A set of Request/Response pairs to be used with prepopulated_cache_test().
5 var simple_entries = {
6 a: {
7 request: new Request('http://example.com/a'),
8 response: new Response('')
9 },
10
11 b: {
12 request: new Request('http://example.com/b'),
13 response: new Response('')
14 },
15
16 a_with_query: {
17 request: new Request('http://example.com/a?q=r'),
18 response: new Response('')
19 },
20
21 A: {
22 request: new Request('http://example.com/A'),
23 response: new Response('')
24 },
25
26 a_https: {
27 request: new Request('https://example.com/a'),
28 response: new Response('')
29 },
30
31 a_org: {
32 request: new Request('http://example.org/a'),
33 response: new Response('')
34 },
35
36 cat: {
37 request: new Request('http://example.com/cat'),
38 response: new Response('')
39 },
40
41 cat_with_fragment: {
42 request: new Request('http://example.com/cat#mouse'),
43 response: new Response('')
44 },
45
46 cat_in_the_hat: {
47 request: new Request('http://example.com/cat/in/the/hat'),
48 response: new Response('')
49 }
50 };
51
52 // A set of Request/Response pairs to be used with prepopulated_cache_test().
53 // These contain a mix of test cases that use Vary headers.
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 prepopulated_cache_test(simple_entries, function(cache) {
89 return cache.matchAll(simple_entries.a.request.url)
90 .then(function(result) {
91 assert_array_objects_equals(result, [simple_entries.a.response],
92 'Cache.matchAll should match by URL.');
93 });
94 }, 'Cache.matchAll with URL');
95
96 prepopulated_cache_test(simple_entries, function(cache) {
97 return cache.match(simple_entries.a.request.url)
98 .then(function(result) {
99 assert_object_equals(result, simple_entries.a.response,
100 'Cache.match should match by URL.');
101 });
102 }, 'Cache.match with URL');
103
104 prepopulated_cache_test(simple_entries, function(cache) {
105 return cache.matchAll(simple_entries.a.request)
106 .then(function(result) {
107 assert_array_objects_equals(
108 result, [simple_entries.a.response],
109 'Cache.matchAll should match by Request.');
110 });
111 }, 'Cache.matchAll with Request');
112
113 prepopulated_cache_test(simple_entries, function(cache) {
114 return cache.match(simple_entries.a.request)
115 .then(function(result) {
116 assert_object_equals(result, simple_entries.a.response,
117 'Cache.match should match by Request.');
118 });
119 }, 'Cache.match with Request');
120
121 prepopulated_cache_test(simple_entries, function(cache) {
122 return cache.matchAll(new Request(simple_entries.a.request.url))
123 .then(function(result) {
124 assert_array_objects_equals(
125 result, [simple_entries.a.response],
126 'Cache.matchAll should match by Request.');
127 });
128 }, 'Cache.matchAll with new Request');
129
130 prepopulated_cache_test(simple_entries, function(cache) {
131 return cache.match(new Request(simple_entries.a.request.url))
132 .then(function(result) {
133 assert_object_equals(result, simple_entries.a.response,
134 'Cache.match should match by Request.');
135 });
136 }, 'Cache.match with new Request');
137
138 cache_test(function(cache) {
139 var request = new Request('https://example.com/foo', {
140 method: 'GET',
141 body: 'Hello world!'
142 });
143 var response = new Response('Booyah!', {
144 status: 200,
145 headers: {'Content-Type': 'text/plain'}
146 });
147
148 return cache.put(request.clone(), response.clone())
149 .then(function() {
150 assert_false(
151 request.bodyUsed,
152 '[https://fetch.spec.whatwg.org/#concept-body-used-flag] ' +
153 'Request.bodyUsed flag should be initially false.');
154 })
155 .then(function() {
156 return cache.match(request);
157 })
158 .then(function(result) {
159 assert_false(request.bodyUsed,
160 'Cache.match should not consume Request body.');
161 });
162 }, 'Cache.match with Request containing non-empty body');
163
164 prepopulated_cache_test(simple_entries, function(cache) {
165 return cache.matchAll(simple_entries.a.request,
166 {ignoreSearch: true})
167 .then(function(result) {
168 assert_array_equivalent(
169 result,
170 [
171 simple_entries.a.response,
172 simple_entries.a_with_query.response
173 ],
174 'Cache.matchAll with ignoreSearch should ignore the ' +
175 'search parameters of cached request.');
176 });
177 },
178 'Cache.matchAll with ignoreSearch option (request with no search ' +
179 'parameters)');
180
181 prepopulated_cache_test(simple_entries, function(cache) {
182 return cache.matchAll(simple_entries.a_with_query.request,
183 {ignoreSearch: true})
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 prepopulated_cache_test(simple_entries, function(cache) {
198 return cache.matchAll(simple_entries.cat.request)
199 .then(function(result) {
200 assert_array_equivalent(
201 result,
202 [
203 simple_entries.cat.response,
204 simple_entries.cat_with_fragment.response
205 ],
206 'Cache.matchAll should ignore URL hash.');
207 });
208 }, 'Cache.matchAll with request containing hash');
209
210 prepopulated_cache_test(simple_entries, function(cache) {
211 return cache.matchAll('http')
212 .then(function(result) {
213 assert_array_equivalent(
214 result, [],
215 'Cache.matchAll should treat query as a URL and not ' +
216 'just a string fragment.');
217 });
218 }, 'Cache.matchAll with string fragment "http" as query');
219
220 prepopulated_cache_test(simple_entries, function(cache) {
221 return cache.matchAll('http://example.com/cat',
222 {prefixMatch: true})
223 .then(function(result) {
224 assert_array_equivalent(
225 result,
226 [
227 simple_entries.cat.response,
228 simple_entries.cat_with_fragment.response,
229 simple_entries.cat_in_the_hat.response
230 ],
231 'Cache.matchAll should honor prefixMatch.');
232 });
233 }, 'Cache.matchAll with prefixMatch option');
234
235 prepopulated_cache_test(simple_entries, function(cache) {
236 return cache.matchAll('http://example.com/cat/',
237 {prefixMatch: true})
238 .then(function(result) {
239 assert_array_equivalent(
240 result, [simple_entries.cat_in_the_hat.response],
241 'Cache.matchAll should honor prefixMatch.');
242 });
243 }, 'Cache.matchAll with prefixMatch option');
244
245 prepopulated_cache_test(vary_entries, function(cache) {
246 return cache.matchAll('http://example.com/c')
247 .then(function(result) {
248 assert_array_equivalent(
249 result,
250 [
251 vary_entries.no_vary_header.response,
252 vary_entries.vary_wildcard.response,
253 vary_entries.vary_cookie_absent.response
254 ],
255 'Cache.matchAll should exclude matches if a vary header is ' +
256 'missing in the query request, but is present in the cached ' +
257 'request.');
258 })
259
260 .then(function() {
261 return cache.matchAll(
262 new Request('http://example.com/c',
263 {headers: {'Cookies': 'none-of-the-above'}}));
264 })
265 .then(function(result) {
266 assert_array_equivalent(
267 result,
268 [
269 vary_entries.no_vary_header.response,
270 vary_entries.vary_wildcard.response
271 ],
272 'Cache.matchAll should exclude matches if a vary header is ' +
273 'missing in the cached request, but is present in the query ' +
274 'request.');
275 })
276
277 .then(function() {
278 return cache.matchAll(
279 new Request('http://example.com/c',
280 {headers: {'Cookies': 'is-for-cookie'}}));
281 })
282 .then(function(result) {
283 assert_array_equivalent(
284 result,
285 [vary_entries.vary_cookie_is_cookie.response],
286 'Cache.matchAll should match the entire header if a vary header ' +
287 'is present in both the query and cached requests.');
288 });
289 }, 'Cache.matchAll with responses containing "Vary" header');
290
291 prepopulated_cache_test(vary_entries, function(cache) {
292 return cache.match('http://example.com/c')
293 .then(function(result) {
294 assert_object_in_array(
295 result,
296 [
297 vary_entries.no_vary_header.response,
298 vary_entries.vary_wildcard.response,
299 vary_entries.vary_cookie_absent.response
300 ],
301 'Cache.match should honor "Vary" header.');
302 });
303 }, 'Cache.match with responses containing "Vary" header');
304
305 prepopulated_cache_test(vary_entries, function(cache) {
306 return cache.matchAll('http://example.com/c',
307 {ignoreVary: true})
308 .then(function(result) {
309 assert_array_equivalent(
310 result,
311 [
312 vary_entries.no_vary_header.response,
313 vary_entries.vary_cookie_is_cookie.response,
314 vary_entries.vary_cookie_is_good.response,
315 vary_entries.vary_cookie_absent.response,
316 vary_entries.vary_wildcard.response
317 ],
318 'Cache.matchAll should honor "ignoreVary" parameter.');
319 });
320 }, 'Cache.matchAll with "ignoreVary" parameter');
321
322 cache_test(function(cache) {
323 var request = new Request('http://example.com');
324 var response;
325 var request_url = new URL('simple.txt', location.href).href;
326 return fetch(request_url)
327 .then(function(fetch_result) {
328 response = fetch_result;
329 assert_equals(
330 response.url, request_url,
331 '[https://fetch.spec.whatwg.org/#dom-response-url] ' +
332 'Reponse.url should return the URL of the response.');
333 return cache.put(request, response);
334 })
335 .then(function() {
336 return cache.match(request.url);
337 })
338 .then(function(result) {
339 assert_object_equals(
340 result, response,
341 'Cache.match should reutrn a Response object that has the same ' +
342 'properties as the stored response.');
343 })
344 .then(function() {
345 return assert_promise_rejects(
346 cache.match(response.url),
347 'NotFoundError',
348 'Cache.match should not match cache entry based on response URL.');
349 });
350 }, 'Cache.match with Request and Response objects with different URLs');
351
352 cache_test(function(cache) {
353 var request_url = new URL('simple.txt', location.href).href;
354 return fetch(request_url)
355 .then(function(fetch_result) {
356 return cache.put(new Request(request_url), fetch_result);
357 })
358 .then(function() {
359 return cache.match(request_url);
360 })
361 .then(function(result) {
362 return result.text();
363 })
364 .then(function(body_text) {
365 assert_equals(body_text, 'a simple text file\n',
366 'Cache.match should return a Response object with a ' +
367 'valid body.');
368 })
369 .then(function() {
370 return cache.match(request_url);
371 })
372 .then(function(result) {
373 return result.text();
374 })
375 .then(function(body_text) {
376 assert_equals(body_text, 'a simple text file\n',
377 'Cache.match should return a Response object with a ' +
378 'valid body each time it is called.');
379 });
380 }, 'Cache.match invoked multiple times for the same Request/Response');
381
382 // Helpers ---
383
384 // Run |test_function| with a Cache object as its only parameter. Prior to the
385 // call, the Cache is populated by cache entries from |entries|. The latter is
386 // expected to be an Object mapping arbitrary keys to objects of the form
387 // {request: <Request object>, response: <Response object>}.
388 //
389 // |test_function| should return a Promise that can be used with promise_test.
390 function prepopulated_cache_test(entries, test_function, description) {
391 cache_test(function(cache) {
392 return Promise.all(Object.keys(entries).map(function(k) {
393 return cache.put(entries[k].request, entries[k].response);
394 }))
395 .then(function() {
396 return test_function(cache);
397 });
398 }, description);
399 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698