-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (184 loc) · 6.92 KB
/
index.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict'
const t = require('tap')
const { HttpAgent, HttpsAgent } = require('../')
const Client = require('./fixtures/client.js')
const Proxy = require('./fixtures/proxy.js')
const Server = require('./fixtures/server.js')
// if we were started without any extra parameters, then we're the root test and we need to
// spawn the matrix of child tests. these are written in this specific way because the goal
// of this package is for the behavior to be as similar as possible regardless of proxy usage
// our matrix of test conditions looks like:
// no proxy, http server
// no proxy, https server
// http proxy, http server
// http proxy, https server
// https proxy, http server
// https proxy, https server
if (process.argv.length <= 2) {
for (const proxyType of ['null', 'http', 'https']) {
for (const serverType of ['http', 'https']) {
t.spawn(process.execPath,
[__filename, proxyType, serverType],
`proxy = ${proxyType}, server = ${serverType}`)
}
}
return
}
const [,, proxyType, serverType] = process.argv
const agentGenerator = {
http: (options) => new HttpAgent({ ...options, rejectUnauthorized: false }),
https: (options) => new HttpsAgent({ ...options, rejectUnauthorized: false }),
}
// returned when proxyType is 'null'
const nullProxy = {
start: () => nullProxy,
stop: () => {},
sockets: [],
address: false,
fake: true,
}
const proxyGenerator = {
null: () => nullProxy,
http: (options) => new Proxy(options),
https: (options) => new Proxy({ ...options, tls: true }),
}
const serverGenerator = {
http: (options) => new Server(options),
https: (options) => new Server({ ...options, tls: true }),
}
t.test('single request', async (t) => {
const proxy = await proxyGenerator[proxyType]().start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
const res = await client.get('/')
t.equal(res.statusCode, 200)
t.equal(res.headers.connection, 'keep-alive', 'keep-alive by default')
t.equal(res.body, 'OK!')
t.equal(res.req.agent, agent)
})
t.test('can disable keep-alive', async (t) => {
const proxy = await proxyGenerator[proxyType]().start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address, keepAlive: false })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
const res = await client.get('/')
t.equal(res.statusCode, 200)
t.equal(res.headers.connection, 'close', 'keep-alive can be disabled')
t.equal(res.body, 'OK!')
t.equal(res.req.agent, agent)
})
t.test('can limit sockets', async (t) => {
const proxy = await proxyGenerator[proxyType]().start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address, maxSockets: 1 })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
if (!proxy.fake) {
// when a proxy is in use, the createConnection method of the agent becomes
// async. in order to limit sockets appropriately, they can't all come on the
// same tick, so we send one request by itself, then a batch after the socket
// has been established
const initialRes = await client.get('/')
t.equal(initialRes.statusCode, 200)
t.equal(initialRes.headers.connection, 'keep-alive', 'keep-alive by default')
t.equal(initialRes.body, 'OK!')
t.equal(initialRes.req.agent, agent)
}
const results = await Promise.all([
client.get('/'),
client.get('/'),
client.get('/'),
client.get('/'),
client.get('/'),
])
for (const res of results) {
t.equal(res.statusCode, 200)
t.equal(res.headers.connection, 'keep-alive')
t.equal(res.body, 'OK!')
t.equal(res.req.agent, agent)
}
if (!proxy.fake) {
t.equal(proxy.sockets.size, 1, 'only made 1 connection to proxy')
}
t.equal(server.sockets.size, 1, 'only made 1 connection to server')
})
// all tests after this point are for specific behaviors found in a proxy, so if this run is not
// using a proxy, we're done and we can just return right now
if (proxyType === 'null') {
return
}
t.test('can send auth to proxy', async (t) => {
const proxy = await proxyGenerator[proxyType]({ auth: true }).start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
const res = await client.get('/')
t.equal(res.statusCode, 200)
t.equal(res.headers.connection, 'keep-alive', 'keep-alive by default')
t.equal(res.body, 'OK!')
t.equal(res.req.agent, agent)
})
t.test('can send auth to both proxy and server', async (t) => {
const proxy = await proxyGenerator[proxyType]({ auth: true }).start()
const server = await serverGenerator[serverType]({ auth: true }).start()
const agent = agentGenerator[serverType]({ proxy: proxy.address })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
const res = await client.get('/')
t.equal(res.statusCode, 200)
t.equal(res.headers.connection, 'keep-alive', 'keep-alive by default')
t.equal(res.body, 'OK!')
t.equal(res.req.agent, agent)
})
t.test('invalid proxy auth rejects', async (t) => {
const proxy = await proxyGenerator[proxyType]({ auth: true }).start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address.replace('@', 'broken@') })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
await t.rejects(client.get('/'), { code: 'EINVALIDRESPONSE' })
})
t.test('rejects if proxy does not return 200', async (t) => {
const proxy = await proxyGenerator[proxyType]({ failConnect: true }).start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
await t.rejects(client.get('/'), { code: 'EINVALIDRESPONSE' })
})
t.test('rejects if proxy connection times out', async (t) => {
const proxy = await proxyGenerator[proxyType]({ failTimeout: true }).start()
const server = await serverGenerator[serverType]().start()
const agent = agentGenerator[serverType]({ proxy: proxy.address, timeout: 100 })
const client = new Client(agent, server.address)
t.teardown(async () => {
await server.stop()
await proxy.stop()
})
await t.rejects(client.get('/'), { code: 'ETIMEDOUT' })
})