forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-agent.js
236 lines (195 loc) · 6.65 KB
/
proxy-agent.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict'
const { test } = require('tap')
const { request, setGlobalDispatcher, getGlobalDispatcher } = require('..')
const { URL } = require('url')
const { InvalidArgumentError } = require('../lib/core/errors')
const ProxyAgent = require('../lib/proxy-agent')
const { createServer } = require('http')
const proxy = require('proxy')
const { once } = require('events')
const nodeMajor = Number(process.versions.node.split('.', 1)[0])
test('should throw error when no uri is provided', (t) => {
t.plan(2)
t.throws(() => new ProxyAgent(), InvalidArgumentError)
t.throws(() => new ProxyAgent({}), InvalidArgumentError)
})
test('should accept string and object as options', (t) => {
t.plan(2)
t.doesNotThrow(() => new ProxyAgent('http://example.com'))
t.doesNotThrow(() => new ProxyAgent({ uri: 'http://example.com' }))
})
test('use proxy-agent to connect through proxy', async (t) => {
t.plan(6)
const server = await buildServer()
const proxy = await buildProxy()
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent(proxyUrl)
const parsedOrigin = new URL(serverUrl)
proxy.on('request', () => {
t.pass('should call proxy')
})
server.on('request', (req, res) => {
t.equal(req.url, '/')
t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const {
statusCode,
headers,
body
} = await request(serverUrl, { dispatcher: proxyAgent })
const json = await body.json()
t.equal(statusCode, 200)
t.same(json, { hello: 'world' })
t.equal(headers.connection, 'keep-alive', 'should remain the connection open')
server.close()
proxy.close()
proxyAgent.close()
})
test('use proxy-agent to connect through proxy using path with params', async (t) => {
t.plan(6)
const server = await buildServer()
const proxy = await buildProxy()
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent(proxyUrl)
const parsedOrigin = new URL(serverUrl)
proxy.on('request', () => {
t.pass('should call proxy')
})
server.on('request', (req, res) => {
t.equal(req.url, '/hello?foo=bar')
t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const {
statusCode,
headers,
body
} = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent })
const json = await body.json()
t.equal(statusCode, 200)
t.same(json, { hello: 'world' })
t.equal(headers.connection, 'keep-alive', 'should remain the connection open')
server.close()
proxy.close()
proxyAgent.close()
})
test('use proxy-agent with auth', async (t) => {
t.plan(7)
const server = await buildServer()
const proxy = await buildProxy()
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent(proxyUrl)
const parsedOrigin = new URL(serverUrl)
proxy.authenticate = function (req, fn) {
t.pass('authentication should be called')
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
}
proxy.on('request', () => {
t.pass('proxy should be called')
})
server.on('request', (req, res) => {
t.equal(req.url, '/hello?foo=bar')
t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const {
statusCode,
headers,
body
} = await request(
serverUrl + '/hello?foo=bar',
{
headers: {
'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`
},
dispatcher: proxyAgent
}
)
const json = await body.json()
t.equal(statusCode, 200)
t.same(json, { hello: 'world' })
t.equal(headers.connection, 'keep-alive', 'should remain the connection open')
server.close()
proxy.close()
proxyAgent.close()
})
test('use proxy-agent with setGlobalDispatcher', async (t) => {
t.plan(6)
const defaultDispatcher = getGlobalDispatcher()
const server = await buildServer()
const proxy = await buildProxy()
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent(proxyUrl)
const parsedOrigin = new URL(serverUrl)
setGlobalDispatcher(proxyAgent)
t.teardown(() => setGlobalDispatcher(defaultDispatcher))
proxy.on('request', () => {
t.pass('should call proxy')
})
server.on('request', (req, res) => {
t.equal(req.url, '/hello?foo=bar')
t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})
const {
statusCode,
headers,
body
} = await request(serverUrl + '/hello?foo=bar')
const json = await body.json()
t.equal(statusCode, 200)
t.same(json, { hello: 'world' })
t.equal(headers.connection, 'keep-alive', 'should remain the connection open')
server.close()
proxy.close()
proxyAgent.close()
})
test('ProxyAgent correctly sends headers when using fetch - #1355', { skip: nodeMajor < 16 }, async (t) => {
const { getGlobalDispatcher, setGlobalDispatcher, fetch } = require('../index')
const expectedHeaders = {
host: '127.0.0.1:9000',
connection: 'keep-alive',
'test-header': 'value',
accept: '*/*',
'accept-language': '*',
'sec-fetch-mode': 'cors',
'user-agent': 'undici',
'accept-encoding': 'gzip, deflate'
}
const oldDispatcher = getGlobalDispatcher()
const server = createServer((req, res) => {
t.same(req.headers, expectedHeaders)
res.end('goodbye')
}).listen(0)
t.teardown(server.close.bind(server))
await once(server, 'listening')
setGlobalDispatcher(new ProxyAgent(`http://localhost:${server.address().port}`))
await fetch('http://127.0.0.1:9000', {
headers: { 'Test-header': 'value' }
})
setGlobalDispatcher(oldDispatcher)
t.end()
})
function buildServer () {
return new Promise((resolve) => {
const server = createServer()
server.listen(0, () => resolve(server))
})
}
function buildProxy (listener) {
return new Promise((resolve) => {
const server = listener
? proxy(createServer(listener))
: proxy(createServer())
server.listen(0, () => resolve(server))
})
}