| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | |
| 2 | import request from 'supertest'; |
| 3 | import assert from 'assert'; |
| 4 | import {parse} from 'url'; |
| 5 | import listen from './helpers/listen'; |
| 6 | import {Client as WebSocket} from 'faye-websocket'; |
| 7 | |
| 8 | describe('tiny-lr', () => { |
| 9 | before(listen()); |
| 10 | it('accepts ws clients', function (done) { |
| 11 | const url = parse(this.request.url); |
| 12 | const server = this.app; |
| 13 | |
| 14 | const ws = this.ws = new WebSocket('ws://' + url.host + '/livereload'); |
| 15 | |
| 16 | ws.onopen = event => { |
| 17 | const hello = { |
| 18 | command: 'hello', |
| 19 | protocols: ['http://livereload.com/protocols/official-7'] |
| 20 | }; |
| 21 | |
| 22 | ws.send(JSON.stringify(hello)); |
| 23 | }; |
| 24 | |
| 25 | ws.onmessage = event => { |
| 26 | assert.deepEqual(event.data, JSON.stringify({ |
| 27 | command: 'hello', |
| 28 | protocols: ['http://livereload.com/protocols/official-7'], |
| 29 | serverName: 'tiny-lr' |
| 30 | })); |
| 31 | |
| 32 | assert.ok(Object.keys(server.clients).length); |
| 33 | done(); |
| 34 | }; |
| 35 | }); |
| 36 | |
| 37 | it('properly cleans up established connection on exit', function (done) { |
| 38 | const ws = this.ws; |
| 39 | |
| 40 | ws.onclose = done.bind(null, null); |
| 41 | |
| 42 | request(this.server) |
| 43 | .get('/kill') |
| 44 | .expect(200, () => {}); |
| 45 | }); |
| 46 | }); |