From d96831de1920f0f7a771f419ae4c3c994305efbf Mon Sep 17 00:00:00 2001 From: Roy Hyunjin Han Date: Tue, 17 Feb 2015 16:40:43 -0500 Subject: [PATCH] Restore proxy server --- .travis.yml | 6 ++++-- README.rst | 8 ++++++-- socketIO_client/tests/proxy.js | 30 ++++++++++++++++++++++++++++++ socketIO_client/tests/serve.js | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 socketIO_client/tests/proxy.js diff --git a/.travis.yml b/.travis.yml index e6415c3..af9d4b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,12 +7,14 @@ before_install: - sudo apt-get update - sudo apt-get install nodejs install: - - npm install -G socket.io@0.9 + - npm install -G socket.io + - npm install -G http-proxy - pip install -U requests - pip install -U six - pip install -U websocket-client - pip install -U coverage before_script: - - node serve-tests.js & + - DEBUG=* node tests/serve.js & + - DEBUG=* node tests/proxy.js & - sleep 3 script: nosetests diff --git a/README.rst b/README.rst index 9dab304..9031bbe 100644 --- a/README.rst +++ b/README.rst @@ -30,10 +30,14 @@ Activate isolated environment. :: VIRTUAL_ENV=$HOME/.virtualenv source $VIRTUAL_ENV/bin/activate -Launch a socket.io server. :: +Launch your server. :: + # Get package folder PACKAGE_FOLDER=`python -c "import os, socketIO_client; print(os.path.dirname(socketIO_client.__file__))"` - node $PACKAGE_FOLDER/tests/serve.js + # Start socket.io server + DEBUG=* node $PACKAGE_FOLDER/tests/serve.js + # Start proxy server in a separate terminal on the same machine + DEBUG=* node $PACKAGE_FOLDER/tests/proxy.js For debugging information, run these commands first. :: diff --git a/socketIO_client/tests/proxy.js b/socketIO_client/tests/proxy.js new file mode 100644 index 0000000..178c752 --- /dev/null +++ b/socketIO_client/tests/proxy.js @@ -0,0 +1,30 @@ +var proxy = require('http-proxy').createProxyServer({ + target: {host: 'localhost', port: 9000} +}); +var server = require('http').createServer(function(req, res) { + console.log('[REQUEST.%s] %s', req.method, req.url); + console.log(req['headers']); + if (req.method == 'POST') { + var body = ''; + req.on('data', function (data) { + body += data; + }); + req.on('end', function () { + print_body('[REQUEST.BODY] ', body); + }); + } + var write = res.write; + res.write = function(data) { + print_body('[RESPONSE.BODY] ', data); + write.call(res, data); + } + proxy.web(req, res); +}); +function print_body(header, body) { + var text = String(body); + console.log(header + text); + for (var i = 0; i < text.length; i++) { + console.log('body[%s] = %s = %s', i, text[i], text.charCodeAt(i)); + } +} +server.listen(8000); diff --git a/socketIO_client/tests/serve.js b/socketIO_client/tests/serve.js index 7dd9232..c3e10c8 100644 --- a/socketIO_client/tests/serve.js +++ b/socketIO_client/tests/serve.js @@ -1,6 +1,6 @@ // DEBUG=* node serve.js -var app = require('http').createServer(serve).listen(8000); +var app = require('http').createServer(serve).listen(9000); var io = require('socket.io')(app); var fs = require('fs'); var PAYLOAD = {'xxx': 'yyy'};