diff --git a/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts b/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts index b4c1af86f..75ee07f1f 100644 --- a/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts +++ b/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts @@ -44,14 +44,14 @@ function calculateEffectiveBoundaries( let effectiveStart: number; let effectiveEnd: number; - if (start && end) { + if (start !== undefined && end !== undefined) { effectiveStart = start; // Range values are inclusive, so add 1 to the value. effectiveEnd = end + 1; - } else if (start && !end) { + } else if (start !== undefined && end === undefined) { effectiveStart = start; effectiveEnd = blobSize; - } else if (end && !start) { + } else if (end !== undefined && start === undefined) { effectiveStart = blobSize - end; effectiveEnd = blobSize; } diff --git a/test/workbox-range-requests/sw/test-createPartialResponse.mjs b/test/workbox-range-requests/sw/test-createPartialResponse.mjs index 32c56986d..b3fdbb5ab 100644 --- a/test/workbox-range-requests/sw/test-createPartialResponse.mjs +++ b/test/workbox-range-requests/sw/test-createPartialResponse.mjs @@ -30,10 +30,8 @@ describe(`createPartialResponse()`, function() { }, }); - const VALID_RESPONSE = new Response(SOURCE_BLOB); - it(`should return a Response with status 416 when the 'request' parameter isn't valid`, async function() { - const response = await createPartialResponse(null, VALID_RESPONSE); + const response = await createPartialResponse(null, new Response(SOURCE_BLOB)); expect(response.status).to.equal(416); }); @@ -44,12 +42,12 @@ describe(`createPartialResponse()`, function() { it(`should return a Response with status 416 when there's no Range: header in the request`, async function() { const noRangeHeaderRequest = new Request('/'); - const response = await createPartialResponse(noRangeHeaderRequest, VALID_RESPONSE); + const response = await createPartialResponse(noRangeHeaderRequest, new Response(SOURCE_BLOB)); expect(response.status).to.equal(416); }); it(`should return the expected Response when it's called with valid parameters`, async function() { - const response = await createPartialResponse(VALID_REQUEST, VALID_RESPONSE); + const response = await createPartialResponse(VALID_REQUEST, new Response(SOURCE_BLOB)); expect(response.status).to.equal(206); expect(response.headers.get('Content-Length')).to.equal('101'); expect(response.headers.get('Content-Range')).to.equal(`bytes 100-200/${SOURCE_BLOB_SIZE}`); @@ -61,6 +59,24 @@ describe(`createPartialResponse()`, function() { .to.equal(await (new Response(expectedBlob)).text()); }); + it(`should return the full body when it's called with bytes=0-`, async function() { + const fullBodyRequest = new Request('/', { + headers: { + range: 'bytes=0-', + }, + }); + const response = await createPartialResponse(fullBodyRequest, new Response(SOURCE_BLOB)); + expect(response.status).to.equal(206); + expect(response.headers.get('Content-Length')).to.equal(`${SOURCE_BLOB_SIZE}`); + expect(response.headers.get('Content-Range')).to.equal(`bytes 0-${SOURCE_BLOB_SIZE - 1}/${SOURCE_BLOB_SIZE}`); + + const responseBlob = await response.blob(); + const expectedBlob = constructBlob(SOURCE_BLOB_SIZE); + + expect(await (new Response(responseBlob)).text()) + .to.equal(await (new Response(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); diff --git a/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs b/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs index 1804a1ceb..773fd29e3 100644 --- a/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs +++ b/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs @@ -57,8 +57,8 @@ describe(`calculateEffectiveBoundaries()`, function() { it(`should return the expected boundaries when it's called with valid parameters`, function() { const testCases = [ [{start: 100, end: 200}, {start: 100, end: 201}], - [{start: null, end: 200}, {start: 56, end: 256}], - [{start: 100, end: null}, {start: 100, end: 256}], + [{start: undefined, end: 200}, {start: 56, end: 256}], + [{start: 100, end: undefined}, {start: 100, end: 256}], ]; for (const [sourceBoundaries, expectedBoundaries] of testCases) {