-
-
Notifications
You must be signed in to change notification settings - Fork 535
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
feat: isolate parent and child frames when handling requests #2324
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,12 @@ self.addEventListener('message', async function (event) { | |
|
||
sendToClient(client, { | ||
type: 'MOCKING_ENABLED', | ||
payload: true, | ||
payload: { | ||
client: { | ||
id: client.id, | ||
frameType: client.frameType, | ||
}, | ||
}, | ||
}) | ||
break | ||
} | ||
|
@@ -155,6 +160,10 @@ async function handleRequest(event, requestId) { | |
async function resolveMainClient(event) { | ||
const client = await self.clients.get(event.clientId) | ||
|
||
if (activeClientIds.has(event.clientId)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix. |
||
return client | ||
} | ||
|
||
if (client?.frameType === 'top-level') { | ||
return client | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/browser/msw-api/setup-worker/scenarios/iframe/multiple-workers/child.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { http } from 'msw' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
const worker = setupWorker( | ||
http.get('/resource', () => { | ||
return new Response('hello world') | ||
}), | ||
) | ||
|
||
worker.start() |
33 changes: 33 additions & 0 deletions
33
...er/msw-api/setup-worker/scenarios/iframe/multiple-workers/iframe-multiple-workers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { test, expect } from '../../../../../playwright.extend' | ||
|
||
test('intercepts a request issued by child frame when both child and parent have MSW', async ({ | ||
webpackServer, | ||
page, | ||
}) => { | ||
const parentCompilation = await webpackServer.compile([ | ||
require.resolve('./parent.mocks.ts'), | ||
]) | ||
const childCompilation = await webpackServer.compile([ | ||
require.resolve('./child.mocks.ts'), | ||
]) | ||
|
||
await page.goto(parentCompilation.previewUrl, { waitUntil: 'networkidle' }) | ||
|
||
await page.evaluate((childFrameUrl) => { | ||
const iframe = document.createElement('iframe') | ||
iframe.setAttribute('id', 'child-frame') | ||
iframe.setAttribute('src', childFrameUrl) | ||
document.body.appendChild(iframe) | ||
}, childCompilation.previewUrl) | ||
|
||
const childFrameElement = await page.locator('#child-frame').elementHandle() | ||
const childFrame = await childFrameElement!.contentFrame() | ||
await childFrame!.waitForLoadState('networkidle') | ||
|
||
const responseText = await childFrame!.evaluate(async () => { | ||
const response = await fetch('/resource') | ||
return response.text() | ||
}) | ||
|
||
expect(responseText).toBe('hello world') | ||
}) |
7 changes: 7 additions & 0 deletions
7
test/browser/msw-api/setup-worker/scenarios/iframe/multiple-workers/parent.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { setupWorker } from 'msw/browser' | ||
|
||
// The parent frame has a worker without any handlers. | ||
const worker = setupWorker() | ||
|
||
// This registration is awaited by the `loadExample` command in the test. | ||
worker.start() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also improving the activation message to include the client ID that prompted it, as well as the client frame type (top-level/nested). This helps during debugging.