Skip to content

Commit

Permalink
fix(#2986): bad start check (#2992)
Browse files Browse the repository at this point in the history
* fix: bad start check

* refactor: remove unnecessary checks
  • Loading branch information
metcoder95 committed Mar 25, 2024
1 parent 83f36b7 commit 0ec5a40
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,12 @@ class RetryHandler {
}

const { start, size, end = size } = range

assert(
start != null && Number.isFinite(start) && this.start !== start,
start != null && Number.isFinite(start),
'content-range mismatch'
)
assert(Number.isFinite(start))
assert(
end != null && Number.isFinite(end) && this.end !== end,
end != null && Number.isFinite(end),
'invalid content-length'
)

Expand Down
103 changes: 103 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,106 @@ test('Should be able to properly pass the minTimeout to the RetryContext when co

await t.completed
})
test('Issue#2986 - Handle custom 206', { only: true }, async t => {

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.

Check notice on line 879 in test/retry-handler.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

'only' and 'runOnly' require the --test-only command-line option.
t = tspl(t, { plan: 8 })

const chunks = []
let counter = 0

// Took from: https://github.com/nxtedition/nxt-lib/blob/4b001ebc2f22cf735a398f35ff800dd553fe5933/test/undici/retry.js#L47
let x = 0
const server = createServer((req, res) => {
if (x === 0) {
t.deepStrictEqual(req.headers.range, 'bytes=0-3')
res.setHeader('etag', 'asd')
res.write('abc')
setTimeout(() => {
res.destroy()
}, 1e2)
} else if (x === 1) {
t.deepStrictEqual(req.headers.range, 'bytes=3-')
res.setHeader('content-range', 'bytes 3-6/6')
res.setHeader('etag', 'asd')
res.statusCode = 206
res.end('def')
}
x++
})

const dispatchOptions = {
retryOptions: {
retry: function (err, _, done) {
counter++

if (err.code && err.code === 'UND_ERR_DESTROYED') {
return done(false)
}

if (err.statusCode === 206) return done(err)

setTimeout(done, 800)
}
},
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const handler = new RetryHandler(dispatchOptions, {
dispatch: (...args) => {
return client.dispatch(...args)
},
handler: {
onRequestSent () {
t.ok(true, 'pass')
},
onConnect () {
t.ok(true, 'pass')
},
onBodySent () {
t.ok(true, 'pass')
},
onHeaders (status, _rawHeaders, resume, _statusMessage) {
t.strictEqual(status, 200)
return true
},
onData (chunk) {
chunks.push(chunk)
return true
},
onComplete () {
t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'abcdef')
t.strictEqual(counter, 1)
},
onError () {
t.fail()
}
}
})

client.dispatch(
{
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json',
Range: 'bytes=0-3'
}
},
handler
)

after(async () => {
await client.close()

server.close()
await once(server, 'close')
})
})

await t.completed
})

0 comments on commit 0ec5a40

Please sign in to comment.