Skip to content

Commit 27df8d7

Browse files
committed
[test] testing the onResponse proxy method
1 parent abf1d90 commit 27df8d7

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

test/lib-caronte-streams-proxy-test.js

+40-8
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@ describe('lib/caronte/streams/proxy.js', function () {
2222
});
2323
});
2424

25-
describe('should pipe the request and finish it', function () {
25+
describe('caronte createProxyServer() method', function () {
2626
it('should make the request on pipe and finish it', function(done) {
27-
var result;
28-
29-
var p = caronte.createProxyServer({
27+
var proxy = caronte.createProxyServer({
3028
target: 'http://127.0.0.1:8080'
3129
}).listen('8081');
3230

33-
var s = http.createServer(function(req, res) {
31+
var source = http.createServer(function(req, res) {
3432
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
35-
s.close();
36-
p.close();
33+
source.close();
34+
proxy.close();
3735
done();
3836
});
3937

40-
s.listen('8080');
38+
source.listen('8080');
4139

4240
http.request({
4341
hostname: '127.0.0.1',
@@ -49,4 +47,38 @@ describe('lib/caronte/streams/proxy.js', function () {
4947
}, function() {}).end();
5048
});
5149
});
50+
51+
describe('caronte createProxyServer() method with response', function () {
52+
it('should make the request, handle response and finish it', function(done) {
53+
var proxy = caronte.createProxyServer({
54+
target: 'http://127.0.0.1:8080'
55+
}).listen('8081');
56+
57+
var source = http.createServer(function(req, res) {
58+
expect(req.method).to.eql('GET');
59+
res.writeHead(200, {'Content-Type': 'text/plain'})
60+
res.end('Hello from ' + source.address().port);
61+
});
62+
63+
source.listen('8080');
64+
65+
http.request({
66+
hostname: '127.0.0.1',
67+
port: '8081',
68+
method: 'GET',
69+
}, function(res) {
70+
expect(res.statusCode).to.eql(200);
71+
72+
res.on('data', function (data) {
73+
expect(data.toString()).to.eql('Hello from 8080');
74+
});
75+
76+
res.on('end', function () {
77+
source.close();
78+
proxy.close();
79+
done();
80+
});
81+
}).end();
82+
});
83+
});
5284
});

0 commit comments

Comments
 (0)