liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
 
import request from 'supertest';
import assert from 'assert';
import {parse} from 'url';
import listen from './helpers/listen';
import {Client as WebSocket} from 'faye-websocket';
 
describe('tiny-lr', () => {
  before(listen());
  it('accepts ws clients', function (done) {
    const url = parse(this.request.url);
    const server = this.app;
 
    const ws = this.ws = new WebSocket('ws://' + url.host + '/livereload');
 
    ws.onopen = event => {
      const hello = {
        command: 'hello',
        protocols: ['http://livereload.com/protocols/official-7']
      };
 
      ws.send(JSON.stringify(hello));
    };
 
    ws.onmessage = event => {
      assert.deepEqual(event.data, JSON.stringify({
        command: 'hello',
        protocols: ['http://livereload.com/protocols/official-7'],
        serverName: 'tiny-lr'
      }));
 
      assert.ok(Object.keys(server.clients).length);
      done();
    };
  });
 
  it('properly cleans up established connection on exit', function (done) {
    const ws = this.ws;
 
    ws.onclose = done.bind(null, null);
 
    request(this.server)
      .get('/kill')
      .expect(200, () => {});
  });
});