From 31cb10bcff05725f8ee55e03458f5db69f3b5248 Mon Sep 17 00:00:00 2001 From: Tejas Dinkar Date: Sun, 27 Jan 2019 09:01:27 +0530 Subject: [PATCH] fix(Example): Apparently response headers are immutable. Closes [#27] (#28) --- examples/unit-test-a-worker/worker.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/unit-test-a-worker/worker.js b/examples/unit-test-a-worker/worker.js index 5e64e95..36f9c0f 100644 --- a/examples/unit-test-a-worker/worker.js +++ b/examples/unit-test-a-worker/worker.js @@ -1,3 +1,6 @@ +// Add any more global vars (like Request, Response) to the below list as they are used +/* global addEventListener fetch Response Headers */ + addEventListener("fetch", e => { e.respondWith(fetchAndAddHeader(e.request)); }); @@ -5,11 +8,17 @@ addEventListener("fetch", e => { async function fetchAndAddHeader(request) { const response = await fetch(request); + const headers = new Headers(response.headers); + if (response.status === 200) { - response.headers.set("Foo", "Bar"); + headers.set("Foo", "Bar"); } else { - response.headers.set("Foo", "Not Bar"); + headers.set("Foo", "Not Bar"); } - return response; + return new Response(response.body, { + headers: headers, + status: response.status, + statusText: response.statusText, + }); }