|
| 1 | +/* |
| 2 | +modify-response-middleware.test.ts -- Middleware which modifies response |
| 3 | +
|
| 4 | +pnpm test modify-response-middleware.test.ts |
| 5 | +*/ |
| 6 | + |
| 7 | +import * as httpProxy from "../.."; |
| 8 | +import * as http from "http"; |
| 9 | +import getPort from "../get-port"; |
| 10 | +import connect from "connect"; |
| 11 | + |
| 12 | +describe("Using the connect-gzip middleware from connect with http-proxy-3", () => { |
| 13 | + let ports; |
| 14 | + it("gets ports", async () => { |
| 15 | + ports = { http: await getPort(), proxy: await getPort() }; |
| 16 | + }); |
| 17 | + |
| 18 | + let servers: any = {}; |
| 19 | + it("creates target http server", () => { |
| 20 | + servers.http = http.createServer(async (_req, res) => { |
| 21 | + res.writeHead(200, { "Content-Type": "text/html" }); |
| 22 | + res.write("i am hosted at http-party"); |
| 23 | + res.end(); |
| 24 | + }); |
| 25 | + servers.http.listen(ports.http); |
| 26 | + }); |
| 27 | + |
| 28 | + it("creates connect server that uses a proxy as middleware to actually satisfy the request, after applying compression", () => { |
| 29 | + const proxy = httpProxy.createProxyServer({ |
| 30 | + target: `http://localhost:${ports.http}`, |
| 31 | + }); |
| 32 | + const rewrite = (_req, res, next) => { |
| 33 | + const _write = res.write; |
| 34 | + res.write = (data) => { |
| 35 | + _write.call(res, data.toString().replace("http-party", "cocalc")); |
| 36 | + }; |
| 37 | + next(); |
| 38 | + }; |
| 39 | + servers.app = connect().use(rewrite).use(proxy.web).listen(ports.proxy); |
| 40 | + }); |
| 41 | + |
| 42 | + it("test the http server", async () => { |
| 43 | + const a = await (await fetch(`http://localhost:${ports.http}`)).text(); |
| 44 | + expect(a).toContain("i am hosted at http-party"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("test the proxy server", async () => { |
| 48 | + const a = await (await fetch(`http://localhost:${ports.proxy}`)).text(); |
| 49 | + expect(a).toContain("i am hosted at cocalc"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("Clean up", () => { |
| 53 | + Object.values(servers).map((x: any) => x?.close()); |
| 54 | + }); |
| 55 | +}); |
0 commit comments