Unverified Commit 656aebfb authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

[16.3] testmode: Fix infinite recursion in testmode passthrough fetch (#97691)

Backport https://github.com/vercel/next.js/pull/96525Co-authored-by: 's avatarLazizbek Ergashev <lazerg2@gmail.com>
parent f37c1d65
......@@ -82,6 +82,18 @@ function buildResponse(proxyResponse: ProxyFetchResponse): Response {
})
}
function passthroughFetch(
originalFetch: Fetch,
request: Request
): Promise<Response> {
// Mark the request so the ClientRequest interception in `httpget.ts` sends it
// to the real server instead of intercepting it again. @mswjs/interceptors
// intercepts at the TCP level, so an unmarked passthrough would re-enter the
// interception listener from within itself, recursing indefinitely.
request.headers.set('next-test-internal', '1')
return originalFetch(request)
}
export async function handleFetch(
originalFetch: Fetch,
request: Request
......@@ -89,7 +101,7 @@ export async function handleFetch(
const testInfo = getTestReqInfo(request, reader)
if (!testInfo) {
// Passthrough non-test requests.
return originalFetch(request)
return passthroughFetch(originalFetch, request)
}
const { testData, proxyPort } = testInfo
......@@ -119,7 +131,7 @@ export async function handleFetch(
const { api } = proxyResponse
switch (api) {
case 'continue':
return originalFetch(request)
return passthroughFetch(originalFetch, request)
case 'abort':
case 'unhandled':
throw new Error(
......
......@@ -56,6 +56,11 @@ describe('testmode', () => {
expect(html).not.toContain('<pre>test1</pre>')
})
it('should pass http.get through to the real server when Next-Test-* headers are not present', async () => {
const html = await (await next.fetch('/app/rsc-httpget')).text()
expect(html).toContain('Example Domain')
})
it('should handle RSC with fetch in serverless function', async () => {
const html = await (await fetchForTest('/app/rsc-fetch')).text()
expect(html).toContain('<pre>test1</pre>')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment