Skip to content

Commit 0386e91

Browse files
bdibonclaude
andcommitted
✅ Test WebSocket passthrough at the instrumentation layer
The collection layer only observes what webSocketObservable emits, so asserting that application handlers and payloads survive belongs with the instrumentation. Handler passthrough was already covered there; add the missing send-payload case and drop the collection-level duplicate. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent ba4b635 commit 0386e91

3 files changed

Lines changed: 15 additions & 28 deletions

File tree

packages/browser-core/src/browser/webSocketObservable.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ describe('webSocketObservable', () => {
111111
expect(customerHandler).toHaveBeenCalledTimes(1)
112112
expect(getContexts('closed').length).toBe(1)
113113
})
114+
115+
it('forwards the sent payload to the native send unaltered', () => {
116+
const ws = createMockWebSocket('wss://example.com/socket')
117+
const payload = 'hello'
118+
119+
ws.send(payload)
120+
121+
expect(ws.sentData).toEqual([payload])
122+
expect(getContexts('message-out').length).toBe(1)
123+
})
114124
})
115125

116126
describe('open context', () => {

packages/browser-core/test/emulate/mockWebSocket.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export class MockWebSocket extends EventTarget {
3838
onmessage: ((event: MessageEvent) => void) | null = null
3939
onopen: ((event: Event) => void) | null = null
4040
onclose: ((event: CloseEvent) => void) | null = null
41+
// Payloads that reached the socket, in order, so that specs can check instrumentation forwards
42+
// them unaltered. Tests set `bufferedAmount` before calling send to verify it is sampled.
43+
sentData: Array<string | ArrayBufferLike | Blob | ArrayBufferView> = []
4144

4245
constructor(url: string | URL, protocols?: string | string[]) {
4346
super()
@@ -47,8 +50,8 @@ export class MockWebSocket extends EventTarget {
4750
}
4851
}
4952

50-
send(_data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
51-
// no-op; tests will set `bufferedAmount` before calling send to verify it is sampled.
53+
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
54+
this.sentData.push(data)
5255
}
5356

5457
close(_code?: number, _reason?: string): void {

packages/browser-rum-core/src/domain/resource/webSocketCollection.spec.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -667,32 +667,6 @@ describe('webSocketCollection', () => {
667667
})
668668
})
669669

670-
it('leaves application-set handlers and exchanged payloads untouched', () => {
671-
const openHandler = jasmine.createSpy<(event: Event) => void>()
672-
const messageHandler = jasmine.createSpy<(event: MessageEvent) => void>()
673-
const closeHandler = jasmine.createSpy<(event: CloseEvent) => void>()
674-
// spied before instrumentation is installed, so that the instrumented `send` delegates to it
675-
const sendSpy = spyOn(window.WebSocket.prototype, 'send').and.callThrough()
676-
677-
startCollection()
678-
const socket = notifyConnecting()
679-
socket.onopen = openHandler
680-
socket.onmessage = messageHandler
681-
socket.onclose = closeHandler
682-
683-
notifyOpen(socket, 10)
684-
setClock(20)
685-
socket.simulateMessage('hello')
686-
setClock(30)
687-
socket.send('world')
688-
notifyClosed(socket, 40, 1000, 'bye', true)
689-
690-
expect(openHandler).toHaveBeenCalledTimes(1)
691-
expect(messageHandler.calls.mostRecent().args[0].data).toBe('hello')
692-
expect(closeHandler.calls.mostRecent().args[0].code).toBe(1000)
693-
expect(sendSpy).toHaveBeenCalledOnceWith('world')
694-
})
695-
696670
it('finalizes open connections with tracking_end_reason="session_end" when the session expires', () => {
697671
const endClocks = relativeToClocks(clock.relative(40))
698672
startCollection()

0 commit comments

Comments
 (0)