Skip to content

Commit

Permalink
fix: respond with a 502 when remote is unreachable
Browse files Browse the repository at this point in the history
Merge pull request #146 from redhataccess/bad-gateway
  • Loading branch information
mwcz committed Jun 25, 2020
2 parents 61334d1 + 4ac3e24 commit b877b27
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ priv.doProxy = (proxy, req, res, target, confProxy = null) => {

if (confProxy) {
const regex = RegExp(confProxy.pattern);

// if the target URL passes the regex test based on the
// pattern provided in the proxy.pattern property,
// add a new HttpsProxyAgent
Expand All @@ -44,7 +44,10 @@ priv.doProxy = (proxy, req, res, target, confProxy = null) => {

proxy.web(req, res, options, e => {
console.error(e);
res.writeHead(200, { "Content-Type": "text/plain" });
res.writeHead(502, { "Content-Type": "text/plain" });
res.write(
`HTTP 502 Bad gateway\n\nRequest to ${req.url} was proxied to ${target} which did not respond.`
);
res.end();
});
} else {
Expand Down
10 changes: 10 additions & 0 deletions spec/helpers/configs/bad-gateway/spandx.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
host: "localhost",
port: 1337,
silent: true,
routes: {
"/": {
host: "http://localhost:4014"
}
}
};
12 changes: 12 additions & 0 deletions spec/spandx/spandxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,18 @@ describe("spandx", () => {
server.close(done);
});
});
it("if the remote is unreachable, return bad gateway", async done => {
await spandx.init(
"../spec/helpers/configs/bad-gateway/spandx.config.js"
);
frisby
.get("http://localhost:1337/")
.expect("status", 502)
.expect("bodyContains", /bad gateway/i)
.done(() => {
done();
});
});
});

describe("routing order", () => {
Expand Down

0 comments on commit b877b27

Please sign in to comment.