From dc9d7e5452c7d39ae1d242cb8021ca75e4f736d4 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 16 Sep 2013 18:59:48 -0500 Subject: [PATCH] [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(); + }); + }); });