<html>
<head>
<script src="resources/sw-test-pre.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
promise_test(test => {
    // Triggering potential preflight through custom header.
    var fetchPromise = fetch("http://localhost:8080/resources/square100.png.fromserviceworker", { headers: {"custom": "header"}});
    return promise_rejects_js(test, TypeError, fetchPromise);
}, "Testing unintercepted fetch with preflight, fetch should fail");

var frame;
promise_test(async test => {
    frame = await interceptedFrame("resources/service-worker-crossorigin-fetch-worker.js", "/workers/service/resources/");
}, "Registering service worker and controlled frame");

promise_test(async test => {
    // Triggering potential preflight through custom header.
    var response = await frame.contentWindow.fetch("http://localhost:8080/resources/square100.png.fromserviceworker", { headers: {"custom": "header"}});
    var buffer =  await response.arrayBuffer();
    assert_true(buffer.byteLength > 0);
}, "Testing intercepted fetch with preflight, fetch should succeed");

promise_test(test => {
    // Triggering potential preflight through custom header.
    var fetchPromise = frame.contentWindow.fetch("http://localhost:8080/resources/square100.png.error.fromserviceworker", { headers: {"custom": "header"}});
    return promise_rejects_js(test, frame.contentWindow.TypeError, fetchPromise);
}, "Testing intercepted fetch with preflight, fetch should fail");
</script>
</body>
</html>
