From 10ed2ecdca4321c16fd81b15854cb87bd1541b5b Mon Sep 17 00:00:00 2001 From: swarthy Date: Tue, 25 Jan 2022 22:53:35 +0500 Subject: [PATCH] fix: onResponse request argument (#225) --- index.js | 2 +- test/onResponse.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/onResponse.js diff --git a/index.js b/index.js index 5362311a..768dcdd0 100644 --- a/index.js +++ b/index.js @@ -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) } diff --git a/test/onResponse.js b/test/onResponse.js new file mode 100644 index 00000000..22dc36df --- /dev/null +++ b/test/onResponse.js @@ -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') + } + ) + }) +})