Skip to content

Commit

Permalink
fix: properly handle Express.js send payload (#221)
Browse files Browse the repository at this point in the history
Co-authored-by: extern-chalkin-andrey <Andrey.Chalkin@Solveva.com>
  • Loading branch information
L2jLiga and extern-chalkin-andrey committed Sep 9, 2022
1 parent 654c956 commit 931157c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ function doInject (dispatchFunc, options, callback) {
? Request.CustomRequest
: Request

// Express.js detection
if (dispatchFunc.request && dispatchFunc.request.app === dispatchFunc) {
Object.setPrototypeOf(Object.getPrototypeOf(dispatchFunc.request), RequestConstructor.prototype)
Object.setPrototypeOf(Object.getPrototypeOf(dispatchFunc.response), Response.prototype)
}

if (typeof callback === 'function') {
const req = new RequestConstructor(options)
const res = new Response(req, callback)
Expand Down
27 changes: 10 additions & 17 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ Response.prototype.setTimeout = function (msecs, callback) {
Response.prototype.writeHead = function () {
const result = http.ServerResponse.prototype.writeHead.apply(this, arguments)

copyHeaders(this)
this._lightMyRequest.headers = Object.assign({}, this.getHeaders())

// Add raw headers
;['Date', 'Connection', 'Transfer-Encoding'].forEach((name) => {
const regex = new RegExp('\\r\\n' + name + ': ([^\\r]*)\\r\\n')
const field = this._header.match(regex)
if (field) {
this._lightMyRequest.headers[name.toLowerCase()] = field[1]
}
})

return result
}
Expand Down Expand Up @@ -109,9 +118,6 @@ Response.prototype.addTrailers = function (trailers) {
}

function generatePayload (response) {
if (response._lightMyRequest.headers === null) {
copyHeaders(response)
}
// Prepare response object
const res = {
raw: {
Expand Down Expand Up @@ -155,17 +161,4 @@ function getNullSocket () {
})
}

function copyHeaders (response) {
response._lightMyRequest.headers = Object.assign({}, response.getHeaders())

// Add raw headers
;['Date', 'Connection', 'Transfer-Encoding'].forEach((name) => {
const regex = new RegExp('\\r\\n' + name + ': ([^\\r]*)\\r\\n')
const field = response._header.match(regex)
if (field) {
response._lightMyRequest.headers[name.toLowerCase()] = field[1]
}
})
}

module.exports = Response
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1949,3 +1949,19 @@ test('should leave the headers user-agent and content-type undefined when the he
t.error(err)
})
})

test("passes payload when using express' send", (t) => {
t.plan(3)

const app = express()

app.get('/hello', (req, res) => {
res.send('some text')
})

inject(app, { method: 'GET', url: 'http://example.com:8080/hello' }, (err, res) => {
t.error(err)
t.equal(res.headers['content-length'], '9')
t.equal(res.payload, 'some text')
})
})

0 comments on commit 931157c

Please sign in to comment.