Skip to content

Commit

Permalink
Fixes calculateEffectiveBoundaries bug when start=0 (#2237)
Browse files Browse the repository at this point in the history
* Fix bug when start=0

* Strict equality

* Fix up test case
  • Loading branch information
jeffposnick committed Oct 1, 2019
1 parent 985dcda commit 6300658
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 21 additions & 5 deletions test/workbox-range-requests/sw/test-createPartialResponse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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}`);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6300658

Please sign in to comment.