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

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

Powered by Google App Engine
This is Rietveld 408576698