Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix(WebSocket): patch WebSocket instance
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Feb 4, 2015
1 parent 4a48f96 commit 7b8e1e6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
86 changes: 86 additions & 0 deletions test/patch/WebSocket.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

describe('WebSocket', function () {
var socket,
TEST_SERVER_URL = 'ws://localhost:8001',
flag;

beforeEach(function (done) {
socket = new WebSocket(TEST_SERVER_URL);
socket.addEventListener('open', done);
});

afterEach(function () {
socket.close();
});

it('should work with addEventListener', function () {
var parent = window.zone;

socket.addEventListener('message', function (contents) {
expect(window.zone.parent).toBe(parent);
expect(contents).toBe('HI');
done();
});
socket.send('hi');
});

it('should respect removeEventListener', function (done) {
var log = '';
function logOnMessage () {
log += 'a';

expect(log).toEqual('a');

socket.removeEventListener('message', logOnMessage);
socket.send('hi');

setTimeout(function () {
expect(log).toEqual('a');
done();
}, 10);
};

socket.addEventListener('message', logOnMessage);
socket.send('hi');
});

it('should work with onmessage', function (done) {
var parent = window.zone;
socket.onmessage = function (contents) {
expect(window.zone.parent).toBe(parent);
expect(contents.data).toBe('hi');
done();
};
socket.send('hi');
});


it('should only allow one onmessage handler', function (done) {
var log = '';
socket.onmessage = function () {
log += 'a';
expect(log).toEqual('b');
done();
};
socket.onmessage = function () {
log += 'b';
expect(log).toEqual('b');
done();
};

socket.send('hi');
});

it('should handle removing onmessage', function () {
var log = '';
socket.onmessage = function () {
log += 'a';
};
socket.onmessage = null;

socket.send('hi');
expect(log).toEqual('');
});

});
8 changes: 8 additions & 0 deletions test/ws-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var ws = require('nodejs-websocket');

// simple echo server
ws.createServer(function (conn) {
conn.on('text', function (str) {
conn.sendText(str.toString());
});
}).listen(8001);
4 changes: 2 additions & 2 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ Zone.patchViaCapturingAllTheEvents = function () {
});
};


// we have to patch the instance since the proto is non-configurable
Zone.patchWebSocket = function() {
var WS = window.WebSocket;
window.WebSocket = function(a, b) {
var socket = arguments.length > 1 ? new WS(a, b) : new WS(a);
Zone.patchProperties(socket, ['onmessage']);
Zone.patchProperties(socket, ['onclose', 'onerror', 'onmessage', 'onopen']);
return socket;
};
}
Expand Down

0 comments on commit 7b8e1e6

Please sign in to comment.