Skip to content

Instantly share code, notes, and snippets.

@cburgmer
Created April 16, 2021 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburgmer/293d22e609be1b8adcd95bf575d92bc6 to your computer and use it in GitHub Desktop.
Save cburgmer/293d22e609be1b8adcd95bf575d92bc6 to your computer and use it in GitHub Desktop.
Work around Wiremock not supporting server send events (SSE) when recording HTTP traffic. This proxy will sit between Wiremock and target and close the connection proactively from the client side.
// Proxy that will close any connection for a response with the partial payload "event: end"
// This allows us to treat server send events (SSE) as if the server would close the connection, easing integration with regular HTTP tools
const http = require("http"),
httpProxy = require("http-proxy"),
zlib = require("zlib");
const target = process.env.TARGET_BASE_URL || "http://localhost:8000",
port = process.env.PORT || 3333;
const proxy = httpProxy.createProxyServer({});
proxy.on("proxyReq", function (proxyReq, req, res, options) {
// Is it wiremock that adds this header? Let's keep it plain text
proxyReq.setHeader("Accept-Encoding", "");
});
proxy.on("proxyRes", function (proxyRes, req, res) {
proxyRes.on("data", (d) => {
if (/event: end/.test(d.toString())) {
setTimeout(() => res.end(), 10);
}
});
});
console.log("Opening on connection", port, "proxying", target);
http
.createServer(function (req, res) {
proxy.web(req, res, { target });
})
.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment