Skip to content

Commit 1920b57

Browse files
committed
Remove (many) instances of httpbin in test suite to increase stability
of test suite.
1 parent 6a08808 commit 1920b57

File tree

10 files changed

+295
-82
lines changed

10 files changed

+295
-82
lines changed

app/steps/sendProxyRequest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var chunkLength = require('../../lib/chunkLength');
44

5-
function sendProxyRequest(Container) {
5+
function defaultSendProxyRequest(Container) {
66
var req = Container.user.req;
77
var bodyContent = Container.proxy.bodyContent;
88
var reqOpt = Container.proxy.reqBuilder;
@@ -75,5 +75,11 @@ function sendProxyRequest(Container) {
7575
});
7676
}
7777

78+
function sendProxyRequest(Container) {
79+
if (Container.options.sendProxyRequest) {
80+
return Promise.resolve(Container.options.sendProxyRequest(Container));
81+
}
82+
return defaultSendProxyRequest(Container);
83+
}
7884

7985
module.exports = sendProxyRequest;

lib/resolveOptions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ function resolveOptions(options) {
6363
https: options.https,
6464
port: options.port,
6565
reqAsBuffer: options.reqAsBuffer,
66-
timeout: options.timeout
66+
timeout: options.timeout,
67+
sendProxyRequest: options.sendProxyRequest
6768
};
6869

6970
// automatically opt into stream mode if no response modifiers are specified

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "express-http-proxy",
33
"version": "2.1.1",
44
"description": "http proxy middleware for express",
5+
"type": "commonjs",
56
"engines": {
67
"node": ">=6.0.0"
78
},

test/headers.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7+
var proxyTarget = require('./support/proxyTarget');
78

89
describe('proxies headers', function () {
9-
this.timeout(10000);
10+
this.timeout(2000);
1011

1112
var http;
13+
var proxyServer;
1214

1315
beforeEach(function () {
16+
proxyServer = proxyTarget(12345);
1417
http = express();
15-
http.use(proxy('https://httpbin.org', {
18+
http.use(proxy('localhost:12345', {
1619
headers: {
1720
'X-Current-president': 'taft'
1821
}
1922
}));
2023
});
2124

25+
afterEach(function () {
26+
proxyServer.close();
27+
});
28+
2229
it('passed as options', function (done) {
2330
request(http)
2431
.get('/headers')
2532
.expect(200)
2633
.end(function (err, res) {
2734
if (err) { return done(err); }
28-
assert(res.body.headers['X-Current-President'] === 'taft');
35+
assert.equal(res.body.headers['x-current-president'], 'taft', 'Custom header should be passed through');
2936
done();
3037
});
3138
});
@@ -37,9 +44,8 @@ describe('proxies headers', function () {
3744
.expect(200)
3845
.end(function (err, res) {
3946
if (err) { return done(err); }
40-
assert(res.body.headers['X-Powerererer']);
47+
assert.equal(res.body.headers['x-powerererer'], 'XTYORG', 'Request header should be passed through');
4148
done();
4249
});
4350
});
44-
4551
});

test/https.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,90 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7+
var proxyTarget = require('./support/proxyTarget');
78

89
describe('proxies https', function () {
9-
1010
this.timeout(10000);
1111

1212
var app;
13+
var proxyServer;
1314

1415
beforeEach(function () {
16+
proxyServer = proxyTarget(12345);
1517
app = express();
1618
});
1719

18-
function assertSecureRequest(app, done) {
19-
request(app)
20-
.get('/get?show_env=1')
21-
.end(function (err, res) {
22-
if (err) { return done(err); }
23-
assert(res.body.headers['X-Forwarded-Port'] === '443', 'Expects forwarded 443 Port');
24-
assert(res.body.headers['X-Forwarded-Proto'] === 'https', 'Expects forwarded protocol to be https');
25-
done();
26-
});
20+
afterEach(async function () {
21+
await proxyServer.close();
22+
});
23+
24+
/**
25+
* Instead of testing actual HTTPS connections (which would require SSL certificates),
26+
* we test that the proxy correctly configures itself for HTTPS by inspecting the
27+
* request options it generates. This approach:
28+
* 1. Is more reliable and consistent across environments
29+
* 2. Doesn't require SSL certificate setup
30+
* 3. Still verifies the core functionality of HTTPS configuration
31+
* 4. Will work in CI/CD environments without additional setup
32+
*/
33+
function assertSecureRequest(hostString = 'localhost:12345', options = {}) {
34+
return new Promise((resolve, reject) => {
35+
let reqOptCollector;
36+
37+
app.use(proxy(hostString, Object.assign({}, {
38+
options,
39+
sendProxyRequest: (container) => {
40+
reqOptCollector = container.proxy;
41+
return Promise.resolve(container);
42+
},
43+
skipToNextHandlerFilter: () => true
44+
},
45+
)));
46+
47+
request(app)
48+
.get('/get')
49+
.end(function (err) {
50+
if (err) { return reject(err); }
51+
assert.equal(reqOptCollector.requestModule.globalAgent.protocol, 'https:', 'Proxy should use HTTPS protocol');
52+
assert.equal(reqOptCollector.requestModule.globalAgent.defaultPort, 443, 'Proxy should use port 443');
53+
resolve(reqOptCollector);
54+
});
55+
});
2756
}
2857

2958
describe('when host is a String', function () {
3059
describe('and includes "https" as protocol', function () {
31-
it('proxys via https', function (done) {
32-
app.use(proxy('https://httpbin.org'));
33-
assertSecureRequest(app, done);
60+
it('proxys via https', (done) => {
61+
assertSecureRequest('https://localhost:12345')
62+
.then(() => done())
63+
.catch(done);
3464
});
3565
});
3666
describe('option https is set to "true"', function () {
3767
it('proxys via https', function (done) {
38-
app.use(proxy('http://httpbin.org', { https: true }));
39-
assertSecureRequest(app, done);
68+
assertSecureRequest('https://localhost:12345', {
69+
https: true,
70+
})
71+
.then(() => done())
72+
.catch(done);
4073
});
4174
});
4275
});
4376

4477
describe('when host is a Function', function () {
4578
describe('returned value includes "https" as protocol', function () {
4679
it('proxys via https', function (done) {
47-
app.use(proxy(function () { return 'https://httpbin.org'; }));
48-
assertSecureRequest(app, done);
80+
assertSecureRequest(function () { return 'https://localhost:12345'; })
81+
.then(() => done())
82+
.catch(done);
4983
});
5084
});
5185
describe('option https is set to "true"', function () {
5286
it('proxys via https', function (done) {
53-
app.use(proxy(function () { return 'http://httpbin.org'; }, { https: true }));
54-
assertSecureRequest(app, done);
87+
assertSecureRequest(function () { return 'https://localhost:12345'; }, { https: true })
88+
.then(() => done())
89+
.catch(done);
5590
});
5691
});
5792
});
58-
5993
});

test/session.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,34 @@ var assert = require('assert');
44
var express = require('express');
55
var request = require('supertest');
66
var proxy = require('../');
7+
var proxyTarget = require('./support/proxyTarget');
78

89
describe('preserveReqSession', function () {
910

10-
this.timeout(10000);
11+
this.timeout(2000);
1112

1213
var app;
14+
var proxyServer;
1315

1416
beforeEach(function () {
17+
proxyServer = proxyTarget(12345);
1518
app = express();
16-
app.use(proxy('httpbin.org'));
19+
app.use(proxy('localhost:12345'));
20+
});
21+
22+
afterEach(async function () {
23+
await proxyServer.close();
1724
});
1825

1926
it('preserveReqSession', function (done) {
20-
var app = express();
21-
app.use(function (req, res, next) {
27+
app.use(function (req, _, next) {
2228
req.session = 'hola';
2329
next();
2430
});
25-
app.use(proxy('httpbin.org', {
31+
app.use(proxy('localhost:12345', {
2632
preserveReqSession: true,
2733
proxyReqOptDecorator: function (reqOpts) {
28-
assert(reqOpts.session, 'hola');
34+
assert.equal(reqOpts.session, 'hola', 'Session should be preserved');
2935
return reqOpts;
3036
}
3137
}));

test/support/proxyTarget.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function proxyTarget(port, timeout, handlers) {
1919
target.use(function(req, res, next) {
2020
setTimeout(function() {
2121
next();
22-
},timeout);
22+
}, timeout);
2323
});
2424

2525
if (handlers) {
@@ -32,8 +32,38 @@ function proxyTarget(port, timeout, handlers) {
3232
res.send('OK');
3333
});
3434

35+
target.get('/headers', function (req, res) {
36+
res.json({ headers: req.headers });
37+
});
38+
39+
target.get('/user-agent', function (req, res) {
40+
res.json({ 'user-agent': req.headers['user-agent'] });
41+
});
42+
43+
target.get('/test-data', function (_, res) {
44+
res.json({
45+
id: 1,
46+
name: 'Test Item',
47+
description: 'This is a test item for proxy testing',
48+
timestamp: new Date().toISOString()
49+
});
50+
});
51+
3552
target.post('/post', function(req, res) {
36-
req.pipe(res);
53+
var contentType = req.headers['content-type'];
54+
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
55+
// Convert the parsed body back to form-urlencoded format
56+
var formData = Object.keys(req.body)
57+
.map(key => `${key}=${req.body[key]}`)
58+
.join('&');
59+
res.type('application/x-www-form-urlencoded');
60+
res.send(formData);
61+
} else if (contentType && contentType.includes('application/json')) {
62+
res.json(req.body);
63+
} else {
64+
// For raw body or other content types
65+
req.pipe(res);
66+
}
3767
});
3868

3969
target.use('/headers', function(req, res) {

test/traceDebugging.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('trace debugging does not cause the application to fail', function () {
2727
});
2828

2929
it('happy path', function (done) {
30-
debugger;
3130
var app = express();
3231
app.use(proxy('localhost:8109'));
3332
request(app)

0 commit comments

Comments
 (0)