Skip to content

Commit

Permalink
Merge 093e4b0 into feeb501
Browse files Browse the repository at this point in the history
  • Loading branch information
jschumacher-dexcom committed Mar 15, 2022
2 parents feeb501 + 093e4b0 commit 6066b83
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ Replaces the original request body with what is specified. Unless
[`contentType`](#contentType) is specified, the content will be passed
through `JSON.stringify()`.
Setting this option for GET, HEAD requests will throw an error "Rewriting the body when doing a {GET|HEAD} is not allowed".
Setting this option to `undefined` will strip the body (and `content-type` header) entirely from the proxied request.

#### `retriesCount`

Expand Down
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,26 @@ module.exports = fp(function from (fastify, opts, next) {
const qs = getQueryString(url.search, req.url, opts)
let body = ''

if (opts.body) {
if (typeof opts.body.pipe === 'function') {
throw new Error('sending a new body as a stream is not supported yet')
}
if ('body' in opts) {
if (opts.body !== undefined) {
if (typeof opts.body.pipe === 'function') {
throw new Error('sending a new body as a stream is not supported yet')
}

if (opts.contentType) {
body = opts.body
} else {
body = JSON.stringify(opts.body)
opts.contentType = 'application/json'
}

if (opts.contentType) {
body = opts.body
headers['content-length'] = Buffer.byteLength(body)
headers['content-type'] = opts.contentType
} else {
body = JSON.stringify(opts.body)
opts.contentType = 'application/json'
body = undefined
headers['content-length'] = 0
delete headers['content-type']
}

headers['content-length'] = Buffer.byteLength(body)
headers['content-type'] = opts.contentType
} else if (this.request.body) {
if (this.request.body instanceof Stream) {
body = this.request.body
Expand Down
61 changes: 61 additions & 0 deletions test/full-rewrite-body-to-empty-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'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, {
http: true
})

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

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'POST')
t.equal(req.headers['content-type'], 'application/json')
t.equal(req.headers['content-length'], '2')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
t.same(JSON.parse(data), '')
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'fastify' }))
})
})

instance.post('/', (request, reply) => {
reply.from(`http://localhost:${target.address().port}`, {
body: ''
})
})

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

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

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

get({
url: `http://localhost:${instance.server.address().port}`,
method: 'POST',
json: true,
body: {
hello: 'world'
}
}, (err, res, data) => {
t.error(err)
t.same(data, { hello: 'fastify' })
})
})
})
61 changes: 61 additions & 0 deletions test/full-rewrite-body-to-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'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, {
http: true
})

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

const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'POST')
t.notOk('content-type' in req.headers)
t.equal(req.headers['content-length'], '0')
let data = ''
req.setEncoding('utf8')
req.on('data', (d) => {
data += d
})
req.on('end', () => {
t.equal(data.length, 0)
res.statusCode = 200
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'fastify' }))
})
})

instance.post('/', (request, reply) => {
reply.from(`http://localhost:${target.address().port}`, {
body: undefined
})
})

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

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

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

get({
url: `http://localhost:${instance.server.address().port}`,
method: 'POST',
json: true,
body: {
hello: 'world'
}
}, (err, res, data) => {
t.error(err)
t.same(data, { hello: 'fastify' })
})
})
})

0 comments on commit 6066b83

Please sign in to comment.