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
7 changes: 2 additions & 5 deletions packages/playwright-msw/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ export const handleRoute = async (route: Route, handlers: RequestHandler[]) => {
*/
baseUrl: url.origin,
},
onMockedResponse: async ({
status,
headers: rawHeaders,
body: rawBody,
}) => {
onMockedResponse: async (response) => {

Choose a reason for hiding this comment

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

I think it's much better to use a higher-level getResponse() instead of handleRequest(). Any history as to why handleRequest is used instead? Is it to tap into resolutionContext.baseUrl?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure i'm following... Where do you mean i would use handleRequest?
The only difference in this PR is that i accept the request parameter provided by onMockedResponse, which i then use to clone the response, before mutating it. The reason for the clone is because the stream is "locked" as per this error message.

ReadableStream is locked

Copy link

@kettanaito kettanaito Jul 1, 2025

Choose a reason for hiding this comment

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

My comment isn't related to your changes. If you scroll a few lines above your change, you see the usage of handleRequest. Funneling the mocked response from onMockedResponse to Playwright isn't the best place so I'm suggesting using getResponse() instead.

const { status, headers: rawHeaders, body: rawBody } = response.clone();
const contentType = rawHeaders.get('content-type') ?? undefined;
const headers = objectifyHeaders(rawHeaders);
const body = await readableStreamToBuffer(contentType, rawBody);
Expand Down