Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backporting the 301 redirect fix to v3 branch #1722

Merged
merged 1 commit into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions infra/testing/sw-env-mocks/Headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions packages/workbox-precaching/utils/cleanRedirect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
15 changes: 15 additions & 0 deletions test/workbox-precaching/node/utils/test-cleanRedirect.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});