2 <script src=
"../resources/testharness.js"></script>
3 <script src=
"../resources/testharnessreport.js"></script>
4 <script src=
"resources/test-helpers.js"></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
)
13 return wait_for_state(t
, reg
.installing
, 'activated');
15 .then(function() { return with_iframe(scope
); })
16 .then(function(frame
) {
18 frame
.contentDocument
.body
.textContent
,
20 'Service Worker should respond to fetch with a test string');
22 frame
.contentDocument
.contentType
,
24 'The content type of the response created with a string should be text/plain');
26 frame
.contentDocument
.characterSet
,
28 'The character set of the response created with a string should be UTF-8');
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
)
39 return wait_for_state(t
, reg
.installing
, 'activated');
41 .then(function() { return with_iframe(scope
); })
42 .then(function(frame
) {
44 frame
.contentDocument
.body
.textContent
,
46 'Service Worker should respond to fetch with a test string');
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
)
57 return wait_for_state(t
, reg
.installing
, 'activated');
59 .then(function() { return with_iframe(scope
); })
60 .then(function(frame
) {
62 frame
.contentDocument
.body
.textContent
,
63 'Referrer: ' + document
.location
.href
,
64 'Service Worker should respond to fetch with the referrer URL');
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
)
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');
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
)
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
,
98 'Response should be the empty string');
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');
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');
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
;
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
);
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
) {
174 frame
.contentDocument
.body
.textContent
,
176 'Response should be the argument of the first respondWith() call.');
178 return with_iframe(scope
);
180 .then(function(frame
) {
182 frame
.contentDocument
.body
.textContent
,
183 '(0)(1)[InvalidStateError](2)[InvalidStateError](0)',
184 'Multiple calls of respondWith must throw InvalidStateErrors.');
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');
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.
211 frame
.contentDocument
.body
.textContent
,
213 'event.respondWith must set the used flag.');
215 return service_worker_unregister_and_done(t
, scope
);
217 .catch(unreached_rejection(t
));
218 }, 'Service Worker event.respondWith must set the used flag');