Skip to content

Commit bd35703

Browse files
add tests
1 parent 15e6ba8 commit bd35703

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ function noop () { }
8484

8585
function proxyWebSockets (logger, source, target, hooks) {
8686
function close (code, reason) {
87+
if (hooks.onDisconnect) {
88+
waitConnection(target, () => {
89+
try {
90+
hooks.onDisconnect(source)
91+
} catch (err) {
92+
logger.error({ err }, 'proxy ws error from onDisconnect hook')
93+
}
94+
})
95+
}
8796
closeWebSocket(source, code, reason)
8897
closeWebSocket(target, code, reason)
8998
}

test/websocket.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,44 @@ test('should handle throwing an error in onIncomingMessage and onOutgoingMessage
769769
await waitForLogMessage(loggerSpy, 'proxy ws error from onOutgoingMessage hook')
770770
})
771771

772-
// TODO onConnect, onDisconnect
772+
test('should call onConnect hook', async (t) => {
773+
const onConnect = () => {
774+
logger.info('onConnect called')
775+
}
776+
777+
const { loggerSpy, logger } = await createServices({ t, wsHooks: { onConnect } })
778+
779+
await waitForLogMessage(loggerSpy, 'onConnect called')
780+
})
781+
782+
test('should handle throwing an error in onConnect hook', async (t) => {
783+
const onConnect = () => {
784+
throw new Error('onConnect error')
785+
}
786+
787+
const { loggerSpy } = await createServices({ t, wsHooks: { onConnect } })
788+
789+
await waitForLogMessage(loggerSpy, 'proxy ws error from onConnect hook')
790+
})
791+
792+
test('should call onDisconnect hook', async (t) => {
793+
const onDisconnect = () => {
794+
logger.info('onDisconnect called')
795+
}
796+
797+
const { loggerSpy, logger, client } = await createServices({ t, wsHooks: { onDisconnect } })
798+
client.close()
799+
800+
await waitForLogMessage(loggerSpy, 'onDisconnect called')
801+
})
802+
803+
test('should handle throwing an error in onDisconnect hook', async (t) => {
804+
const onDisconnect = () => {
805+
throw new Error('onDisconnect error')
806+
}
807+
808+
const { loggerSpy, client } = await createServices({ t, wsHooks: { onDisconnect } })
809+
client.close()
810+
811+
await waitForLogMessage(loggerSpy, 'proxy ws error from onDisconnect hook')
812+
})

test/ws-reconnect.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,60 @@ test('should handle throwing an error in onIncomingMessage and onOutgoingMessage
268268
await waitForLogMessage(loggerSpy, 'proxy ws error from onOutgoingMessage hook')
269269
})
270270

271-
// TODO onConnect, onDisconnect
271+
test('should call onConnect hook', async (t) => {
272+
const onConnect = () => {
273+
logger.info('onConnect called')
274+
}
275+
276+
const wsReconnectOptions = {
277+
logs: true,
278+
}
279+
280+
const { loggerSpy, logger } = await createServices({ t, wsReconnectOptions, wsHooks: { onConnect } })
281+
282+
await waitForLogMessage(loggerSpy, 'onConnect called')
283+
})
284+
285+
test('should handle throwing an error in onConnect hook', async (t) => {
286+
const onConnect = () => {
287+
throw new Error('onConnect error')
288+
}
289+
290+
const wsReconnectOptions = {
291+
logs: true,
292+
}
293+
294+
const { loggerSpy } = await createServices({ t, wsReconnectOptions, wsHooks: { onConnect } })
295+
296+
await waitForLogMessage(loggerSpy, 'proxy ws error from onConnect hook')
297+
})
298+
299+
test('should call onDisconnect hook', async (t) => {
300+
const onDisconnect = () => {
301+
logger.info('onDisconnect called')
302+
}
303+
304+
const wsReconnectOptions = {
305+
logs: true,
306+
}
307+
308+
const { loggerSpy, logger, client } = await createServices({ t, wsReconnectOptions, wsHooks: { onDisconnect } })
309+
client.close()
310+
311+
await waitForLogMessage(loggerSpy, 'onDisconnect called')
312+
})
313+
314+
test('should handle throwing an error in onDisconnect hook', async (t) => {
315+
const onDisconnect = () => {
316+
throw new Error('onDisconnect error')
317+
}
318+
319+
const wsReconnectOptions = {
320+
logs: true,
321+
}
322+
323+
const { loggerSpy, client } = await createServices({ t, wsReconnectOptions, wsHooks: { onDisconnect } })
324+
client.close()
325+
326+
await waitForLogMessage(loggerSpy, 'proxy ws error from onDisconnect hook')
327+
})

0 commit comments

Comments
 (0)