Skip to content
Closed
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
15 changes: 9 additions & 6 deletions packages/nextjs/src/middleware/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ const createMockRequest = (
}

const mockFetch = (responseInit: Partial<Response>) => {
global.fetch = jest.fn().mockResolvedValue(
new Response(responseInit.body || "", {
headers: new Headers(responseInit.headers || {}),
status: responseInit.status || 200,
}),
)
const response = new Response(responseInit.body || "", {
headers: new Headers(responseInit.headers || {}),
status: responseInit.status || 200,
})
// simulate immutable headers like those returned by undici's fetch
Object.defineProperty(response.headers, "append", { value: null })
Object.defineProperty(response.headers, "set", { value: null })
Object.defineProperty(response.headers, "delete", { value: null })
global.fetch = jest.fn().mockResolvedValue(response)
}

const createOptions = (): OryMiddlewareOptions => ({
Expand Down
8 changes: 7 additions & 1 deletion packages/nextjs/src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function proxyRequest(
upstreamRequestHeaders.set("Ory-No-Custom-Domain-Redirect", "true")

// Fetch the upstream response
const upstreamResponse = await fetch(upstreamUrl.toString(), {
let upstreamResponse = await fetch(upstreamUrl.toString(), {
method: request.method,
headers: upstreamRequestHeaders,
body:
Expand All @@ -93,6 +93,12 @@ export async function proxyRequest(
: null,
redirect: "manual",
})
upstreamResponse = new Response(upstreamResponse.body, {
status: upstreamResponse.status,
statusText: upstreamResponse.statusText,
// response may have immutable headers
headers: new Headers(upstreamResponse.headers),
})

// Delete headers that should not be forwarded
defaultOmitHeaders.forEach((header) => {
Expand Down
Loading