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

Set test headers via page.router API #55087

Merged
merged 3 commits into from
Sep 8, 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
7 changes: 1 addition & 6 deletions packages/next/src/experimental/testmode/playwright/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,11 @@ export const test = base.test.extend<
],

next: [
async (
{ nextOptions, _nextWorker, page, extraHTTPHeaders },
use,
testInfo
) => {
async ({ nextOptions, _nextWorker, page }, use, testInfo) => {
await applyNextFixture(use, {
testInfo,
nextWorker: _nextWorker,
page,
extraHTTPHeaders,
nextOptions,
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class NextFixtureImpl implements NextFixture {
private worker: NextWorkerFixture,
private page: Page
) {
const testHeaders = {
'Next-Test-Proxy-Port': String(worker.proxyPort),
'Next-Test-Data': testId,
}
const handleFetch = this.handleFetch.bind(this)
worker.onFetch(testId, handleFetch)
this.page.route('**', (route) => handleRoute(route, page, handleFetch))
this.page.route('**', (route) =>
handleRoute(route, page, testHeaders, handleFetch)
)
}

teardown(): void {
Expand Down Expand Up @@ -51,13 +57,11 @@ export async function applyNextFixture(
nextOptions,
nextWorker,
page,
extraHTTPHeaders,
}: {
testInfo: TestInfo
nextOptions: NextOptions
nextWorker: NextWorkerFixture
page: Page
extraHTTPHeaders: Record<string, string> | undefined
}
): Promise<void> {
const fixture = new NextFixtureImpl(
Expand All @@ -66,11 +70,6 @@ export async function applyNextFixture(
nextWorker,
page
)
page.setExtraHTTPHeaders({
...extraHTTPHeaders,
'Next-Test-Proxy-Port': String(nextWorker.proxyPort),
'Next-Test-Data': fixture.testId,
})

await use(fixture)

Expand Down
22 changes: 18 additions & 4 deletions packages/next/src/experimental/testmode/playwright/page-route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import type { Page, Route } from '@playwright/test'
import type { Page, Route, Request } from '@playwright/test'
import type { FetchHandler } from './next-worker-fixture'

function continueRoute(
route: Route,
request: Request,
testHeaders: Record<string, string>
): Promise<void> {
return route.continue({
headers: {
...request.headers(),
...testHeaders,
},
})
}

export async function handleRoute(
route: Route,
page: Page,
testHeaders: Record<string, string>,
fetchHandler: FetchHandler | null
) {
const request = route.request()

// Continue the navigation and non-fetch requests.
if (request.isNavigationRequest() || request.resourceType() !== 'fetch') {
return route.continue()
return continueRoute(route, request, testHeaders)
}

// Continue the local requests. The followup requests will be intercepted
// on the server.
const pageOrigin = new URL(page.url()).origin
const requestOrigin = new URL(request.url()).origin
if (pageOrigin === requestOrigin) {
return route.continue()
return continueRoute(route, request, testHeaders)
}

if (!fetchHandler) {
Expand All @@ -44,7 +58,7 @@ export async function handleRoute(
return route.abort()
}
if (proxyResponse === 'continue') {
return route.continue()
return continueRoute(route, request, testHeaders)
}
const { status, headers, body } = proxyResponse
return route.fulfill({
Expand Down