OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: the fallback behavior of FetchEvent</title> | 2 <title>Service Worker: the fallback behavior of FetchEvent</title> |
3 <script src="../resources/testharness.js"></script> | 3 <script src="../resources/testharness.js"></script> |
4 <script src="../resources/testharnessreport.js"></script> | 4 <script src="../resources/testharnessreport.js"></script> |
5 <script src="../resources/get-host-info.js"></script> | 5 <script src="../resources/get-host-info.js"></script> |
6 <script src="resources/test-helpers.js?pipe=sub"></script> | 6 <script src="resources/test-helpers.js?pipe=sub"></script> |
falken
2015/08/11 09:15:33
should we do ?pipe=sub on all our layout tests? I'
horo
2015/08/12 11:28:10
It was intended to get the port number with get_ho
falken
2015/08/13 06:22:03
Oops :) So I guess all get-host-info.js should use
| |
7 <script> | 7 <script> |
8 var expected_urls = []; | 8 function xhr_test(frame, url, expect_success) { |
9 | |
10 function xhr_fail_test(frame, url) { | |
11 expected_urls.push(url); | |
12 return new Promise(function(resolve, reject) { | 9 return new Promise(function(resolve, reject) { |
13 frame.contentWindow.xhr(url) | 10 frame.contentWindow.xhr(url) |
14 .then(function(){ | 11 .then(function(){ |
falken
2015/08/11 09:15:33
nit: space before {
horo
2015/08/12 11:28:10
removed.
| |
15 reject(url + ' should fail.'); | 12 if (expect_success) |
13 resolve(); | |
14 else | |
15 reject(url + ' should fail.'); | |
16 }) | 16 }) |
17 .catch(function(){ | 17 .catch(function(){ |
falken
2015/08/11 09:15:33
ditto
horo
2015/08/12 11:28:10
removed.
| |
18 resolve(); | 18 if (expect_success) |
19 reject(url + ' should succeed.'); | |
20 else | |
21 resolve(); | |
19 }); | 22 }); |
falken
2015/08/11 09:15:33
This pattern seems dangerous, if you have:
f(x) {
horo
2015/08/12 11:28:10
assert_resolves/assert_rejects looks good.
I rewro
| |
20 }); | 23 }); |
21 } | 24 } |
22 | 25 |
23 function xhr_succeed_test(frame, url) { | 26 function img_test(frame, url, cross_origin, expect_success) { |
24 expected_urls.push(url); | |
25 return new Promise(function(resolve, reject) { | 27 return new Promise(function(resolve, reject) { |
26 frame.contentWindow.xhr(url) | 28 frame.contentWindow.load_image(url, cross_origin) |
27 .then(function(){ | 29 .then(function(){ |
28 resolve(); | 30 if (expect_success) |
31 resolve(); | |
32 else | |
33 reject(url + ' should fail.'); | |
29 }) | 34 }) |
30 .catch(function(){ | 35 .catch(function(){ |
31 reject(url + ' should succeed.'); | 36 if (expect_success) |
37 reject(url + ' should succeed.'); | |
38 else | |
39 resolve(); | |
32 }); | 40 }); |
33 }); | 41 }); |
34 } | 42 } |
35 | 43 |
44 function get_fetched_urls(worker) { | |
45 return new Promise(function(resolve) { | |
46 var channel = new MessageChannel(); | |
47 channel.port1.onmessage = function(msg) { resolve(msg); }; | |
48 worker.postMessage({port: channel.port2}, [channel.port2]); | |
49 }) | |
falken
2015/08/11 09:15:32
nit: semicolon
horo
2015/08/12 11:28:10
Done.
| |
50 } | |
51 | |
52 function check_urls(worker, expected_requests, description) { | |
53 return get_fetched_urls(worker) | |
54 .then(function(msg) { | |
55 var requests = msg.data.requests; | |
56 assert_object_equals(requests, expected_requests, description); | |
57 }) | |
falken
2015/08/11 09:15:32
semicolon
horo
2015/08/12 11:28:10
Done.
| |
58 } | |
59 | |
36 async_test(function(t) { | 60 async_test(function(t) { |
37 var SCOPE = 'resources/fetch-request-fallback-iframe.html'; | 61 var SCOPE = 'resources/fetch-request-fallback-iframe.html'; |
38 var SCRIPT = 'resources/fetch-request-fallback-worker.js'; | 62 var SCRIPT = 'resources/fetch-request-fallback-worker.js'; |
39 var host_info = get_host_info(); | 63 var host_info = get_host_info(); |
40 var BASE_URL = host_info['HTTP_ORIGIN'] + | 64 var BASE_URL = host_info['HTTP_ORIGIN'] + |
41 '/serviceworker/resources/fetch-access-control.php?'; | 65 '/serviceworker/resources/fetch-access-control.php?'; |
66 var BASE_PNG_URL = BASE_URL + 'PNGIMAGE&'; | |
42 var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + | 67 var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + |
43 '/serviceworker/resources/fetch-access-control.php?'; | 68 '/serviceworker/resources/fetch-access-control.php?'; |
69 var OTHER_BASE_PNG_URL = OTHER_BASE_URL + 'PNGIMAGE&'; | |
44 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + | 70 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
45 '/serviceworker/resources/redirect.php?Redirect='; | 71 '/serviceworker/resources/redirect.php?Redirect='; |
46 var frame; | 72 var frame; |
47 var worker; | 73 var worker; |
48 service_worker_unregister_and_register(t, SCRIPT, SCOPE) | 74 service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
49 .then(function(registration) { | 75 .then(function(registration) { |
50 worker = registration.installing; | 76 worker = registration.installing; |
51 return wait_for_state(t, worker, 'activated'); | 77 return wait_for_state(t, worker, 'activated'); |
52 }) | 78 }) |
53 .then(function() { return with_iframe(SCOPE); }) | 79 .then(function() { return with_iframe(SCOPE); }) |
54 .then(function(f) { | 80 .then(function(f) { |
55 frame = f; | 81 frame = f; |
56 return xhr_succeed_test(frame, BASE_URL); | 82 return check_urls( |
57 }) | 83 worker, |
58 .then(function(f) { | 84 [{ |
59 return xhr_fail_test(frame, OTHER_BASE_URL); | 85 url: host_info['HTTP_ORIGIN'] + '/serviceworker/' + SCOPE, |
60 }) | 86 mode: 'no-cors' |
61 .then(function(f) { | 87 }], |
62 return xhr_succeed_test(frame, OTHER_BASE_URL + 'ACAOrigin=*'); | 88 "The SW must catch the request of page load."); |
falken
2015/08/11 09:15:32
s/catch/intercept/ throughout
"the request of pag
horo
2015/08/12 11:28:10
Done.
| |
63 }) | |
64 .then(function(f) { | |
65 return xhr_succeed_test(frame, | |
66 REDIRECT_URL + encodeURIComponent(BASE_URL)); | |
67 }) | 89 }) |
68 .then(function() { | 90 .then(function() { |
69 return xhr_fail_test( | 91 return xhr_test(frame, BASE_URL, true); |
70 frame, | |
71 REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL)); | |
72 }) | 92 }) |
73 .then(function() { | 93 .then(function() { |
74 return xhr_succeed_test( | 94 return check_urls( |
95 worker, | |
96 [{ url: BASE_URL, mode: 'cors' }], | |
97 "The SW must catch the request of same origin XHR."); | |
98 }) | |
99 .then(function() { | |
100 return xhr_test(frame, OTHER_BASE_URL, false); | |
101 }) | |
102 .then(function() { | |
103 return check_urls( | |
104 worker, | |
105 [{ url: OTHER_BASE_URL, mode: 'cors' }], | |
106 "The SW must catch the request of CORS unsupported other origin XH R."); | |
falken
2015/08/11 09:15:33
"CORS-unsupported" is easier to read
All theses d
horo
2015/08/12 11:28:10
Done.
| |
107 }) | |
108 .then(function() { | |
109 return xhr_test(frame, OTHER_BASE_URL + 'ACAOrigin=*', true); | |
110 }) | |
111 .then(function() { | |
112 return check_urls( | |
113 worker, | |
114 [{ url: OTHER_BASE_URL + 'ACAOrigin=*', mode: 'cors' }], | |
115 "The SW must catch the request of CORS supported other origin XHR. "); | |
falken
2015/08/11 09:15:33
CORS-supported
horo
2015/08/12 11:28:10
Done.
| |
116 }) | |
117 .then(function() { | |
118 return xhr_test(frame, | |
119 REDIRECT_URL + encodeURIComponent(BASE_URL), | |
120 true); | |
121 }) | |
122 .then(function() { | |
123 return check_urls( | |
124 worker, | |
125 [{ | |
126 url: REDIRECT_URL + encodeURIComponent(BASE_URL), | |
127 mode: 'cors' | |
128 }], | |
129 "The SW must catch only the first request of redirected XHR."); | |
130 }) | |
131 .then(function() { | |
132 return xhr_test( | |
133 frame, | |
134 REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL), | |
135 false); | |
136 }) | |
137 .then(function() { | |
138 return check_urls( | |
139 worker, | |
140 [{ | |
141 url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_URL), | |
142 mode: 'cors' | |
143 }], | |
144 "The SW must catch only the first request for XHR which is redirec ted to CORS unsupported other origin."); | |
145 }) | |
146 .then(function() { | |
147 return xhr_test( | |
75 frame, | 148 frame, |
76 REDIRECT_URL + | 149 REDIRECT_URL + |
77 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*')); | 150 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'), |
151 true); | |
78 }) | 152 }) |
79 .then(function() { | 153 .then(function() { |
80 return new Promise(function(resolve) { | 154 return check_urls( |
81 var channel = new MessageChannel(); | 155 worker, |
82 channel.port1.onmessage = t.step_func(function(msg) { | 156 [{ |
83 frame.remove(); | 157 url: REDIRECT_URL + |
84 resolve(msg); | 158 encodeURIComponent(OTHER_BASE_URL + 'ACAOrigin=*'), |
85 }); | 159 mode: 'cors' |
86 worker.postMessage({port: channel.port2}, [channel.port2]); | 160 }], |
87 }); | 161 "The SW must catch only the first request for XHR which is redirec ted to CORS supported other origin."); |
88 }) | 162 }) |
89 .then(function(msg) { | 163 .then(function() { return img_test(frame, BASE_PNG_URL, "", true); }) |
90 var requests = msg.data.requests; | 164 .then(function() { |
91 assert_equals(requests.length, expected_urls.length + 1, | 165 return check_urls( |
92 'The count of the requests which are passed to the ' + | 166 worker, |
93 'ServiceWorker must be correct.'); | 167 [{ url: BASE_PNG_URL, mode: 'no-cors' }], |
94 assert_equals(requests[0].url, new URL(SCOPE, location).toString(), | 168 "The SW must catch the request for image.") |
95 'The first request to the SW must be the request for ' + | 169 }) |
96 'the page.'); | 170 .then(function() { |
97 assert_equals(requests[0].mode, 'no-cors', | 171 return img_test(frame, OTHER_BASE_PNG_URL, "", true); |
98 'The mode of the first request to the SW must be ' + | 172 }) |
99 'no-cors.'); | 173 .then(function() { |
100 for (var i = 0; i < expected_urls.length; ++i) { | 174 return check_urls( |
101 assert_equals(requests[i + 1].url, expected_urls[i], | 175 worker, |
102 'The URL of the request which was passed from XHR ' + | 176 [{ url: OTHER_BASE_PNG_URL, mode: 'no-cors' }], |
103 'to the ServiceWorker must be correct.'); | 177 "The SW must catch the request for other origin image.") |
104 assert_equals(requests[i + 1].mode, 'cors', | 178 }) |
105 'The mode of the request which was passed from XHR ' + | 179 .then(function() { |
106 'to the ServiceWorker must be cors.'); | 180 return img_test(frame, OTHER_BASE_PNG_URL, "anonymous", false); |
107 } | 181 }) |
182 .then(function() { | |
183 return check_urls( | |
184 worker, | |
185 [{ url: OTHER_BASE_PNG_URL, mode: 'cors' }], | |
186 "The SW must catch the request for CORS unsupported other origin i mage.") | |
187 }) | |
188 .then(function() { | |
189 return img_test( | |
190 frame, OTHER_BASE_PNG_URL + 'ACAOrigin=*', "anonymous", true); | |
191 }) | |
192 .then(function() { | |
193 return check_urls( | |
194 worker, | |
195 [{ url: OTHER_BASE_PNG_URL + 'ACAOrigin=*', mode: 'cors' }], | |
196 "The SW must catch the request for CORS supported other origin ima ge.") | |
197 }) | |
198 .then(function() { | |
199 return img_test(frame, REDIRECT_URL + encodeURIComponent(BASE_PNG_URL) , "", true); | |
200 }) | |
201 .then(function() { | |
202 return check_urls( | |
203 worker, | |
204 [{ url: REDIRECT_URL + encodeURIComponent(BASE_PNG_URL), mode: 'no -cors' }], | |
205 "The SW must catch only the first request for redirected image res ource."); | |
206 }) | |
207 .then(function() { | |
208 return img_test(frame, REDIRECT_URL + encodeURIComponent(OTHER_BASE_PN G_URL), "", true); | |
209 }) | |
210 .then(function() { | |
211 return check_urls( | |
212 worker, | |
213 [{ url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), mod e: 'no-cors' }], | |
214 "The SW must catch only the first request for image resource which is redirected to othre origin."); | |
falken
2015/08/11 09:15:33
other
horo
2015/08/12 11:28:10
Done.
| |
215 }) | |
216 .then(function() { | |
217 return img_test(frame, REDIRECT_URL + encodeURIComponent(OTHER_BASE_PN G_URL), "anonymous", false); | |
218 }) | |
219 .then(function() { | |
220 return check_urls( | |
221 worker, | |
222 [{ url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL), mod e: 'cors' }], | |
223 "The SW must catch only the first request for image resource which is redirected to CORS unsupported other origin."); | |
224 }) | |
225 .then(function() { | |
226 return img_test(frame, REDIRECT_URL + encodeURIComponent(OTHER_BASE_PN G_URL + 'ACAOrigin=*'), "anonymous", true); | |
227 }) | |
228 .then(function() { | |
229 return check_urls( | |
230 worker, | |
231 [{ url: REDIRECT_URL + encodeURIComponent(OTHER_BASE_PNG_URL + 'AC AOrigin=*'), mode: 'cors' }], | |
232 "The SW must catch only the first request for image resource which is redirected to CORS supported other origin."); | |
233 }) | |
234 .then(function() { | |
235 frame.remove(); | |
108 service_worker_unregister_and_done(t, SCOPE); | 236 service_worker_unregister_and_done(t, SCOPE); |
109 }) | 237 }) |
110 .catch(unreached_rejection(t)); | 238 .catch(unreached_rejection(t)); |
111 }, 'Verify the fallback behavior of FetchEvent'); | 239 }, 'Verify the fallback behavior of FetchEvent'); |
112 </script> | 240 </script> |
OLD | NEW |