Commit 07b83d70 authored by mike's avatar mike Committed by Commit bot

Upstream service worker `fetch` tests to WPT

Reconcile the remaining Service Worker tests that concern the Fetch
standard. As noted below, some tests must be persisted in the Chromium
project due to unresolved timeout issues in the build environment (see
https://crbug.com/658997).

**fetch-event-async-respond-with**

The Chromium-specific version of this test is almost identical to the
version provided by the Web Platform Test suite, though it suffers from
timeout issues in the build environment.

Rename the Chromium-specific version to document its deprecated status
and improve the upstream version to defer frame removal and omit
unnecessary resource.

**fetch-event-network-error**

The upstream version of this test is almost identical to the
Chromium-specific version.

The Chromium-specific version of this test is almost identical to the
version available upstream. The upstream version is preferable because
it contains an additional assertion not found in the Chromium version.

Remove the Chromium-specific version of this test, and update the
upstream version to omit an unnecessary resource.

**fetch-event-respond-with-argument**

Re-locate Chromium-specific test to the Web Platform Test suite
directory so that it may be included in that project.

**fetch-event-respond-with-stops-propagation**

The Chromium-specific version of this test is almost identical to the
version provided by the Web Platform Test suite, though it suffers from
timeout issues in the build environment.

Rename the Chromium-specific version to reflect its deprecated status,
and improve the upstream version to defer frame removal and omit
unnecessary resource.

**fetch-event**

Remove sub-tests from the Chromium-specific version that are present in
the upstream version. Rename the test file to reflect the
implementation-specific nature of the test.

Extend the upstream version with a relaxed version of Chromium's
sub-test for request headers.

In both versions, formalize tear down logic to ensure consistent frame
removal and subsequent client de-registration.

BUG=688116, 658997
R=mek@chromium.org

Review-Url: https://codereview.chromium.org/2836233002
Cr-Commit-Position: refs/heads/master@{#468341}
parent 76aca775
......@@ -15504,7 +15504,7 @@ crbug.com/591099 http/tests/serviceworker/chromium/resolve-after-window-close.ht
crbug.com/591099 http/tests/serviceworker/chromium/sandboxed-iframe-fetch-event.html [ Crash ]
crbug.com/591099 http/tests/serviceworker/chromium/service-worker-gc.html [ Failure ]
crbug.com/591099 http/tests/serviceworker/chromium/window-close-during-registration.html [ Failure ]
crbug.com/591099 http/tests/serviceworker/fetch-event.html [ Crash ]
crbug.com/591099 http/tests/serviceworker/chromium.fetch-event-headers.html [ Crash ]
crbug.com/591099 http/tests/serviceworker/fetch-frame-resource.html [ Crash ]
crbug.com/591099 http/tests/serviceworker/fetch-mixed-content-to-inscope.html [ Crash ]
crbug.com/591099 http/tests/serviceworker/fetch-request-fallback.html [ Crash ]
......@@ -2986,8 +2986,8 @@ crbug.com/658997 external/wpt/service-workers/service-worker/fetch-canvas-tainti
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-canvas-tainting.https.html [ Pass Timeout ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-cors-xhr.https.html [ Pass Timeout ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-csp.https.html [ Pass Timeout ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-event-async-respond-with.https.html [ Skip ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-event-respond-with-stops-propagation.https.html [ Skip ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-event-async-respond-with.https.html [ Pass Timeout ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-event-respond-with-stops-propagation.https.html [ Pass Timeout ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-request-css-base-url.https.html [ Skip ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-request-xhr.https.html [ Skip ]
crbug.com/658997 external/wpt/service-workers/service-worker/fetch-response-xhr.https.html [ Skip ]
......
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
......@@ -16,6 +15,7 @@ promise_test(function(t) {
return with_iframe(scope);
})
.then(function(frame) {
add_completion_callback(function() { frame.remove(); });
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = function(e) { resolve(e.data); }
......@@ -23,7 +23,6 @@ promise_test(function(t) {
var worker = frame.contentWindow.navigator.serviceWorker.controller;
worker.postMessage({port: channel.port2}, [channel.port2]);
frame.remove();
return saw_message;
})
.then(function(message) {
......
<!DOCTYPE html>
<title>Service Worker: Fetch event network error</title>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
......
<!DOCTYPE html>
<title>Service Worker: FetchEvent.respondWith() argument type test.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var resolve_test_done;
......
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
......@@ -17,6 +16,7 @@ promise_test(function(t) {
return with_iframe(scope);
})
.then(function(frame) {
t.add_cleanup(function() { frame.remove(); });
var channel = new MessageChannel();
var saw_message = new Promise(function(resolve) {
channel.port1.onmessage = function(e) { resolve(e.data); }
......@@ -24,7 +24,6 @@ promise_test(function(t) {
var worker = frame.contentWindow.navigator.serviceWorker.controller;
worker.postMessage({port: channel.port2}, [channel.port2]);
frame.remove();
return saw_message;
})
.then(function(message) {
......
This is a testharness.js-based test.
PASS Service Worker headers in the request of a fetch event
PASS Service Worker responds to fetch event with string
PASS Service Worker responds to fetch event with blob body
PASS Service Worker responds to fetch event with the referrer URL
......
......@@ -7,6 +7,35 @@
<script>
var worker = 'resources/fetch-event-test-worker.js';
async_test(function(t) {
const scope = 'resources/simple.html?headers';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
t.add_cleanup(function() { frame.remove(); });
const headers = JSON.parse(frame.contentDocument.body.textContent);
const header_names = {};
for (const [name, value] of headers) {
header_names[name] = true;
}
assert_true(
header_names.hasOwnProperty('accept'),
'request includes "Accept" header as inserted by Fetch'
);
assert_true(
header_names.hasOwnProperty('upgrade-insecure-requests'),
'request specifies "Upgrade-Insecure Requests header as inserted by Fetch'
);
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker headers in the request of a fetch event');
async_test(function(t) {
var scope = 'resources/simple.html?string';
service_worker_unregister_and_register(t, worker, scope)
......
......@@ -113,6 +113,11 @@ function handleIntegrity(event) {
event.respondWith(new Response(event.request.integrity));
}
function handleHeaders(event) {
const headers = Array.from(event.request.headers);
event.respondWith(new Response(JSON.stringify(headers)));
}
self.addEventListener('fetch', function(event) {
var url = event.request.url;
var handlers = [
......@@ -132,6 +137,7 @@ self.addEventListener('fetch', function(event) {
{ pattern: '?cache', fn: handleCache },
{ pattern: '?eventsource', fn: handleEventSource },
{ pattern: '?integrity', fn: handleIntegrity },
{ pattern: '?headers', fn: handleHeaders },
];
var handler = null;
......
<!DOCTYPE html>
<!-- This test is prefixed with `chromium.` because the equivalent version
available in Web Platform Tests is known to cause timeout errors in the
Chromium automated build system. They should be maintained only to preserve
test converage until the corresponding versions in Web Platform Tests can be
made to pass consistently. See https://crbug.com/658997 -->
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
......
<!DOCTYPE html>
<!-- This test is prefixed with `chromium.` because it asserts a precise list
of expected header names, but but such precision is not mandated by the
relevant specifications. It should be maintained only to the extent that this
precision is relevant to Chromium internals; the Web Platform Tests project
includes a relaxed version which allows for additional headers. -->
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
var worker = 'resources/fetch-event-test-worker.js';
promise_test(function(t) {
const scope = 'resources/simple.html?headers';
return service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
t.add_cleanup(function() { frame.remove(); });
// We have this test to prevent unexpected exposure of headers to a
// ServiceWorker. Feel free to rebaseline this expectation if it
// looks good.
const headers = JSON.parse(frame.contentDocument.body.textContent);
const header_names = [];
for (const [name, value] of headers) {
header_names.push(name);
}
header_names.sort();
assert_array_equals(
header_names,
["accept", "upgrade-insecure-requests", "user-agent"],
'event.request has the expected headers.');
return service_worker_unregister_and_done(t, scope);
});
}, 'Service Worker headers in the request of a fetch event');
</script>
</body>
<!DOCTYPE html>
<!-- This test is prefixed with `chromium.` because the equivalent version
available in Web Platform Tests is known to cause timeout errors in the
Chromium automated build system. They should be maintained only to preserve
test converage until the corresponding versions in Web Platform Tests can be
made to pass consistently. See https://crbug.com/658997 -->
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
......
<!DOCTYPE html>
<title>Service Worker: Fetch event network error</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
var resolve_test_done;
var test_done_promise = new Promise(function(resolve) {
resolve_test_done = resolve;
});
// Called by the child frame.
function notify_test_done(result) {
resolve_test_done(result);
}
promise_test(function(t) {
var scope = 'resources/fetch-event-network-error-controllee-iframe.html';
var script = 'resources/fetch-event-network-error-worker.js';
var frame;
return service_worker_unregister_and_register(t, script, scope)
.then(function(registration) {
return wait_for_state(t, registration.installing, 'activated');
})
.then(function() {
return with_iframe(scope);
})
.then(function(f) {
frame = f;
return test_done_promise;
})
.then(function(result) {
frame.remove();
assert_equals(result, 'PASS');
return service_worker_unregister_and_done(t, scope);
});
}, 'Rejecting the fetch event or using preventDefault() causes a network ' +
'error');
</script>
<!DOCTYPE html>
<script>
function fetch_url(url) {
return new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.addEventListener('load', function(event) {
resolve();
});
request.addEventListener('error', function(event) {
reject();
});
request.open('GET', url);
request.send();
});
}
function make_test(testcase) {
var name = testcase.name;
return fetch_url(window.location.href + '?' + name)
.then(
function() {
if (testcase.expect_load)
return Promise.resolve();
return Promise.reject(new Error(
name + ': expected network error but loaded'));
},
function() {
if (!testcase.expect_load)
return Promise.resolve();
return Promise.reject(new Error(
name + ': expected to load but got network error'));
});
}
function run_tests() {
var tests = [
{ name: 'prevent-default-and-respond-with', expect_load: true },
{ name: 'prevent-default', expect_load: false },
{ name: 'reject', expect_load: false },
{ name: 'unused-body', expect_load: true },
{ name: 'used-body', expect_load: false },
{ name: 'unused-fetched-body', expect_load: true },
{ name: 'used-fetched-body', expect_load: false }
].map(make_test);
Promise.all(tests)
.then(function() {
window.parent.notify_test_done('PASS');
})
.catch(function(error) {
window.parent.notify_test_done('FAIL: ' + error.message);
});
}
if (!navigator.serviceWorker.controller)
window.parent.notify_done('FAIL: no controller');
else
run_tests();
</script>
// Test that multiple fetch handlers do not confuse the implementation.
self.addEventListener('fetch', function(event) {});
self.addEventListener('fetch', function(event) {
var testcase = new URL(event.request.url).search;
switch (testcase) {
case '?reject':
event.respondWith(Promise.reject());
break;
case '?prevent-default':
event.preventDefault();
break;
case '?prevent-default-and-respond-with':
event.preventDefault();
break;
case '?unused-body':
event.respondWith(new Response('body'));
break;
case '?used-body':
var res = new Response('body');
res.text();
event.respondWith(res);
break;
case '?unused-fetched-body':
event.respondWith(fetch('progressive.php').then(function(res){
return res;
}));
break;
case '?used-fetched-body':
event.respondWith(fetch('progressive.php').then(function(res){
res.text();
return res;
}));
break;
}
});
self.addEventListener('fetch', function(event) {});
self.addEventListener('fetch', function(event) {
var testcase = new URL(event.request.url).search;
if (testcase == '?prevent-default-and-respond-with')
event.respondWith(new Response('responding!'));
});
self.addEventListener('fetch', function(event) {});
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