-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
95 lines (67 loc) · 1.85 KB
/
test.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict'
const test = require('tap').test
test('expose a WebSocket in path', t => {
t.plan(4)
const fastify = require('fastify')()
const fastifyWS = require('.')
const WebSocket = require('ws')
const path = '/ws'
fastify.register(fastifyWS, { path: path })
fastify.ready(err => {
t.error(err)
fastify.ws
.on('connection', socket => {
socket.send('hello client')
socket.on('message', msg => {
t.equal(msg, 'hello server')
fastify.close()
})
})
})
fastify.listen(0, err => {
t.error(err)
const client = new WebSocket(`ws://localhost:${fastify.server.address().port}${path}`)
client.on('open', () => {
client.send('hello server')
client.onmessage = msg => t.equal(msg.data, 'hello client')
client.close()
})
})
})
test('expose a WebSocket', t => {
t.plan(4)
const fastify = require('fastify')()
const fastifyWS = require('.')
const WebSocket = require('ws')
fastify.register(fastifyWS)
fastify.ready(err => {
t.error(err)
fastify.ws
.on('connection', socket => {
socket.send('hello client')
socket.on('message', msg => {
t.equal(msg, 'hello server')
fastify.close()
})
})
})
fastify.listen(0, err => {
t.error(err)
const client = new WebSocket('ws://localhost:' + fastify.server.address().port)
client.on('open', () => {
client.send('hello server')
client.onmessage = msg => t.equal(msg.data, 'hello client')
client.close()
})
})
})
test('Error | invalid library option', t => {
t.plan(1)
const fastify = require('fastify')()
const fastifyWS = require('.')
fastify.register(fastifyWS, { library: 'invalid' })
fastify.listen(0, err => {
fastify.close()
t.equal(err.message, `Invalid "library" option`)
})
})