forked from sagemathinc/http-proxy-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-https-to-https.test.ts
More file actions
65 lines (55 loc) · 1.91 KB
/
Copy pathproxy-https-to-https.test.ts
File metadata and controls
65 lines (55 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
pnpm test proxy-https-to-https.test.ts
*/
import * as https from "node:https";
import * as httpProxy from "../..";
import getPort from "../get-port";
import { join } from "node:path";
import { readFile } from "node:fs/promises";
import fetch from "node-fetch";
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
const fixturesDir = join(__dirname, "..", "fixtures");
describe("Basic example of proxying over HTTPS to a target HTTPS server", () => {
let ports: Record<'https' | 'proxy', number>;
beforeAll(async () => {
// Gets ports
ports = { https: await getPort(), proxy: await getPort() };
});
const servers: any = {};
let ssl: { key: string; cert: string };
it("Create the target HTTPS server", async () => {
ssl = {
key: await readFile(join(fixturesDir, "agent2-key.pem"), "utf8"),
cert: await readFile(join(fixturesDir, "agent2-cert.pem"), "utf8"),
};
servers.https = https
.createServer(ssl, (_req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.write("hello over https\n");
res.end();
})
.listen(ports.https);
});
it("Create the HTTPS proxy server", async () => {
servers.proxy = httpProxy
.createServer({
target: `https://localhost:${ports.https}`,
ssl,
// without secure false, clients will fail and this is broken:
secure: false,
})
.listen(ports.proxy);
});
it("Use fetch to test direct non-proxied https server", async () => {
const r = await (await fetch(`https://localhost:${ports.https}`)).text();
expect(r).toContain("hello over https");
});
it("Use fetch to test the proxy server", async () => {
const r = await (await fetch(`https://localhost:${ports.proxy}`)).text();
expect(r).toContain("hello over https");
});
afterAll(async () => {
// cleanup
Object.values(servers).map((x: any) => x?.close());
});
});