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 { ...@@ -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( export async function handleFetch(
originalFetch: Fetch, originalFetch: Fetch,
request: Request request: Request
...@@ -89,7 +101,7 @@ export async function handleFetch( ...@@ -89,7 +101,7 @@ export async function handleFetch(
const testInfo = getTestReqInfo(request, reader) const testInfo = getTestReqInfo(request, reader)
if (!testInfo) { if (!testInfo) {
// Passthrough non-test requests. // Passthrough non-test requests.
return originalFetch(request) return passthroughFetch(originalFetch, request)
} }
const { testData, proxyPort } = testInfo const { testData, proxyPort } = testInfo
...@@ -119,7 +131,7 @@ export async function handleFetch( ...@@ -119,7 +131,7 @@ export async function handleFetch(
const { api } = proxyResponse const { api } = proxyResponse
switch (api) { switch (api) {
case 'continue': case 'continue':
return originalFetch(request) return passthroughFetch(originalFetch, request)
case 'abort': case 'abort':
case 'unhandled': case 'unhandled':
throw new Error( throw new Error(
......
...@@ -56,6 +56,11 @@ describe('testmode', () => { ...@@ -56,6 +56,11 @@ describe('testmode', () => {
expect(html).not.toContain('<pre>test1</pre>') 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 () => { it('should handle RSC with fetch in serverless function', async () => {
const html = await (await fetchForTest('/app/rsc-fetch')).text() const html = await (await fetchForTest('/app/rsc-fetch')).text()
expect(html).toContain('<pre>test1</pre>') 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