Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(server): add SockJSServer test (#1956)
* test(server): added SockJSServer test * test(server): change test done callback
- Loading branch information
1 parent
eecc1e4
commit d346a53
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
const express = require('express'); | ||
const SockJS = require('sockjs-client/dist/sockjs'); | ||
const SockJSServer = require('../lib/servers/SockJSServer'); | ||
|
||
describe('SockJSServer', () => { | ||
let socketServer; | ||
let listeningApp; | ||
|
||
beforeAll((done) => { | ||
// eslint-disable-next-line new-cap | ||
const app = new express(); | ||
listeningApp = http.createServer(app); | ||
listeningApp.listen(8080, 'localhost', () => { | ||
const server = { | ||
log: { | ||
error: () => {}, | ||
debug: () => {}, | ||
}, | ||
sockPath: '/sockjs-node', | ||
listeningApp, | ||
}; | ||
socketServer = new SockJSServer(server); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('server', () => { | ||
it('should recieve connection, send message, and close client', (done) => { | ||
const data = []; | ||
socketServer.onConnection((connection) => { | ||
data.push('open'); | ||
socketServer.send(connection, 'hello world'); | ||
setTimeout(() => { | ||
socketServer.close(connection); | ||
}, 1000); | ||
}); | ||
|
||
const client = new SockJS('http://localhost:8080/sockjs-node'); | ||
|
||
client.onmessage = function(e) { | ||
data.push(e.data); | ||
}; | ||
|
||
client.onclose = function() { | ||
data.push('close'); | ||
}; | ||
|
||
setTimeout(() => { | ||
expect(data.length).toEqual(3); | ||
expect(data[0]).toEqual('open'); | ||
expect(data[1]).toEqual('hello world'); | ||
expect(data[2]).toEqual('close'); | ||
done(); | ||
}, 3000); | ||
}); | ||
}); | ||
|
||
afterAll((done) => { | ||
listeningApp.close(done); | ||
}); | ||
}); |