From 27df8d72ad86d02cfce00a6e5c183d93dd50f97e Mon Sep 17 00:00:00 2001 From: cronopio Date: Wed, 28 Aug 2013 13:45:09 -0500 Subject: [PATCH] [test] testing the onResponse proxy method --- test/lib-caronte-streams-proxy-test.js | 48 +++++++++++++++++++++----- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/test/lib-caronte-streams-proxy-test.js b/test/lib-caronte-streams-proxy-test.js index 0c0debb8a..a68f99141 100644 --- a/test/lib-caronte-streams-proxy-test.js +++ b/test/lib-caronte-streams-proxy-test.js @@ -22,22 +22,20 @@ describe('lib/caronte/streams/proxy.js', function () { }); }); - describe('should pipe the request and finish it', function () { + describe('caronte createProxyServer() method', function () { it('should make the request on pipe and finish it', function(done) { - var result; - - var p = caronte.createProxyServer({ + var proxy = caronte.createProxyServer({ target: 'http://127.0.0.1:8080' }).listen('8081'); - var s = http.createServer(function(req, res) { + var source = http.createServer(function(req, res) { expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1'); - s.close(); - p.close(); + source.close(); + proxy.close(); done(); }); - s.listen('8080'); + source.listen('8080'); http.request({ hostname: '127.0.0.1', @@ -49,4 +47,38 @@ describe('lib/caronte/streams/proxy.js', function () { }, 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(); + }); + }); });