| @@ -0,0 +1,103 @@ | ||
| var common = require('../lib/http-proxy/common'), | ||
| expect = require('expect.js'); | ||
|
|
||
| describe('lib/http-proxy/common.js', function () { | ||
| describe('#setupOutgoing', function () { | ||
| it('should setup the correct headers', function () { | ||
| var outgoing = {}; | ||
| common.setupOutgoing(outgoing, | ||
| { | ||
| agent : '?', | ||
| target: { | ||
| host : 'hey', | ||
| hostname : 'how', | ||
| socketPath: 'are', | ||
| port : 'you', | ||
| }, | ||
| headers: {'fizz': 'bang', 'overwritten':true}, | ||
| }, | ||
| { | ||
| method : 'i', | ||
| url : 'am', | ||
| headers : {'pro':'xy','overwritten':false} | ||
| }); | ||
|
|
||
| expect(outgoing.host).to.eql('hey'); | ||
| 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.method).to.eql('i'); | ||
| expect(outgoing.path).to.eql('am'); | ||
|
|
||
| expect(outgoing.headers.pro).to.eql('xy'); | ||
| expect(outgoing.headers.fizz).to.eql('bang'); | ||
| expect(outgoing.headers.overwritten).to.eql(true); | ||
| }); | ||
|
|
||
| it('should set the agent to false if none is given', function () { | ||
| var outgoing = {}; | ||
| common.setupOutgoing(outgoing, {target: | ||
| 'http://localhost' | ||
| }, { url: '/' }); | ||
| expect(outgoing.agent).to.eql(false); | ||
| }); | ||
|
|
||
| it('set the port according to the protocol', function () { | ||
| var outgoing = {}; | ||
| common.setupOutgoing(outgoing, | ||
| { | ||
| agent : '?', | ||
| target: { | ||
| host : 'how', | ||
| hostname : 'are', | ||
| socketPath: 'you', | ||
| 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); | ||
| }); | ||
| }); | ||
| }); |
| @@ -0,0 +1,130 @@ | ||
| var webPasses = require('../lib/http-proxy/passes/web-incoming'), | ||
| httpProxy = require('../lib/http-proxy'), | ||
| expect = require('expect.js'), | ||
| http = require('http'); | ||
|
|
||
| describe('lib/http-proxy/passes/web.js', function() { | ||
| describe('#deleteLength', function() { | ||
| it('should change `content-length`', function() { | ||
| var stubRequest = { | ||
| method: 'DELETE', | ||
| headers: {} | ||
| }; | ||
| webPasses.deleteLength(stubRequest, {}, {}); | ||
| expect(stubRequest.headers['content-length']).to.eql('0'); | ||
| }) | ||
| }); | ||
|
|
||
| describe('#timeout', function() { | ||
| it('should set timeout on the socket', function() { | ||
| var done = false, stubRequest = { | ||
| socket: { | ||
| setTimeout: function(value) { done = value; } | ||
| } | ||
| } | ||
|
|
||
| webPasses.timeout(stubRequest, {}, { timeout: 5000}); | ||
| expect(done).to.eql(5000); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#XHeaders', function () { | ||
| var stubRequest = { | ||
| connection: { | ||
| remoteAddress: '192.168.1.2', | ||
| remotePort: '8080' | ||
| }, | ||
| headers: { | ||
| host: '192.168.1.2:8080' | ||
| } | ||
| } | ||
|
|
||
| it('set the correct x-forwarded-* headers', function () { | ||
| webPasses.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('#createProxyServer.web() using own http server', function () { | ||
| it('should proxy the request using the web proxy handler', function (done) { | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:8080' | ||
| }); | ||
|
|
||
| function requestHandler(req, res) { | ||
| proxy.web(req, res); | ||
| } | ||
|
|
||
| var proxyServer = http.createServer(requestHandler); | ||
|
|
||
| var source = http.createServer(function(req, res) { | ||
| source.close(); | ||
| proxyServer.close(); | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql('8081'); | ||
| done(); | ||
| }); | ||
|
|
||
| proxyServer.listen('8081'); | ||
| source.listen('8080'); | ||
|
|
||
| http.request('http://127.0.0.1:8081', function() {}).end(); | ||
| }); | ||
|
|
||
| it('should proxy the request and handle error via callback', function(done) { | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:8080' | ||
| }); | ||
|
|
||
| var proxyServer = http.createServer(requestHandler); | ||
|
|
||
| function requestHandler(req, res) { | ||
| proxy.web(req, res, function (err) { | ||
| proxyServer.close(); | ||
| expect(err).to.be.an(Error); | ||
| expect(err.code).to.be('ECONNREFUSED'); | ||
| done(); | ||
| }); | ||
| } | ||
|
|
||
| proxyServer.listen('8082'); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: '8082', | ||
| method: 'GET', | ||
| }, function() {}).end(); | ||
| }); | ||
|
|
||
| it('should proxy the request and handle error via event listener', function(done) { | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:8080' | ||
| }); | ||
|
|
||
| var proxyServer = http.createServer(requestHandler); | ||
|
|
||
| function requestHandler(req, res) { | ||
| proxy.once('error', function (err, errReq, errRes) { | ||
| proxyServer.close(); | ||
| expect(err).to.be.an(Error); | ||
| expect(errReq).to.be.equal(req); | ||
| expect(errRes).to.be.equal(res); | ||
| expect(err.code).to.be('ECONNREFUSED'); | ||
| done(); | ||
| }); | ||
|
|
||
| proxy.web(req, res); | ||
| } | ||
|
|
||
| proxyServer.listen('8083'); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: '8083', | ||
| method: 'GET', | ||
| }, function() {}).end(); | ||
| }); | ||
| }); |
| @@ -0,0 +1,104 @@ | ||
| var httpProxy = require('../lib/http-proxy/passes/web-outgoing'), | ||
| expect = require('expect.js'); | ||
|
|
||
| describe('lib/http-proxy/passes/web-outgoing.js', function () { | ||
| describe('#setConnection', function () { | ||
| it('set the right connection with 1.0 - `close`', function() { | ||
| var proxyRes = { headers: {} }; | ||
| httpProxy.setConnection({ | ||
| httpVersion: '1.0', | ||
| headers: { | ||
| connection: null | ||
| } | ||
| }, {}, proxyRes); | ||
|
|
||
| expect(proxyRes.headers.connection).to.eql('close'); | ||
| }); | ||
|
|
||
| it('set the right connection with 1.0 - req.connection', function() { | ||
| var proxyRes = { headers: {} }; | ||
| httpProxy.setConnection({ | ||
| httpVersion: '1.0', | ||
| headers: { | ||
| connection: 'hey' | ||
| } | ||
| }, {}, proxyRes); | ||
|
|
||
| expect(proxyRes.headers.connection).to.eql('hey'); | ||
| }); | ||
|
|
||
| it('set the right connection - req.connection', function() { | ||
| var proxyRes = { headers: {} }; | ||
| httpProxy.setConnection({ | ||
| httpVersion: null, | ||
| headers: { | ||
| connection: 'hola' | ||
| } | ||
| }, {}, proxyRes); | ||
|
|
||
| expect(proxyRes.headers.connection).to.eql('hola'); | ||
| }); | ||
|
|
||
| it('set the right connection - `keep-alive`', function() { | ||
| var proxyRes = { headers: {} }; | ||
| httpProxy.setConnection({ | ||
| httpVersion: null, | ||
| headers: { | ||
| connection: null | ||
| } | ||
| }, {}, proxyRes); | ||
|
|
||
| expect(proxyRes.headers.connection).to.eql('keep-alive'); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| describe('#writeStatusCode', function () { | ||
| it('should write status code', function() { | ||
| var res = { | ||
| writeHead: function(n) { | ||
| expect(n).to.eql(200); | ||
| } | ||
| } | ||
|
|
||
| httpProxy.writeStatusCode({}, res, { statusCode: 200 }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#writeHeaders', function() { | ||
| var proxyRes = { | ||
| headers: { | ||
| hey: 'hello', | ||
| how: 'are you?' | ||
| } | ||
| }; | ||
|
|
||
| var res = { | ||
| setHeader: function(k, v) { | ||
| this.headers[k] = v; | ||
| }, | ||
| headers: {} | ||
| }; | ||
|
|
||
| httpProxy.writeHeaders({}, res, proxyRes); | ||
|
|
||
| expect(res.headers.hey).to.eql('hello'); | ||
| expect(res.headers.how).to.eql('are you?'); | ||
| }); | ||
|
|
||
|
|
||
| describe('#removeChunked', function() { | ||
| var proxyRes = { | ||
| headers: { | ||
| 'transfer-encoding': 'hello' | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| httpProxy.removeChunked({ httpVersion: '1.0' }, {}, proxyRes); | ||
|
|
||
| expect(proxyRes.headers['transfer-encoding']).to.eql(undefined); | ||
| }); | ||
|
|
||
| }); | ||
|
|
| @@ -0,0 +1,120 @@ | ||
| var httpProxy = require('../lib/http-proxy/passes/ws-incoming'), | ||
| expect = require('expect.js'); | ||
|
|
||
| describe('lib/http-proxy/passes/ws-incoming.js', function () { | ||
| describe('#checkMethodAndHeader', function () { | ||
| it('should drop non-GET connections', function () { | ||
| var destroyCalled = false, | ||
| stubRequest = { | ||
| method: 'DELETE', | ||
| headers: {} | ||
| }, | ||
| stubSocket = { | ||
| destroy: function () { | ||
| // Simulate Socket.destroy() method when call | ||
| destroyCalled = true; | ||
| } | ||
| } | ||
| returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); | ||
| expect(returnValue).to.be(true); | ||
| expect(destroyCalled).to.be(true); | ||
| }) | ||
|
|
||
| it('should drop connections when no upgrade header', function () { | ||
| var destroyCalled = false, | ||
| stubRequest = { | ||
| method: 'GET', | ||
| headers: {} | ||
| }, | ||
| stubSocket = { | ||
| destroy: function () { | ||
| // Simulate Socket.destroy() method when call | ||
| destroyCalled = true; | ||
| } | ||
| } | ||
| returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); | ||
| expect(returnValue).to.be(true); | ||
| expect(destroyCalled).to.be(true); | ||
| }) | ||
|
|
||
| it('should drop connections when upgrade header is different of `websocket`', function () { | ||
| var destroyCalled = false, | ||
| stubRequest = { | ||
| method: 'GET', | ||
| headers: { | ||
| upgrade: 'anotherprotocol' | ||
| } | ||
| }, | ||
| stubSocket = { | ||
| destroy: function () { | ||
| // Simulate Socket.destroy() method when call | ||
| destroyCalled = true; | ||
| } | ||
| } | ||
| returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); | ||
| expect(returnValue).to.be(true); | ||
| expect(destroyCalled).to.be(true); | ||
| }) | ||
|
|
||
| it('should return nothing when all is ok', function () { | ||
| var destroyCalled = false, | ||
| stubRequest = { | ||
| method: 'GET', | ||
| headers: { | ||
| upgrade: 'websocket' | ||
| } | ||
| }, | ||
| stubSocket = { | ||
| destroy: function () { | ||
| // Simulate Socket.destroy() method when call | ||
| destroyCalled = true; | ||
| } | ||
| } | ||
| returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket); | ||
| expect(returnValue).to.be(undefined); | ||
| expect(destroyCalled).to.be(false); | ||
| }) | ||
| }); | ||
|
|
||
| describe('#XHeaders', function () { | ||
| it('return if no forward request', function () { | ||
| var returnValue = httpProxy.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: { | ||
| host: '192.168.1.2:8080' | ||
| } | ||
| } | ||
| httpProxy.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: { | ||
| host: '192.168.1.3:8181' | ||
| } | ||
| }; | ||
| httpProxy.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'); | ||
| }); | ||
| }); | ||
| }); |
| @@ -0,0 +1,297 @@ | ||
| var httpProxy = require('../lib/http-proxy'), | ||
| expect = require('expect.js'), | ||
| http = require('http'), | ||
| ws = require('ws') | ||
| io = require('socket.io'), | ||
| ioClient = require('socket.io-client'); | ||
|
|
||
| // | ||
| // Expose a port number generator. | ||
| // thanks to @3rd-Eden | ||
| // | ||
| var initialPort = 1024, gen = {}; | ||
| Object.defineProperty(gen, 'port', { | ||
| get: function get() { | ||
| return initialPort++; | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| describe('lib/http-proxy.js', function() { | ||
| describe('#createProxyServer', function() { | ||
| it.skip('should throw without options', function() { | ||
| var error; | ||
| try { | ||
| httpProxy.createProxyServer(); | ||
| } catch(e) { | ||
| error = e; | ||
| } | ||
|
|
||
| expect(error).to.be.an(Error); | ||
| }) | ||
|
|
||
| it('should return an object otherwise', function() { | ||
| var obj = httpProxy.createProxyServer({ | ||
| target: 'http://www.google.com:80' | ||
| }); | ||
|
|
||
| expect(obj.web).to.be.a(Function); | ||
| expect(obj.ws).to.be.a(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 ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| forward: 'http://127.0.0.1:' + ports.source | ||
| }).listen(ports.proxy); | ||
|
|
||
| var source = http.createServer(function(req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
| http.request('http://127.0.0.1:' + ports.proxy, function() {}).end(); | ||
| }) | ||
| }); | ||
|
|
||
| describe('#createProxyServer using the web-incoming passes', function () { | ||
| it('should make the request on pipe and finish it', function(done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:' + ports.source | ||
| }).listen(ports.proxy); | ||
|
|
||
| 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(ports.proxy); | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| 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 ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:' + ports.source | ||
| }).listen(ports.proxy); | ||
|
|
||
| var source = http.createServer(function(req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| res.writeHead(200, {'Content-Type': 'text/plain'}) | ||
| res.end('Hello from ' + source.address().port); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| method: 'GET' | ||
| }, function(res) { | ||
| expect(res.statusCode).to.eql(200); | ||
|
|
||
| res.on('data', function (data) { | ||
| expect(data.toString()).to.eql('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| res.on('end', function () { | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }); | ||
| }).end(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#createProxyServer() method with error response', function () { | ||
| it('should make the request and emit the error event', function(done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:' + ports.source | ||
| }); | ||
|
|
||
| proxy.on('error', function (err) { | ||
| expect(err).to.be.an(Error); | ||
| expect(err.code).to.be('ECONNREFUSED'); | ||
| proxy._server.close(); | ||
| done(); | ||
| }) | ||
|
|
||
| proxy.listen(ports.proxy); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| method: 'GET', | ||
| }, function() {}).end(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#createProxyServer setting the correct timeout value', function () { | ||
| it('should hang up the socket at the timeout', function (done) { | ||
| this.timeout(30); | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:' + ports.source, | ||
| timeout: 3 | ||
| }).listen(ports.proxy); | ||
|
|
||
| var source = http.createServer(function(req, res) { | ||
| setTimeout(function () { | ||
| res.end('At this point the socket should be closed'); | ||
| }, 5) | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| var testReq = http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| method: 'GET', | ||
| }, function() {}); | ||
|
|
||
| testReq.on('error', function (e) { | ||
| expect(e).to.be.an(Error); | ||
| expect(e.code).to.be.eql('ECONNRESET'); | ||
| proxy._server.close(); | ||
| source.close(); | ||
| done(); | ||
| }); | ||
|
|
||
| testReq.end(); | ||
| }) | ||
| }) | ||
|
|
||
| // describe('#createProxyServer using the web-incoming passes', function () { | ||
| // it('should emit events correclty', function(done) { | ||
| // var proxy = httpProxy.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('http-proxy:**', 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('http-proxy:outgoing:web:begin'); | ||
| // expect(events).to.contain('http-proxy:outgoing:web:end'); | ||
| // source.close(); | ||
| // proxyServer._server.close(); | ||
| // done(); | ||
| // }); | ||
| // }).end(); | ||
| // }); | ||
| // }); | ||
|
|
||
| describe('#createProxyServer using the ws-incoming passes', function () { | ||
| it('should proxy the websockets stream', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'ws://127.0.0.1:' + ports.source, | ||
| ws: true | ||
| }), | ||
| proxyServer = proxy.listen(ports.proxy), | ||
| destiny = new ws.Server({ port: ports.source }, function () { | ||
| var client = new ws('ws://127.0.0.1:' + ports.proxy); | ||
|
|
||
| client.on('open', function () { | ||
| client.send('hello there'); | ||
| }); | ||
|
|
||
| client.on('message', function (msg) { | ||
| expect(msg).to.be('Hello over websockets'); | ||
| client.close(); | ||
| proxyServer._server.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'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#createProxyServer using the ws-incoming passes', function () { | ||
| it('should proxy a socket.io stream', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'ws://127.0.0.1:' + ports.source, | ||
| ws: true | ||
| }), | ||
| proxyServer = proxy.listen(ports.proxy), | ||
| destiny = io.listen(ports.source, function () { | ||
| var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy); | ||
|
|
||
| client.on('connect', function () { | ||
| client.emit('incoming', 'hello there'); | ||
| }); | ||
|
|
||
| client.on('outgoing', function (data) { | ||
| expect(data).to.be('Hello over websockets'); | ||
| proxyServer._server.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'); | ||
| }); | ||
| }) | ||
| }); | ||
| }) | ||
| }); |
| @@ -0,0 +1,222 @@ | ||
| var httpProxy = require('../lib/http-proxy'), | ||
| expect = require('expect.js'), | ||
| http = require('http') | ||
| https = require('https'), | ||
| path = require('path'), | ||
| fs = require('fs'); | ||
|
|
||
| // | ||
| // Expose a port number generator. | ||
| // thanks to @3rd-Eden | ||
| // | ||
| var initialPort = 1024, gen = {}; | ||
| Object.defineProperty(gen, 'port', { | ||
| get: function get() { | ||
| return initialPort++; | ||
| } | ||
| }); | ||
|
|
||
| describe('lib/http-proxy.js', function() { | ||
| describe('HTTPS #createProxyServer', function() { | ||
| describe('HTTPS to HTTP', function () { | ||
| it('should proxy the request en send back the response', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var source = http.createServer(function(req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'http://127.0.0.1:' + ports.source, | ||
| ssl: { | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| } | ||
| }).listen(ports.proxy); | ||
|
|
||
| https.request({ | ||
| host: 'localhost', | ||
| port: ports.proxy, | ||
| path: '/', | ||
| method: 'GET', | ||
| rejectUnauthorized: false | ||
| }, function(res) { | ||
| expect(res.statusCode).to.eql(200); | ||
|
|
||
| res.on('data', function (data) { | ||
| expect(data.toString()).to.eql('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| res.on('end', function () { | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }) | ||
| }).end(); | ||
| }) | ||
| }); | ||
| describe('HTTP to HTTPS', function () { | ||
| it('should proxy the request en send back the response', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var source = https.createServer({ | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| }, function (req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'https://127.0.0.1:' + ports.source, | ||
| // Allow to use SSL self signed | ||
| secure: false | ||
| }).listen(ports.proxy); | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| method: 'GET' | ||
| }, function(res) { | ||
| expect(res.statusCode).to.eql(200); | ||
|
|
||
| res.on('data', function (data) { | ||
| expect(data.toString()).to.eql('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| res.on('end', function () { | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }); | ||
| }).end(); | ||
| }) | ||
| }) | ||
| describe('HTTPS to HTTPS', function () { | ||
| it('should proxy the request en send back the response', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var source = https.createServer({ | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| }, function(req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'https://127.0.0.1:' + ports.source, | ||
| ssl: { | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| }, | ||
| secure: false | ||
| }).listen(ports.proxy); | ||
|
|
||
| https.request({ | ||
| host: 'localhost', | ||
| port: ports.proxy, | ||
| path: '/', | ||
| method: 'GET', | ||
| rejectUnauthorized: false | ||
| }, function(res) { | ||
| expect(res.statusCode).to.eql(200); | ||
|
|
||
| res.on('data', function (data) { | ||
| expect(data.toString()).to.eql('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| res.on('end', function () { | ||
| source.close(); | ||
| proxy._server.close(); | ||
| done(); | ||
| }) | ||
| }).end(); | ||
| }) | ||
| }); | ||
| describe('HTTPS not allow SSL self signed', function () { | ||
| it('should fail with error', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var source = https.createServer({ | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| }).listen(ports.source); | ||
|
|
||
| var proxy = httpProxy.createProxyServer({ | ||
| target: 'https://127.0.0.1:' + ports.source, | ||
| secure: true | ||
| }); | ||
|
|
||
| proxy.listen(ports.proxy); | ||
|
|
||
| proxy.on('error', function (err, req, res) { | ||
| expect(err).to.be.an(Error); | ||
| expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT') | ||
| done(); | ||
| }) | ||
|
|
||
| http.request({ | ||
| hostname: '127.0.0.1', | ||
| port: ports.proxy, | ||
| method: 'GET' | ||
| }).end(); | ||
| }) | ||
| }) | ||
| describe('HTTPS to HTTP using own server', function () { | ||
| it('should proxy the request en send back the response', function (done) { | ||
| var ports = { source: gen.port, proxy: gen.port }; | ||
| var source = http.createServer(function(req, res) { | ||
| expect(req.method).to.eql('GET'); | ||
| expect(req.headers.host.split(':')[1]).to.eql(ports.proxy); | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| source.listen(ports.source); | ||
|
|
||
| var proxy = httpProxy.createServer({ | ||
| agent: new http.Agent({ maxSockets: 2 }) | ||
| }); | ||
|
|
||
| var ownServer = https.createServer({ | ||
| key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')), | ||
| cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')), | ||
| }, function (req, res) { | ||
| proxy.web(req, res, { | ||
| target: 'http://127.0.0.1:' + ports.source | ||
| }) | ||
| }).listen(ports.proxy); | ||
|
|
||
| https.request({ | ||
| host: 'localhost', | ||
| port: ports.proxy, | ||
| path: '/', | ||
| method: 'GET', | ||
| rejectUnauthorized: false | ||
| }, function(res) { | ||
| expect(res.statusCode).to.eql(200); | ||
|
|
||
| res.on('data', function (data) { | ||
| expect(data.toString()).to.eql('Hello from ' + ports.source); | ||
| }); | ||
|
|
||
| res.on('end', function () { | ||
| source.close(); | ||
| ownServer.close(); | ||
| done(); | ||
| }) | ||
| }).end(); | ||
| }) | ||
| }) | ||
| }); | ||
| }); |