Commit 398ba6bd authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

service worker: Add WPT tests for resultingClientId.

This adds a ?client variant to navigation-redirect.https.html, which
does a lot of redirect tests. This tests the following:
- resultingClientId is the id of the client, if one was created via
  that request.
- get(resultingClientId) for requests that did not create a client
  resolves with undefined.
- resultingClientId is reset on cross-origin redirects.
- Client.url is the creation URL.

Bug: 778497
Change-Id: I08387e96a97df4656d800637862b67b7c8466e14
Reviewed-on: https://chromium-review.googlesource.com/1206054Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592363}
parent b637dfc6
......@@ -11,6 +11,7 @@ FAIL SW-fallbacked redirect to same-origin same-scope with different hash fragme
PASS SW-fallbacked redirect to same-origin other-scope.
PASS SW-fallbacked redirect to other-origin out-scope.
PASS SW-fallbacked redirect to other-origin in-scope.
PASS SW-fallbacked redirect to other-origin and back to same-origin.
PASS SW-generated redirect to same-origin out-scope.
FAIL SW-generated redirect to same-origin out-scope with a hash fragment. assert_object_equals: Intercepted URLs should match. property "0" expected ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F#ref"] got ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F"]
FAIL SW-generated redirect to same-origin out-scope with different hash fragments. assert_object_equals: Intercepted URLs should match. property "0" expected ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F%23ref2#ref"] got ["https://web-platform.test:8444/service-workers/service-worker/resources/navigation-redirect-scope1.py?sw=gen&url=https%3A%2F%2Fweb-platform.test%3A8444%2Fservice-workers%2Fservice-worker%2Fresources%2Fnavigation-redirect-out-scope.py%3F%23ref2"]
......
......@@ -44,6 +44,20 @@ function get_request_infos(worker) {
});
}
function get_clients(worker, actual_ids) {
return new Promise(function(resolve) {
var channel = new MessageChannel();
channel.port1.onmessage = (msg) => {
resolve(msg.data.clients);
};
worker.postMessage({
command: 'getClients',
actual_ids,
port: channel.port2
}, [channel.port2]);
});
}
window.addEventListener('message', on_message, false);
function on_message(e) {
......@@ -59,6 +73,11 @@ function on_message(e) {
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'get_clients') {
get_clients(worker, e.data.message.actual_ids)
.then(function(data) {
send_result(e.data.id, data);
});
} else if (command == 'unregister') {
registration.unregister()
.then(function() {
......
......@@ -5,6 +5,13 @@ var cacheName = 'urls-' + self.registration.scope;
var waitUntilPromiseList = [];
// Sends the requests seen by this worker. The output is:
// {
// requestInfos: [
// {url: url1, resultingClientId: id1},
// {url: url2, resultingClientId: id2},
// ]
// }
async function getRequestInfos(event) {
// Wait for fetch events to finish.
await Promise.all(waitUntilPromiseList);
......@@ -15,8 +22,11 @@ async function getRequestInfos(event) {
const requestList = await cache.keys();
const requestInfos = [];
for (let i = 0; i < requestList.length; i++) {
const response = await cache.match(requestList[i]);
const body = await response.json();
requestInfos[i] = {
url: requestList[i].url,
resultingClientId: body.resultingClientId
};
}
await caches.delete(cacheName);
......@@ -24,13 +34,51 @@ async function getRequestInfos(event) {
event.data.port.postMessage({requestInfos});
}
// Sends the results of clients.get(id) from this worker. The
// input is:
// {
// actual_ids: {a: id1, b: id2, x: id3}
// }
//
// The output is:
// {
// clients: {
// a: {found: false},
// b: {found: false},
// x: {
// id: id3,
// url: url1,
// found: true
// }
// }
// }
async function getClients(event) {
// |actual_ids| is like:
// {a: id1, b: id2, x: id3}
const actual_ids = event.data.actual_ids;
const result = {}
for (let key of Object.keys(actual_ids)) {
const id = actual_ids[key];
const client = await self.clients.get(id);
if (client === undefined) {
result[key] = {found: false};
return;
}
result[key] = {found: true, url: client.url, id: client.id};
}
event.data.port.postMessage({clients: result});
}
self.addEventListener('message', async function(event) {
if (event.data.command == 'getRequestInfos') {
event.waitUntil(getRequestInfos(event));
return;
}
// TODO(falken): Add a getClientInfos command to test Clients API.
if (event.data.command == 'getClients') {
event.waitUntil(getClients(event));
return;
}
});
function get_query_params(url) {
......@@ -49,7 +97,11 @@ function get_query_params(url) {
self.addEventListener('fetch', function(event) {
var waitUntilPromise = caches.open(cacheName).then(function(cache) {
return cache.put(event.request, new Response());
const responseBody = {};
responseBody['resultingClientId'] = event.resultingClientId;
const headers = new Headers({'Content-Type': 'application/json'});
const response = new Response(JSON.stringify(responseBody), {headers});
return cache.put(event.request, response);
});
event.waitUntil(waitUntilPromise);
......
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