Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ async function fastifyHttpProxy (fastify, opts) {
fastify.addHook('preValidation', opts.preValidation)
} else if (opts.proxyPayloads !== false) {
fastify.addContentTypeParser('application/json', bodyParser)
fastify.addContentTypeParser('text/plain', bodyParser)
fastify.addContentTypeParser('*', bodyParser)
}

Expand Down
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { after, test } = require('node:test')
const http = require('node:http')
const Fastify = require('fastify')
const proxy = require('../')
const { Unauthorized } = require('http-errors')
Expand Down Expand Up @@ -237,6 +238,48 @@ async function run () {
t.assert.deepStrictEqual(body, { something: 'posted' })
})

test('streams text/plain payloads without corruption or bodyLimit restriction', async t => {
let receivedBuffer
const target = http.createServer((req, res) => {
const chunks = []
req.on('data', chunk => chunks.push(chunk))
req.on('end', () => {
receivedBuffer = Buffer.concat(chunks)
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ bytesReceived: receivedBuffer.length }))
})
})

await new Promise((resolve) => target.listen(0, resolve))
t.after(() => target.close())

const server = Fastify()
server.register(proxy, {
upstream: `http://localhost:${target.address().port}`
})

await server.listen({ port: 0 })
t.after(() => server.close())

const latin1 = Buffer.from('café crème', 'latin1')
const res1 = await fetch(`http://localhost:${server.server.address().port}/upload`, {
method: 'POST',
headers: { 'content-type': 'text/plain; charset=iso-8859-1' },
body: latin1
})
t.assert.strictEqual(res1.status, 200)
t.assert.deepStrictEqual(receivedBuffer, latin1)

const largeBody = Buffer.alloc(1.5 * 1024 * 1024, 'a')
const res2 = await fetch(`http://localhost:${server.server.address().port}/upload`, {
method: 'POST',
headers: { 'content-type': 'text/plain' },
body: largeBody
})
t.assert.strictEqual(res2.status, 200)
t.assert.strictEqual(receivedBuffer.length, largeBody.length)
})

test('preValidation post payload contains invalid data', async t => {
const server = Fastify()
server.register(proxy, {
Expand Down
Loading