Skip to content

Commit

Permalink
Change onresponse contract to allow request, reply, and res. (#43)
Browse files Browse the repository at this point in the history
* Changes signature to include request, reply, and stream (not just stream). Updates existing test

* Update README to show contract change for onResponse
  • Loading branch information
thejones authored and mcollina committed Feb 10, 2019
1 parent 4e27722 commit 171abe4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ instance with a `from` method, which will reply to the original request
__from the desired source__. The options allows to override any part of
the request or response being sent or received to/from the source.

#### onResponse(res)
#### onResponse(request, reply, res)

Called when an http response is received from the source.
The default behavior is `reply.send(res)`, which will be disabled if the
Expand Down
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const lru = require('tiny-lru')
const querystring = require('querystring')
const Stream = require('stream')
const buildRequest = require('./lib/request')
const { filterPseudoHeaders, copyHeaders, stripHttp1ConnectionHeaders } = require('./lib/utils')
const {
filterPseudoHeaders,
copyHeaders,
stripHttp1ConnectionHeaders
} = require('./lib/utils')

module.exports = fp(function from (fastify, opts, next) {
const cache = lru(opts.cacheURLs || 100)
Expand Down Expand Up @@ -94,13 +98,16 @@ module.exports = fp(function from (fastify, opts, next) {
}
this.request.log.info('response received')
if (sourceHttp2) {
copyHeaders(rewriteHeaders(stripHttp1ConnectionHeaders(res.headers)), this)
copyHeaders(
rewriteHeaders(stripHttp1ConnectionHeaders(res.headers)),
this
)
} else {
copyHeaders(rewriteHeaders(res.headers), this)
}
this.code(res.statusCode)
if (onResponse) {
onResponse(res.stream)
onResponse(this.request.req, this, res.stream)
} else {
this.send(res.stream)
}
Expand Down
39 changes: 23 additions & 16 deletions test/transform-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,38 @@ const target = http.createServer((req, res) => {

instance.get('/', (request, reply) => {
reply.from(`http://localhost:${target.address().port}`, {
onResponse: (res) => {
reply.send(res.pipe(new Transform({
transform: function (chunk, enc, cb) {
this.push(chunk.toString().toUpperCase())
cb()
}
})))
onResponse: (request, reply, res) => {
reply.send(
res.pipe(
new Transform({
transform: function (chunk, enc, cb) {
this.push(chunk.toString().toUpperCase())
cb()
}
})
)
)
}
})
})

t.tearDown(target.close.bind(target))

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

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

get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'HELLO WORLD')
})
get(
`http://localhost:${instance.server.address().port}`,
(err, res, data) => {
t.error(err)
t.equal(res.headers['content-type'], 'text/plain')
t.equal(res.headers['x-my-header'], 'hello!')
t.equal(res.statusCode, 205)
t.equal(data.toString(), 'HELLO WORLD')
}
)
})
})

0 comments on commit 171abe4

Please sign in to comment.