heyujie
2021-05-20 6ebdefb4a5b2be82a8c452c0bb4624f3d85a17b7
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var ws = require('./')
var test = require('tape')
var Buffer = require('safe-buffer').Buffer
 
test('echo works', function(t) {
  var stream = ws('ws://localhost:8343')
  stream.on('data', function(o) {
    t.ok(Buffer.isBuffer(o), 'is buffer')
    t.equal(o.toString(), 'hello', 'got hello back')
    stream.destroy()
    t.end()
  })
  stream.write(Buffer.from('hello'))
})
 
test('echo works two times', function(t) {
  var stream = ws('ws://localhost:8343')
  stream.once('data', function(o) {
    t.equal(o.toString(), 'hello', 'got first hello back')
    stream.write(Buffer.from('hello'))
    stream.once('data', function(o) {
      t.equal(o.toString(), 'hello', 'got second hello back')
      stream.destroy()
      t.end()
    })
  })
  stream.write(Buffer.from('hello'))
})
 
test('with bare WebSocket, strings as strings', function (t) {
  var socket = new WebSocket('ws://localhost:8344')
 
  socket.onmessage = function (e) {
    var data = e.data
    t.ok(typeof data === 'string', 'data must be a string')
    socket.close()
    t.end()
  }
})
 
test('with bare WebSocket, binary only', function (t) {
  var socket = new WebSocket('ws://localhost:8345')
 
  socket.onmessage = function (e) {
    var data = e.data
    t.notOk(typeof data === 'string', 'data must not be a string')
    socket.close()
    t.end()
  }
})
 
test('coerce client data as binary', function(t) {
  var stream = ws('ws://localhost:8346', { binary: true })
  stream.on('data', function(o) {
    t.ok(Buffer.isBuffer(o), 'is buffer')
    t.equal(o.toString(), 'success', 'success!')
    stream.destroy()
    t.end()
  })
  stream.write('hello')
})
test('cork logic test', function (t) {
  var stream = ws('ws://localhost:8343', { binary: true })
  stream.on('data', function(o) {
    t.equal(o.toString(), 'hello', 'success!')
    stream.destroy()
    t.end()
  })
  stream.cork()
  stream.write('he')
  stream.write('l')
  stream.write('lo')
  stream.uncork()
})