Skip to content

Commit

Permalink
chore: fix broken tests from github PR patches (my own)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 7, 2024
1 parent 9dcd798 commit f296f0b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
16 changes: 7 additions & 9 deletions packages/verified-fetch/src/utils/byte-range-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,17 @@ export class ByteRangeContext {
* 2. slicing the body
*/
public get offset (): number {
if (this.byteStart == null || this.byteStart === 0) {
if (this.byteStart === 0) {
return 0
}
// if byteStart is undefined, unixfs.cat and ArrayBuffer.slice will not use an inclusive offset, so we have to subtract by 1
if (this.isPrefixLengthRequest || this.isSuffixLengthRequest) {
return this.byteStart - 1
if (this.byteStart != null) {
// we have to subtract by 1 because the offset is inclusive
return this.byteStart - 1
}
}
return this.byteStart

return this.byteStart ?? 0
}

/**
Expand All @@ -232,11 +235,6 @@ export class ByteRangeContext {
* 2. slicing the body
*/
public get length (): number | undefined {
// this value will be passed to unixfs.cat.
if (this.isSuffixLengthRequest) {
// this is a suffix-length request and unixfs has a bug where it doesn't always respect the length parameter
return undefined
}
return this.byteSize ?? undefined
}

Expand Down
8 changes: 8 additions & 0 deletions packages/verified-fetch/src/utils/response-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ export function getContentRangeHeader ({ byteStart, byteEnd, byteSize }: { byteS
const total = byteSize ?? '*' // if we don't know the total size, we should use *

if (byteStart != null && byteEnd == null) {
// only byteStart in range
if (byteSize == null) {
return `bytes */${total}`
}
return `bytes ${byteStart}-${byteSize}/${byteSize}`
}

if (byteStart == null && byteEnd != null) {
// only byteEnd in range
if (byteSize == null) {
return `bytes */${total}`
}
return `bytes ${byteSize - byteEnd + 1}-${byteSize}/${byteSize}`
}

if (byteStart == null && byteEnd == null) {
// neither are provided, we can't return a valid range.
return `bytes */${total}`
}

return `bytes ${byteStart}-${byteEnd}/${total}`
}
1 change: 1 addition & 0 deletions packages/verified-fetch/test/range-requests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('range requests', () => {
}
]
validTestCases.forEach(({ bytes, contentRange, rangeHeader }) => {
// if these fail, check response-headers.spec.ts first
it(`should return correct 206 Partial Content response for ${rangeHeader}`, async () => {
const expected: SuccessfulTestExpectation = {
bytes,
Expand Down
6 changes: 5 additions & 1 deletion packages/verified-fetch/test/utils/response-headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ describe('response-headers', () => {
expect(getContentRangeHeader({ byteStart: undefined, byteEnd: 9, byteSize: 11 })).to.equal('bytes 3-11/11')
})

it('should return correct content range header when only byteStart and byteSize are provided', () => {
expect(getContentRangeHeader({ byteStart: 5, byteEnd: undefined, byteSize: 11 })).to.equal('bytes 5-11/11')
})

it('should return correct content range header when only byteStart is provided', () => {
expect(getContentRangeHeader({ byteStart: 500, byteEnd: undefined, byteSize: undefined })).to.equal('bytes 500-*/*')
expect(getContentRangeHeader({ byteStart: 500, byteEnd: undefined, byteSize: undefined })).to.equal('bytes */*')
})

it('should return correct content range header when only byteEnd is provided', () => {
Expand Down

0 comments on commit f296f0b

Please sign in to comment.