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: Report (full) response body correctly (#275) #276

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 28 additions & 11 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 @@ -87,20 +88,18 @@ function Client (opts) {
this.resData[this.cer].headers = opts
}

// Simply record all body parts that come in
this.parser[HTTPParser.kOnBody] = (body, start, len) => {
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)
}

// Once the response is complete, take the following actions:
// - emit 'mismatch' if the body doesn't match `expectBody`
// - call `recordBody` with the full response body, which will
// in turn call `onResponse`
// - emit 'response'
// - reset the request body and bytes length
this.parser[HTTPParser.kOnMessageComplete] = () => {
const resp = this.resData[this.cer]
const end = process.hrtime(resp.startTime)
Expand All @@ -110,7 +109,25 @@ function Client (opts) {
return this._resetConnection()
}

this.emit('response', resp.headers.statusCode, resp.bytes, resp.duration, this.rate)
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