-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5.websocket.spec.ts
33 lines (29 loc) · 973 Bytes
/
5.websocket.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { describe, expect, it } from 'vitest'
import fexios from '../src/index'
import { ECHO_BASE_URL } from './constants'
const WS_URL = `${ECHO_BASE_URL}/_ws`
describe('WebSocket', () => {
it('Should return WebSocket', async () => {
const { data } = await fexios.get<WebSocket>(WS_URL)
expect(data).to.be.instanceOf(WebSocket)
data?.close()
})
it('Should handle ws:// or wss://', async () => {
const { data } = await fexios.get<WebSocket>(WS_URL.replace(/^http/, 'ws'))
expect(data).to.be.instanceOf(WebSocket)
data?.close()
})
it('WebSocket message', async () => {
const { data: ws } = await fexios.get<WebSocket>(WS_URL)
const now = '' + Date.now()
ws.send(now)
const response = await new Promise<string>((resolve) => {
ws.addEventListener('message', (event) => {
if (event.data === WS_URL) return
resolve(event.data)
})
})
expect(response).to.equal(now)
ws.close()
})
})