Skip to content

Commit

Permalink
Return a HTTP 206 response as-is (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored Oct 18, 2018
1 parent 79f9056 commit cd5ef34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/workbox-range-requests/createPartialResponse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import './_version.mjs';
* Given a `Request` and `Response` objects as input, this will return a
* promise for a new `Response`.
*
* If the original `Response` already contains partial content (i.e. it has
* a status of 206), then this assumes it already fulfills the `Range:`
* requirements, and will return it as-is.
*
* @param {Request} request A request, which should contain a Range:
* header.
* @param {Response} originalResponse An original response containing the full
* content.
* @param {Response} originalResponse A response.
* @return {Promise<Response>} Either a `206 Partial Content` response, with
* the response body set to the slice of content specified by the request's
* `Range:` header, or a `416 Range Not Satisfiable` response if the
Expand All @@ -47,6 +50,12 @@ async function createPartialResponse(request, originalResponse) {
});
}

if (originalResponse.status === 206) {
// If we already have a 206, then just pass it through as-is;
// see https://github.com/GoogleChrome/workbox/issues/1720
return originalResponse;
}

const rangeHeader = request.headers.get('range');
if (!rangeHeader) {
throw new WorkboxError('no-range-header');
Expand Down
8 changes: 8 additions & 0 deletions test/workbox-range-requests/node/createPartialResponse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,13 @@ describe(`[workbox-range-requests] createPartialResponse`, function() {
const expectedBlob = constructBlob(101);
expect(responseBlob._text).to.eql(expectedBlob._text);
});

it(`should handle being passed a Response with a status of 206 by returning it as-is`, async function() {
const originalPartialResponse = new Response('expected text', {status: 206});
const createdPartialResponse = await createPartialResponse(VALID_REQUEST, originalPartialResponse);

// We should get back the exact same response.
expect(createdPartialResponse).to.eql(originalPartialResponse);
});
});
});

0 comments on commit cd5ef34

Please sign in to comment.