Skip to content

Commit b877b27

Browse files
committed
fix: respond with a 502 when remote is unreachable
Merge pull request #146 from redhataccess/bad-gateway
2 parents 61334d1 + 4ac3e24 commit b877b27

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

app/router.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ priv.doProxy = (proxy, req, res, target, confProxy = null) => {
3333

3434
if (confProxy) {
3535
const regex = RegExp(confProxy.pattern);
36-
36+
3737
// if the target URL passes the regex test based on the
3838
// pattern provided in the proxy.pattern property,
3939
// add a new HttpsProxyAgent
@@ -44,7 +44,10 @@ priv.doProxy = (proxy, req, res, target, confProxy = null) => {
4444

4545
proxy.web(req, res, options, e => {
4646
console.error(e);
47-
res.writeHead(200, { "Content-Type": "text/plain" });
47+
res.writeHead(502, { "Content-Type": "text/plain" });
48+
res.write(
49+
`HTTP 502 Bad gateway\n\nRequest to ${req.url} was proxied to ${target} which did not respond.`
50+
);
4851
res.end();
4952
});
5053
} else {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
host: "localhost",
3+
port: 1337,
4+
silent: true,
5+
routes: {
6+
"/": {
7+
host: "http://localhost:4014"
8+
}
9+
}
10+
};

spec/spandx/spandxSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,18 @@ describe("spandx", () => {
10101010
server.close(done);
10111011
});
10121012
});
1013+
it("if the remote is unreachable, return bad gateway", async done => {
1014+
await spandx.init(
1015+
"../spec/helpers/configs/bad-gateway/spandx.config.js"
1016+
);
1017+
frisby
1018+
.get("http://localhost:1337/")
1019+
.expect("status", 502)
1020+
.expect("bodyContains", /bad gateway/i)
1021+
.done(() => {
1022+
done();
1023+
});
1024+
});
10131025
});
10141026

10151027
describe("routing order", () => {

0 commit comments

Comments
 (0)