From 4ac3e24f8c3752c4691b08d9285fda4ac8932f6d Mon Sep 17 00:00:00 2001 From: mwcz Date: Thu, 25 Jun 2020 11:14:41 -0400 Subject: [PATCH] respond with a 502 when remote is unreachable --- app/router.js | 7 +++++-- spec/helpers/configs/bad-gateway/spandx.config.js | 10 ++++++++++ spec/spandx/spandxSpec.js | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 spec/helpers/configs/bad-gateway/spandx.config.js diff --git a/app/router.js b/app/router.js index 831edd8c..3e2e23b3 100644 --- a/app/router.js +++ b/app/router.js @@ -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 @@ -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 { diff --git a/spec/helpers/configs/bad-gateway/spandx.config.js b/spec/helpers/configs/bad-gateway/spandx.config.js new file mode 100644 index 00000000..8471a14e --- /dev/null +++ b/spec/helpers/configs/bad-gateway/spandx.config.js @@ -0,0 +1,10 @@ +module.exports = { + host: "localhost", + port: 1337, + silent: true, + routes: { + "/": { + host: "http://localhost:4014" + } + } +}; diff --git a/spec/spandx/spandxSpec.js b/spec/spandx/spandxSpec.js index 7bbd062a..40639d1b 100644 --- a/spec/spandx/spandxSpec.js +++ b/spec/spandx/spandxSpec.js @@ -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", () => {