Commit 4e6d16f6 authored by Makoto Shimazu's avatar Makoto Shimazu Committed by Commit Bot

Add tests of requests initiated from COEP-protected service worker

This CL adds tests to initiate requests from a service worker with
`require-corp` policy. This currently fails but it'll be fixed once a
new service worker starts to respect the COEP value.

Bug: 1039613
Change-Id: Ic610cbe8ceb9c5c0d407507918fcdf2c69eb93b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2065257
Commit-Queue: Makoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarYutaka Hirano <yhirano@chromium.org>
Auto-Submit: Makoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744922}
parent 1cb66edf
This is a testharness.js-based test.
PASS Set up global state
PASS fetch() to 'CORP: cross-origin' response should succeed.
FAIL fetch() to no CORP response should not succeed. assert_equals: expected "TypeError: Failed to fetch" but got "opaque"
PASS Clean up global state
Harness: the test ran to completion.
<!doctype html>
<title>Cross Origin Embedder Policy: requests initiated from a service worker with 'require-corp'</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script>
'use strict';
const SCRIPT = 'resources/require-corp-sw.js';
const SCOPE = 'resources/in-scope';
let worker = null;
promise_test(async t => {
const registration =
await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
promise_test(async t => registration.unregister(), 'Clean up global state');
worker = registration.installing;
await wait_for_state(t, worker, 'activated');
}, 'Set up global state');
promise_test(async t => {
const p = new Promise(resolve =>
navigator.serviceWorker.addEventListener('message', resolve,
{once: true}));
worker.postMessage('WithCorp');
assert_equals((await p).data, 'opaque');
}, "fetch() to 'CORP: cross-origin' response should succeed.");
promise_test(async t => {
const p = new Promise(resolve =>
navigator.serviceWorker.addEventListener('message', resolve,
{once: true}));
worker.postMessage('WithoutCorp');
assert_equals((await p).data, 'TypeError: Failed to fetch');
}, "fetch() to no CORP response should not succeed.");
</script>
// Service worker with 'COEP: require-corp' response header.
// This service worker issues a network request for a resource with or without
// CORP response header.
importScripts("/common/get-host-info.sub.js");
self.addEventListener('message', e => {
e.waitUntil((async () => {
let result;
try {
let url;
if (e.data === 'WithCorp') {
url = get_host_info().HTTPS_REMOTE_ORIGIN +
'/html/cross-origin-embedder-policy/resources/' +
'nothing-cross-origin-corp.txt';
} else if (e.data === 'WithoutCorp') {
url = get_host_info().HTTPS_REMOTE_ORIGIN + '/common/blank.html';
}
const response = await fetch(url, { mode: 'no-cors' });
result = response.type;
} catch (error) {
result = `${error.name}: ${error.message}`;
} finally {
e.source.postMessage(result);
}
})());
});
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