-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
167 lines (144 loc) · 4.62 KB
/
index.test.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import fetchEnhanced, {TimeoutError} from "./index.ts";
import enableDestroy from "server-destroy";
import {createServer} from "node:http";
import nodeFetch from "node-fetch";
import {fetch as undiciFetch} from "undici";
import {promisify} from "node:util";
import getPort from "get-port";
import {createProxy} from "proxy";
import type {ProxyServer} from "proxy";
import type {AddressInfo} from "node:net";
import type {Server} from "node:http";
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms).unref());
function makeUrl(server: Server) {
const {port} = server.address() as AddressInfo;
return String(Object.assign(new URL("http://127.0.0.1"), {port})).replace(/\/$/, "");
}
type DestroyableServer = Server & {destroy?: () => void};
type DestroyableProxyServer = ProxyServer & {destroy?: () => void};
let server: DestroyableServer;
let proxyServer: DestroyableProxyServer;
let url: string;
let proxyUrl: string;
let serverConnects: number;
let proxyConnects: number;
beforeAll(async () => {
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
server = createServer(async (_, res) => {
await sleep(500);
res.statusCode = 204;
res.end();
});
enableDestroy(server);
await promisify(server.listen).bind(server)(await getPort(), "127.0.0.1");
url = makeUrl(server);
proxyServer = createProxy(createServer());
enableDestroy(proxyServer);
await promisify(proxyServer.listen).bind(proxyServer)();
proxyUrl = makeUrl(proxyServer);
process.env.HTTP_PROXY = proxyUrl;
process.env.HTTPS_PROXY = proxyUrl;
serverConnects = 0;
proxyConnects = 0;
server.on("connection", () => {
serverConnects++;
});
proxyServer.on("connection", () => {
proxyConnects++;
});
});
afterAll(() => {
server.destroy();
proxyServer.destroy();
});
describe("node-fetch", () => {
const fetch = fetchEnhanced(nodeFetch, {undici: false});
afterAll(() => {
serverConnects = 0;
proxyConnects = 0;
});
test("proxy working", async () => {
const res = await fetch(url, {method: "HEAD"});
expect(res.ok).toEqual(true);
expect(res.status).toEqual(204);
expect(proxyConnects).toEqual(1);
expect(serverConnects).toEqual(1);
});
test("timeout proxy", async () => {
try {
await fetch(url, {method: "HEAD", timeout: 50});
throw new Error("No error thrown");
} catch (err) {
if (!(err instanceof TimeoutError)) {
console.error(err);
}
expect(err).toBeInstanceOf(TimeoutError);
}
expect(proxyConnects).toEqual(2);
expect(serverConnects).toEqual(2);
});
test("timeout no proxy", async () => {
try {
await fetch(url, {method: "HEAD", timeout: 20, agentOpts: {noProxy: true}});
throw new Error("No error thrown");
} catch (err) {
if (!(err instanceof TimeoutError)) {
console.error(err);
}
expect(err).toBeInstanceOf(TimeoutError);
}
expect(proxyConnects).toBeLessThan(serverConnects);
});
test("no timeout", async () => {
const res = await fetch(url, {method: "HEAD", timeout: 1000, agentOpts: {noProxy: true}});
expect(res.ok).toEqual(true);
expect(res.status).toEqual(204);
expect(proxyConnects).toBeLessThan(serverConnects);
});
});
describe("undici", () => {
const fetch = fetchEnhanced(undiciFetch, {undici: true});
afterAll(() => {
serverConnects = 0;
proxyConnects = 0;
});
test("proxy working", async () => {
const res = await fetch(url, {method: "HEAD"});
expect(res.ok).toEqual(true);
expect(res.status).toEqual(204);
expect(proxyConnects).toEqual(1);
expect(serverConnects).toEqual(1);
});
test("timeout proxy", async () => {
try {
await fetch(url, {method: "HEAD", timeout: 50});
throw new Error("No error thrown");
} catch (err) {
if (!(err instanceof TimeoutError)) {
console.error(err);
}
expect(err).toBeInstanceOf(TimeoutError);
}
expect(proxyConnects).toEqual(2);
expect(serverConnects).toEqual(2);
});
test("timeout no proxy", async () => {
try {
await fetch(url, {method: "HEAD", timeout: 20, agentOpts: {noProxy: true}});
throw new Error("No error thrown");
} catch (err) {
if (!(err instanceof TimeoutError)) {
console.error(err);
}
expect(err).toBeInstanceOf(TimeoutError);
}
expect(proxyConnects).toBeLessThan(serverConnects);
});
test("no timeout", async () => {
const res = await fetch(url, {method: "HEAD", timeout: 1000, agentOpts: {noProxy: true}});
expect(res.ok).toEqual(true);
expect(res.status).toEqual(204);
expect(proxyConnects).toBeLessThan(serverConnects);
});
});