[test] testing the onResponse proxy method

This commit is contained in:
cronopio 2013-08-28 13:45:09 -05:00
parent abf1d90fdf
commit 27df8d72ad

View File

@ -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) { it('should make the request on pipe and finish it', function(done) {
var result; var proxy = caronte.createProxyServer({
var p = caronte.createProxyServer({
target: 'http://127.0.0.1:8080' target: 'http://127.0.0.1:8080'
}).listen('8081'); }).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'); expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
s.close(); source.close();
p.close(); proxy.close();
done(); done();
}); });
s.listen('8080'); source.listen('8080');
http.request({ http.request({
hostname: '127.0.0.1', hostname: '127.0.0.1',
@ -49,4 +47,38 @@ describe('lib/caronte/streams/proxy.js', function () {
}, function() {}).end(); }, 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();
});
});
}); });