Skip to content
Open
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
85 changes: 46 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {
Page,
PlaywrightTestArgs,
PlaywrightWorkerArgs,
Request,
Route,
TestFixture,
WebSocketRoute,
} from '@playwright/test'
Expand Down Expand Up @@ -50,9 +52,9 @@ export function createNetworkFixture(
args?: CreateNetworkFixtureArgs,
/** @todo `onUnhandledRequest`? */
): [
TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>,
{ auto: boolean },
] {
TestFixture<NetworkFixture, PlaywrightTestArgs & PlaywrightWorkerArgs>,
{ auto: boolean },
] {
return [
async ({ page }, use) => {
const worker = new NetworkFixture({
Expand All @@ -77,44 +79,51 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
}) {
super(...args.initialHandlers)
this.#page = args.page

}

public async start() {
// Handle HTTP requests.
await this.#page.route(/.+/, async (route, request) => {
const fetchRequest = new Request(request.url(), {
method: request.method(),
headers: new Headers(await request.allHeaders()),
body: request.postDataBuffer(),
})
async httpHandler(route: Route, request: Request) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I was checking out this work locally and found that running the project's test script started to produce the following error with your changes:

    TypeError: Cannot read properties of undefined (reading 'handlersController')

One way to rectify this error is to turn httpHandler into an arrow function:

Suggested change
async httpHandler(route: Route, request: Request) {
httpHandler = async (route: Route, request: Request) => {

That will make the tests pass.

const fetchRequest = new Request(request.url(), {
method: request.method(),
headers: new Headers(await request.allHeaders()),
body: request.postDataBuffer(),
})

const response = await getResponse(
this.handlersController.currentHandlers().filter((handler) => {
return handler instanceof RequestHandler
}),
fetchRequest,
{
baseUrl: this.getPageUrl(),
},
)

if (response) {
if (response.status === 0) {
route.abort()
return
}

route.fulfill({
status: response.status,
headers: Object.fromEntries(response.headers),
body: response.body
? Buffer.from(await response.arrayBuffer())
: undefined,
})
const response = await getResponse(
this.handlersController.currentHandlers().filter((handler) => {
return handler instanceof RequestHandler
}),
fetchRequest,
{
baseUrl: this.getPageUrl(),
},
)

if (response) {
if (response.status === 0) {
route.abort()
return
}

route.continue()
route.fulfill({
status: response.status,
headers: Object.fromEntries(response.headers),
body: response.body
? Buffer.from(await response.arrayBuffer())
: undefined,
})
return
}

route.continue()
}

public async start() {
// Handle HTTP requests.
await this.#page.route(/.+/, this.httpHandler)

this.subscriptions.push(async () => {
await this.#page.unroute(/.+/, this.httpHandler)
})

// Handle WebSocket connections.
Expand Down Expand Up @@ -160,8 +169,7 @@ export class NetworkFixture extends SetupApi<LifeCycleEventsMap> {
}

class PlaywrightWebSocketClientConnection
implements WebSocketClientConnectionProtocol
{
implements WebSocketClientConnectionProtocol {
public id: string
public url: URL

Expand Down Expand Up @@ -264,8 +272,7 @@ class PlaywrightWebSocketClientConnection
}

class PlaywrightWebSocketServerConnection
implements WebSocketServerConnectionProtocol
{
implements WebSocketServerConnectionProtocol {
#server?: WebSocketRoute
#bufferedEvents: Array<
Parameters<WebSocketServerConnectionProtocol['addEventListener']>
Expand Down