-
Notifications
You must be signed in to change notification settings - Fork 2
/
sws.spec.js
163 lines (132 loc) · 3.41 KB
/
sws.spec.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const bl = require('bl')
const sws = require('../src')
const SW = require('simple-websocket')
describe('.createServer', () => {
it('create instance without options', () => {
const s = sws.createServer()
expect(s).to.exist
})
it('create instance with options', () => {
const s = sws.createServer({})
expect(s).to.exist
})
describe('server instance', () => {
const options = {
port: 9090
}
let server
beforeEach(() => {
server = sws.createServer()
})
it('listen, check for callback', (done) => {
server.listen(options, () => {
server.close(done)
})
})
it('listen, check for listening event', (done) => {
server.listen(options)
server.on('listening', () => {
server.close(done)
})
})
it('listen, check for close event', (done) => {
server.listen(options)
server.on('listening', () => {
server.on('close', done)
server.close()
})
})
it('listen, use port as arg', (done) => {
server.listen(9090)
server.on('listening', () => {
server.close(done)
})
})
})
})
describe('listen and dial', () => {
const options = {
port: 9090
}
it('dial and echo', (done) => {
const server = sws.createServer((socket) => {
socket.pipe(socket)
})
server.listen(options, dial)
function dial () {
const socket = new SW('ws://localhost:9090')
socket.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
server.close(done)
}))
socket.write('hey')
socket.end()
}
})
it('dial and check for "connection" event on the listener', (done) => {
const server = sws.createServer()
server.on('connection', (socket) => {
socket.pipe(socket)
})
server.listen(options, dial)
function dial () {
const socket = new SW('ws://localhost:9090')
socket.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
server.close(done)
}))
socket.write('hey')
socket.end()
}
})
it('dial and destroy from client', (done) => {
const server = sws.createServer((socket) => {
socket.on('close', () => {
server.close(done)
})
})
server.listen(options, dial)
function dial () {
const socket = new SW('ws://localhost:9090')
socket.write('hey')
socket.destroy()
}
})
it('dial and destroy from server', (done) => {
const server = sws.createServer((socket) => {
socket.destroy()
})
server.listen(options, dial)
function dial () {
const socket = new SW('ws://localhost:9090')
socket.on('close', () => {
server.close(done)
})
}
})
it.skip('dial and try to close before socket end', (done) => {
const server = sws.createServer((socket) => {
setTimeout(() => {
socket.pipe(socket)
}, 500)
server.close()
})
server.listen(options, dial)
function dial () {
const socket = new SW('ws://localhost:9090')
socket.pipe(bl((err, data) => {
console.log('got data')
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
server.close(done)
}))
socket.write('hey')
socket.end()
}
})
})