From 73d1082e5fd194000c613bf6bfa9587d5ca1e82a Mon Sep 17 00:00:00 2001 From: Tomasz Janczuk Date: Tue, 11 Sep 2012 16:50:23 -0700 Subject: [PATCH] websocket echo test --- test/functional/scripts/setup.bat | 6 +++ test/functional/tests/131_websocket_echo.js | 52 +++++++++++++++++++ .../www/130_websocket_onetwothree/server.js | 1 - .../www/131_websocket_echo/server.js | 23 ++++++++ .../www/131_websocket_echo/web.config | 8 +++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/functional/tests/131_websocket_echo.js create mode 100644 test/functional/www/131_websocket_echo/server.js create mode 100644 test/functional/www/131_websocket_echo/web.config diff --git a/test/functional/scripts/setup.bat b/test/functional/scripts/setup.bat index b6fae4f3..a5aa767a 100644 --- a/test/functional/scripts/setup.bat +++ b/test/functional/scripts/setup.bat @@ -58,6 +58,12 @@ if %ERRORLEVEL% neq 0 if %ERRORLEVEL% neq 183 ( exit /b -1 ) +%appcmd% add app /site.name:%site% /path:/131_websocket_echo /physicalPath:"%www%\131_websocket_echo" /applicationPool:%apppool% >> %log% +if %ERRORLEVEL% neq 0 if %ERRORLEVEL% neq 183 ( + echo FAILED. Cannot add app 131_websocket_echo to site %site%. + exit /b -1 +) + set RETRY=0 :retry diff --git a/test/functional/tests/131_websocket_echo.js b/test/functional/tests/131_websocket_echo.js new file mode 100644 index 00000000..d5c87147 --- /dev/null +++ b/test/functional/tests/131_websocket_echo.js @@ -0,0 +1,52 @@ +/* +A WebSocket connection receives 3 messages from the server +*/ + +var WebSocket = require('faye-websocket') + , iisnodeassert = require('iisnodeassert') + , assert = require('assert'); + +var lines = ['The', 'cat', 'in', 'the', 'hat']; +var address = iisnodeassert.getAddress('ws', '/131_websocket_echo/server.js'); +//var address = 'ws://localhost:8888/'; +var ws = new WebSocket.Client(address); +var receiveIndex = 0; + +var timeout = setTimeout(function () { + assert.fail('10s', '< 10s', 'The messages had not been echoed within 10s'); +}, 10000); + +ws.onmessage = function (event) { + console.log('onmessage: ' + event.data); + assert.strictEqual(lines[receiveIndex], event.data); + receiveIndex++; + if (receiveIndex === lines.length) + ws.close(); +}; + +ws.onclose = function (event) { + console.log('onclose'); + clearTimeout(timeout); + assert.strictEqual(lines.length, receiveIndex, 'All messages were received'); +}; + +ws.onerror = function (error) { + console.log('onerror'); + assert.ifError(error); +}; + +ws.onopen = function () { + console.log('onopen'); + var scheduleSendLine = function (line) { + if (line < lines.length) + setTimeout(function () { + console.log('sending ' + lines[line]); + ws.send(lines[line]); + scheduleSendLine(line + 1); + }, 200); + }; + + console.log('sending ' + lines[0]); + ws.send(lines[0]); + scheduleSendLine(1); +}; \ No newline at end of file diff --git a/test/functional/www/130_websocket_onetwothree/server.js b/test/functional/www/130_websocket_onetwothree/server.js index ddb90a79..5e268f82 100644 --- a/test/functional/www/130_websocket_onetwothree/server.js +++ b/test/functional/www/130_websocket_onetwothree/server.js @@ -10,7 +10,6 @@ var server = http.createServer(function (req, res) { }); server.addListener('upgrade', function (request, socket, head) { - console.log(request.headers); var ws = new WebSocket(request, socket, head); function schedule(line) { diff --git a/test/functional/www/131_websocket_echo/server.js b/test/functional/www/131_websocket_echo/server.js new file mode 100644 index 00000000..c061bad3 --- /dev/null +++ b/test/functional/www/131_websocket_echo/server.js @@ -0,0 +1,23 @@ + +var WebSocket = require('faye-websocket') + , http = require('http'); + +var server = http.createServer(function (req, res) { + res.writeHead(400); + res.end(); +}); + +server.addListener('upgrade', function (request, socket, head) { + var ws = new WebSocket(request, socket, head); + + ws.onmessage = function (event) { + if (ws) + ws.send(event.data); + }; + + ws.onclose = function (event) { + ws = null; + }; +}); + +server.listen(process.env.PORT || 8888); diff --git a/test/functional/www/131_websocket_echo/web.config b/test/functional/www/131_websocket_echo/web.config new file mode 100644 index 00000000..49c5e5db --- /dev/null +++ b/test/functional/www/131_websocket_echo/web.config @@ -0,0 +1,8 @@ + + + + + + + +