Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2986): bad start check #2992

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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
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 @@

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 (20, macos-latest) / Test with Node.js 20 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 (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.
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
})
Loading