|
| 1 | +import * as httpProxy from "../.."; |
| 2 | +import * as http from "node:http"; |
| 3 | +import * as http2 from "node:http2"; |
| 4 | +import getPort from "../get-port"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { readFileSync } from "node:fs"; |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | + |
| 9 | +const ports: { [port: string]: number } = {}; |
| 10 | +let portIndex = -1; |
| 11 | +const gen = {} as { port: number }; |
| 12 | +Object.defineProperty(gen, "port", { |
| 13 | + get: function get() { |
| 14 | + portIndex++; |
| 15 | + return ports[portIndex]; |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +describe("HTTP2 to HTTP", () => { |
| 20 | + it("creates some ports", async () => { |
| 21 | + for (let n = 0; n < 50; n++) { |
| 22 | + ports[n] = await getPort(); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + it("should proxy the request, then send back the response", () => new Promise<void>(done => { |
| 27 | + const ports = { source: gen.port, proxy: gen.port }; |
| 28 | + const source = http |
| 29 | + .createServer((req, res) => { |
| 30 | + expect(req.method).toEqual("GET"); |
| 31 | + expect(req.headers.host?.split(":")[1]).toEqual(`${ports.proxy}`); |
| 32 | + res.writeHead(200, { "Content-Type": "text/plain" }); |
| 33 | + res.end("Hello from " + ports.source); |
| 34 | + }) |
| 35 | + .listen(ports.source); |
| 36 | + |
| 37 | + const proxy = httpProxy |
| 38 | + .createProxyServer({ |
| 39 | + target: "http://127.0.0.1:" + ports.source, |
| 40 | + ssl: { |
| 41 | + key: readFileSync( |
| 42 | + join(__dirname, "..", "fixtures", "agent2-key.pem"), |
| 43 | + ), |
| 44 | + cert: readFileSync( |
| 45 | + join(__dirname, "..", "fixtures", "agent2-cert.pem"), |
| 46 | + ), |
| 47 | + ciphers: "AES128-GCM-SHA256", |
| 48 | + }, |
| 49 | + }) |
| 50 | + .listen(ports.proxy); |
| 51 | + |
| 52 | + const client = http2.connect(`https://localhost:${ports.proxy}`) |
| 53 | + const req = client.request({ ':path': '/' }); |
| 54 | + req.on('response', (headers, _flags) => { |
| 55 | + expect(headers[':status']).toEqual(200); |
| 56 | + req.setEncoding('utf8'); |
| 57 | + req.on('data', (chunk) => { |
| 58 | + expect(chunk.toString()).toEqual("Hello from " + ports.source); |
| 59 | + }); |
| 60 | + req.on('end', () => { |
| 61 | + source.close(); |
| 62 | + proxy.close(); |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + req.end(); |
| 67 | + })); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("HTTP2 to HTTP using own server", () => { |
| 71 | + it("should proxy the request, then send back the response", () => new Promise<void>(done => { |
| 72 | + const ports = { source: gen.port, proxy: gen.port }; |
| 73 | + const source = http |
| 74 | + .createServer((req, res) => { |
| 75 | + expect(req.method).toEqual("GET"); |
| 76 | + expect(req.headers.host?.split(":")[1]).toEqual(`${ports.proxy}`); |
| 77 | + res.writeHead(200, { "Content-Type": "text/plain" }); |
| 78 | + res.end("Hello from " + ports.source); |
| 79 | + }) |
| 80 | + .listen(ports.source); |
| 81 | + |
| 82 | + const proxy = httpProxy.createServer({ |
| 83 | + agent: new http.Agent({ maxSockets: 2 }), |
| 84 | + }); |
| 85 | + |
| 86 | + const ownServer = http2 |
| 87 | + .createSecureServer( |
| 88 | + { |
| 89 | + key: readFileSync( |
| 90 | + join(__dirname, "..", "fixtures", "agent2-key.pem"), |
| 91 | + ), |
| 92 | + cert: readFileSync( |
| 93 | + join(__dirname, "..", "fixtures", "agent2-cert.pem"), |
| 94 | + ), |
| 95 | + ciphers: "AES128-GCM-SHA256", |
| 96 | + }, |
| 97 | + (req, res) => { |
| 98 | + // @ts-expect-error -- ignore type incompatibility |
| 99 | + proxy.web(req, res, { |
| 100 | + target: "http://127.0.0.1:" + ports.source, |
| 101 | + }); |
| 102 | + }, |
| 103 | + ) |
| 104 | + .listen(ports.proxy); |
| 105 | + |
| 106 | + const client = http2.connect(`https://localhost:${ports.proxy}`) |
| 107 | + const req = client.request({ ':path': '/' }); |
| 108 | + req.on('response', (headers, _flags) => { |
| 109 | + expect(headers[':status']).toEqual(200); |
| 110 | + req.setEncoding('utf8'); |
| 111 | + req.on('data', (chunk) => { |
| 112 | + expect(chunk.toString()).toEqual("Hello from " + ports.source); |
| 113 | + }); |
| 114 | + req.on('end', () => { |
| 115 | + source.close(); |
| 116 | + ownServer.close(); |
| 117 | + done(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + req.end(); |
| 121 | + })); |
| 122 | +}); |
0 commit comments