Skip to content

Commit 7c72f3b

Browse files
committed
[tests] move contributions of @mmoulton to correct place
1 parent 0bfb9be commit 7c72f3b

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

test/lib-http-proxy-passes-web-incoming-test.js

+88-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
var httpProxy = require('../lib/http-proxy/passes/web-incoming'),
2-
expect = require('expect.js');
1+
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
2+
httpProxy = require('../lib/http-proxy'),
3+
expect = require('expect.js'),
4+
http = require('http');
35

46
describe('lib/http-proxy/passes/web.js', function() {
57
describe('#deleteLength', function() {
@@ -8,7 +10,7 @@ describe('lib/http-proxy/passes/web.js', function() {
810
method: 'DELETE',
911
headers: {}
1012
};
11-
httpProxy.deleteLength(stubRequest, {}, {});
13+
webPasses.deleteLength(stubRequest, {}, {});
1214
expect(stubRequest.headers['content-length']).to.eql('0');
1315
})
1416
});
@@ -21,7 +23,7 @@ describe('lib/http-proxy/passes/web.js', function() {
2123
}
2224
}
2325

24-
httpProxy.timeout(stubRequest, {}, { timeout: 5000});
26+
webPasses.timeout(stubRequest, {}, { timeout: 5000});
2527
expect(done).to.eql(5000);
2628
});
2729
});
@@ -36,10 +38,91 @@ describe('lib/http-proxy/passes/web.js', function() {
3638
}
3739

3840
it('set the correct x-forwarded-* headers', function () {
39-
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
41+
webPasses.XHeaders(stubRequest, {}, { xfwd: true });
4042
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
4143
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
4244
expect(stubRequest.headers['x-forwarded-proto']).to.be('http');
4345
});
4446
});
4547
});
48+
49+
describe('#createProxyServer.web() using own http server', function () {
50+
it('should proxy the request using the web proxy handler', function (done) {
51+
var proxy = httpProxy.createProxyServer({
52+
target: 'http://127.0.0.1:8080'
53+
});
54+
55+
function requestHandler(req, res) {
56+
proxy.web(req, res);
57+
}
58+
59+
var proxyServer = http.createServer(requestHandler);
60+
61+
var source = http.createServer(function(req, res) {
62+
source.close();
63+
proxyServer.close();
64+
expect(req.method).to.eql('GET');
65+
expect(req.headers.host.split(':')[1]).to.eql('8081');
66+
done();
67+
});
68+
69+
proxyServer.listen('8081');
70+
source.listen('8080');
71+
72+
http.request('http://127.0.0.1:8081', function() {}).end();
73+
});
74+
75+
it('should proxy the request and handle error via callback', function(done) {
76+
var proxy = httpProxy.createProxyServer({
77+
target: 'http://127.0.0.1:8080'
78+
});
79+
80+
var proxyServer = http.createServer(requestHandler);
81+
82+
function requestHandler(req, res) {
83+
proxy.web(req, res, function (err) {
84+
proxyServer.close();
85+
expect(err).to.be.an(Error);
86+
expect(err.code).to.be('ECONNREFUSED');
87+
done();
88+
});
89+
}
90+
91+
proxyServer.listen('8081');
92+
93+
http.request({
94+
hostname: '127.0.0.1',
95+
port: '8081',
96+
method: 'GET',
97+
}, function() {}).end();
98+
});
99+
100+
it('should proxy the request and handle error via event listener', function(done) {
101+
var proxy = httpProxy.createProxyServer({
102+
target: 'http://127.0.0.1:8080'
103+
});
104+
105+
var proxyServer = http.createServer(requestHandler);
106+
107+
function requestHandler(req, res) {
108+
proxy.once('error', function (err, errReq, errRes) {
109+
proxyServer.close();
110+
expect(err).to.be.an(Error);
111+
expect(errReq).to.be.equal(req);
112+
expect(errRes).to.be.equal(res);
113+
expect(err.code).to.be('ECONNREFUSED');
114+
done();
115+
});
116+
117+
proxy.web(req, res);
118+
}
119+
120+
proxyServer.listen('8081');
121+
122+
http.request({
123+
hostname: '127.0.0.1',
124+
port: '8081',
125+
method: 'GET',
126+
}, function() {}).end();
127+
});
128+
});

0 commit comments

Comments
 (0)