OLD | NEW |
| (Empty) |
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'; | |
8 | |
9 async_test(function(t) { | |
10 const scope = 'resources/simple.html?headers'; | |
11 service_worker_unregister_and_register(t, worker, scope) | |
12 .then(function(reg) { | |
13 return wait_for_state(t, reg.installing, 'activated'); | |
14 }) | |
15 .then(function() { return with_iframe(scope); }) | |
16 .then(function(frame) { | |
17 // We have this test to prevent unexpected exposure of headers to a | |
18 // ServiceWorker. Feel free to rebaseline this expectation if it | |
19 // looks good. | |
20 const headers = JSON.parse(frame.contentDocument.body.textContent); | |
21 const header_names = []; | |
22 for (const [name, value] of headers) { | |
23 header_names.push(name); | |
24 } | |
25 header_names.sort(); | |
26 assert_array_equals( | |
27 header_names, | |
28 ["accept", "upgrade-insecure-requests", "user-agent"], | |
29 'event.request has the expected headers.'); | |
30 | |
31 frame.remove(); | |
32 return service_worker_unregister_and_done(t, scope); | |
33 }) | |
34 .catch(unreached_rejection(t)); | |
35 }, 'Service Worker headers in the request of a fetch event'); | |
36 | |
37 async_test(function(t) { | |
38 var scope = 'resources/simple.html?string'; | |
39 service_worker_unregister_and_register(t, worker, scope) | |
40 .then(function(reg) { | |
41 return wait_for_state(t, reg.installing, 'activated'); | |
42 }) | |
43 .then(function() { return with_iframe(scope); }) | |
44 .then(function(frame) { | |
45 assert_equals( | |
46 frame.contentDocument.body.textContent, | |
47 'Test string', | |
48 'Service Worker should respond to fetch with a test string'); | |
49 assert_equals( | |
50 frame.contentDocument.contentType, | |
51 'text/plain', | |
52 'The content type of the response created with a string should be te
xt/plain'); | |
53 assert_equals( | |
54 frame.contentDocument.characterSet, | |
55 'UTF-8', | |
56 'The character set of the response created with a string should be U
TF-8'); | |
57 frame.remove(); | |
58 return service_worker_unregister_and_done(t, scope); | |
59 }) | |
60 .catch(unreached_rejection(t)); | |
61 }, 'Service Worker responds to fetch event with string'); | |
62 | |
63 async_test(function(t) { | |
64 var scope = 'resources/simple.html?blob'; | |
65 service_worker_unregister_and_register(t, worker, scope) | |
66 .then(function(reg) { | |
67 return wait_for_state(t, reg.installing, 'activated'); | |
68 }) | |
69 .then(function() { return with_iframe(scope); }) | |
70 .then(function(frame) { | |
71 assert_equals( | |
72 frame.contentDocument.body.textContent, | |
73 'Test blob', | |
74 'Service Worker should respond to fetch with a test string'); | |
75 frame.remove(); | |
76 return service_worker_unregister_and_done(t, scope); | |
77 }) | |
78 .catch(unreached_rejection(t)); | |
79 }, 'Service Worker responds to fetch event with blob body'); | |
80 | |
81 async_test(function(t) { | |
82 var scope = 'resources/simple.html?referrer'; | |
83 service_worker_unregister_and_register(t, worker, scope) | |
84 .then(function(reg) { | |
85 return wait_for_state(t, reg.installing, 'activated'); | |
86 }) | |
87 .then(function() { return with_iframe(scope); }) | |
88 .then(function(frame) { | |
89 assert_equals( | |
90 frame.contentDocument.body.textContent, | |
91 'Referrer: ' + document.location.href, | |
92 'Service Worker should respond to fetch with the referrer URL'); | |
93 frame.remove(); | |
94 return service_worker_unregister_and_done(t, scope); | |
95 }) | |
96 .catch(unreached_rejection(t)); | |
97 }, 'Service Worker responds to fetch event with the referrer URL'); | |
98 | |
99 async_test(function(t) { | |
100 var scope = 'resources/simple.html?clientId'; | |
101 var frame; | |
102 var client_id1, client_id2; | |
103 service_worker_unregister_and_register(t, worker, scope) | |
104 .then(function(reg) { | |
105 return wait_for_state(t, reg.installing, 'activated'); | |
106 }) | |
107 .then(function() { return with_iframe(scope); }) | |
108 .then(function(f) { | |
109 frame = f; | |
110 assert_equals( | |
111 frame.contentDocument.body.textContent.substr(0, 19), | |
112 'Client ID Not Found', | |
113 'Service Worker should respond to navigation fetch with no ' + | |
114 'client id'); | |
115 return frame.contentWindow.fetch('resources/other.html?clientId'); | |
116 }) | |
117 .then(function(response) { return response.text(); }) | |
118 .then(function(response_text) { | |
119 client_id1 = response_text.substr(17, 36); | |
120 assert_equals( | |
121 response_text.substr(0, 15), | |
122 'Client ID Found', | |
123 'Service Worker should respond to fetch with a client id'); | |
124 return frame.contentWindow.fetch('resources/other.html?clientId'); | |
125 }) | |
126 .then(function(response) { return response.text(); }) | |
127 .then(function(response_text) { | |
128 client_id2 = response_text.substr(17, 36); | |
129 assert_equals( | |
130 client_id1, | |
131 client_id2, | |
132 'Service Worker should respond to another fetch from the same ' + | |
133 'client with the same client id'); | |
134 frame.remove(); | |
135 return service_worker_unregister_and_done(t, scope); | |
136 }) | |
137 .catch(unreached_rejection(t)); | |
138 }, 'Service Worker responds to fetch event with a client id'); | |
139 | |
140 async_test(function(t) { | |
141 var scope = 'resources/simple.html?ignore'; | |
142 service_worker_unregister_and_register(t, worker, scope) | |
143 .then(function(reg) { | |
144 return wait_for_state(t, reg.installing, 'activated'); | |
145 }) | |
146 .then(function() { return with_iframe(scope); }) | |
147 .then(function(frame) { | |
148 assert_equals(frame.contentDocument.body.textContent, | |
149 'Here\'s a simple html file.\n', | |
150 'Response should come from fallback to native fetch'); | |
151 frame.remove(); | |
152 return service_worker_unregister_and_done(t, scope); | |
153 }) | |
154 .catch(unreached_rejection(t)); | |
155 }, 'Service Worker does not respond to fetch event'); | |
156 | |
157 async_test(function(t) { | |
158 var scope = 'resources/simple.html?null'; | |
159 service_worker_unregister_and_register(t, worker, scope) | |
160 .then(function(reg) { | |
161 return wait_for_state(t, reg.installing, 'activated'); | |
162 }) | |
163 .then(function() { return with_iframe(scope); }) | |
164 .then(function(frame) { | |
165 assert_equals(frame.contentDocument.body.textContent, | |
166 '', | |
167 'Response should be the empty string'); | |
168 frame.remove(); | |
169 return service_worker_unregister_and_done(t, scope); | |
170 }) | |
171 .catch(unreached_rejection(t)); | |
172 }, 'Service Worker responds to fetch event with null response body'); | |
173 | |
174 async_test(function(t) { | |
175 var scope = 'resources/simple.html?fetch'; | |
176 service_worker_unregister_and_register(t, worker, scope) | |
177 .then(function(reg) { | |
178 return wait_for_state(t, reg.installing, 'activated'); | |
179 }) | |
180 .then(function() { return with_iframe(scope); }) | |
181 .then(function(frame) { | |
182 assert_equals(frame.contentDocument.body.textContent, | |
183 'Here\'s an other html file.\n', | |
184 'Response should come from fetched other file'); | |
185 frame.remove(); | |
186 return service_worker_unregister_and_done(t, scope); | |
187 }) | |
188 .catch(unreached_rejection(t)); | |
189 }, 'Service Worker fetches other file in fetch event'); | |
190 | |
191 async_test(function(t) { | |
192 var scope = 'resources/simple.html?form-post'; | |
193 var frame_name = 'xhr-post-frame'; | |
194 service_worker_unregister_and_register(t, worker, scope) | |
195 .then(function(reg) { | |
196 return wait_for_state(t, reg.installing, 'activated'); | |
197 }) | |
198 .then(function(sw) { | |
199 return new Promise(function(resolve) { | |
200 var frame = document.createElement('iframe'); | |
201 frame.name = frame_name; | |
202 document.body.appendChild(frame); | |
203 var form = document.createElement('form'); | |
204 form.target = frame_name; | |
205 form.action = scope; | |
206 form.method = 'post'; | |
207 var input1 = document.createElement('input'); | |
208 input1.type = 'text'; | |
209 input1.value = 'testValue1'; | |
210 input1.name = 'testName1' | |
211 form.appendChild(input1); | |
212 var input2 = document.createElement('input'); | |
213 input2.type = 'text'; | |
214 input2.value = 'testValue2'; | |
215 input2.name = 'testName2' | |
216 form.appendChild(input2); | |
217 document.body.appendChild(form); | |
218 frame.onload = function() { | |
219 document.body.removeChild(form); | |
220 resolve(frame); | |
221 }; | |
222 form.submit(); | |
223 }); | |
224 }) | |
225 .then(function(frame) { | |
226 assert_equals(frame.contentDocument.body.textContent, | |
227 'POST:application/x-www-form-urlencoded:' + | |
228 'testName1=testValue1&testName2=testValue2'); | |
229 frame.remove(); | |
230 return service_worker_unregister_and_done(t, scope); | |
231 }) | |
232 .catch(unreached_rejection(t)); | |
233 }, 'Service Worker responds to fetch event with POST form'); | |
234 | |
235 async_test(function(t) { | |
236 var scope = 'resources/simple.html?multiple-respond-with'; | |
237 service_worker_unregister_and_register(t, worker, scope) | |
238 .then(function(reg) { | |
239 return wait_for_state(t, reg.installing, 'activated'); | |
240 }) | |
241 .then(function() { return with_iframe(scope); }) | |
242 .then(function(frame) { | |
243 assert_equals( | |
244 frame.contentDocument.body.textContent, | |
245 '(0)(1)[InvalidStateError](2)[InvalidStateError]', | |
246 'Multiple calls of respondWith must throw InvalidStateErrors.'); | |
247 frame.remove(); | |
248 return service_worker_unregister_and_done(t, scope); | |
249 }) | |
250 .catch(unreached_rejection(t)); | |
251 }, 'Multiple calls of respondWith must throw InvalidStateErrors'); | |
252 | |
253 async_test(function(t) { | |
254 var scope = 'resources/simple.html?used-check'; | |
255 var first_frame; | |
256 service_worker_unregister_and_register(t, worker, scope) | |
257 .then(function(reg) { | |
258 return wait_for_state(t, reg.installing, 'activated'); | |
259 }) | |
260 .then(function() { return with_iframe(scope); }) | |
261 .then(function(frame) { | |
262 assert_equals(frame.contentDocument.body.textContent, | |
263 'Here\'s an other html file.\n', | |
264 'Response should come from fetched other file'); | |
265 first_frame = frame; | |
266 return with_iframe(scope); | |
267 }) | |
268 .then(function(frame) { | |
269 // When we access to the scope in the second time, the content of the | |
270 // response is generated inside the ServiceWorker. The body contains | |
271 // the value of bodyUsed of the first response which is already | |
272 // consumed by FetchEvent.respondWith method. | |
273 assert_equals( | |
274 frame.contentDocument.body.textContent, | |
275 'bodyUsed: true', | |
276 'event.respondWith must set the used flag.'); | |
277 first_frame.remove(); | |
278 frame.remove(); | |
279 return service_worker_unregister_and_done(t, scope); | |
280 }) | |
281 .catch(unreached_rejection(t)); | |
282 }, 'Service Worker event.respondWith must set the used flag'); | |
283 | |
284 </script> | |
285 </body> | |
OLD | NEW |