From f1aeb0500cde39b63e570323e0e478530d1222ab Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 15:26:44 -0500 Subject: [PATCH 01/11] [misc] use the local mocha instead the global --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0f556b1..f11557c 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ }, "scripts" : { "blanket" : { "pattern": "caronte/lib" }, - "test" : "mocha -R landing test/*-test.js", - "test-cov" : "mocha --require blanket -R html-cov > cov/coverage.html" + "test" : "./node_modules/.bin/mocha -R landing test/*-test.js", + "test-cov" : "./node_modules/.bin/mocha --require blanket -R html-cov > cov/coverage.html" }, "engines" : { From 275a5192fa257f78287a954b347e65023795487d Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 16:49:51 -0500 Subject: [PATCH 02/11] [fix] typo --- lib/caronte/passes/ws-incoming.js | 2 +- ...sses-web-test.js => lib-caronte-passes-web-incoming-test.js} | 0 ...passes-ws-test.js => lib-caronte-passes-ws-incoming-test.js} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename test/{lib-caronte-passes-web-test.js => lib-caronte-passes-web-incoming-test.js} (100%) rename test/{lib-caronte-passes-ws-test.js => lib-caronte-passes-ws-incoming-test.js} (100%) diff --git a/lib/caronte/passes/ws-incoming.js b/lib/caronte/passes/ws-incoming.js index 3b5e1f5..0adab3d 100644 --- a/lib/caronte/passes/ws-incoming.js +++ b/lib/caronte/passes/ws-incoming.js @@ -55,7 +55,7 @@ function XHeaders(req, socket, options) { if(!options.xfwd) return; var values = { - for : req.connection.remoteAddsockets || req.socket.remoteAddsockets, + for : req.connection.remoteAddress || req.socket.remoteAddress, port : req.connection.remotePort || req.socket.remotePort, proto: req.connection.pair ? 'wss' : 'ws' }; diff --git a/test/lib-caronte-passes-web-test.js b/test/lib-caronte-passes-web-incoming-test.js similarity index 100% rename from test/lib-caronte-passes-web-test.js rename to test/lib-caronte-passes-web-incoming-test.js diff --git a/test/lib-caronte-passes-ws-test.js b/test/lib-caronte-passes-ws-incoming-test.js similarity index 100% rename from test/lib-caronte-passes-ws-test.js rename to test/lib-caronte-passes-ws-incoming-test.js From 5bb83b967edb514402698eecfe3db7ab5fe60b06 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 16:50:30 -0500 Subject: [PATCH 03/11] [tests] make the tests run with the last refactor --- test/lib-caronte-passes-web-incoming-test.js | 2 +- test/lib-caronte-passes-ws-incoming-test.js | 150 +++++++++++++------ 2 files changed, 105 insertions(+), 47 deletions(-) diff --git a/test/lib-caronte-passes-web-incoming-test.js b/test/lib-caronte-passes-web-incoming-test.js index 4ec5ca1..cc0a760 100644 --- a/test/lib-caronte-passes-web-incoming-test.js +++ b/test/lib-caronte-passes-web-incoming-test.js @@ -1,4 +1,4 @@ -var caronte = require('../lib/caronte/passes/web'), +var caronte = require('../lib/caronte/passes/web-incoming'), expect = require('expect.js'); describe('lib/caronte/passes/web.js', function() { diff --git a/test/lib-caronte-passes-ws-incoming-test.js b/test/lib-caronte-passes-ws-incoming-test.js index f794cca..2c077d4 100644 --- a/test/lib-caronte-passes-ws-incoming-test.js +++ b/test/lib-caronte-passes-ws-incoming-test.js @@ -1,87 +1,145 @@ -var caronte = require('../lib/caronte/passes/ws'), +var caronte = require('../lib/caronte/passes/ws-incoming'), expect = require('expect.js'); -describe('lib/caronte/passes/ws.js', function () { +describe('lib/caronte/passes/ws-incoming.js', function () { describe('#checkMethodAndHeader', function () { it('should drop non-GET connections', function () { - var endCalled = false, + var destroyCalled = false, stubRequest = { method: 'DELETE', - headers: {}, - end: function () { - // Simulate Stream.end() method when call - endCalled = true; - } + headers: {} }, - returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {}); + stubSocket = { + destroy: function () { + // Simulate Socket.destroy() method when call + destroyCalled = true; + } + } + returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); - expect(endCalled).to.be(true); + expect(destroyCalled).to.be(true); }) it('should drop connections when no upgrade header', function () { - var endCalled = false, + var destroyCalled = false, stubRequest = { method: 'GET', - headers: {}, - end: function () { - // Simulate Stream.end() method when call - endCalled = true; - } + headers: {} }, - returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {}); + stubSocket = { + destroy: function () { + // Simulate Socket.destroy() method when call + destroyCalled = true; + } + } + returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); - expect(endCalled).to.be(true); + expect(destroyCalled).to.be(true); }) it('should drop connections when upgrade header is different of `websocket`', function () { - var endCalled = false, + var destroyCalled = false, stubRequest = { method: 'GET', headers: { upgrade: 'anotherprotocol' - }, - end: function () { - // Simulate Stream.end() method when call - endCalled = true; } }, - returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {}); + stubSocket = { + destroy: function () { + // Simulate Socket.destroy() method when call + destroyCalled = true; + } + } + returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(true); - expect(endCalled).to.be(true); + expect(destroyCalled).to.be(true); }) it('should return nothing when all is ok', function () { - var endCalled = false, + var destroyCalled = false, stubRequest = { method: 'GET', headers: { upgrade: 'websocket' - }, - end: function () { - // Simulate Stream.end() method when call - endCalled = true; } }, - returnValue = caronte.checkMethodAndHeader(stubRequest, {}, {}); + stubSocket = { + destroy: function () { + // Simulate Socket.destroy() method when call + destroyCalled = true; + } + } + returnValue = caronte.checkMethodAndHeader(stubRequest, stubSocket); expect(returnValue).to.be(undefined); - expect(endCalled).to.be(false); + expect(destroyCalled).to.be(false); }) }); - describe('#XHeaders', function () { - // var stubRequest = { - // connection: { - // remoteAddress: '192.168.1.2', - // remotePort: '8080' - // }, - // headers: {} - // } + describe('#setupSocket', function () { + it('Set the correct config to the socket', function () { + var stubSocket = { + setTimeout: function (num) { + // Simulate Socket.setTimeout() + socketConfig.timeout = num; + }, + setNoDelay: function (bol) { + // Simulate Socket.setNoDelay() + socketConfig.nodelay = bol; + }, + setKeepAlive: function (bol) { + // Simulate Socket.setKeepAlive() + socketConfig.keepalive = bol; + } + }, + socketConfig = { + timeout: null, + nodelay: false, + keepalive: false + }, + returnValue = caronte.setupSocket({}, stubSocket); + expect(returnValue).to.be(undefined); + expect(socketConfig.timeout).to.eql(0); + expect(socketConfig.nodelay).to.eql(true); + expect(socketConfig.keepalive).to.eql(true); + }); + }); - // it('set the correct x-forwarded-* headers', function () { - // caronte.XHeaders(stubRequest, {}, { xfwd: true }); - // expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2'); - // expect(stubRequest.headers['x-forwarded-port']).to.be('8080'); - // expect(stubRequest.headers['x-forwarded-proto']).to.be('http'); - // }); + describe('#XHeaders', function () { + it('return if no forward request', function () { + var returnValue = caronte.XHeaders({}, {}, {}); + expect(returnValue).to.be(undefined); + }); + + it('set the correct x-forwarded-* headers from req.connection', function () { + var stubRequest = { + connection: { + remoteAddress: '192.168.1.2', + remotePort: '8080' + }, + headers: {} + } + caronte.XHeaders(stubRequest, {}, { xfwd: true }); + expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2'); + expect(stubRequest.headers['x-forwarded-port']).to.be('8080'); + expect(stubRequest.headers['x-forwarded-proto']).to.be('ws'); + }); + + it('set the correct x-forwarded-* headers from req.socket', function () { + var stubRequest = { + socket: { + remoteAddress: '192.168.1.3', + remotePort: '8181' + }, + connection: { + pair: true + }, + headers: {} + }; + caronte.XHeaders(stubRequest, {}, { xfwd: true }); + expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3'); + expect(stubRequest.headers['x-forwarded-port']).to.be('8181'); + expect(stubRequest.headers['x-forwarded-proto']).to.be('wss'); + }); }); }); From 1cb967b90aaa5b9da57727b8acbd95108437797a Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 17:27:06 -0500 Subject: [PATCH 04/11] [tests] fixed according new refactor and added test to common.setupSocket() --- test/lib-caronte-common-test.js | 66 ++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/test/lib-caronte-common-test.js b/test/lib-caronte-common-test.js index 3c2ccd1..1523cff 100644 --- a/test/lib-caronte-common-test.js +++ b/test/lib-caronte-common-test.js @@ -1,9 +1,9 @@ var common = require('../lib/caronte/common'), expect = require('expect.js'); -describe('lib/caronte/common.js', function() { - describe('#setupOutgoing', function() { - it('should setup the right headers', function() { +describe('lib/caronte/common.js', function () { + describe('#setupOutgoing', function () { + it('should setup the correct headers', function () { var outgoing = {}; common.setupOutgoing(outgoing, { @@ -17,7 +17,7 @@ describe('lib/caronte/common.js', function() { }, { method : 'i', - path : 'am', + url : 'am', headers : 'proxy' }); @@ -25,11 +25,67 @@ describe('lib/caronte/common.js', function() { expect(outgoing.hostname).to.eql('how'); expect(outgoing.socketPath).to.eql('are'); expect(outgoing.port).to.eql('you'); - //expect(outgoing.agent).to.eql('?'); + expect(outgoing.agent).to.eql('?'); expect(outgoing.method).to.eql('i'); expect(outgoing.path).to.eql('am'); expect(outgoing.headers).to.eql('proxy') }); + + it('set the port according to the protocol', function () { + var outgoing = {}; + common.setupOutgoing(outgoing, + { + target: { + host : 'how', + hostname : 'are', + socketPath: 'you', + agent : '?', + protocol: 'https:' + } + }, + { + method : 'i', + url : 'am', + headers : 'proxy' + }); + + expect(outgoing.host).to.eql('how'); + expect(outgoing.hostname).to.eql('are'); + expect(outgoing.socketPath).to.eql('you'); + expect(outgoing.agent).to.eql('?'); + + expect(outgoing.method).to.eql('i'); + expect(outgoing.path).to.eql('am'); + expect(outgoing.headers).to.eql('proxy') + + expect(outgoing.port).to.eql(443); + }); + }); + + describe('#setupSocket', function () { + it('should setup a socket', function () { + var socketConfig = { + timeout: null, + nodelay: false, + keepalive: false + }, + stubSocket = { + setTimeout: function (num) { + socketConfig.timeout = num; + }, + setNoDelay: function (bol) { + socketConfig.nodelay = bol; + }, + setKeepAlive: function (bol) { + socketConfig.keepalive = bol; + } + } + returnValue = common.setupSocket(stubSocket); + + expect(socketConfig.timeout).to.eql(0); + expect(socketConfig.nodelay).to.eql(true); + expect(socketConfig.keepalive).to.eql(true); + }); }); }); \ No newline at end of file From dc9d7e5452c7d39ae1d242cb8021ca75e4f736d4 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 18:59:48 -0500 Subject: [PATCH 05/11] [tests] drop the test of own streams, moved the usable tests --- test/lib-caronte-streams-forward-test.js | 45 ------- test/lib-caronte-test.js | 154 ++++++++++++++++++++++- 2 files changed, 153 insertions(+), 46 deletions(-) delete mode 100644 test/lib-caronte-streams-forward-test.js diff --git a/test/lib-caronte-streams-forward-test.js b/test/lib-caronte-streams-forward-test.js deleted file mode 100644 index 5289856..0000000 --- a/test/lib-caronte-streams-forward-test.js +++ /dev/null @@ -1,45 +0,0 @@ -var caronte = require('../'), - ForwardStream = require('../lib/caronte/streams/forward'); - expect = require('expect.js'), - Writable = require('stream').Writable, - http = require('http'); - - -describe('lib/caronte/streams/forward.js', function () { - describe('forward stream constructor', function () { - it('should be an instance of Writable stream and get the correct options and methods', function () { - var stubOptions = { - key: 'value' - }; - var forwardProxy = new ForwardStream(stubOptions); - - expect(forwardProxy).to.be.a(Writable); - expect(forwardProxy.options).to.eql({ key: 'value' }); - expect(forwardProxy.onPipe).to.be.a('function'); - expect(forwardProxy.onFinish).to.be.a('function'); - expect(forwardProxy._events).to.have.property('pipe'); - expect(forwardProxy._events).to.have.property('finish'); - }); - }); - - describe('should pipe the request and finish it', function () { - it('should make the request on pipe and finish it', function(done) { - var result; - - var p = caronte.createProxyServer({ - forward: 'http://127.0.0.1:8080' - }).listen('8081') - - var s = http.createServer(function(req, res) { - expect(req.method).to.eql('GET'); - s.close(); - p.close(); - done(); - }); - - s.listen('8080'); - - http.request('http://127.0.0.1:8081', function() {}).end(); - }); - }); -}); diff --git a/test/lib-caronte-test.js b/test/lib-caronte-test.js index 8358c5f..dbb278f 100644 --- a/test/lib-caronte-test.js +++ b/test/lib-caronte-test.js @@ -1,5 +1,6 @@ var caronte = require('../lib/caronte'), - expect = require('expect.js'); + expect = require('expect.js'), + http = require('http'); describe('lib/caronte.js', function() { describe('#createProxyServer', function() { @@ -24,4 +25,155 @@ describe('lib/caronte.js', function() { expect(obj.listen).to.be.a(Function); }); }); + + describe('#createProxyServer with forward options and using web-incoming passes', function () { + it('should pipe the request using web-incoming#stream method', function (done) { + var proxy = caronte.createProxyServer({ + forward: 'http://127.0.0.1:8080' + }).listen('8081') + + var source = http.createServer(function(req, res) { + expect(req.method).to.eql('GET'); + expect(req.headers.host.split(':')[1]).to.eql('8081'); + source.close(); + proxy.close(); + done(); + }); + + source.listen('8080'); + + http.request('http://127.0.0.1:8081', function() {}).end(); + }) + }); + + describe('#createProxyServer using the web-incoming passes', function () { + it('should make the request on pipe and finish it', function(done) { + var proxy = caronte.createProxyServer({ + target: 'http://127.0.0.1:8080' + }).listen('8081'); + + var source = http.createServer(function(req, res) { + expect(req.method).to.eql('POST'); + expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); + expect(req.headers.host.split(':')[1]).to.eql('8081'); + source.close(); + proxy.close(); + done(); + }); + + source.listen('8080'); + + http.request({ + hostname: '127.0.0.1', + port: '8081', + method: 'POST', + headers: { + 'x-forwarded-for': '127.0.0.1' + } + }, function() {}).end(); + }); + }); + + describe('#createProxyServer using the web-incoming passes', function () { + it('should make the request, handle response and finish it', function(done) { + var proxy = caronte.createProxyServer({ + target: 'http://127.0.0.1:8080' + }).listen('8081'); + + var source = http.createServer(function(req, res) { + expect(req.method).to.eql('GET'); + expect(req.headers.host.split(':')[1]).to.eql('8081'); + res.writeHead(200, {'Content-Type': 'text/plain'}) + res.end('Hello from ' + source.address().port); + }); + + source.listen('8080'); + + http.request({ + hostname: '127.0.0.1', + port: '8081', + method: 'GET', + }, function(res) { + expect(res.statusCode).to.eql(200); + + res.on('data', function (data) { + expect(data.toString()).to.eql('Hello from 8080'); + }); + + res.on('end', function () { + source.close(); + proxy.close(); + done(); + }); + }).end(); + }); + }); + + describe('#createProxyServer() method with error response', function () { + it('should make the request and emit the error event', function(done) { + var proxy = caronte.createProxyServer({ + target: 'http://127.0.0.1:8080' + }); + + proxy.ee.on('caronte:outgoing:web:error', function (err) { + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNREFUSED'); + proxyServer.close(); + done(); + }) + + var proxyServer = proxy.listen('8081'); + + http.request({ + hostname: '127.0.0.1', + port: '8081', + method: 'GET', + }, function() {}).end(); + }); + }); + + describe('#createProxyServer using the web-incoming passes', function () { + it('should emit events correclty', function(done) { + var proxy = caronte.createProxyServer({ + target: 'http://127.0.0.1:8080' + }), + + proxyServer = proxy.listen('8081'), + + source = http.createServer(function(req, res) { + expect(req.method).to.eql('GET'); + expect(req.headers.host.split(':')[1]).to.eql('8081'); + res.writeHead(200, {'Content-Type': 'text/plain'}) + res.end('Hello from ' + source.address().port); + }), + + events = []; + + source.listen('8080'); + + proxy.ee.on('caronte:**', function (uno, dos, tres) { + events.push(this.event); + }) + + http.request({ + hostname: '127.0.0.1', + port: '8081', + method: 'GET', + }, function(res) { + expect(res.statusCode).to.eql(200); + + res.on('data', function (data) { + expect(data.toString()).to.eql('Hello from 8080'); + }); + + res.on('end', function () { + expect(events).to.contain('caronte:outgoing:web:begin'); + expect(events).to.contain('caronte:outgoing:web:end'); + source.close(); + proxyServer.close(); + done(); + }); + }).end(); + }); + }); }); From 7e25bded27effc1b3d47121ce21465a4e2ec7c0b Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 19:04:49 -0500 Subject: [PATCH 06/11] [tests] removed unused tests --- test/lib-caronte-streams-proxy-test.js | 109 -------------------- test/lib-caronte-streams-websockets-test.js | 109 -------------------- 2 files changed, 218 deletions(-) delete mode 100644 test/lib-caronte-streams-proxy-test.js delete mode 100644 test/lib-caronte-streams-websockets-test.js diff --git a/test/lib-caronte-streams-proxy-test.js b/test/lib-caronte-streams-proxy-test.js deleted file mode 100644 index 671f80c..0000000 --- a/test/lib-caronte-streams-proxy-test.js +++ /dev/null @@ -1,109 +0,0 @@ -var caronte = require('../'), - ProxyStream = require('../lib/caronte/streams/proxy'); - expect = require('expect.js'), - Duplex = require('stream').Duplex, - http = require('http'); - - -describe('lib/caronte/streams/proxy.js', function () { - describe('proxy stream constructor', function () { - it('should be an instance of Duplex stream and get the correct options and methods', function () { - var stubOptions = { - key: 'value' - }; - var proxyStream = new ProxyStream(stubOptions); - - expect(proxyStream).to.be.a(Duplex); - expect(proxyStream.options).to.eql({ key: 'value' }); - expect(proxyStream.onPipe).to.be.a('function'); - expect(proxyStream.onFinish).to.be.a('function'); - expect(proxyStream._events).to.have.property('pipe'); - expect(proxyStream._events).to.have.property('finish'); - }); - }); - - describe('caronte createProxyServer() method', function () { - it('should make the request on pipe and finish it', function(done) { - var proxy = caronte.createProxyServer({ - target: 'http://127.0.0.1:8080' - }).listen('8081'); - - var source = http.createServer(function(req, res) { - expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); - source.close(); - proxy.close(); - done(); - }); - - source.listen('8080'); - - http.request({ - hostname: '127.0.0.1', - port: '8081', - method: 'POST', - headers: { - 'x-forwarded-for': '127.0.0.1' - } - }, function() {}).end(); - }); - }); - - describe('caronte createProxyServer() method with response', function () { - it('should make the request, handle response and finish it', function(done) { - var proxy = caronte.createProxyServer({ - target: 'http://127.0.0.1:8080' - }).listen('8081'); - - var source = http.createServer(function(req, res) { - expect(req.method).to.eql('GET'); - res.writeHead(200, {'Content-Type': 'text/plain'}) - res.end('Hello from ' + source.address().port); - }); - - source.listen('8080'); - - http.request({ - hostname: '127.0.0.1', - port: '8081', - method: 'GET', - }, function(res) { - expect(res.statusCode).to.eql(200); - - res.on('data', function (data) { - expect(data.toString()).to.eql('Hello from 8080'); - }); - - res.on('end', function () { - source.close(); - proxy.close(); - done(); - }); - }).end(); - }); - }); - - describe('caronte createProxyServer() method with error response', function () { - it('should make the request and response with error', function(done) { - var proxy = caronte.createProxyServer({ - target: 'http://127.0.0.1:8080' - }).listen('8081'); - - http.request({ - hostname: '127.0.0.1', - port: '8081', - method: 'GET', - }, function(res) { - expect(res.statusCode).to.eql(500); - - res.on('data', function (data) { - expect(data.toString()).to.eql('Internal Server Error'); - }); - - res.on('end', function () { - proxy.close(); - done(); - }); - }).end(); - }); - }); -}); diff --git a/test/lib-caronte-streams-websockets-test.js b/test/lib-caronte-streams-websockets-test.js deleted file mode 100644 index 74248e7..0000000 --- a/test/lib-caronte-streams-websockets-test.js +++ /dev/null @@ -1,109 +0,0 @@ -var caronte = require('../'), - WebSocket = require('../lib/caronte/streams/websocket'); - expect = require('expect.js'), - Duplex = require('stream').Duplex, - http = require('http'); - - -describe('lib/caronte/streams/websocket.js', function () { - describe('WebSocket stream constructor', function () { - it('should be an instance of Duplex stream and get the correct options and methods', function () { - var stubOptions = { - key: 'value' - }; - var WebSocketStream = new WebSocket(stubOptions); - - expect(WebSocketStream).to.be.a(Duplex); - expect(WebSocketStream.options).to.eql({ key: 'value' }); - expect(WebSocketStream.onPipe).to.be.a('function'); - expect(WebSocketStream.onFinish).to.be.a('function'); - expect(WebSocketStream._events).to.have.property('pipe'); - expect(WebSocketStream._events).to.have.property('finish'); - }); - }); - - describe('caronte createWebSocketServer() method', function () { - // it('should make the request on pipe and finish it', function(done) { - // var proxy = caronte.createProxyServer({ - // target: 'http://127.0.0.1:8080' - // }).listen('8081'); - - // var source = http.createServer(function(req, res) { - // expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); - // source.close(); - // proxy.close(); - // done(); - // }); - - // source.listen('8080'); - - // http.request({ - // hostname: '127.0.0.1', - // port: '8081', - // method: 'POST', - // headers: { - // 'x-forwarded-for': '127.0.0.1' - // } - // }, function() {}).end(); - // }); - }); - - describe('caronte createProxyServer() method with response', function () { - // it('should make the request, handle response and finish it', function(done) { - // var proxy = caronte.createProxyServer({ - // target: 'http://127.0.0.1:8080' - // }).listen('8081'); - - // var source = http.createServer(function(req, res) { - // expect(req.method).to.eql('GET'); - // res.writeHead(200, {'Content-Type': 'text/plain'}) - // res.end('Hello from ' + source.address().port); - // }); - - // source.listen('8080'); - - // http.request({ - // hostname: '127.0.0.1', - // port: '8081', - // method: 'GET', - // }, function(res) { - // expect(res.statusCode).to.eql(200); - - // res.on('data', function (data) { - // expect(data.toString()).to.eql('Hello from 8080'); - // }); - - // res.on('end', function () { - // source.close(); - // proxy.close(); - // done(); - // }); - // }).end(); - // }); - }); - - describe('caronte createProxyServer() method with error response', function () { - // it('should make the request and response with error', function(done) { - // var proxy = caronte.createProxyServer({ - // target: 'http://127.0.0.1:8080' - // }).listen('8081'); - - // http.request({ - // hostname: '127.0.0.1', - // port: '8081', - // method: 'GET', - // }, function(res) { - // expect(res.statusCode).to.eql(500); - - // res.on('data', function (data) { - // expect(data.toString()).to.eql('Internal Server Error'); - // }); - - // res.on('end', function () { - // proxy.close(); - // done(); - // }); - // }).end(); - // }); - }); -}); From 40902506af3361b642b8798350b48404fe0a4e78 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 19:37:28 -0500 Subject: [PATCH 07/11] [tests] fix code coverage, changed pattern on blanket options --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f11557c..bc8c4c0 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "ws" : "*" }, "scripts" : { - "blanket" : { "pattern": "caronte/lib" }, + "blanket" : { "pattern": "lib/caronte" }, "test" : "./node_modules/.bin/mocha -R landing test/*-test.js", "test-cov" : "./node_modules/.bin/mocha --require blanket -R html-cov > cov/coverage.html" }, From 02007ed0fb38f798436ae5669bb18d4f27496667 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 20:59:10 -0500 Subject: [PATCH 08/11] [tests] added tests for websockets --- test/lib-caronte-test.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/lib-caronte-test.js b/test/lib-caronte-test.js index dbb278f..06ab11e 100644 --- a/test/lib-caronte-test.js +++ b/test/lib-caronte-test.js @@ -1,6 +1,7 @@ var caronte = require('../lib/caronte'), expect = require('expect.js'), - http = require('http'); + http = require('http'), + ws = require('ws'); describe('lib/caronte.js', function() { describe('#createProxyServer', function() { @@ -176,4 +177,35 @@ describe('lib/caronte.js', function() { }).end(); }); }); + + describe('#createProxyServer using the ws-incoming passes', function () { + it('should proxy the websockets stream', function (done) { + var proxy = caronte.createProxyServer({ + target: 'ws://127.0.0.1:8080', + ws: true + }), + proxyServer = proxy.listen('8081'), + destiny = new ws.Server({ port: 8080 }, function () { + var client = new ws('ws://127.0.0.1:8081'); + + client.on('open', function () { + client.send('hello there'); + }); + + client.on('message', function (msg) { + expect(msg).to.be('Hello over websockets'); + proxyServer.close(); + destiny.close(); + done(); + }); + }); + + destiny.on('connection', function (socket) { + socket.on('message', function (msg) { + expect(msg).to.be('hello there'); + socket.send('Hello over websockets'); + }); + }); + }); + }); }); From 06025002303f351f71d9e5f78a93895257f0d283 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 21:00:34 -0500 Subject: [PATCH 09/11] [tests] added .travis.yml file --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..87fb964 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - 0.8 + - "0.10" + - "0.11" + +notifications: + email: + - travis@nodejitsu.com + irc: "irc.freenode.org#nodejitsu" \ No newline at end of file From 10a0db4f0dd4594839f9098b9d67130085a067bc Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 21:37:49 -0500 Subject: [PATCH 10/11] [tests] added test for socket.io proxying --- package.json | 4 +++- test/lib-caronte-test.js | 43 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index bc8c4c0..c0b7417 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "coveralls" : "*", "mocha-lcov-reporter": "*", "blanket" : "*", - "ws" : "*" + "ws" : "*", + "socket.io" : "*", + "socket.io-client" : "*" }, "scripts" : { "blanket" : { "pattern": "lib/caronte" }, diff --git a/test/lib-caronte-test.js b/test/lib-caronte-test.js index 06ab11e..0558ef9 100644 --- a/test/lib-caronte-test.js +++ b/test/lib-caronte-test.js @@ -1,7 +1,10 @@ -var caronte = require('../lib/caronte'), - expect = require('expect.js'), - http = require('http'), - ws = require('ws'); +var caronte = require('../lib/caronte'), + expect = require('expect.js'), + http = require('http'), + ws = require('ws') + io = require('socket.io'), + ioClient = require('socket.io-client'); + describe('lib/caronte.js', function() { describe('#createProxyServer', function() { @@ -194,6 +197,7 @@ describe('lib/caronte.js', function() { client.on('message', function (msg) { expect(msg).to.be('Hello over websockets'); + client.close(); proxyServer.close(); destiny.close(); done(); @@ -208,4 +212,35 @@ describe('lib/caronte.js', function() { }); }); }); + + describe('#createProxyServer using the ws-incoming passes', function () { + it('should proxy a socket.io stream', function (done) { + var proxy = caronte.createProxyServer({ + target: 'ws://127.0.0.1:8080', + ws: true + }), + proxyServer = proxy.listen('8081'), + destiny = io.listen(8080, function () { + var client = ioClient.connect('ws://127.0.0.1:8081'); + + client.on('connect', function () { + client.emit('incoming', 'hello there'); + }); + + client.on('outgoing', function (data) { + expect(data).to.be('Hello over websockets'); + proxyServer.close(); + destiny.server.close(); + done(); + }); + }); + + destiny.sockets.on('connection', function (socket) { + socket.on('incoming', function (msg) { + expect(msg).to.be('hello there'); + socket.emit('outgoing', 'Hello over websockets'); + }); + }) + }); + }) }); From 8eff1a1f26bb739dfc5a1ad90b140ff2a18921d5 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 21:40:42 -0500 Subject: [PATCH 11/11] [test][misc] remove node@0.8 to test on travis --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 87fb964..835fed5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: node_js node_js: - - 0.8 - "0.10" - "0.11" notifications: email: - travis@nodejitsu.com - irc: "irc.freenode.org#nodejitsu" \ No newline at end of file + irc: "irc.freenode.org#nodejitsu"