Commit 0917ff14 authored by horo@chromium.org's avatar horo@chromium.org

Add LayoutTests for the behavior of navigation redirect with Service Worker.

This CL depends on https://codereview.chromium.org/1271733002/ (chromium) and https://codereview.chromium.org/1280733002/ (blink).

BUG=518713,510650

Review URL: https://codereview.chromium.org/1286153003

git-svn-id: svn://svn.chromium.org/blink/trunk@200881 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent e65ad9b4
<!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(
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>
<!DOCTYPE html>
<script src="../../resources/get-host-info.js"></script>
<script src="test-helpers.js?pipe=sub"></script>
<script>
var host_info = get_host_info();
var SCOPE = 'navigation-redirect-scope1.php';
var SCRIPT = 'navigation-redirect-worker.js';
var registration;
var worker;
var wait_for_worker_promise = navigator.serviceWorker.getRegistration(SCOPE)
.then(function(reg) {
if (reg)
return reg.unregister();
})
.then(function() {
return navigator.serviceWorker.register(SCRIPT, {scope: SCOPE});
})
.then(function(reg) {
registration = reg;
worker = reg.installing;
return new Promise(function(resolve) {
worker.addEventListener('statechange', function() {
if (worker.state == 'activated')
resolve();
});
});
});
function send_result(message_id, result) {
window.parent.postMessage(
{id: message_id, result: result},
host_info['HTTP_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]);
});
}
window.addEventListener('message', on_message, false);
function on_message(e) {
if (e.origin != host_info['HTTP_ORIGIN']) {
console.error('invalid origin: ' + e.origin);
return;
}
if (e.data.message == 'wait_for_worker') {
wait_for_worker_promise.then(function() { send_result(e.data.id, 'ok'); });
} else if (e.data.message == 'get_intercepted_urls') {
get_intercepted_urls(worker)
.then(function(urls) {
send_result(e.data.id, urls);
});
} else if (e.data.message == 'unregister') {
registration.unregister()
.then(function() {
send_result(e.data.id, 'ok');
});
}
}
</script>
<?php
if (isset($_GET['url'])) {
header("HTTP/1.1 302");
$url = $_GET['url'];
header("Location: $url");
exit;
}
?>
<!DOCTYPE html>
<script>
window.parent.postMessage(
{
id: 'last_url',
result: location.href
},
'http://127.0.0.1:8000');
</script>
// TODO(horo): Service worker can be killed at some point during the test. So we
// should use storage API instead of this global variable.
var urls = [];
self.addEventListener('message', function(event) {
event.data.port.postMessage({urls: urls});
urls = [];
});
function get_query_params(url) {
var search = (new URL(url)).search;
if (!search) {
return {};
}
var ret = {};
var params = search.substring(1).split('&');
params.forEach(function(param) {
var element = param.split('=');
ret[decodeURIComponent(element[0])] = decodeURIComponent(element[1]);
});
return ret;
}
self.addEventListener('fetch', function(event) {
urls.push(event.request.url)
var params = get_query_params(event.request.url);
if (params['sw'] == 'gen') {
event.respondWith(Response.redirect(params['url']));
} else if (params['sw'] == 'fetch') {
event.respondWith(fetch(event.request));
} else if (params['sw'] == 'opaque') {
event.respondWith(fetch(
new Request(event.request.url, {redirect: 'manual'})));
} else if (params['sw'] == 'opaqueThroughCache') {
var url = event.request.url;
var cache;
event.respondWith(
self.caches.delete(url)
.then(function() { return self.caches.open(url); })
.then(function(c) {
cache = c;
return fetch(new Request(url, {redirect: 'manual'}));
})
.then(function(res) { return cache.put(event.request, res); })
.then(function() { return cache.match(url); }));
}
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment