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

chore: validate responses can be consumed without a Content-Length or… #2995

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
42 changes: 42 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,48 @@ test('async iterator yield object error', async (t) => {
await t.completed
})

test('Successfully get a Response when neither a Transfer-Encoding or Content-Length header is present', async (t) => {
t = tspl(t, { plan: 4 })
const server = createServer((req, res) => {
req.on('data', (data) => {
})
req.on('end', () => {
res.removeHeader('transfer-encoding')
res.writeHead(200, {
// Header isn't actually necessary, but tells node to close after response
connection: 'close',
foo: 'bar'
})
res.flushHeaders()
res.end('a response body')
})
})
after(() => server.close())

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

client.request({ path: '/', method: 'GET' }, (err, { body, headers }) => {
t.ifError(err)
t.equal(headers['content-length'], undefined)
t.equal(headers['transfer-encoding'], undefined)
const bufs = []
body.on('error', () => {
t.fail('Closing the connection is valid')
})
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.equal('a response body', Buffer.concat(bufs).toString('utf8'))
})
})
})

await t.completed
})

function buildParams (path) {
const cleanPath = path.replace('/?', '').replace('/', '').split('&')
const builtParams = cleanPath.reduce((acc, entry) => {
Expand Down
Loading