Index: LayoutTests/http/tests/serviceworker/navigation-redirect.html |
diff --git a/LayoutTests/http/tests/serviceworker/navigation-redirect.html b/LayoutTests/http/tests/serviceworker/navigation-redirect.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e8b64f5ed2a075f012513a833a17a73781c1b57e |
--- /dev/null |
+++ b/LayoutTests/http/tests/serviceworker/navigation-redirect.html |
@@ -0,0 +1,411 @@ |
+<!DOCTYPE html> |
+<title>Service Worker: Navigation redirection</title> |
+<script src="../resources/testharness.js"></script> |
+<script src="../resources/testharnessreport.js"></script> |
+<script src="../resources/get-host-info.js?pipe=sub"></script> |
+<script src="resources/test-helpers.js"></script> |
+<body> |
+<script> |
+function get_effective_worker(registration) { |
+ if (registration.active) |
+ return registration.active; |
+ if (registration.waiting) |
+ return registration.waiting; |
+ if (registration.installing) |
+ return registration.installing; |
+} |
+ |
+var host_info = get_host_info(); |
+ |
+window.addEventListener('message', on_message, false); |
+ |
+var message_resolvers = {}; |
+var next_message_id = 0; |
+ |
+function on_message(e) { |
+ if (e.origin != host_info['HTTP_REMOTE_ORIGIN'] && |
+ e.origin != host_info['HTTP_ORIGIN'] ) { |
+ console.error('invalid origin: ' + e.origin); |
+ return; |
+ } |
+ var resolve = message_resolvers[e.data.id]; |
+ delete message_resolvers[e.data.id]; |
+ resolve(e.data.result); |
+} |
+ |
+function send_to_iframe(frame, message) { |
+ var message_id = next_message_id++; |
+ return new Promise(function(resolve) { |
+ message_resolvers[message_id] = resolve; |
+ frame.contentWindow.postMessage( |
+ {id: message_id, message: message}, |
+ host_info['HTTP_REMOTE_ORIGIN']); |
+ }); |
+} |
+ |
+function get_intercepted_urls(worker) { |
+ return new Promise(function(resolve) { |
+ var channel = new MessageChannel(); |
+ channel.port1.onmessage = function(msg) { resolve(msg.data.urls); }; |
+ worker.postMessage({port: channel.port2}, [channel.port2]); |
+ }); |
+} |
+ |
+async_test(function(t) { |
+ // This test registers three Service Workers at SCOPE1, SCOPE2 and |
+ // OTHER_ORIGIN_SCOPE. And checks the redirected page's URL and the requests |
+ // which are intercepted by Service Worker while loading redirect page. |
+ var BASE_URL = host_info['HTTP_ORIGIN'] + base_path(); |
+ var OTHER_BASE_URL = host_info['HTTP_REMOTE_ORIGIN'] + base_path(); |
+ |
+ var SCOPE1 = BASE_URL + 'resources/navigation-redirect-scope1.php?'; |
+ var SCOPE2 = BASE_URL + 'resources/navigation-redirect-scope2.php?'; |
+ var OUT_SCOPE = BASE_URL + 'resources/navigation-redirect-out-scope.php?'; |
+ var SCRIPT = 'resources/navigation-redirect-worker.js'; |
+ |
+ var OTHER_ORIGIN_IFRAME_URL = |
+ OTHER_BASE_URL + 'resources/navigation-redirect-other-origin.html'; |
+ var OTHER_ORIGIN_SCOPE = |
+ OTHER_BASE_URL + 'resources/navigation-redirect-scope1.php?'; |
+ var OTHER_ORIGIN_OUT_SCOPE = |
+ OTHER_BASE_URL + 'resources/navigation-redirect-out-scope.php?'; |
+ |
+ var workers; |
+ var other_origin_frame; |
+ |
+ function check_all_intercepted_urls(expected_urls, description) { |
+ return Promise.all([ |
+ // Gets the request URLs which are intercepted by SCOPE1's SW. |
+ get_intercepted_urls(workers[0]), |
+ // Gets the request URLs which are intercepted by SCOPE2's SW. |
+ get_intercepted_urls(workers[1]), |
+ // Gets the request URLs which are intercepted by OTHER_ORIGIN_SCOPE's |
+ // SW. This promise will resolve when get_intercepted_urls() in |
+ // OTHER_ORIGIN_IFRAME_URL resolves. |
+ send_to_iframe(other_origin_frame, 'get_intercepted_urls')]) |
+ .then(function(urls) { |
+ assert_object_equals( |
+ urls, expected_urls, |
+ 'Intercepted URLs should match: ' + description); |
+ }); |
+ } |
+ |
+ function test_redirect(url, expected_last_url, |
+ expected_intercepted_urls, description) { |
+ var message_promise = new Promise(function(resolve) { |
+ // A message which ID is 'last_url' will be sent from the iframe. |
+ message_resolvers['last_url'] = resolve; |
+ }); |
+ return with_iframe(url) |
+ .then(function(f) { |
+ f.remove(); |
+ return check_all_intercepted_urls(expected_intercepted_urls, |
+ description); |
+ }) |
+ .then(function() { return message_promise; }) |
+ .then(function(last_url) { |
+ assert_equals( |
+ last_url, expected_last_url, |
+ 'Last URL should match: ' + description); |
+ }); |
+ } |
+ |
+ with_iframe(OTHER_ORIGIN_IFRAME_URL) |
+ .then(function(f) { |
+ // In this frame we register a Service Worker at OTHER_ORIGIN_SCOPE. |
+ // And will use this frame to communicate with the worker. |
+ other_origin_frame = f; |
+ return Promise.all( |
+ [service_worker_unregister_and_register(t, SCRIPT, SCOPE1), |
+ service_worker_unregister_and_register(t, SCRIPT, SCOPE2)]); |
+ }) |
+ .then(function(registrations) { |
+ workers = registrations.map(get_effective_worker); |
+ return Promise.all([ |
+ wait_for_state(t, workers[0], 'activated'), |
+ wait_for_state(t, workers[1], 'activated'), |
+ // This promise will resolve when |wait_for_worker_promise| |
+ // in OTHER_ORIGIN_IFRAME_URL resolves. |
+ send_to_iframe(other_origin_frame, "wait_for_worker")]); |
+ }) |
+ // Normal redirect. |
+ .then(function() { |
+ return test_redirect( |
+ OUT_SCOPE + "url=" + encodeURIComponent(SCOPE1), |
+ SCOPE1, |
+ [[SCOPE1], [], []], |
+ 'Normal redirect to same-origin scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ OUT_SCOPE + "url=" + encodeURIComponent(OTHER_ORIGIN_SCOPE), |
+ OTHER_ORIGIN_SCOPE, |
+ [[], [], [OTHER_ORIGIN_SCOPE]], |
+ 'Normal redirect to other-origin scope.'); |
+ }) |
+ |
+ // SW fallbacked redirect. SW doesn't handle the fetch request. |
+ .then(function() { |
+ return test_redirect( |
bkelly
2015/08/19 14:21:06
If we intend to uplift these as WPT tests, it woul
horo
2015/08/20 06:35:51
Acknowledged.
I will create crbug for this issue a
|
+ SCOPE1 + "url=" + encodeURIComponent(OUT_SCOPE), |
+ OUT_SCOPE, |
+ [[SCOPE1 + "url=" + encodeURIComponent(OUT_SCOPE)], |
+ [], |
+ []], |
+ 'SW-fallbacked redirect to same-origin out-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "url=" + encodeURIComponent(SCOPE1), |
+ SCOPE1, |
+ [[SCOPE1 + "url=" + encodeURIComponent(SCOPE1), SCOPE1], |
+ [], |
+ []], |
+ 'SW-fallbacked redirect to same-origin same-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "url=" + encodeURIComponent(SCOPE2), |
+ SCOPE2, |
+ [[SCOPE1 + "url=" + encodeURIComponent(SCOPE2)], |
+ [SCOPE2], |
+ []], |
+ 'SW-fallbacked redirect to same-origin other-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "url=" + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), |
+ OTHER_ORIGIN_OUT_SCOPE, |
+ [[SCOPE1 + "url=" + encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], |
+ [], |
+ []], |
+ 'SW-fallbacked redirect to other-origin out-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "url=" + encodeURIComponent(OTHER_ORIGIN_SCOPE), |
+ OTHER_ORIGIN_SCOPE, |
+ [[SCOPE1 + "url=" + encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
+ [], |
+ [OTHER_ORIGIN_SCOPE]], |
+ 'SW-fallbacked redirect to other-origin in-scope.'); |
+ }) |
+ |
+ // SW generated redirect. |
+ // SW: event.respondWith(Response.redirect(params['url'])); |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=gen&url=" + encodeURIComponent(OUT_SCOPE), |
+ OUT_SCOPE, |
+ [[SCOPE1 + "sw=gen&url=" + encodeURIComponent(OUT_SCOPE)], |
+ [], |
+ []], |
+ 'SW-generated redirect to same-origin out-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=gen&url=" + encodeURIComponent(SCOPE1), |
+ SCOPE1, |
+ [[SCOPE1 + "sw=gen&url=" + encodeURIComponent(SCOPE1), SCOPE1], |
+ [], |
+ []], |
+ 'SW-generated redirect to same-origin same-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=gen&url=" + encodeURIComponent(SCOPE2), |
+ SCOPE2, |
+ [[SCOPE1 + "sw=gen&url=" + encodeURIComponent(SCOPE2)], |
+ [SCOPE2], |
+ []], |
+ 'SW-generated redirect to same-origin other-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=gen&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), |
+ OTHER_ORIGIN_OUT_SCOPE, |
+ [[SCOPE1 + "sw=gen&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], |
+ [], |
+ []], |
+ 'SW-generated redirect to other-origin out-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=gen&url=" + encodeURIComponent(OTHER_ORIGIN_SCOPE), |
+ OTHER_ORIGIN_SCOPE, |
+ [[SCOPE1 + "sw=gen&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
+ [], |
+ [OTHER_ORIGIN_SCOPE]], |
+ 'SW-generated redirect to other-origin in-scope.'); |
+ }) |
+ |
+ // SW fetched redirect. |
+ // SW: event.respondWith(fetch(event.request)); |
+ // TODO(horo): When we change Request.redirect of navigation requests to |
+ // 'manual', the expected last URL shuold be changed. (crbug.com/510650) |
+ // Spec Issue: https://github.com/whatwg/fetch/issues/106 |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(OUT_SCOPE), |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(OUT_SCOPE), |
+ [[SCOPE1 + "sw=fetch&url=" + encodeURIComponent(OUT_SCOPE)], |
+ [], |
+ []], |
+ 'SW-fetched redirect to same-origin out-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE1), |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE1), |
+ [[SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE1)], |
+ [], |
+ []], |
+ 'SW-fetched redirect to same-origin same-scope.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE2), |
+ SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE2), |
+ [[SCOPE1 + "sw=fetch&url=" + encodeURIComponent(SCOPE2)], |
+ [], |
+ []], |
+ 'SW fetched redirect to same-origin other-scope.'); |
+ }) |
+ |
+ // Opaque redirect. |
+ // SW: event.respondWith(fetch( |
+ // new Request(event.request.url, {redirect: 'manual'}))); |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaque&url=" + encodeURIComponent(OUT_SCOPE), |
+ OUT_SCOPE, |
+ [[SCOPE1 + "sw=opaque&url=" + encodeURIComponent(OUT_SCOPE)], |
+ [], |
+ []], |
+ 'Redirect to same-origin out-scope with opaque redirect ' + |
+ 'response.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaque&url=" + encodeURIComponent(SCOPE1), |
+ SCOPE1, |
+ [[SCOPE1 + "sw=opaque&url=" + encodeURIComponent(SCOPE1), SCOPE1], |
+ [], |
+ []], |
+ 'Redirect to same-origin same-scope with opaque redirect ' + |
+ 'response.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaque&url=" + encodeURIComponent(SCOPE2), |
+ SCOPE2, |
+ [[SCOPE1 + "sw=opaque&url=" + encodeURIComponent(SCOPE2)], |
+ [SCOPE2], |
+ []], |
+ 'Redirect to same-origin other-scope with opaque redirect ' + |
+ 'response.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaque&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), |
+ OTHER_ORIGIN_OUT_SCOPE, |
+ [[SCOPE1 + "sw=opaque&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], |
+ [], |
+ []], |
+ 'Redirect to other-origin out-scope with opaque redirect ' + |
+ 'response.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaque&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_SCOPE), |
+ OTHER_ORIGIN_SCOPE, |
+ [[SCOPE1 + "sw=opaque&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
+ [], |
+ [OTHER_ORIGIN_SCOPE]], |
+ 'Redirect to other-origin in-scope with opaque redirect ' + |
+ 'response.'); |
+ }) |
+ |
+ // Opaque redirect passed through Cache. |
+ // SW responds with an opaque redirectresponse from the Cache API. |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OUT_SCOPE), |
+ OUT_SCOPE, |
+ [[SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OUT_SCOPE)], |
+ [], |
+ []], |
+ 'Redirect to same-origin out-scope with opaque redirect ' + |
+ 'response which is passed through Cache.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(SCOPE1), |
+ SCOPE1, |
+ [[SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(SCOPE1), SCOPE1], |
+ [], |
+ []], |
+ 'Redirect to same-origin same-scope with opaque redirect ' + |
+ 'response which is passed through Cache.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(SCOPE2), |
+ SCOPE2, |
+ [[SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(SCOPE2)], |
+ [SCOPE2], |
+ []], |
+ 'Redirect to same-origin other-scope with opaque redirect ' + |
+ 'response which is passed through Cache.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE), |
+ OTHER_ORIGIN_OUT_SCOPE, |
+ [[SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_OUT_SCOPE)], |
+ [], |
+ []], |
+ 'Redirect to other-origin out-scope with opaque redirect ' + |
+ 'response which is passed through Cache.'); |
+ }) |
+ .then(function() { |
+ return test_redirect( |
+ SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_SCOPE), |
+ OTHER_ORIGIN_SCOPE, |
+ [[SCOPE1 + "sw=opaqueThroughCache&url=" + |
+ encodeURIComponent(OTHER_ORIGIN_SCOPE)], |
+ [], |
+ [OTHER_ORIGIN_SCOPE]], |
+ 'Redirect to other-origin in-scope with opaque redirect ' + |
+ 'response which is passed through Cache.'); |
+ }) |
+ .then(function() { |
+ return Promise.all( |
+ [service_worker_unregister(t, SCOPE1), |
+ service_worker_unregister(t, SCOPE2), |
+ send_to_iframe(other_origin_frame, 'unregister')]); |
+ }) |
+ .then(function() { |
+ other_origin_frame.remove(); |
+ t.done(); |
+ }) |
+ .catch(unreached_rejection(t)); |
+ }, 'Verify the behavior of navigation redirection with Service Worker.'); |
+</script> |
+</body> |