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

Side by Side Diff: third_party/WebKit/LayoutTests/imported/wpt/service-workers/service-worker/fetch-event.https.html

Issue 2415873002: Import w3c tests for the service workers (Closed)
Patch Set: Rebase Created 4 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
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <script src="/resources/testharness.js"></script>
3 <script src="/resources/testharnessreport.js"></script>
4 <script src="resources/get-host-info.sub.js"></script>
5 <script src="resources/test-helpers.sub.js"></script>
6 <body>
7 <script>
8 var worker = 'resources/fetch-event-test-worker.js';
9
10 async_test(function(t) {
11 var scope = 'resources/simple.html?string';
12 service_worker_unregister_and_register(t, worker, scope)
13 .then(function(reg) {
14 return wait_for_state(t, reg.installing, 'activated');
15 })
16 .then(function() { return with_iframe(scope); })
17 .then(function(frame) {
18 assert_equals(
19 frame.contentDocument.body.textContent,
20 'Test string',
21 'Service Worker should respond to fetch with a test string');
22 assert_equals(
23 frame.contentDocument.contentType,
24 'text/plain',
25 'The content type of the response created with a string should be te xt/plain');
26 assert_equals(
27 frame.contentDocument.characterSet,
28 'UTF-8',
29 'The character set of the response created with a string should be U TF-8');
30 frame.remove();
31 return service_worker_unregister_and_done(t, scope);
32 })
33 .catch(unreached_rejection(t));
34 }, 'Service Worker responds to fetch event with string');
35
36 async_test(function(t) {
37 var scope = 'resources/simple.html?blob';
38 service_worker_unregister_and_register(t, worker, scope)
39 .then(function(reg) {
40 return wait_for_state(t, reg.installing, 'activated');
41 })
42 .then(function() { return with_iframe(scope); })
43 .then(function(frame) {
44 assert_equals(
45 frame.contentDocument.body.textContent,
46 'Test blob',
47 'Service Worker should respond to fetch with a test string');
48 frame.remove();
49 return service_worker_unregister_and_done(t, scope);
50 })
51 .catch(unreached_rejection(t));
52 }, 'Service Worker responds to fetch event with blob body');
53
54 async_test(function(t) {
55 var scope = 'resources/simple.html?referrer';
56 service_worker_unregister_and_register(t, worker, scope)
57 .then(function(reg) {
58 return wait_for_state(t, reg.installing, 'activated');
59 })
60 .then(function() { return with_iframe(scope); })
61 .then(function(frame) {
62 assert_equals(
63 frame.contentDocument.body.textContent,
64 'Referrer: ' + document.location.href,
65 'Service Worker should respond to fetch with the referrer URL');
66 frame.remove();
67 return service_worker_unregister_and_done(t, scope);
68 })
69 .catch(unreached_rejection(t));
70 }, 'Service Worker responds to fetch event with the referrer URL');
71
72 function run_referrer_policy_tests(frame, referrer, href, origin) {
73 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
74 {method: "GET", referrer: referrer})
75 .then(function(response) { return response.text(); })
76 .then(function(response_text) {
77 assert_equals(
78 response_text,
79 'Referrer: ' + href + '\n' +
80 'ReferrerPolicy: no-referrer-when-downgrade',
81 'Service Worker should respond to fetch with the referrer URL when a member of RequestInit is present');
82 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
83 '/resources/simple.html?referrerFull';
84 return frame.contentWindow.fetch(http_url,
85 {method: "GET", referrer: referrer});
86 })
87 .then(function(response) { return response.text(); })
88 .then(function(response_text) {
89 assert_equals(
90 response_text,
91 'Referrer: about:client\n' +
92 'ReferrerPolicy: no-referrer-when-downgrade',
93 'Service Worker should respond to fetch with no referrer when a memb er of RequestInit is present with an HTTP request');
94 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
95 {referrerPolicy: "", referrer: referr er});
96 })
97 .then(function(response) { return response.text(); })
98 .then(function(response_text) {
99 assert_equals(
100 response_text,
101 'Referrer: ' + href + '\n' +
102 'ReferrerPolicy: no-referrer-when-downgrade',
103 'Service Worker should respond to fetch with the referrer with ""');
104 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
105 '/resources/simple.html?referrerFull';
106 return frame.contentWindow.fetch(http_url,
107 {referrerPolicy: "", referrer: referr er});
108 })
109 .then(function(response) { return response.text(); })
110 .then(function(response_text) {
111 assert_equals(
112 response_text,
113 'Referrer: about:client\n' +
114 'ReferrerPolicy: no-referrer-when-downgrade',
115 'Service Worker should respond to fetch with no referrer with ""');
116 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
117 {referrerPolicy: "origin", referrer: referrer});
118 })
119 .then(function(response) { return response.text(); })
120 .then(function(response_text) {
121 assert_equals(
122 response_text,
123 'Referrer: ' + origin + '/' + '\n' +
124 'ReferrerPolicy: origin',
125 'Service Worker should respond to fetch with the referrer origin wit h "origin" and a same origin request');
126 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
127 '/resources/simple.html?referrerFull';
128 return frame.contentWindow.fetch(http_url,
129 {referrerPolicy: "origin", referrer: referrer});
130 })
131 .then(function(response) { return response.text(); })
132 .then(function(response_text) {
133 assert_equals(
134 response_text,
135 'Referrer: ' + origin + '/' + '\n' +
136 'ReferrerPolicy: origin',
137 'Service Worker should respond to fetch with the referrer origin wit h "origin" and a cross origin request');
138 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
139 {referrerPolicy: "origin-when-cross-o rigin", referrer: referrer});
140 })
141 .then(function(response) { return response.text(); })
142 .then(function(response_text) {
143 assert_equals(
144 response_text,
145 'Referrer: ' + href + '\n' +
146 'ReferrerPolicy: origin-when-cross-origin',
147 'Service Worker should respond to fetch with the referrer URL with " origin-when-cross-origin" and a same origin request');
148 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
149 '/resources/simple.html?referrerFull';
150 return frame.contentWindow.fetch(http_url,
151 {referrerPolicy: "origin-when-cross-o rigin", referrer: referrer});
152 })
153 .then(function(response) { return response.text(); })
154 .then(function(response_text) {
155 assert_equals(
156 response_text,
157 'Referrer: ' + origin + '/' + '\n' +
158 'ReferrerPolicy: origin-when-cross-origin',
159 'Service Worker should respond to fetch with the referrer origin wit h "origin-when-cross-origin" and a cross origin request');
160 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
161 {referrerPolicy: "no-referrer-when-do wngrade", referrer: referrer});
162 })
163 .then(function(response) { return response.text(); })
164 .then(function(response_text) {
165 assert_equals(
166 response_text,
167 'Referrer: ' + href + '\n' +
168 'ReferrerPolicy: no-referrer-when-downgrade',
169 'Service Worker should respond to fetch with no referrer with "no-re ferrer-when-downgrade" and a same origin request');
170 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
171 '/resources/simple.html?referrerFull';
172 return frame.contentWindow.fetch(http_url,
173 {referrerPolicy: "no-referrer-when-do wngrade", referrer: referrer});
174 })
175 .then(function(response) { return response.text(); })
176 .then(function(response_text) {
177 assert_equals(
178 response_text,
179 'Referrer: about:client\n' +
180 'ReferrerPolicy: no-referrer-when-downgrade',
181 'Service Worker should respond to fetch with no referrer with "no-re ferrer-when-downgrade" and an HTTP request');
182 var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
183 '/resources/simple.html?referrerFull';
184 return frame.contentWindow.fetch(http_url, {referrerPolicy: "unsafe-ur l", referrer: referrer});
185 })
186 .then(function(response) { return response.text(); })
187 .then(function(response_text) {
188 assert_equals(
189 response_text,
190 'Referrer: ' + href + '\n' +
191 'ReferrerPolicy: unsafe-url',
192 'Service Worker should respond to fetch with no referrer with "unsaf e-url"');
193 return frame.contentWindow.fetch('resources/simple.html?referrerFull',
194 {referrerPolicy: "no-referrer", refer rer: referrer});
195 })
196 .then(function(response) { return response.text(); })
197 .then(function(response_text) {
198 assert_equals(
199 response_text,
200 'Referrer: about:client\n' +
201 'ReferrerPolicy: no-referrer',
202 'Service Worker should respond to fetch with no referrer URL with "n o-referrer"');
203 });
204 }
205
206 async_test(function(t) {
207 var scope = 'resources/simple.html?referrerPolicy';
208 var frame;
209 service_worker_unregister_and_register(t, worker, scope)
210 .then(function(reg) {
211 return wait_for_state(t, reg.installing, 'activated');
212 })
213 .then(function() { return with_iframe(scope); })
214 .then(function(f) {
215 frame = f;
216 assert_equals(
217 frame.contentDocument.body.textContent,
218 'ReferrerPolicy: no-referrer-when-downgrade',
219 'Service Worker should respond to fetch with the default referrer po licy');
220 // First, run the referrer policy tests without passing a referrer in RequestInit.
221 return run_referrer_policy_tests(frame, undefined, frame.contentDocume nt.location.href,
222 frame.contentDocument.location.origin );
223 })
224 .then(function() {
225 // Now, run the referrer policy tests while passing a referrer in Requ estInit.
226 var referrer = get_host_info()['HTTPS_ORIGIN'] + base_path() + 'fake-r eferrer';
227 return run_referrer_policy_tests(frame, 'fake-referrer', referrer,
228 frame.contentDocument.location.origin );
229 })
230 .then(function() {
231 frame.remove();
232 return service_worker_unregister_and_done(t, scope);
233 })
234 .catch(unreached_rejection(t));
235 }, 'Service Worker responds to fetch event with the referrer URL');
236
237 async_test(function(t) {
238 var scope = 'resources/simple.html?clientId';
239 var frame;
240 service_worker_unregister_and_register(t, worker, scope)
241 .then(function(reg) {
242 return wait_for_state(t, reg.installing, 'activated');
243 })
244 .then(function() { return with_iframe(scope); })
245 .then(function(f) {
246 frame = f;
247 assert_equals(
248 frame.contentDocument.body.textContent,
249 'Client ID Not Found',
250 'Service Worker should respond to fetch with a client id');
251 return frame.contentWindow.fetch('resources/other.html?clientId');
252 })
253 .then(function(response) { return response.text(); })
254 .then(function(response_text) {
255 var new_client_id = response_text.substr(17);
256 assert_equals(
257 response_text.substr(0, 15),
258 'Client ID Found',
259 'Service Worker should respond to fetch with an existing client id') ;
260 frame.remove();
261 return service_worker_unregister_and_done(t, scope);
262 })
263 .catch(unreached_rejection(t));
264 }, 'Service Worker responds to fetch event with an existing client id');
265
266 async_test(function(t) {
267 var scope = 'resources/simple.html?ignore';
268 service_worker_unregister_and_register(t, worker, scope)
269 .then(function(reg) {
270 return wait_for_state(t, reg.installing, 'activated');
271 })
272 .then(function() { return with_iframe(scope); })
273 .then(function(frame) {
274 assert_equals(frame.contentDocument.body.textContent,
275 'Here\'s a simple html file.\n',
276 'Response should come from fallback to native fetch');
277 frame.remove();
278 return service_worker_unregister_and_done(t, scope);
279 })
280 .catch(unreached_rejection(t));
281 }, 'Service Worker does not respond to fetch event');
282
283 async_test(function(t) {
284 var scope = 'resources/simple.html?null';
285 service_worker_unregister_and_register(t, worker, scope)
286 .then(function(reg) {
287 return wait_for_state(t, reg.installing, 'activated');
288 })
289 .then(function() { return with_iframe(scope); })
290 .then(function(frame) {
291 assert_equals(frame.contentDocument.body.textContent,
292 '',
293 'Response should be the empty string');
294 frame.remove();
295 return service_worker_unregister_and_done(t, scope);
296 })
297 .catch(unreached_rejection(t));
298 }, 'Service Worker responds to fetch event with null response body');
299
300 async_test(function(t) {
301 var scope = 'resources/simple.html?fetch';
302 service_worker_unregister_and_register(t, worker, scope)
303 .then(function(reg) {
304 return wait_for_state(t, reg.installing, 'activated');
305 })
306 .then(function() { return with_iframe(scope); })
307 .then(function(frame) {
308 assert_equals(frame.contentDocument.body.textContent,
309 'Here\'s an other html file.\n',
310 'Response should come from fetched other file');
311 frame.remove();
312 return service_worker_unregister_and_done(t, scope);
313 })
314 .catch(unreached_rejection(t));
315 }, 'Service Worker fetches other file in fetch event');
316
317 async_test(function(t) {
318 var scope = 'resources/simple.html?form-post';
319 var frame_name = 'xhr-post-frame';
320 service_worker_unregister_and_register(t, worker, scope)
321 .then(function(reg) {
322 return wait_for_state(t, reg.installing, 'activated');
323 })
324 .then(function(sw) {
325 return new Promise(function(resolve) {
326 var frame = document.createElement('iframe');
327 frame.name = frame_name;
328 document.body.appendChild(frame);
329 var form = document.createElement('form');
330 form.target = frame_name;
331 form.action = scope;
332 form.method = 'post';
333 var input1 = document.createElement('input');
334 input1.type = 'text';
335 input1.value = 'testValue1';
336 input1.name = 'testName1'
337 form.appendChild(input1);
338 var input2 = document.createElement('input');
339 input2.type = 'text';
340 input2.value = 'testValue2';
341 input2.name = 'testName2'
342 form.appendChild(input2);
343 document.body.appendChild(form);
344 frame.onload = function() {
345 document.body.removeChild(form);
346 resolve(frame);
347 };
348 form.submit();
349 });
350 })
351 .then(function(frame) {
352 assert_equals(frame.contentDocument.body.textContent,
353 'POST:application/x-www-form-urlencoded:' +
354 'testName1=testValue1&testName2=testValue2');
355 frame.remove();
356 return service_worker_unregister_and_done(t, scope);
357 })
358 .catch(unreached_rejection(t));
359 }, 'Service Worker responds to fetch event with POST form');
360
361 async_test(function(t) {
362 var scope = 'resources/simple.html?multiple-respond-with';
363 service_worker_unregister_and_register(t, worker, scope)
364 .then(function(reg) {
365 return wait_for_state(t, reg.installing, 'activated');
366 })
367 .then(function() { return with_iframe(scope); })
368 .then(function(frame) {
369 assert_equals(
370 frame.contentDocument.body.textContent,
371 '(0)(1)[InvalidStateError](2)[InvalidStateError]',
372 'Multiple calls of respondWith must throw InvalidStateErrors.');
373 frame.remove();
374 return service_worker_unregister_and_done(t, scope);
375 })
376 .catch(unreached_rejection(t));
377 }, 'Multiple calls of respondWith must throw InvalidStateErrors');
378
379 async_test(function(t) {
380 var scope = 'resources/simple.html?used-check';
381 var first_frame;
382 service_worker_unregister_and_register(t, worker, scope)
383 .then(function(reg) {
384 return wait_for_state(t, reg.installing, 'activated');
385 })
386 .then(function() { return with_iframe(scope); })
387 .then(function(frame) {
388 assert_equals(frame.contentDocument.body.textContent,
389 'Here\'s an other html file.\n',
390 'Response should come from fetched other file');
391 first_frame = frame;
392 return with_iframe(scope);
393 })
394 .then(function(frame) {
395 // When we access to the scope in the second time, the content of the
396 // response is generated inside the ServiceWorker. The body contains
397 // the value of bodyUsed of the first response which is already
398 // consumed by FetchEvent.respondWith method.
399 assert_equals(
400 frame.contentDocument.body.textContent,
401 'bodyUsed: true',
402 'event.respondWith must set the used flag.');
403 first_frame.remove();
404 frame.remove();
405 return service_worker_unregister_and_done(t, scope);
406 })
407 .catch(unreached_rejection(t));
408 }, 'Service Worker event.respondWith must set the used flag');
409
410 async_test(function(t) {
411 var scope = 'resources/simple.html?fragment-check';
412 var fragment = '#/some/fragment';
413 var first_frame;
414 service_worker_unregister_and_register(t, worker, scope)
415 .then(function(reg) {
416 return wait_for_state(t, reg.installing, 'activated');
417 })
418 .then(function() { return with_iframe(scope + fragment); })
419 .then(function(frame) {
420 assert_equals(
421 frame.contentDocument.body.textContent,
422 'Fragment Not Found',
423 'Service worker should not expose URL fragments.');
424 frame.remove();
425 return service_worker_unregister_and_done(t, scope);
426 })
427 .catch(unreached_rejection(t));
428 }, 'Service Worker must not expose FetchEvent URL fragments.');
429
430 async_test(function(t) {
431 var scope = 'resources/simple.html?cache';
432 var frame;
433 var cacheTypes = [
434 undefined, 'default', 'no-store', 'reload', 'no-cache', 'force-cache', 'on ly-if-cached'
435 ];
436 service_worker_unregister_and_register(t, worker, scope)
437 .then(function(reg) {
438 return wait_for_state(t, reg.installing, 'activated');
439 })
440 .then(function() { return with_iframe(scope); })
441 .then(function(f) {
442 frame = f;
443 assert_equals(frame.contentWindow.document.body.textContent, 'default' );
444 var tests = cacheTypes.map(function(type) {
445 return new Promise(function(resolve, reject) {
446 var init = {cache: type};
447 if (type === 'only-if-cached') {
448 // For privacy reasons, for the time being, only-if-cached
449 // requires the mode to be same-origin.
450 init.mode = 'same-origin';
451 }
452 return frame.contentWindow.fetch(scope + '=' + type, init)
453 .then(function(response) { return response.text(); })
454 .then(function(response_text) {
455 var expected = (type === undefined) ? 'default' : type;
456 assert_equals(response_text, expected,
457 'Service Worker should respond to fetch with the correct type');
458 })
459 .then(resolve)
460 .catch(reject);
461 });
462 });
463 })
464 .then(function() {
465 return new Promise(function(resolve, reject) {
466 frame.addEventListener('load', function onLoad() {
467 frame.removeEventListener('load', onLoad);
468 try {
469 assert_equals(frame.contentWindow.document.body.textContent,
470 'no-cache');
471 resolve();
472 } catch (e) {
473 reject(e);
474 }
475 });
476 frame.contentWindow.location.reload();
477 });
478 })
479 .then(function() {
480 frame.remove();
481 return service_worker_unregister_and_done(t, scope);
482 })
483 .catch(unreached_rejection(t));
484 }, 'Service Worker responds to fetch event with the correct cache types');
485
486 async_test(function(t) {
487 var scope = 'resources/simple.html?eventsource';
488 var frame;
489
490 function test_eventsource(opts) {
491 return new Promise(function(resolve, reject) {
492 var eventSource = new frame.contentWindow.EventSource(scope, opts);
493 eventSource.addEventListener('message', function(msg) {
494 eventSource.close();
495 try {
496 var data = JSON.parse(msg.data);
497 assert_equals(data.mode, 'cors',
498 'EventSource should make CORS requests.');
499 assert_equals(data.cache, 'no-store',
500 'EventSource should bypass the http cache.');
501 var expectedCredentials = opts.withCredentials ? 'include'
502 : 'same-origin';
503 assert_equals(data.credentials, expectedCredentials,
504 'EventSource should pass correct credentials mode.');
505 resolve();
506 } catch (e) {
507 reject(e);
508 }
509 });
510 eventSource.addEventListener('error', function(e) {
511 eventSource.close();
512 reject('The EventSource fired an error event.');
513 });
514 });
515 }
516
517 service_worker_unregister_and_register(t, worker, scope)
518 .then(function(reg) {
519 return wait_for_state(t, reg.installing, 'activated');
520 })
521 .then(function() { return with_iframe(scope); })
522 .then(function(f) {
523 frame = f;
524 return test_eventsource({ withCredentials: false });
525 })
526 .then(function() {
527 return test_eventsource({ withCredentials: true });
528 })
529 .then(function() {
530 frame.remove();
531 return service_worker_unregister_and_done(t, scope);
532 })
533 .catch(unreached_rejection(t));
534 }, 'Service Worker should intercept EventSource');
535
536 async_test(function(t) {
537 var scope = 'resources/simple.html?integrity';
538 var frame;
539 var integrity_metadata = 'gs0nqru8KbsrIt5YToQqS9fYao4GQJXtcId610g7cCU=';
540
541 service_worker_unregister_and_register(t, worker, scope)
542 .then(function(reg) {
543 return wait_for_state(t, reg.installing, 'activated');
544 })
545 .then(function() { return with_iframe(scope); })
546 .then(function(f) {
547 frame = f;
548 // A request has associated integrity metadata (a string).
549 // Unless stated otherwise, it is the empty string.
550 assert_equals(
551 frame.contentDocument.body.textContent, '');
552
553 return new Promise(function(resolve, reject) {
554 return frame.contentWindow.fetch(scope,
555 {'integrity': integrity_metadata})
556 .then(function(response) {
557 return response.text();
558 })
559 .then(function(response_text) {
560 // Should get the same integrity metadata.
561 assert_equals(response_text, integrity_metadata,
562 'Service Worker should respond to fetch with t he correct integrity');
563 })
564 .then(resolve())
565 .catch(reject());
566 });
567 })
568 .then(function() {
569 frame.remove();
570 return service_worker_unregister_and_done(t, scope);
571 })
572 .catch(unreached_rejection(t));
573 }, 'Service Worker responds to fetch event with the correct integrity_metadata ');
574
575 </script>
576 </body>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698