From 8dd4e03a42f93e86702c213fd974aa478a397198 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Thu, 4 Oct 2018 16:05:39 -0400 Subject: [PATCH] Backporting #1678 --- infra/testing/sw-env-mocks/Headers.js | 8 ++++++-- .../workbox-precaching/utils/cleanRedirect.mjs | 11 ++++++----- .../node/utils/test-cleanRedirect.mjs | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/infra/testing/sw-env-mocks/Headers.js b/infra/testing/sw-env-mocks/Headers.js index a26c52c4b..53c3138b3 100644 --- a/infra/testing/sw-env-mocks/Headers.js +++ b/infra/testing/sw-env-mocks/Headers.js @@ -17,8 +17,12 @@ // Stub missing/broken Headers API methods in `service-worker-mock`. // https://fetch.spec.whatwg.org/#headers-class class Headers { - constructor(obj = {}) { - this.obj = Object.assign({}, obj); + constructor(init = {}) { + if (init instanceof Headers) { + this.obj = Object.assign({}, init.obj); + } else { + this.obj = Object.assign({}, init); + } } has(key) { diff --git a/packages/workbox-precaching/utils/cleanRedirect.mjs b/packages/workbox-precaching/utils/cleanRedirect.mjs index 23d9f81ad..aa3013ed0 100644 --- a/packages/workbox-precaching/utils/cleanRedirect.mjs +++ b/packages/workbox-precaching/utils/cleanRedirect.mjs @@ -21,7 +21,7 @@ import '../_version.mjs'; * @return {Response} * * @private - * @memberof module:workbox-precachig + * @memberof module:workbox-precaching */ const cleanRedirect = async (response) => { const clonedResponse = response.clone(); @@ -35,10 +35,11 @@ const cleanRedirect = async (response) => { const body = await bodyPromise; // new Response() is happy when passed either a stream or a Blob. - return new Response(body, ['headers', 'status', 'statusText'].map((key) => { - return clonedResponse[key]; - }) - ); + return new Response(body, { + headers: clonedResponse.headers, + status: clonedResponse.status, + statusText: clonedResponse.statusText, + }); }; export default cleanRedirect; diff --git a/test/workbox-precaching/node/utils/test-cleanRedirect.mjs b/test/workbox-precaching/node/utils/test-cleanRedirect.mjs index 748d709f4..24a576f24 100644 --- a/test/workbox-precaching/node/utils/test-cleanRedirect.mjs +++ b/test/workbox-precaching/node/utils/test-cleanRedirect.mjs @@ -29,4 +29,19 @@ describe(`[workbox-precaching] cleanRedirect()`, function() { const cleanedResponseBody = await cleanedResponse.text(); expect(cleanedResponseBody).to.equal('Blob Body'); }); + + it(`should use the statusText, status, and headers from the original response`, async function() { + const headers = { + 'x-test': 1, + }; + const statusText = 'Non-Authoritative'; + const status = 203; + + const response = new Response('', {headers, statusText, status}); + const clonedResponse = await cleanRedirect(response); + + expect(response.headers).to.eql(clonedResponse.headers); + expect(response.status).to.eql(clonedResponse.status); + expect(response.statusText).to.eql(clonedResponse.statusText); + }); });