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: handle large response body correctly #301

Merged
merged 3 commits into from
Nov 17, 2020
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
23 changes: 8 additions & 15 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function Client (opts) {
for (let i = 0; i < pipelining; i++) {
this.resData[i] = {
bytes: 0,
body: '',
headers: {},
startTime: [0, 0]
}
Expand Down Expand Up @@ -88,19 +89,8 @@ function Client (opts) {
}

this.parser[HTTPParser.kOnBody] = (body, start, len) => {
this.bodyRecorded = true

this.resData[this.cer].body += body.slice(start, start + len)
this.emit('body', body)
const bodyString = '' + body.slice(start, start + len)

if (this.opts.expectBody) {
if (this.opts.expectBody !== bodyString) {
return this.emit('mismatch', bodyString)
}
}

const resp = this.resData[this.cer]
this.requestIterator.recordBody(resp.req, resp.headers.statusCode, bodyString)
}

this.parser[HTTPParser.kOnMessageComplete] = () => {
Expand All @@ -112,11 +102,15 @@ function Client (opts) {
return this._resetConnection()
}

if (!this.bodyRecorded) {
this.requestIterator.recordBody(resp.req, resp.headers.statusCode, undefined)
if (this.opts.expectBody && this.opts.expectBody !== resp.body) {
return this.emit('mismatch', resp.body)
}

this.requestIterator.recordBody(resp.req, resp.headers.statusCode, resp.body)

this.emit('response', resp.headers.statusCode, resp.bytes, resp.duration, this.rate)

resp.body = ''
resp.bytes = 0
this.cer = this.cer === pipelining - 1 ? 0 : this.cer++
this._doRequest(this.cer)
Expand Down Expand Up @@ -181,7 +175,6 @@ Client.prototype._doRequest = function (rpi) {
this.emit('reset')
}
}
this.bodyRecorded = false
this.resData[rpi].req = this.requestIterator.currentRequest
this.resData[rpi].startTime = process.hrtime()
this.conn.write(this.getRequestBuffer())
Expand Down
34 changes: 34 additions & 0 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const os = require('os')
const http = require('http')
const path = require('path')
const test = require('tap').test
const Client = require('../lib/httpClient')
Expand Down Expand Up @@ -886,3 +887,36 @@ test('client invokes appropriate onResponse when using pipelining', (t) => {
}
})
})

test('client supports receiving large response body', (t) => {
t.plan(2)

const mockBody = Array.from({ length: 1024 * 10 }, (_, i) => `str-${i}`).join('\n')
const server = http.createServer((req, res) => {
res.end(mockBody)
})
server.listen(0)
server.unref()

let onResponseCalled = 0
const opts = server.address()
opts.method = 'POST'
opts.body = Buffer.from('hello world')
opts.requests = [
{
path: '/',
method: 'GET',
onResponse: (...args) => {
onResponseCalled++
}
}
]

const client = new Client(opts)

client.on('response', (statusCode, length) => {
t.equal(onResponseCalled, 1, 'onResponse should be called only once')
t.equal(statusCode, 200, 'status code matches')
client.destroy()
})
})
2 changes: 1 addition & 1 deletion test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ test('should get onResponse callback invoked even when there is no body', async
method: 'GET',
onResponse (status, body) {
t.same(status, 204)
t.same(body, undefined)
t.same(body, '')
}
}
]
Expand Down