|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const http = require('http'); |
| 4 | +const express = require('express'); |
| 5 | +const SockJS = require('sockjs-client/dist/sockjs'); |
| 6 | +const SockJSServer = require('../lib/servers/SockJSServer'); |
| 7 | + |
| 8 | +describe('SockJSServer', () => { |
| 9 | + let socketServer; |
| 10 | + let listeningApp; |
| 11 | + |
| 12 | + beforeAll((done) => { |
| 13 | + // eslint-disable-next-line new-cap |
| 14 | + const app = new express(); |
| 15 | + listeningApp = http.createServer(app); |
| 16 | + listeningApp.listen(8080, 'localhost', () => { |
| 17 | + const server = { |
| 18 | + log: { |
| 19 | + error: () => {}, |
| 20 | + debug: () => {}, |
| 21 | + }, |
| 22 | + sockPath: '/sockjs-node', |
| 23 | + listeningApp, |
| 24 | + }; |
| 25 | + socketServer = new SockJSServer(server); |
| 26 | + done(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('server', () => { |
| 31 | + it('should recieve connection, send message, and close client', (done) => { |
| 32 | + const data = []; |
| 33 | + socketServer.onConnection((connection) => { |
| 34 | + data.push('open'); |
| 35 | + socketServer.send(connection, 'hello world'); |
| 36 | + setTimeout(() => { |
| 37 | + socketServer.close(connection); |
| 38 | + }, 1000); |
| 39 | + }); |
| 40 | + |
| 41 | + const client = new SockJS('http://localhost:8080/sockjs-node'); |
| 42 | + |
| 43 | + client.onmessage = function(e) { |
| 44 | + data.push(e.data); |
| 45 | + }; |
| 46 | + |
| 47 | + client.onclose = function() { |
| 48 | + data.push('close'); |
| 49 | + }; |
| 50 | + |
| 51 | + setTimeout(() => { |
| 52 | + expect(data.length).toEqual(3); |
| 53 | + expect(data[0]).toEqual('open'); |
| 54 | + expect(data[1]).toEqual('hello world'); |
| 55 | + expect(data[2]).toEqual('close'); |
| 56 | + done(); |
| 57 | + }, 3000); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + afterAll((done) => { |
| 62 | + listeningApp.close(done); |
| 63 | + }); |
| 64 | +}); |
0 commit comments