diff options
| author | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2012-09-24 16:22:07 -0400 |
| commit | 686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch) | |
| tree | a5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/socket.io/support/node-websocket-client/test/test-basic.js | |
ok
Diffstat (limited to 'node_modules/socket.io/support/node-websocket-client/test/test-basic.js')
| -rw-r--r-- | node_modules/socket.io/support/node-websocket-client/test/test-basic.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/node_modules/socket.io/support/node-websocket-client/test/test-basic.js b/node_modules/socket.io/support/node-websocket-client/test/test-basic.js new file mode 100644 index 0000000..f010424 --- /dev/null +++ b/node_modules/socket.io/support/node-websocket-client/test/test-basic.js @@ -0,0 +1,68 @@ +// Verify that we can connect to a WebSocket server, exchange messages, and +// shut down cleanly. + +var assert = require('assert'); +var WebSocket = require('../lib/websocket').WebSocket; +var WebSocketServer = require('websocket-server/ws/server').Server; + +var PORT = 1024 + Math.floor(Math.random() * 4096); +var C_MSG = 'Client test: ' + (Math.random() * 100); +var S_MSG = 'Server test: ' + (Math.random() * 100); + +var serverGotConnection = false; +var clientGotOpen = false; +var clientGotData = false; +var clientGotMessage = false; +var serverGotMessage = false; +var serverGotClose = false; +var clientGotClose = false; + +var wss = new WebSocketServer(); +wss.listen(PORT, 'localhost'); +wss.on('connection', function(c) { + serverGotConnection = true; + + c.on('message', function(m) { + assert.equal(m, C_MSG); + serverGotMessage = true; + + c.close(); + }); + + c.on('close', function() { + serverGotClose = true; + wss.close(); + }); + + c.write(S_MSG); +}); + +var ws = new WebSocket('ws://localhost:' + PORT + '/', 'biff'); +ws.on('open', function() { + clientGotOpen = true; +}); +ws.on('data', function(buf) { + assert.equal(typeof buf, 'object'); + assert.equal(buf.toString('utf8'), S_MSG); + + clientGotData = true; + + ws.send(C_MSG); +}); +ws.onmessage = function(m) { + assert.deepEqual(m, {data : S_MSG}); + clientGotMessage = true; +}; +ws.onclose = function() { + clientGotClose = true; +}; + +process.on('exit', function() { + assert.ok(serverGotConnection); + assert.ok(clientGotOpen); + assert.ok(clientGotData); + assert.ok(clientGotMessage); + assert.ok(serverGotMessage); + assert.ok(serverGotClose); + assert.ok(clientGotClose); +}); |
