Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changeset/fix-from-web-content-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/platform": patch
---

HttpServerResponse: fix `fromWeb` to preserve Content-Type header when response has a body

Previously, when converting a web `Response` to an `HttpServerResponse` via `fromWeb`, the `Content-Type` header was not passed to `Body.stream()`, causing it to default to `application/octet-stream`. This affected any code using `HttpApp.fromWebHandler` to wrap web handlers, as JSON responses would incorrectly have their Content-Type set to `application/octet-stream` instead of `application/json`.
12 changes: 8 additions & 4 deletions packages/platform/src/HttpServerResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,16 @@ export const fromWeb = (response: Response): HttpServerResponse => {
cookies: Cookies.fromSetCookie(setCookieHeaders)
})
if (response.body) {
const contentType = headers.get("content-type")
self = setBody(
self,
Body.stream(Stream.fromReadableStream({
evaluate: () => response.body!,
onError: (e) => e
}))
Body.stream(
Stream.fromReadableStream({
evaluate: () => response.body!,
onError: (e) => e
}),
contentType ?? undefined
)
)
}
return self
Expand Down
29 changes: 29 additions & 0 deletions packages/platform/test/HttpApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe("Http/App", () => {
})
})

test("json preserves content-type", async () => {
const handler = HttpApp.toWebHandler(HttpServerResponse.json({ foo: "bar" }))
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(response.headers.get("Content-Type"), "application/json")
})

test("cookies", async () => {
const handler = HttpApp.toWebHandler(
HttpServerResponse.unsafeJson({ foo: "bar" }).pipe(
Expand Down Expand Up @@ -186,5 +192,28 @@ describe("Http/App", () => {
const response = await finalHandler(new Request("http://localhost:3000/"))
deepStrictEqual(await response.json(), { source: "effect" })
})

test("preserves response content-type header", async () => {
const webHandler = async (_request: Request) => {
return Response.json({ message: "hello" })
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(response.headers.get("Content-Type"), "application/json")
deepStrictEqual(await response.json(), { message: "hello" })
})

test("preserves custom content-type header", async () => {
const webHandler = async (_request: Request) => {
return new Response("<html></html>", {
headers: { "Content-Type": "text/html; charset=utf-8" }
})
}
const app = HttpApp.fromWebHandler(webHandler)
const handler = HttpApp.toWebHandler(app)
const response = await handler(new Request("http://localhost:3000/"))
strictEqual(response.headers.get("Content-Type"), "text/html; charset=utf-8")
})
})
})