Skip to content

Commit

Permalink
fix: onResponse request argument (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarthy authored Jan 25, 2022
1 parent 1d16730 commit 10ed2ec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ module.exports = fp(function from (fastify, opts, next) {
}
this.code(res.statusCode)
if (onResponse) {
onResponse(this.request.raw, this, res.stream)
onResponse(this.request, this, res.stream)
} else {
this.send(res.stream)
}
Expand Down
48 changes: 48 additions & 0 deletions test/onResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const t = require('tap')
const Fastify = require('fastify')
const From = require('..')
const http = require('http')
const get = require('simple-get').concat

const instance = Fastify()
instance.register(From)

t.plan(8)
t.teardown(instance.close.bind(instance))

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
res.statusCode = 200
res.end('hello world')
})

instance.get('/', (request1, reply) => {
reply.from(`http://localhost:${target.address().port}`, {
onResponse: (request2, reply, res) => {
t.equal(request1.raw, request2.raw)
reply.send(res)
}
})
})

t.teardown(target.close.bind(target))

instance.listen(0, (err) => {
t.error(err)

target.listen(0, (err) => {
t.error(err)

get(
`http://localhost:${instance.server.address().port}`,
(err, res, data) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(data.toString(), 'hello world')
}
)
})
})

0 comments on commit 10ed2ec

Please sign in to comment.