Skip to content

Commit baf6500

Browse files
committed
Add test for Server#close()
1 parent d185852 commit baf6500

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

test/test.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('extraHeaders', function () {
1818

1919
after(function (done) {
2020
testConn.close()
21-
testServer.socket.close(done)
21+
testServer.close(done)
2222
})
2323

2424
it('should create a headerString with extra header options', function (done) {
@@ -47,7 +47,7 @@ describe('frames', function () {
4747

4848
after(function (done) {
4949
testClient.close()
50-
testServer.socket.close(done)
50+
testServer.close(done)
5151
})
5252

5353
it('should connect to a websocket server', function (done) {
@@ -198,7 +198,7 @@ describe('handshake', function () {
198198
})
199199

200200
after(function (done) {
201-
testServer.socket.close(done)
201+
testServer.close(done)
202202
})
203203

204204
it('should work when the handshake response is followed by a WS frame', function (done) {
@@ -226,6 +226,40 @@ describe('handshake', function () {
226226
})
227227
})
228228

229+
describe('close', function () {
230+
before(function (done) {
231+
// Create a test server and one client
232+
testServer = ws.createServer(function (conn) {
233+
testConn = conn
234+
}).listen(TEST_PORT, function () {
235+
testClient = ws.connect('ws://localhost:' + TEST_PORT, done)
236+
})
237+
})
238+
239+
var called = false
240+
241+
it('should stop the server from accepting new connections', function (done) {
242+
testServer.close(function () {
243+
called = true
244+
})
245+
246+
var newTestClient = ws.connect('ws://localhost:' + TEST_PORT)
247+
newTestClient.once('error', function (err) {
248+
err.code.should.be.equal('ECONNREFUSED')
249+
done()
250+
})
251+
})
252+
253+
it('should emit close after all client connections are closed', function (done) {
254+
called.should.be.false()
255+
testClient.close()
256+
testServer.once('close', function () {
257+
called.should.be.true()
258+
done()
259+
})
260+
})
261+
})
262+
229263
function getClient() {
230264
testClient.removeAllListeners()
231265
return testClient

0 commit comments

Comments
 (0)