diff --git a/lib/httpClient.js b/lib/httpClient.js index 5bd2d94a..8ba3db1c 100644 --- a/lib/httpClient.js +++ b/lib/httpClient.js @@ -49,6 +49,7 @@ function Client (opts) { for (let i = 0; i < pipelining; i++) { this.resData[i] = { bytes: 0, + body: '', headers: {}, startTime: [0, 0] } @@ -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) @@ -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)