2 <title>Service Worker: the fallback behavior of FetchEvent
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"../resources/get-host-info.js?pipe=sub"></script>
6 <script src=
"resources/test-helpers.js"></script>
9 function assert_resolves(promise
, description
) {
10 return new Promise(function(resolve
, reject
) {
13 function() { resolve(); },
14 function() { reject(description
); });
18 function assert_rejects(promise
, description
) {
19 return new Promise(function(resolve
, reject
) {
22 function() { reject(description
); },
23 function() { resolve(); });
27 function get_fetched_urls(worker
) {
28 return new Promise(function(resolve
) {
29 var channel
= new MessageChannel();
30 channel
.port1
.onmessage = function(msg
) { resolve(msg
); };
31 worker
.postMessage({port
: channel
.port2
}, [channel
.port2
]);
35 function check_urls(worker
, expected_requests
, description
) {
36 return get_fetched_urls(worker
)
38 var requests
= msg
.data
.requests
;
39 assert_object_equals(requests
, expected_requests
, description
);
43 async_test(function(t
) {
44 var SCOPE
= 'resources/fetch-request-fallback-iframe.html';
45 var SCRIPT
= 'resources/fetch-request-fallback-worker.js';
46 var host_info
= get_host_info();
47 var BASE_URL
= host_info
['HTTP_ORIGIN'] +
48 '/serviceworker/resources/fetch-access-control.php?';
49 var BASE_PNG_URL
= BASE_URL
+ 'PNGIMAGE&';
50 var OTHER_BASE_URL
= host_info
['HTTP_REMOTE_ORIGIN'] +
51 '/serviceworker/resources/fetch-access-control.php?';
52 var OTHER_BASE_PNG_URL
= OTHER_BASE_URL
+ 'PNGIMAGE&';
53 var REDIRECT_URL
= host_info
['HTTP_ORIGIN'] +
54 '/serviceworker/resources/redirect.php?Redirect=';
57 service_worker_unregister_and_register(t
, SCRIPT
, SCOPE
)
58 .then(function(registration
) {
59 worker
= registration
.installing
;
60 return wait_for_state(t
, worker
, 'activated');
62 .then(function() { return with_iframe(SCOPE
); })
68 url
: host_info
['HTTP_ORIGIN'] + '/serviceworker/' + SCOPE
,
71 'The SW must intercept the request for a main resourc.');
74 return assert_resolves(
75 frame
.contentWindow
.xhr(BASE_URL
),
76 'SW fallbacked same origin XHR should succeed.');
81 [{ url
: BASE_URL
, mode
: 'cors' }],
82 'The SW must intercept the request of same origin XHR.');
85 return assert_rejects(
86 frame
.contentWindow
.xhr(OTHER_BASE_URL
),
87 'SW fallbacked CORS-unsupported other origin XHR should fail.');
92 [{ url
: OTHER_BASE_URL
, mode
: 'cors' }],
93 'The SW must intercept the request of CORS-unsupported other ' +
97 return assert_resolves(
98 frame
.contentWindow
.xhr(OTHER_BASE_URL
+ 'ACAOrigin=*'),
99 'SW fallbacked CORS-supported other origin XHR should succeed.');
104 [{ url
: OTHER_BASE_URL
+ 'ACAOrigin=*', mode
: 'cors' }],
105 'The SW must intercept the request of CORS-supported other ' +
109 return assert_resolves(
110 frame
.contentWindow
.xhr(
111 REDIRECT_URL
+ encodeURIComponent(BASE_URL
)),
112 'SW fallbacked redirected XHR should succeed.');
118 url
: REDIRECT_URL
+ encodeURIComponent(BASE_URL
),
121 'The SW must intercept only the first request of redirected ' +
125 return assert_rejects(
126 frame
.contentWindow
.xhr(
127 REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_URL
)),
128 'SW fallbacked XHR which is redirected to CORS-unsupported ' +
129 'other origin should fail.');
135 url
: REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_URL
),
138 'The SW must intercept only the first request for XHR which is' +
139 ' redirected to CORS-unsupported other origin.');
142 return assert_resolves(
143 frame
.contentWindow
.xhr(
145 encodeURIComponent(OTHER_BASE_URL
+ 'ACAOrigin=*')),
146 'SW fallbacked XHR which is redirected to CORS-supported other ' +
147 'origin should succeed.');
154 encodeURIComponent(OTHER_BASE_URL
+ 'ACAOrigin=*'),
157 'The SW must intercept only the first request for XHR which is ' +
158 'redirected to CORS-supported other origin.');
161 return assert_resolves(
162 frame
.contentWindow
.load_image(BASE_PNG_URL
, ''),
163 'SW fallbacked image request should succeed.');
168 [{ url
: BASE_PNG_URL
, mode
: 'no-cors' }],
169 'The SW must intercept the request for image.');
172 return assert_resolves(
173 frame
.contentWindow
.load_image(OTHER_BASE_PNG_URL
, ''),
174 'SW fallbacked other origin image request should succeed.');
179 [{ url
: OTHER_BASE_PNG_URL
, mode
: 'no-cors' }],
180 'The SW must intercept the request for other origin image.')
183 return assert_rejects(
184 frame
.contentWindow
.load_image(OTHER_BASE_PNG_URL
, 'anonymous'),
185 'SW fallbacked CORS-unsupported other origin image request ' +
191 [{ url
: OTHER_BASE_PNG_URL
, mode
: 'cors' }],
192 'The SW must intercept the request for CORS-unsupported other ' +
196 return assert_resolves(
197 frame
.contentWindow
.load_image(
198 OTHER_BASE_PNG_URL
+ 'ACAOrigin=*', 'anonymous'),
199 'SW fallbacked CORS-supported other origin image request should' +
205 [{ url
: OTHER_BASE_PNG_URL
+ 'ACAOrigin=*', mode
: 'cors' }],
206 'The SW must intercept the request for CORS-supported other ' +
210 return assert_resolves(
211 frame
.contentWindow
.load_image(
212 REDIRECT_URL
+ encodeURIComponent(BASE_PNG_URL
), ''),
213 'SW fallbacked redirected image request should succeed.');
219 url
: REDIRECT_URL
+ encodeURIComponent(BASE_PNG_URL
),
222 'The SW must intercept only the first request for redirected ' +
226 return assert_resolves(
227 frame
.contentWindow
.load_image(
228 REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_PNG_URL
), ''),
229 'SW fallbacked image request which is redirected to other ' +
230 'origin should succeed.');
236 url
: REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_PNG_URL
),
239 'The SW must intercept only the first request for image ' +
240 'resource which is redirected to other origin.');
243 return assert_rejects(
244 frame
.contentWindow
.load_image(
245 REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_PNG_URL
),
247 'SW fallbacked image request which is redirected to ' +
248 'CORS-unsupported other origin should fail.');
254 url
: REDIRECT_URL
+ encodeURIComponent(OTHER_BASE_PNG_URL
),
257 'The SW must intercept only the first request for image ' +
258 'resource which is redirected to CORS-unsupported other origin.');
261 return assert_resolves(
262 frame
.contentWindow
.load_image(
264 encodeURIComponent(OTHER_BASE_PNG_URL
+ 'ACAOrigin=*'),
266 'SW fallbacked image request which is redirected to ' +
267 'CORS-supported other origin should succeed.');
274 encodeURIComponent(OTHER_BASE_PNG_URL
+ 'ACAOrigin=*'),
277 'The SW must intercept only the first request for image ' +
278 'resource which is redirected to CORS-supported other origin.');
282 service_worker_unregister_and_done(t
, SCOPE
);
284 .catch(unreached_rejection(t
));
285 }, 'Verify the fallback behavior of FetchEvent');