Permalink
Browse files

[test] Increase code coverage

1 parent 52d7842 commit 105374c94e2e08807f58ada9fe00fe0f4e0a2415 @lpinca lpinca committed Mar 28, 2017
Showing with 84 additions and 38 deletions.
  1. +1 −1 lib/BufferUtil.js
  2. +1 −1 lib/Validation.js
  3. +52 −36 test/Sender.test.js
  4. +30 −0 test/WebSocket.test.js
View
@@ -31,7 +31,7 @@ try {
const bufferUtil = require('bufferutil');
module.exports = Object.assign({ concat }, bufferUtil.BufferUtil || bufferUtil);
-} catch (e) {
+} catch (e) /* istanbul ignore next */ {
/**
* Masks a buffer using the given mask.
*
View
@@ -12,6 +12,6 @@ try {
module.exports = typeof isValidUTF8 === 'object'
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
: isValidUTF8;
-} catch (e) {
+} catch (e) /* istanbul ignore next */ {
module.exports = () => true;
}
View
@@ -34,42 +34,6 @@ describe('Sender', function () {
});
});
- describe('#ping', function () {
- it('works with multiple types of data', function (done) {
- let count = 0;
- const sender = new Sender({
- write: (data) => {
- assert.ok(data.equals(Buffer.from([0x89, 0x02, 0x68, 0x69])));
- if (++count === 3) done();
- }
- });
-
- const array = new Uint8Array([0x68, 0x69]);
-
- sender.ping(array.buffer, false);
- sender.ping(array, false);
- sender.ping('hi', false);
- });
- });
-
- describe('#pong', function () {
- it('works with multiple types of data', function (done) {
- let count = 0;
- const sender = new Sender({
- write: (data) => {
- assert.ok(data.equals(Buffer.from([0x8a, 0x02, 0x68, 0x69])));
- if (++count === 3) done();
- }
- });
-
- const array = new Uint8Array([0x68, 0x69]);
-
- sender.pong(array.buffer, false);
- sender.pong(array, false);
- sender.pong('hi', false);
- });
- });
-
describe('#send', function () {
it('compresses data if compress option is enabled', function (done) {
const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
@@ -228,6 +192,58 @@ describe('Sender', function () {
});
});
+ describe('#ping', function () {
+ it('works with multiple types of data', function (done) {
+ const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
+ let count = 0;
+ const sender = new Sender({
+ write: (data) => {
+ if (++count === 1) return;
+
+ assert.ok(data.equals(Buffer.from([0x89, 0x02, 0x68, 0x69])));
+ if (count === 4) done();
+ }
+ }, {
+ 'permessage-deflate': perMessageDeflate
+ });
+
+ perMessageDeflate.accept([{}]);
+
+ const array = new Uint8Array([0x68, 0x69]);
+
+ sender.send('foo', { compress: true, fin: true });
+ sender.ping(array.buffer, false);
+ sender.ping(array, false);
+ sender.ping('hi', false);
+ });
+ });
+
+ describe('#pong', function () {
+ it('works with multiple types of data', function (done) {
+ const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
+ let count = 0;
+ const sender = new Sender({
+ write: (data) => {
+ if (++count === 1) return;
+
+ assert.ok(data.equals(Buffer.from([0x8a, 0x02, 0x68, 0x69])));
+ if (count === 4) done();
+ }
+ }, {
+ 'permessage-deflate': perMessageDeflate
+ });
+
+ perMessageDeflate.accept([{}]);
+
+ const array = new Uint8Array([0x68, 0x69]);
+
+ sender.send('foo', { compress: true, fin: true });
+ sender.pong(array.buffer, false);
+ sender.pong(array, false);
+ sender.pong('hi', false);
+ });
+ });
+
describe('#close', function () {
it('should consume all data before closing', function (done) {
const perMessageDeflate = new PerMessageDeflate({ threshold: 0 });
@@ -1129,6 +1129,21 @@ describe('WebSocket', function () {
});
});
});
+
+ it('does nothing if the connection is already CLOSED', function (done) {
+ const wss = new WebSocketServer({ port: ++port }, () => {
+ const ws = new WebSocket(`ws://localhost:${port}`);
+
+ ws.on('close', (code) => {
+ assert.strictEqual(code, 1000);
+ assert.strictEqual(ws.readyState, WebSocket.CLOSED);
+ ws.close();
+ wss.close(done);
+ });
+ });
+
+ wss.on('connection', (ws) => ws.close());
+ });
});
describe('#terminate', function () {
@@ -1174,6 +1189,21 @@ describe('WebSocket', function () {
ws.on('close', () => done());
});
});
+
+ it('does nothing if the connection is already CLOSED', function (done) {
+ const wss = new WebSocketServer({ port: ++port }, () => {
+ const ws = new WebSocket(`ws://localhost:${port}`);
+
+ ws.on('close', (code) => {
+ assert.strictEqual(code, 1006);
+ assert.strictEqual(ws.readyState, WebSocket.CLOSED);
+ ws.terminate();
+ wss.close(done);
+ });
+ });
+
+ wss.on('connection', (ws) => ws.terminate());
+ });
});
describe('WHATWG API emulation', function () {

0 comments on commit 105374c

Please sign in to comment.