Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ function calculateEffectiveBoundaries(

const blobSize = blob.size;

if ((end && end > blobSize) || (start && start < 0)) {
if (
(end && end > blobSize) ||
(start && start < 0) ||
// A range that starts at or past the end of the blob can't be satisfied.
(start !== undefined && start >= blobSize)
) {
throw new WorkboxError('range-not-satisfiable', {
size: blobSize,
end,
Expand Down
14 changes: 14 additions & 0 deletions test/workbox-range-requests/sw/test-createPartialResponse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ describe(`createPartialResponse()`, function () {
expect(response.status).to.equal(416);
});

it(`should return a Response with status 416 when the range starts at or past the end of the body`, async function () {
for (const range of [
`bytes=${SOURCE_BLOB_SIZE}-`,
`bytes=${SOURCE_BLOB_SIZE + 1}-`,
]) {
const request = new Request('/', {headers: {range}});
const response = await createPartialResponse(
request,
new Response(SOURCE_BLOB),
);
expect(response.status).to.equal(416, `for '${range}'`);
}
});

it(`should return the expected Response when it's called with valid parameters`, async function () {
const response = await createPartialResponse(
VALID_REQUEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ describe(`calculateEffectiveBoundaries()`, function () {
);
});

it(`should throw when it's called with a 'start' parameter equal to or larger than the blob's size`, async function () {
for (const start of [SOURCE_BLOB_SIZE, SOURCE_BLOB_SIZE + 1]) {
await expectError(
() => calculateEffectiveBoundaries(SOURCE_BLOB, start),
'range-not-satisfiable',
);
}
});

it(`should return the expected boundaries when it's called with valid parameters`, function () {
const testCases = [
[
Expand Down
Loading