-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e8c2c0
commit 8c261f2
Showing
5 changed files
with
204 additions
and
4 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
test/modules/WebSocket/third-party/socket.io.browser.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,138 @@ | ||
import { test, expect } from '../../../playwright.extend' | ||
import { Server } from 'socket.io' | ||
import type { io } from 'socket.io-client' | ||
import type { Encoder, Decoder } from 'socket.io-parser' | ||
import type { encodePacket, decodePacket } from 'engine.io-parser' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { DeferredPromise } from '@open-draft/deferred-promise' | ||
import type { WebSocketInterceptor } from '../../../../src/interceptors/WebSocket' | ||
|
||
declare global { | ||
interface Window { | ||
io: typeof io | ||
encoder: Encoder | ||
decoder: Decoder | ||
encodePacket: typeof encodePacket | ||
decodePacket: typeof decodePacket | ||
interceptor: WebSocketInterceptor | ||
} | ||
} | ||
|
||
const httpServer = new HttpServer() | ||
const wsServer = new Server(httpServer['_http'], { | ||
transports: ['websocket'], | ||
}) | ||
|
||
test.beforeAll(async () => { | ||
await httpServer.listen() | ||
}) | ||
|
||
test.afterAll(async () => { | ||
await httpServer.close() | ||
}) | ||
|
||
test('intercepts and modifies data sent to socket.io server', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
await loadExample(require.resolve('./socket.io.runtime.js'), { | ||
/** | ||
* @note The WebSocket interceptor must be applied | ||
* before "socket.io-client" is evaluated. SocketIO | ||
* hoists the global WebSocket class so it cannot | ||
* be patched by the interceptor. | ||
*/ | ||
markup: ` | ||
<script src="https://cdn.socket.io/4.7.4/socket.io.min.js" integrity="sha384-Gr6Lu2Ajx28mzwyVR8CFkULdCU7kMlZ9UthllibdOSo6qAiN+yXNHqtgdTvFXMT4" crossorigin="anonymous" defer> | ||
</script> | ||
`, | ||
}) | ||
|
||
const serverMessagePromise = new DeferredPromise<string>() | ||
wsServer.on('connection', (socket) => { | ||
socket.on('message', (data) => { | ||
serverMessagePromise.resolve(data) | ||
}) | ||
}) | ||
|
||
await page.evaluate((url) => { | ||
const { io, interceptor, encoder, encodePacket, decodePacket, decoder } = | ||
window | ||
|
||
const decodeMessage = ( | ||
// @ts-expect-error | ||
encodedEngineIoPacket | ||
) => { | ||
const decodedEngineIoPacket = decodePacket(encodedEngineIoPacket) | ||
|
||
if (decodedEngineIoPacket.type !== 'message') { | ||
return | ||
} | ||
const decodedSocketIoPacket = decoder['decodeString']( | ||
decodedEngineIoPacket.data | ||
) | ||
/** | ||
* @note You should reference "PacketType.EVENT" | ||
* from "socket.io-parser" here but can't pass | ||
* that through Playwright. | ||
*/ | ||
if (decodedSocketIoPacket.type !== 2) { | ||
return | ||
} | ||
|
||
return decodedSocketIoPacket.data.slice(1) | ||
} | ||
|
||
interceptor.on('connection', ({ client, server }) => { | ||
const sendToSocketIo = ( | ||
// @ts-expect-error | ||
data | ||
) => { | ||
encodePacket( | ||
{ | ||
type: 'message', // 4 | ||
data: encoder.encode({ | ||
type: 2, | ||
/** | ||
* @noto Not sure if prepending "message" | ||
* manually is correct. Either encoder doesn't | ||
* do that though. | ||
*/ | ||
data: ['message', data], | ||
nsp: '/', | ||
}), | ||
}, | ||
true, | ||
(encodedEngineIoPacket) => { | ||
server.send(encodedEngineIoPacket) | ||
} | ||
) | ||
} | ||
|
||
server.connect() | ||
|
||
client.on('message', (event) => { | ||
const data = decodeMessage(event.data) | ||
|
||
if (data?.[0] === 'hello') { | ||
event.stopImmediatePropagation() | ||
sendToSocketIo('mocked hello!') | ||
} | ||
}) | ||
|
||
client.on('message', (event) => { | ||
server.send(event.data) | ||
}) | ||
}) | ||
|
||
const ws = io(url, { | ||
transports: ['websocket'], | ||
}) | ||
|
||
ws.on('connect', () => { | ||
ws.send('hello') | ||
}) | ||
}, httpServer.http.address.href) | ||
|
||
expect(await serverMessagePromise).toBe('mocked hello!') | ||
}) |
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,12 @@ | ||
import { WebSocketInterceptor } from '@mswjs/interceptors/WebSocket' | ||
import { Encoder, Decoder } from 'socket.io-parser' | ||
import { encodePacket, decodePacket } from 'engine.io-parser' | ||
|
||
const interceptor = new WebSocketInterceptor() | ||
interceptor.apply() | ||
window.interceptor = interceptor | ||
|
||
window.encodePacket = encodePacket | ||
window.decodePacket = decodePacket | ||
window.encoder = new Encoder() | ||
window.decoder = new Decoder() |
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