test.websock.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. const expect = chai.expect;
  2. import Websock from '../core/websock.js';
  3. import FakeWebSocket from './fake.websocket.js';
  4. describe('Websock', function () {
  5. "use strict";
  6. describe('Queue methods', function () {
  7. let sock;
  8. const RQ_TEMPLATE = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
  9. beforeEach(function () {
  10. sock = new Websock();
  11. // skip init
  12. sock._allocateBuffers();
  13. sock._rQ.set(RQ_TEMPLATE);
  14. sock._rQlen = RQ_TEMPLATE.length;
  15. });
  16. describe('rQlen', function () {
  17. it('should return the length of the receive queue', function () {
  18. sock.rQi = 0;
  19. expect(sock.rQlen).to.equal(RQ_TEMPLATE.length);
  20. });
  21. it("should return the proper length if we read some from the receive queue", function () {
  22. sock.rQi = 1;
  23. expect(sock.rQlen).to.equal(RQ_TEMPLATE.length - 1);
  24. });
  25. });
  26. describe('rQpeek8', function () {
  27. it('should peek at the next byte without poping it off the queue', function () {
  28. const befLen = sock.rQlen;
  29. const peek = sock.rQpeek8();
  30. expect(sock.rQpeek8()).to.equal(peek);
  31. expect(sock.rQlen).to.equal(befLen);
  32. });
  33. });
  34. describe('rQshift8()', function () {
  35. it('should pop a single byte from the receive queue', function () {
  36. const peek = sock.rQpeek8();
  37. const befLen = sock.rQlen;
  38. expect(sock.rQshift8()).to.equal(peek);
  39. expect(sock.rQlen).to.equal(befLen - 1);
  40. });
  41. });
  42. describe('rQshift16()', function () {
  43. it('should pop two bytes from the receive queue and return a single number', function () {
  44. const befLen = sock.rQlen;
  45. const expected = (RQ_TEMPLATE[0] << 8) + RQ_TEMPLATE[1];
  46. expect(sock.rQshift16()).to.equal(expected);
  47. expect(sock.rQlen).to.equal(befLen - 2);
  48. });
  49. });
  50. describe('rQshift32()', function () {
  51. it('should pop four bytes from the receive queue and return a single number', function () {
  52. const befLen = sock.rQlen;
  53. const expected = (RQ_TEMPLATE[0] << 24) +
  54. (RQ_TEMPLATE[1] << 16) +
  55. (RQ_TEMPLATE[2] << 8) +
  56. RQ_TEMPLATE[3];
  57. expect(sock.rQshift32()).to.equal(expected);
  58. expect(sock.rQlen).to.equal(befLen - 4);
  59. });
  60. });
  61. describe('rQshiftStr', function () {
  62. it('should shift the given number of bytes off of the receive queue and return a string', function () {
  63. const befLen = sock.rQlen;
  64. const befRQi = sock.rQi;
  65. const shifted = sock.rQshiftStr(3);
  66. expect(shifted).to.be.a('string');
  67. expect(shifted).to.equal(String.fromCharCode.apply(null, Array.prototype.slice.call(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3))));
  68. expect(sock.rQlen).to.equal(befLen - 3);
  69. });
  70. it('should shift the entire rest of the queue off if no length is given', function () {
  71. sock.rQshiftStr();
  72. expect(sock.rQlen).to.equal(0);
  73. });
  74. it('should be able to handle very large strings', function () {
  75. const BIG_LEN = 500000;
  76. const RQ_BIG = new Uint8Array(BIG_LEN);
  77. let expected = "";
  78. let letterCode = 'a'.charCodeAt(0);
  79. for (let i = 0; i < BIG_LEN; i++) {
  80. RQ_BIG[i] = letterCode;
  81. expected += String.fromCharCode(letterCode);
  82. if (letterCode < 'z'.charCodeAt(0)) {
  83. letterCode++;
  84. } else {
  85. letterCode = 'a'.charCodeAt(0);
  86. }
  87. }
  88. sock._rQ.set(RQ_BIG);
  89. sock._rQlen = RQ_BIG.length;
  90. const shifted = sock.rQshiftStr();
  91. expect(shifted).to.be.equal(expected);
  92. expect(sock.rQlen).to.equal(0);
  93. });
  94. });
  95. describe('rQshiftBytes', function () {
  96. it('should shift the given number of bytes of the receive queue and return an array', function () {
  97. const befLen = sock.rQlen;
  98. const befRQi = sock.rQi;
  99. const shifted = sock.rQshiftBytes(3);
  100. expect(shifted).to.be.an.instanceof(Uint8Array);
  101. expect(shifted).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, befRQi, 3));
  102. expect(sock.rQlen).to.equal(befLen - 3);
  103. });
  104. it('should shift the entire rest of the queue off if no length is given', function () {
  105. sock.rQshiftBytes();
  106. expect(sock.rQlen).to.equal(0);
  107. });
  108. });
  109. describe('rQslice', function () {
  110. beforeEach(function () {
  111. sock.rQi = 0;
  112. });
  113. it('should not modify the receive queue', function () {
  114. const befLen = sock.rQlen;
  115. sock.rQslice(0, 2);
  116. expect(sock.rQlen).to.equal(befLen);
  117. });
  118. it('should return an array containing the given slice of the receive queue', function () {
  119. const sl = sock.rQslice(0, 2);
  120. expect(sl).to.be.an.instanceof(Uint8Array);
  121. expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 0, 2));
  122. });
  123. it('should use the rest of the receive queue if no end is given', function () {
  124. const sl = sock.rQslice(1);
  125. expect(sl).to.have.length(RQ_TEMPLATE.length - 1);
  126. expect(sl).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1));
  127. });
  128. it('should take the current rQi in to account', function () {
  129. sock.rQi = 1;
  130. expect(sock.rQslice(0, 2)).to.array.equal(new Uint8Array(RQ_TEMPLATE.buffer, 1, 2));
  131. });
  132. });
  133. describe('rQwait', function () {
  134. beforeEach(function () {
  135. sock.rQi = 0;
  136. });
  137. it('should return true if there are not enough bytes in the receive queue', function () {
  138. expect(sock.rQwait('hi', RQ_TEMPLATE.length + 1)).to.be.true;
  139. });
  140. it('should return false if there are enough bytes in the receive queue', function () {
  141. expect(sock.rQwait('hi', RQ_TEMPLATE.length)).to.be.false;
  142. });
  143. it('should return true and reduce rQi by "goback" if there are not enough bytes', function () {
  144. sock.rQi = 5;
  145. expect(sock.rQwait('hi', RQ_TEMPLATE.length, 4)).to.be.true;
  146. expect(sock.rQi).to.equal(1);
  147. });
  148. it('should raise an error if we try to go back more than possible', function () {
  149. sock.rQi = 5;
  150. expect(() => sock.rQwait('hi', RQ_TEMPLATE.length, 6)).to.throw(Error);
  151. });
  152. it('should not reduce rQi if there are enough bytes', function () {
  153. sock.rQi = 5;
  154. sock.rQwait('hi', 1, 6);
  155. expect(sock.rQi).to.equal(5);
  156. });
  157. });
  158. describe('flush', function () {
  159. beforeEach(function () {
  160. sock._websocket = {
  161. send: sinon.spy()
  162. };
  163. });
  164. it('should actually send on the websocket', function () {
  165. sock._websocket.bufferedAmount = 8;
  166. sock._websocket.readyState = WebSocket.OPEN;
  167. sock._sQ = new Uint8Array([1, 2, 3]);
  168. sock._sQlen = 3;
  169. const encoded = sock._encodeMessage();
  170. sock.flush();
  171. expect(sock._websocket.send).to.have.been.calledOnce;
  172. expect(sock._websocket.send).to.have.been.calledWith(encoded);
  173. });
  174. it('should not call send if we do not have anything queued up', function () {
  175. sock._sQlen = 0;
  176. sock._websocket.bufferedAmount = 8;
  177. sock.flush();
  178. expect(sock._websocket.send).not.to.have.been.called;
  179. });
  180. });
  181. describe('send', function () {
  182. beforeEach(function () {
  183. sock.flush = sinon.spy();
  184. });
  185. it('should add to the send queue', function () {
  186. sock.send([1, 2, 3]);
  187. const sq = sock.sQ;
  188. expect(new Uint8Array(sq.buffer, sock._sQlen - 3, 3)).to.array.equal(new Uint8Array([1, 2, 3]));
  189. });
  190. it('should call flush', function () {
  191. sock.send([1, 2, 3]);
  192. expect(sock.flush).to.have.been.calledOnce;
  193. });
  194. });
  195. describe('sendString', function () {
  196. beforeEach(function () {
  197. sock.send = sinon.spy();
  198. });
  199. it('should call send after converting the string to an array', function () {
  200. sock.sendString("\x01\x02\x03");
  201. expect(sock.send).to.have.been.calledWith([1, 2, 3]);
  202. });
  203. });
  204. });
  205. describe('lifecycle methods', function () {
  206. let oldWS;
  207. before(function () {
  208. oldWS = WebSocket;
  209. });
  210. let sock;
  211. beforeEach(function () {
  212. sock = new Websock();
  213. // eslint-disable-next-line no-global-assign
  214. WebSocket = sinon.spy(FakeWebSocket);
  215. });
  216. describe('opening', function () {
  217. it('should pick the correct protocols if none are given', function () {
  218. });
  219. it('should open the actual websocket', function () {
  220. sock.open('ws://localhost:8675', 'binary');
  221. expect(WebSocket).to.have.been.calledWith('ws://localhost:8675', 'binary');
  222. });
  223. // it('should initialize the event handlers')?
  224. });
  225. describe('attaching', function () {
  226. it('should attach to an existing websocket', function () {
  227. let ws = new FakeWebSocket('ws://localhost:8675');
  228. sock.attach(ws);
  229. expect(WebSocket).to.not.have.been.called;
  230. });
  231. });
  232. describe('closing', function () {
  233. beforeEach(function () {
  234. sock.open('ws://localhost');
  235. sock._websocket.close = sinon.spy();
  236. });
  237. it('should close the actual websocket if it is open', function () {
  238. sock._websocket.readyState = WebSocket.OPEN;
  239. sock.close();
  240. expect(sock._websocket.close).to.have.been.calledOnce;
  241. });
  242. it('should close the actual websocket if it is connecting', function () {
  243. sock._websocket.readyState = WebSocket.CONNECTING;
  244. sock.close();
  245. expect(sock._websocket.close).to.have.been.calledOnce;
  246. });
  247. it('should not try to close the actual websocket if closing', function () {
  248. sock._websocket.readyState = WebSocket.CLOSING;
  249. sock.close();
  250. expect(sock._websocket.close).not.to.have.been.called;
  251. });
  252. it('should not try to close the actual websocket if closed', function () {
  253. sock._websocket.readyState = WebSocket.CLOSED;
  254. sock.close();
  255. expect(sock._websocket.close).not.to.have.been.called;
  256. });
  257. it('should reset onmessage to not call _recvMessage', function () {
  258. sinon.spy(sock, '_recvMessage');
  259. sock.close();
  260. sock._websocket.onmessage(null);
  261. try {
  262. expect(sock._recvMessage).not.to.have.been.called;
  263. } finally {
  264. sock._recvMessage.restore();
  265. }
  266. });
  267. });
  268. describe('event handlers', function () {
  269. beforeEach(function () {
  270. sock._recvMessage = sinon.spy();
  271. sock.on('open', sinon.spy());
  272. sock.on('close', sinon.spy());
  273. sock.on('error', sinon.spy());
  274. sock.open('ws://localhost');
  275. });
  276. it('should call _recvMessage on a message', function () {
  277. sock._websocket.onmessage(null);
  278. expect(sock._recvMessage).to.have.been.calledOnce;
  279. });
  280. it('should call the open event handler on opening', function () {
  281. sock._websocket.onopen();
  282. expect(sock._eventHandlers.open).to.have.been.calledOnce;
  283. });
  284. it('should call the close event handler on closing', function () {
  285. sock._websocket.onclose();
  286. expect(sock._eventHandlers.close).to.have.been.calledOnce;
  287. });
  288. it('should call the error event handler on error', function () {
  289. sock._websocket.onerror();
  290. expect(sock._eventHandlers.error).to.have.been.calledOnce;
  291. });
  292. });
  293. describe('ready state', function () {
  294. it('should be "unused" after construction', function () {
  295. let sock = new Websock();
  296. expect(sock.readyState).to.equal('unused');
  297. });
  298. it('should be "connecting" if WebSocket is connecting', function () {
  299. let sock = new Websock();
  300. let ws = new FakeWebSocket();
  301. ws.readyState = WebSocket.CONNECTING;
  302. sock.attach(ws);
  303. expect(sock.readyState).to.equal('connecting');
  304. });
  305. it('should be "open" if WebSocket is open', function () {
  306. let sock = new Websock();
  307. let ws = new FakeWebSocket();
  308. ws.readyState = WebSocket.OPEN;
  309. sock.attach(ws);
  310. expect(sock.readyState).to.equal('open');
  311. });
  312. it('should be "closing" if WebSocket is closing', function () {
  313. let sock = new Websock();
  314. let ws = new FakeWebSocket();
  315. ws.readyState = WebSocket.CLOSING;
  316. sock.attach(ws);
  317. expect(sock.readyState).to.equal('closing');
  318. });
  319. it('should be "closed" if WebSocket is closed', function () {
  320. let sock = new Websock();
  321. let ws = new FakeWebSocket();
  322. ws.readyState = WebSocket.CLOSED;
  323. sock.attach(ws);
  324. expect(sock.readyState).to.equal('closed');
  325. });
  326. it('should be "unknown" if WebSocket state is unknown', function () {
  327. let sock = new Websock();
  328. let ws = new FakeWebSocket();
  329. ws.readyState = 666;
  330. sock.attach(ws);
  331. expect(sock.readyState).to.equal('unknown');
  332. });
  333. it('should be "connecting" if RTCDataChannel is connecting', function () {
  334. let sock = new Websock();
  335. let ws = new FakeWebSocket();
  336. ws.readyState = 'connecting';
  337. sock.attach(ws);
  338. expect(sock.readyState).to.equal('connecting');
  339. });
  340. it('should be "open" if RTCDataChannel is open', function () {
  341. let sock = new Websock();
  342. let ws = new FakeWebSocket();
  343. ws.readyState = 'open';
  344. sock.attach(ws);
  345. expect(sock.readyState).to.equal('open');
  346. });
  347. it('should be "closing" if RTCDataChannel is closing', function () {
  348. let sock = new Websock();
  349. let ws = new FakeWebSocket();
  350. ws.readyState = 'closing';
  351. sock.attach(ws);
  352. expect(sock.readyState).to.equal('closing');
  353. });
  354. it('should be "closed" if RTCDataChannel is closed', function () {
  355. let sock = new Websock();
  356. let ws = new FakeWebSocket();
  357. ws.readyState = 'closed';
  358. sock.attach(ws);
  359. expect(sock.readyState).to.equal('closed');
  360. });
  361. it('should be "unknown" if RTCDataChannel state is unknown', function () {
  362. let sock = new Websock();
  363. let ws = new FakeWebSocket();
  364. ws.readyState = 'foobar';
  365. sock.attach(ws);
  366. expect(sock.readyState).to.equal('unknown');
  367. });
  368. });
  369. after(function () {
  370. // eslint-disable-next-line no-global-assign
  371. WebSocket = oldWS;
  372. });
  373. });
  374. describe('WebSocket Receiving', function () {
  375. let sock;
  376. beforeEach(function () {
  377. sock = new Websock();
  378. sock._allocateBuffers();
  379. });
  380. it('should support adding binary Uint8Array data to the receive queue', function () {
  381. const msg = { data: new Uint8Array([1, 2, 3]) };
  382. sock._mode = 'binary';
  383. sock._recvMessage(msg);
  384. expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03');
  385. });
  386. it('should call the message event handler if present', function () {
  387. sock._eventHandlers.message = sinon.spy();
  388. const msg = { data: new Uint8Array([1, 2, 3]).buffer };
  389. sock._mode = 'binary';
  390. sock._recvMessage(msg);
  391. expect(sock._eventHandlers.message).to.have.been.calledOnce;
  392. });
  393. it('should not call the message event handler if there is nothing in the receive queue', function () {
  394. sock._eventHandlers.message = sinon.spy();
  395. const msg = { data: new Uint8Array([]).buffer };
  396. sock._mode = 'binary';
  397. sock._recvMessage(msg);
  398. expect(sock._eventHandlers.message).not.to.have.been.called;
  399. });
  400. it('should compact the receive queue when a message handler empties it', function () {
  401. sock._eventHandlers.message = () => { sock.rQi = sock._rQlen; };
  402. sock._rQ = new Uint8Array([0, 1, 2, 3, 4, 5, 0, 0, 0, 0]);
  403. sock._rQlen = 6;
  404. sock.rQi = 6;
  405. const msg = { data: new Uint8Array([1, 2, 3]).buffer };
  406. sock._mode = 'binary';
  407. sock._recvMessage(msg);
  408. expect(sock._rQlen).to.equal(0);
  409. expect(sock.rQi).to.equal(0);
  410. });
  411. it('should compact the receive queue when we reach the end of the buffer', function () {
  412. sock._rQ = new Uint8Array(20);
  413. sock._rQbufferSize = 20;
  414. sock._rQlen = 20;
  415. sock.rQi = 10;
  416. const msg = { data: new Uint8Array([1, 2]).buffer };
  417. sock._mode = 'binary';
  418. sock._recvMessage(msg);
  419. expect(sock._rQlen).to.equal(12);
  420. expect(sock.rQi).to.equal(0);
  421. });
  422. it('should automatically resize the receive queue if the incoming message is larger than the buffer', function () {
  423. sock._rQ = new Uint8Array(20);
  424. sock._rQlen = 0;
  425. sock.rQi = 0;
  426. sock._rQbufferSize = 20;
  427. const msg = { data: new Uint8Array(30).buffer };
  428. sock._mode = 'binary';
  429. sock._recvMessage(msg);
  430. expect(sock._rQlen).to.equal(30);
  431. expect(sock.rQi).to.equal(0);
  432. expect(sock._rQ.length).to.equal(240); // keep the invariant that rQbufferSize / 8 >= rQlen
  433. });
  434. it('should automatically resize the receive queue if the incoming message is larger than 1/8th of the buffer and we reach the end of the buffer', function () {
  435. sock._rQ = new Uint8Array(20);
  436. sock._rQlen = 16;
  437. sock.rQi = 16;
  438. sock._rQbufferSize = 20;
  439. const msg = { data: new Uint8Array(6).buffer };
  440. sock._mode = 'binary';
  441. sock._recvMessage(msg);
  442. expect(sock._rQlen).to.equal(6);
  443. expect(sock.rQi).to.equal(0);
  444. expect(sock._rQ.length).to.equal(48);
  445. });
  446. });
  447. describe('Data encoding', function () {
  448. before(function () { FakeWebSocket.replace(); });
  449. after(function () { FakeWebSocket.restore(); });
  450. describe('as binary data', function () {
  451. let sock;
  452. beforeEach(function () {
  453. sock = new Websock();
  454. sock.open('ws://', 'binary');
  455. sock._websocket._open();
  456. });
  457. it('should only send the send queue up to the send queue length', function () {
  458. sock._sQ = new Uint8Array([1, 2, 3, 4, 5]);
  459. sock._sQlen = 3;
  460. const res = sock._encodeMessage();
  461. expect(res).to.array.equal(new Uint8Array([1, 2, 3]));
  462. });
  463. it('should properly pass the encoded data off to the actual WebSocket', function () {
  464. sock.send([1, 2, 3]);
  465. expect(sock._websocket._getSentData()).to.array.equal(new Uint8Array([1, 2, 3]));
  466. });
  467. });
  468. });
  469. });