Skip to content

Commit f618891

Browse files
committed
Allow any node-http-proxy options to be passed through via proxyOptions - re: BrowserSync/browser-sync#430
1 parent 7bf9b57 commit f618891

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var https = require("https");
1313
*/
1414
if (!module.parent) {
1515
handleCli(meow({
16-
help: fs.readFileSync(__dirname + "/help.txt", "utf8")
16+
help: fs.readFileSync(path.join(__dirname, "/help.txt"), "utf8")
1717
}));
1818
}
1919

lib/config.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ var defaults = Immutable.fromJS({
4848
/**
4949
*
5050
*/
51-
middleware: []
51+
middleware: [],
52+
/**
53+
* Proxy Options.
54+
*/
55+
proxyOptions: {
56+
xfwd: false,
57+
headers: {},
58+
target: "",
59+
// secure: false - This forces http-proxy to set rejectUnauthorized: false which allows self-signed certs
60+
// which we are fine with considering this is a development tool, not to be used in production. :)
61+
secure: false
62+
}
5263
});
5364

5465
/**

lib/server.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ module.exports = function (config) {
1515
/**
1616
* Proxy server for final requests
1717
*/
18-
var proxy = httpProxy.createProxyServer({
19-
target: config.get("target"),
20-
headers: config.get("reqHeaders")(config.toJS()),
21-
secure: false // What? This forces http-proxy to set rejectUnauthorized: false which allows self-signed certs
22-
// We are fine with this, considering this is a development tool, not to be used in production. :)
23-
});
18+
var proxy = httpProxy.createProxyServer(config.get('proxyOptions').merge({
19+
target: config.get("target"),
20+
headers: config.get("reqHeaders")(config.toJS())
21+
}).toJS());
2422

2523
/**
2624
* Proxy errors out to user errHandler
@@ -96,4 +94,4 @@ module.exports = function (config) {
9694
}
9795

9896
return app;
99-
};
97+
};

test/src/server/proxyOptions.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var foxy = require("../../../index");
2+
var connect = require("connect");
3+
var sinon = require("sinon");
4+
var http = require("http");
5+
var assert = require("chai").assert;
6+
7+
var output = `
8+
<!doctype html>
9+
<html lang="en-US">
10+
<head>
11+
<meta charset="UTF-8">
12+
<title></title>
13+
</head>
14+
<body>
15+
Hi there
16+
</body>
17+
</html>
18+
`;
19+
20+
describe("Adding node-http-proxy options through configuration", () => {
21+
22+
it("should add xfwd headers if true in options", done => {
23+
let config, app, server, proxyServer, port;
24+
let path = "/templates/page1.html";
25+
config = {
26+
proxyOptions: {
27+
xfwd: true
28+
}
29+
};
30+
app = connect();
31+
app.use(path, (req, res) => {
32+
assert.equal(req.headers["x-forwarded-port"], port);
33+
assert.ok(req.headers["x-forwarded-for"]);
34+
res.end(output);
35+
});
36+
server = http.createServer(app).listen();
37+
proxyServer = foxy(`http://localhost:${server.address().port}`, config).listen();
38+
port = proxyServer.address().port;
39+
let options = {
40+
hostname: "localhost",
41+
method: "GET",
42+
headers: {
43+
accept: "text/html"
44+
},
45+
port,
46+
path
47+
};
48+
http.get(options, (res) => {
49+
res.on("data", () => done());
50+
server.close();
51+
});
52+
});
53+
});
54+

0 commit comments

Comments
 (0)