This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(WebSocket): patch WebSocket instance
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters