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
4 changes: 2 additions & 2 deletions routes/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default withRouteSpec({
}

let body = undefined
if (["POST", "PUT", "PATCH"].includes(req.method)) {
body = await req.clone().text()
if (!["GET", "HEAD"].includes(req.method)) {
body = await req.clone().arrayBuffer()
}

const headers = new Headers(req.headers)
Expand Down
34 changes: 34 additions & 0 deletions tests/routes/proxy-binary-body.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect, test } from "bun:test"
import { getTestServer } from "../fixtures/get-test-server"

test.each(["POST", "PUT", "PATCH", "DELETE", "OPTIONS"])(
"proxy preserves binary request bytes for %s",
async (method) => {
const { url } = await getTestServer()
const payload = new Uint8Array([0x00, 0xff, 0xfe, 0x80, 0x41])
const upstream = Bun.serve({
port: 0,
async fetch(req) {
return new Response(await req.arrayBuffer(), {
headers: { "Content-Type": "application/octet-stream" },
})
},
})

try {
const response = await fetch(`${url}/proxy`, {
method,
headers: {
"X-Target-Url": `http://127.0.0.1:${upstream.port}`,
"Content-Type": "application/octet-stream",
},
body: payload,
signal: AbortSignal.timeout(3000),
})
expect(response.status).toBe(200)
expect(new Uint8Array(await response.arrayBuffer())).toEqual(payload)
} finally {
upstream.stop(true)
}
},
)
Loading