Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testmode: intercept rewrite fetches #54259

Merged
merged 4 commits into from
Aug 21, 2023
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
10 changes: 9 additions & 1 deletion packages/next/src/experimental/testmode/playwright/msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ export const test = base.extend<{
referrer,
referrerPolicy,
})
let isUnhandled = false
let isPassthrough = false
let mockedResponse: MockedResponse | undefined
await handleRequest(
mockedRequest,
handlers.slice(0),
{ onUnhandledRequest: 'error' },
{
onUnhandledRequest: () => {
isUnhandled = true
},
},
emitter as any,
{
onPassthroughResponse: () => {
Expand All @@ -78,6 +83,9 @@ export const test = base.extend<{
}
)

if (isUnhandled) {
return undefined
}
if (isPassthrough) {
return 'continue'
}
Expand Down
31 changes: 28 additions & 3 deletions packages/next/src/experimental/testmode/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AsyncLocalStorage } from 'async_hooks'
import { NodeRequestHandler } from '../../server/next-server'
import type {
ProxyFetchRequest,
ProxyFetchResponse,
ProxyResponse,
} from './proxy'
import { ClientRequestInterceptor } from 'next/dist/compiled/@mswjs/interceptors/ClientRequest'
import { WorkerRequestHandler } from '../../server/lib/setup-server-worker'
import { NodeRequestHandler } from '../../server/next-server'

interface TestReqInfo {
url: string
Expand Down Expand Up @@ -92,7 +93,9 @@ async function handleFetch(
return originalFetch(request)
case 'abort':
case 'unhandled':
throw new Error('Proxy request aborted')
throw new Error(
`Proxy request aborted [${request.method} ${request.url}]`
)
default:
break
}
Expand Down Expand Up @@ -132,7 +135,29 @@ export function interceptTestApis(): () => void {
}
}

export function wrapRequestHandler(
export function wrapRequestHandlerWorker(
handler: WorkerRequestHandler
): WorkerRequestHandler {
return async (req, res) => {
const proxyPortHeader = req.headers['next-test-proxy-port']
if (!proxyPortHeader) {
await handler(req, res)
return
}

const url = req.url ?? ''
const proxyPort = Number(proxyPortHeader)
const testData = (req.headers['next-test-data'] as string | undefined) ?? ''
const testReqInfo: TestReqInfo = {
url,
proxyPort,
testData,
}
await testStorage.run(testReqInfo, () => handler(req, res))
}
}

export function wrapRequestHandlerNode(
handler: NodeRequestHandler
): NodeRequestHandler {
return async (req, res, parsedUrl) => {
Expand Down
13 changes: 12 additions & 1 deletion packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export async function initialize(opts: {
devInstance?.ensureMiddleware
)

const requestHandler: WorkerRequestHandler = async (req, res) => {
const requestHandlerImpl: WorkerRequestHandler = async (req, res) => {
if (compress) {
// @ts-expect-error not express req/res
compress(req, res, () => {})
Expand Down Expand Up @@ -718,6 +718,17 @@ export async function initialize(opts: {
}
}

let requestHandler: WorkerRequestHandler = requestHandlerImpl
if (opts.experimentalTestProxy) {
// Intercept fetch and other testmode apis.
const {
wrapRequestHandlerWorker,
interceptTestApis,
} = require('../../experimental/testmode/server')
requestHandler = wrapRequestHandlerWorker(requestHandler)
interceptTestApis()
}

const upgradeHandler: WorkerUpgradeHandler = async (req, socket, head) => {
try {
req.on('error', (_err) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,10 @@ export default class NextNodeServer extends BaseServer {
public getRequestHandler(): NodeRequestHandler {
const handler = this.makeRequestHandler()
if (this.serverOptions.experimentalTestProxy) {
const { wrapRequestHandler } = require('../experimental/testmode/server')
return wrapRequestHandler(handler)
const {
wrapRequestHandlerNode,
} = require('../experimental/testmode/server')
return wrapRequestHandlerNode(handler)
}
return handler
}
Expand Down
Loading