OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: Navigation redirection</title> | 2 <title>Service Worker: Navigation redirection</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?pipe=sub"></script> | 5 <script src="resources/get-host-info.sub.js"></script> |
6 <script src="resources/test-helpers.js"></script> | 6 <script src="resources/test-helpers.sub.js"></script> |
7 <body> | 7 <body> |
8 <script> | 8 <script> |
9 | |
10 var host_info = get_host_info(); | 9 var host_info = get_host_info(); |
11 | 10 |
12 // This test registers three Service Workers at SCOPE1, SCOPE2 and | 11 // This test registers three Service Workers at SCOPE1, SCOPE2 and |
13 // OTHER_ORIGIN_SCOPE. And checks the redirected page's URL and the requests | 12 // OTHER_ORIGIN_SCOPE. And checks the redirected page's URL and the requests |
14 // which are intercepted by Service Worker while loading redirect page. | 13 // which are intercepted by Service Worker while loading redirect page. |
15 var BASE_URL = host_info['HTTP_ORIGIN'] + base_path(); | 14 var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path(); |
16 var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + base_path(); | 15 var OTHER_BASE_URL = host_info['HTTPS_REMOTE_ORIGIN'] + base_path(); |
17 | 16 |
18 var SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.php?'; | 17 var SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.py?'; |
19 var SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.php?'; | 18 var SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.py?'; |
20 var OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.php?'; | 19 var OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.py?'; |
21 var SCRIPT = 'resources/navigation-redirect-worker.js'; | 20 var SCRIPT = 'resources/navigation-redirect-worker.js'; |
22 | 21 |
23 var OTHER_ORIGIN_IFRAME_URL = | 22 var OTHER_ORIGIN_IFRAME_URL = |
24 OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html'; | 23 OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html'; |
25 var OTHER_ORIGIN_SCOPE = | 24 var OTHER_ORIGIN_SCOPE = |
26 OTHER_BASE_URL + 'resources/navigation-redirect-scope1.php?'; | 25 OTHER_BASE_URL + 'resources/navigation-redirect-scope1.py?'; |
27 var OTHER_ORIGIN_OUT_SCOPE = | 26 var OTHER_ORIGIN_OUT_SCOPE = |
28 OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.php?'; | 27 OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.py?'; |
29 | 28 |
30 var workers; | 29 var workers; |
31 var other_origin_frame; | 30 var other_origin_frame; |
32 var setup_environment_promise; | 31 var setup_environment_promise; |
33 var message_resolvers = {}; | 32 var message_resolvers = {}; |
34 var next_message_id = 0; | 33 var next_message_id = 0; |
35 | 34 |
36 function setup_environment(t) { | 35 function setup_environment(t) { |
37 if (setup_environment_promise) | 36 if (setup_environment_promise) |
38 return setup_environment_promise; | 37 return setup_environment_promise; |
(...skipping 28 matching lines...) Expand all Loading... |
67 function get_effective_worker(registration) { | 66 function get_effective_worker(registration) { |
68 if (registration.active) | 67 if (registration.active) |
69 return registration.active; | 68 return registration.active; |
70 if (registration.waiting) | 69 if (registration.waiting) |
71 return registration.waiting; | 70 return registration.waiting; |
72 if (registration.installing) | 71 if (registration.installing) |
73 return registration.installing; | 72 return registration.installing; |
74 } | 73 } |
75 | 74 |
76 function check_all_intercepted_urls(expected_urls) { | 75 function check_all_intercepted_urls(expected_urls) { |
77 return Promise.all( | 76 var urls = []; |
78 [ | 77 return get_intercepted_urls(workers[0]) |
79 // Gets the request URLs which are intercepted by SCOPE1's SW. | 78 .then(function(url) { |
80 get_intercepted_urls(workers[0]), | 79 urls.push(url); |
81 // Gets the request URLs which are intercepted by SCOPE2's SW. | 80 return get_intercepted_urls(workers[1]); |
82 get_intercepted_urls(workers[1]), | 81 }).then(function(url) { |
83 // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's | 82 urls.push(url); |
84 // SW. This promise will resolve when get_intercepted_urls() in | 83 // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's |
85 // OTHER_ORIGIN_IFRAME_URL resolves. | 84 // SW. This promise will resolve when get_intercepted_urls() in |
86 send_to_iframe(other_origin_frame, 'get_intercepted_urls') | 85 // OTHER_ORIGIN_IFRAME_URL resolves. |
87 ]) | 86 return send_to_iframe(other_origin_frame, 'get_intercepted_urls'); |
88 .then(function(urls) { | 87 }).then(function(url) { |
| 88 urls.push(url); |
| 89 return urls; |
| 90 }).then(function(urls) { |
89 assert_object_equals( | 91 assert_object_equals( |
90 urls, expected_urls, | 92 urls, expected_urls, |
91 'Intercepted URLs should match.'); | 93 'Intercepted URLs should match.'); |
92 }); | 94 }); |
93 } | 95 } |
94 | 96 |
95 function test_redirect(url, expected_last_url, | 97 function test_redirect(url, expected_last_url, |
96 expected_intercepted_urls) { | 98 expected_intercepted_urls) { |
97 var message_promise = new Promise(function(resolve) { | 99 var message_promise = new Promise(function(resolve) { |
98 // A message which ID is 'last_url' will be sent from the iframe. | 100 // A message which ID is 'last_url' will be sent from the iframe. |
99 message_resolvers['last_url'] = resolve; | 101 message_resolvers['last_url'] = resolve; |
100 }); | 102 }); |
101 return with_iframe(url) | 103 return with_iframe(url) |
102 .then(function(f) { | 104 .then(function(f) { |
103 f.remove(); | 105 f.remove(); |
104 return check_all_intercepted_urls(expected_intercepted_urls); | 106 return check_all_intercepted_urls(expected_intercepted_urls); |
105 }) | 107 }) |
106 .then(function() { return message_promise; }) | 108 .then(function() { return message_promise; }) |
107 .then(function(last_url) { | 109 .then(function(last_url) { |
108 assert_equals( | 110 assert_equals( |
109 last_url, expected_last_url, | 111 last_url, expected_last_url, |
110 'Last URL should match.'); | 112 'Last URL should match.'); |
111 }); | 113 }); |
112 } | 114 } |
113 | 115 |
114 window.addEventListener('message', on_message, false); | 116 window.addEventListener('message', on_message, false); |
115 | 117 |
116 function on_message(e) { | 118 function on_message(e) { |
117 if (e.origin != host_info['HTTP_REMOTE_ORIGIN'] && | 119 if (e.origin != host_info['HTTPS_REMOTE_ORIGIN'] && |
118 e.origin != host_info['HTTP_ORIGIN'] ) { | 120 e.origin != host_info['HTTPS_ORIGIN'] ) { |
119 console.error('invalid origin: ' + e.origin); | 121 console.error('invalid origin: ' + e.origin); |
120 return; | 122 return; |
121 } | 123 } |
122 var resolve = message_resolvers[e.data.id]; | 124 var resolve = message_resolvers[e.data.id]; |
123 delete message_resolvers[e.data.id]; | 125 delete message_resolvers[e.data.id]; |
124 resolve(e.data.result); | 126 resolve(e.data.result); |
125 } | 127 } |
126 | 128 |
127 function send_to_iframe(frame, message) { | 129 function send_to_iframe(frame, message) { |
128 var message_id = next_message_id++; | 130 var message_id = next_message_id++; |
129 return new Promise(function(resolve) { | 131 return new Promise(function(resolve) { |
130 message_resolvers[message_id] = resolve; | 132 message_resolvers[message_id] = resolve; |
131 frame.contentWindow.postMessage( | 133 frame.contentWindow.postMessage( |
132 {id: message_id, message: message}, | 134 {id: message_id, message: message}, |
133 host_info['HTTP_REMOTE_ORIGIN']); | 135 host_info['HTTPS_REMOTE_ORIGIN']); |
134 }); | 136 }); |
135 } | 137 } |
136 | 138 |
137 function get_intercepted_urls(worker) { | 139 function get_intercepted_urls(worker) { |
138 return new Promise(function(resolve) { | 140 return new Promise(function(resolve) { |
139 var channel = new MessageChannel(); | 141 var channel = new MessageChannel(); |
140 channel.port1.onmessage = function(msg) { resolve(msg.data.urls); }; | 142 channel.port1.onmessage = function(msg) { resolve(msg.data.urls); }; |
141 worker.postMessage({port: channel.port2}, [channel.port2]); | 143 worker.postMessage({port: channel.port2}, [channel.port2]); |
142 }); | 144 }); |
143 } | 145 } |
(...skipping 23 matching lines...) Expand all Loading... |
167 SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE), | 169 SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE), |
168 OUT_SCOPE, | 170 OUT_SCOPE, |
169 [[SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE)], [], []]); | 171 [[SCOPE1 + 'url=' + encodeURIComponent(OUT_SCOPE)], [], []]); |
170 }); | 172 }); |
171 }, 'SW-fallbacked redirect to same-origin out-scope.'); | 173 }, 'SW-fallbacked redirect to same-origin out-scope.'); |
172 promise_test(function(t) { | 174 promise_test(function(t) { |
173 return setup_environment(t).then(function() { | 175 return setup_environment(t).then(function() { |
174 return test_redirect( | 176 return test_redirect( |
175 SCOPE1 + 'url=' + encodeURIComponent(SCOPE1), | 177 SCOPE1 + 'url=' + encodeURIComponent(SCOPE1), |
176 SCOPE1, | 178 SCOPE1, |
177 [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1), SCOPE1], [], []]); | 179 [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE1)], [], []]); |
178 }); | 180 }); |
179 }, 'SW-fallbacked redirect to same-origin same-scope.'); | 181 }, 'SW-fallbacked redirect to same-origin same-scope.'); |
180 promise_test(function(t) { | 182 promise_test(function(t) { |
181 return setup_environment(t).then(function() { | 183 return setup_environment(t).then(function() { |
182 return test_redirect( | 184 return test_redirect( |
183 SCOPE1 + 'url=' + encodeURIComponent(SCOPE2), | 185 SCOPE1 + 'url=' + encodeURIComponent(SCOPE2), |
184 SCOPE2, | 186 SCOPE2, |
185 [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE2)], [SCOPE2], []]); | 187 [[SCOPE1 + 'url=' + encodeURIComponent(SCOPE2)], [], []]); |
186 }); | 188 }); |
187 }, 'SW-fallbacked redirect to same-origin other-scope.'); | 189 }, 'SW-fallbacked redirect to same-origin other-scope.'); |
188 promise_test(function(t) { | 190 promise_test(function(t) { |
189 return setup_environment(t).then(function() { | 191 return setup_environment(t).then(function() { |
190 return test_redirect( | 192 return test_redirect( |
191 SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), | 193 SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), |
192 OTHER_ORIGIN_OUT_SCOPE, | 194 OTHER_ORIGIN_OUT_SCOPE, |
193 [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], | 195 [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], |
194 [], | 196 [], |
195 []]); | 197 []]); |
196 }); | 198 }); |
197 }, 'SW-fallbacked redirect to other-origin out-scope.'); | 199 }, 'SW-fallbacked redirect to other-origin out-scope.'); |
198 promise_test(function(t) { | 200 promise_test(function(t) { |
199 return setup_environment(t).then(function() { | 201 return setup_environment(t).then(function() { |
200 return test_redirect( | 202 return test_redirect( |
201 SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE), | 203 SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE), |
202 OTHER_ORIGIN_SCOPE, | 204 OTHER_ORIGIN_SCOPE, |
203 [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)], | 205 [[SCOPE1 + 'url=' + encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
204 [], | 206 [], |
205 [OTHER_ORIGIN_SCOPE]]); | 207 []]); |
206 }); | 208 }); |
207 }, 'SW-fallbacked redirect to other-origin in-scope.'); | 209 }, 'SW-fallbacked redirect to other-origin in-scope.'); |
208 | 210 |
209 // SW generated redirect. | 211 // SW generated redirect. |
210 // SW: event.respondWith(Response.redirect(params['url'])); | 212 // SW: event.respondWith(Response.redirect(params['url'])); |
211 promise_test(function(t) { | 213 promise_test(function(t) { |
212 return setup_environment(t).then(function() { | 214 return setup_environment(t).then(function() { |
213 return test_redirect( | 215 return test_redirect( |
214 SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE), | 216 SCOPE1 + 'sw=gen&url=' + encodeURIComponent(OUT_SCOPE), |
215 OUT_SCOPE, | 217 OUT_SCOPE, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 [[SCOPE1 + 'sw=opaqueThroughCache&url=' + | 440 [[SCOPE1 + 'sw=opaqueThroughCache&url=' + |
439 encodeURIComponent(OTHER_ORIGIN_SCOPE)], | 441 encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
440 [], | 442 [], |
441 [OTHER_ORIGIN_SCOPE]]); | 443 [OTHER_ORIGIN_SCOPE]]); |
442 }); | 444 }); |
443 }, | 445 }, |
444 'Redirect to other-origin in-scope with opaque redirect response which ' + | 446 'Redirect to other-origin in-scope with opaque redirect response which ' + |
445 'is passed through Cache.'); | 447 'is passed through Cache.'); |
446 </script> | 448 </script> |
447 </body> | 449 </body> |
OLD | NEW |