test-client.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var ws = require('./')
  2. var test = require('tape')
  3. var Buffer = require('safe-buffer').Buffer
  4. test('echo works', function(t) {
  5. var stream = ws('ws://localhost:8343')
  6. stream.on('data', function(o) {
  7. t.ok(Buffer.isBuffer(o), 'is buffer')
  8. t.equal(o.toString(), 'hello', 'got hello back')
  9. stream.destroy()
  10. t.end()
  11. })
  12. stream.write(Buffer.from('hello'))
  13. })
  14. test('echo works two times', function(t) {
  15. var stream = ws('ws://localhost:8343')
  16. stream.once('data', function(o) {
  17. t.equal(o.toString(), 'hello', 'got first hello back')
  18. stream.write(Buffer.from('hello'))
  19. stream.once('data', function(o) {
  20. t.equal(o.toString(), 'hello', 'got second hello back')
  21. stream.destroy()
  22. t.end()
  23. })
  24. })
  25. stream.write(Buffer.from('hello'))
  26. })
  27. test('with bare WebSocket, strings as strings', function (t) {
  28. var socket = new WebSocket('ws://localhost:8344')
  29. socket.onmessage = function (e) {
  30. var data = e.data
  31. t.ok(typeof data === 'string', 'data must be a string')
  32. socket.close()
  33. t.end()
  34. }
  35. })
  36. test('with bare WebSocket, binary only', function (t) {
  37. var socket = new WebSocket('ws://localhost:8345')
  38. socket.onmessage = function (e) {
  39. var data = e.data
  40. t.notOk(typeof data === 'string', 'data must not be a string')
  41. socket.close()
  42. t.end()
  43. }
  44. })
  45. test('coerce client data as binary', function(t) {
  46. var stream = ws('ws://localhost:8346', { binary: true })
  47. stream.on('data', function(o) {
  48. t.ok(Buffer.isBuffer(o), 'is buffer')
  49. t.equal(o.toString(), 'success', 'success!')
  50. stream.destroy()
  51. t.end()
  52. })
  53. stream.write('hello')
  54. })
  55. test('cork logic test', function (t) {
  56. var stream = ws('ws://localhost:8343', { binary: true })
  57. stream.on('data', function(o) {
  58. t.equal(o.toString(), 'hello', 'success!')
  59. stream.destroy()
  60. t.end()
  61. })
  62. stream.cork()
  63. stream.write('he')
  64. stream.write('l')
  65. stream.write('lo')
  66. stream.uncork()
  67. })