-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathupgrade.test.js
More file actions
52 lines (44 loc) · 1.75 KB
/
upgrade.test.js
File metadata and controls
52 lines (44 loc) · 1.75 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict'
const { describe, test } = require('node:test')
const Fastify = require('..')
const { connect } = require('node:net')
const { once } = require('node:events')
const dns = require('node:dns').promises
describe('upgrade to both servers', async () => {
const localAddresses = await dns.lookup('localhost', { all: true })
const skip = localAddresses.length === 1 && 'requires both IPv4 and IPv6'
await test('upgrade IPv4 and IPv6', { skip }, async t => {
t.plan(2)
const fastify = Fastify()
fastify.server.on('upgrade', (req, socket, head) => {
t.assert.ok(`upgrade event ${JSON.stringify(socket.address())}`)
socket.end()
})
fastify.get('/', (req, res) => {
res.send()
})
await fastify.listen()
t.after(() => fastify.close())
{
const clientIPv4 = connect(fastify.server.address().port, '127.0.0.1')
clientIPv4.write('GET / HTTP/1.1\r\n')
clientIPv4.write('Upgrade: websocket\r\n')
clientIPv4.write('Connection: Upgrade\r\n')
clientIPv4.write('Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n')
clientIPv4.write('Sec-WebSocket-Protocol: com.xxx.service.v1\r\n')
clientIPv4.write('Sec-WebSocket-Version: 13\r\n\r\n')
clientIPv4.write('\r\n\r\n')
await once(clientIPv4, 'close')
}
{
const clientIPv6 = connect(fastify.server.address().port, '::1')
clientIPv6.write('GET / HTTP/1.1\r\n')
clientIPv6.write('Upgrade: websocket\r\n')
clientIPv6.write('Connection: Upgrade\r\n')
clientIPv6.write('Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n')
clientIPv6.write('Sec-WebSocket-Protocol: com.xxx.service.v1\r\n')
clientIPv6.write('Sec-WebSocket-Version: 13\r\n\r\n')
await once(clientIPv6, 'close')
}
})
})