Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / fetch-event.html
blob8b9cb40ceaca384bbeb2211249544c4dfd018703
1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script>
4 <script src="resources/test-helpers.js"></script>
5 <body>
6 <script>
7 var worker = 'resources/fetch-event-test-worker.js';
9 async_test(function(t) {
10 var scope = 'resources/simple.html?string';
11 service_worker_unregister_and_register(t, worker, scope)
12 .then(function(reg) {
13 return wait_for_state(t, reg.installing, 'activated');
15 .then(function() { return with_iframe(scope); })
16 .then(function(frame) {
17 assert_equals(
18 frame.contentDocument.body.textContent,
19 'Test string',
20 'Service Worker should respond to fetch with a test string');
21 assert_equals(
22 frame.contentDocument.contentType,
23 'text/plain',
24 'The content type of the response created with a string should be text/plain');
25 assert_equals(
26 frame.contentDocument.characterSet,
27 'UTF-8',
28 'The character set of the response created with a string should be UTF-8');
29 frame.remove();
30 return service_worker_unregister_and_done(t, scope);
32 .catch(unreached_rejection(t));
33 }, 'Service Worker responds to fetch event with string');
35 async_test(function(t) {
36 var scope = 'resources/simple.html?blob';
37 service_worker_unregister_and_register(t, worker, scope)
38 .then(function(reg) {
39 return wait_for_state(t, reg.installing, 'activated');
41 .then(function() { return with_iframe(scope); })
42 .then(function(frame) {
43 assert_equals(
44 frame.contentDocument.body.textContent,
45 'Test blob',
46 'Service Worker should respond to fetch with a test string');
47 frame.remove();
48 return service_worker_unregister_and_done(t, scope);
50 .catch(unreached_rejection(t));
51 }, 'Service Worker responds to fetch event with blob body');
53 async_test(function(t) {
54 var scope = 'resources/simple.html?referrer';
55 service_worker_unregister_and_register(t, worker, scope)
56 .then(function(reg) {
57 return wait_for_state(t, reg.installing, 'activated');
59 .then(function() { return with_iframe(scope); })
60 .then(function(frame) {
61 assert_equals(
62 frame.contentDocument.body.textContent,
63 'Referrer: ' + document.location.href,
64 'Service Worker should respond to fetch with the referrer URL');
65 frame.remove();
66 return service_worker_unregister_and_done(t, scope);
68 .catch(unreached_rejection(t));
69 }, 'Service Worker responds to fetch event with the referrer URL');
71 async_test(function(t) {
72 var scope = 'resources/simple.html?ignore';
73 service_worker_unregister_and_register(t, worker, scope)
74 .then(function(reg) {
75 return wait_for_state(t, reg.installing, 'activated');
77 .then(function() { return with_iframe(scope); })
78 .then(function(frame) {
79 assert_equals(frame.contentDocument.body.textContent,
80 'Here\'s a simple html file.\n',
81 'Response should come from fallback to native fetch');
82 frame.remove();
83 return service_worker_unregister_and_done(t, scope);
85 .catch(unreached_rejection(t));
86 }, 'Service Worker does not respond to fetch event');
88 async_test(function(t) {
89 var scope = 'resources/simple.html?null';
90 service_worker_unregister_and_register(t, worker, scope)
91 .then(function(reg) {
92 return wait_for_state(t, reg.installing, 'activated');
94 .then(function() { return with_iframe(scope); })
95 .then(function(frame) {
96 assert_equals(frame.contentDocument.body.textContent,
97 '',
98 'Response should be the empty string');
99 frame.remove();
100 return service_worker_unregister_and_done(t, scope);
102 .catch(unreached_rejection(t));
103 }, 'Service Worker responds to fetch event with null response body');
105 async_test(function(t) {
106 var scope = 'resources/simple.html?fetch';
107 service_worker_unregister_and_register(t, worker, scope)
108 .then(function(reg) {
109 return wait_for_state(t, reg.installing, 'activated');
111 .then(function() { return with_iframe(scope); })
112 .then(function(frame) {
113 assert_equals(frame.contentDocument.body.textContent,
114 'Here\'s an other html file.\n',
115 'Response should come from fetched other file');
116 frame.remove();
117 return service_worker_unregister_and_done(t, scope);
119 .catch(unreached_rejection(t));
120 }, 'Service Worker fetches other file in fetch event');
122 async_test(function(t) {
123 var scope = 'resources/simple.html?form-post';
124 var frame_name = 'xhr-post-frame';
125 service_worker_unregister_and_register(t, worker, scope)
126 .then(function(reg) {
127 return wait_for_state(t, reg.installing, 'activated');
129 .then(function(sw) {
130 return new Promise(function(resolve) {
131 var frame = document.createElement('iframe');
132 frame.name = frame_name;
133 document.body.appendChild(frame);
134 var form = document.createElement('form');
135 form.target = frame_name;
136 form.action = scope;
137 form.method = 'post';
138 var input1 = document.createElement('input');
139 input1.type = 'text';
140 input1.value = 'testValue1';
141 input1.name = 'testName1'
142 form.appendChild(input1);
143 var input2 = document.createElement('input');
144 input2.type = 'text';
145 input2.value = 'testValue2';
146 input2.name = 'testName2'
147 form.appendChild(input2);
148 document.body.appendChild(form);
149 frame.onload = function() {
150 document.body.removeChild(form);
151 resolve(frame);
153 form.submit();
156 .then(function(frame) {
157 assert_equals(frame.contentDocument.body.textContent,
158 'POST:testName1=testValue1&testName2=testValue2');
159 document.body.removeChild(frame);
160 return service_worker_unregister_and_done(t, scope);
162 .catch(unreached_rejection(t));
163 }, 'Service Worker responds to fetch event with POST form');
165 async_test(function(t) {
166 var scope = 'resources/simple.html?multiple-respond-with';
167 service_worker_unregister_and_register(t, worker, scope)
168 .then(function(reg) {
169 return wait_for_state(t, reg.installing, 'activated');
171 .then(function() { return with_iframe(scope); })
172 .then(function(frame) {
173 assert_equals(
174 frame.contentDocument.body.textContent,
175 '(0)',
176 'Response should be the argument of the first respondWith() call.');
177 frame.remove();
178 return with_iframe(scope);
180 .then(function(frame) {
181 assert_equals(
182 frame.contentDocument.body.textContent,
183 '(0)(1)[InvalidStateError](2)[InvalidStateError](0)',
184 'Multiple calls of respondWith must throw InvalidStateErrors.');
185 frame.remove();
186 return service_worker_unregister_and_done(t, scope);
188 .catch(unreached_rejection(t));
189 }, 'Multiple calls of respondWith must throw InvalidStateErrors');
191 async_test(function(t) {
192 var scope = 'resources/simple.html?used-check';
193 service_worker_unregister_and_register(t, worker, scope)
194 .then(function(reg) {
195 return wait_for_state(t, reg.installing, 'activated');
197 .then(function() { return with_iframe(scope); })
198 .then(function(frame) {
199 assert_equals(frame.contentDocument.body.textContent,
200 'Here\'s an other html file.\n',
201 'Response should come from fetched other file');
202 frame.remove();
203 return with_iframe(scope);
205 .then(function(frame) {
206 // When we access to the scope in the second time, the content of the
207 // response is generated inside the ServiceWorker. The body contains
208 // the value of bodyUsed of the first response which is already
209 // consumed by FetchEvent.respondWith method.
210 assert_equals(
211 frame.contentDocument.body.textContent,
212 'bodyUsed: true',
213 'event.respondWith must set the used flag.');
214 frame.remove();
215 return service_worker_unregister_and_done(t, scope);
217 .catch(unreached_rejection(t));
218 }, 'Service Worker event.respondWith must set the used flag');
220 </script>
221 </body>