summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-error-worker.js
blob: 788252cf3b3b3e5aaaf7013abd2d6940f298a40e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
importScripts("/resources/testharness.js");

function doTest(event)
{
    if (!event.request.url.includes("fetch-error-test"))
        return;

    let counter = 0;
    const stream = new ReadableStream({ pull: controller => {
        switch (++counter) {
        case 1:
            controller.enqueue(new Uint8Array([1]));
            return;
        default:
            // We asynchronously error the stream so that there is ample time to resolve the fetch promise and call text() on the response.
            step_timeout(() => controller.error("Sorry"), 50);
        }
    }});
    event.respondWith(new Response(stream));
}

self.addEventListener("fetch", doTest);