-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
test-proxy.js
244 lines (208 loc) Β· 5.77 KB
/
test-proxy.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
237
238
239
240
241
242
243
244
'use strict'
var server = require('./server')
, request = require('../index')
, tape = require('tape')
var s = server.createServer()
, currResponseHandler
s.on('http://google.com/', function(req, res) {
currResponseHandler(req, res)
res.writeHeader(200)
res.end('ok')
})
var proxyEnvVars = [
'http_proxy',
'HTTP_PROXY',
'https_proxy',
'HTTPS_PROXY',
'no_proxy',
'NO_PROXY'
]
// Set up and run a proxy test. All environment variables pertaining to
// proxies will be deleted before each test. Specify environment variables as
// `options.env`; all other keys on `options` will be passed as additional
// options to `request`.
//
// If `responseHandler` is a function, it should perform asserts on the server
// response. It will be called with parameters (t, req, res). Otherwise,
// `responseHandler` should be truthy to indicate that the proxy should be used
// for this request, or falsy to indicate that the proxy should not be used for
// this request.
function runTest(name, options, responseHandler) {
tape(name, function(t) {
proxyEnvVars.forEach(function(v) {
delete process.env[v]
})
if (options.env) {
for (var v in options.env) {
process.env[v] = options.env[v]
}
delete options.env
}
var called = false
currResponseHandler = function(req, res) {
if (responseHandler) {
called = true
t.equal(req.headers.host, 'google.com')
if (typeof responseHandler === 'function') {
responseHandler(t, req, res)
}
} else {
t.fail('proxy response should not be called')
}
}
var requestOpts = {
url: 'http://google.com'
}
for (var k in options) {
requestOpts[k] = options[k]
}
request(requestOpts, function(err, res, body) {
if (responseHandler && !called) {
t.fail('proxy response should be called')
}
t.equal(err, null)
t.equal(res.statusCode, 200)
if (responseHandler) {
if (body.length > 100) {
body = body.substring(0, 100)
}
t.equal(body, 'ok')
} else {
t.equal(/^<!doctype html>/i.test(body), true)
}
t.end()
})
})
}
tape('setup', function(t) {
s.listen(s.port, function() {
t.end()
})
})
// If the `runTest` function is changed, run the following command and make
// sure both of these tests fail:
//
// TEST_PROXY_HARNESS=y node tests/test-proxy.js
if (process.env.TEST_PROXY_HARNESS) {
runTest('should fail with "proxy response should not be called"', {
proxy : s.url
}, false)
runTest('should fail with "proxy response should be called"', {
proxy : null
}, true)
} else {
// Run the real tests
runTest('basic proxy', {
proxy : s.url,
headers : {
'proxy-authorization': 'Token Fooblez'
}
}, function(t, req, res) {
t.equal(req.headers['proxy-authorization'], 'Token Fooblez')
})
runTest('proxy auth without uri auth', {
proxy : 'http://user:pass@localhost:' + s.port
}, function(t, req, res) {
t.equal(req.headers['proxy-authorization'], 'Basic dXNlcjpwYXNz')
})
runTest('HTTP_PROXY environment variable', {
env : { HTTP_PROXY : s.url }
}, true)
runTest('http_proxy environment variable', {
env : { http_proxy : s.url }
}, true)
runTest('http_proxy with length of one more than the URL', {
env: {
HTTP_PROXY : s.url,
NO_PROXY: 'elgoog1.com' // one more char than google.com
}
}, true)
runTest('NO_PROXY hostnames are case insensitive', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'GOOGLE.COM'
}
}, false)
runTest('NO_PROXY ignored with explicit proxy passed', {
env : { NO_PROXY : '*' },
proxy : s.url
}, true)
runTest('NO_PROXY overrides HTTP_PROXY for specific hostname', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'google.com'
}
}, false)
runTest('no_proxy overrides HTTP_PROXY for specific hostname', {
env : {
HTTP_PROXY : s.url,
no_proxy : 'google.com'
}
}, false)
runTest('NO_PROXY does not override HTTP_PROXY if no hostnames match', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'foo.bar,bar.foo'
}
}, true)
runTest('NO_PROXY overrides HTTP_PROXY if a hostname matches', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'foo.bar,google.com'
}
}, false)
runTest('NO_PROXY allows an explicit port', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'google.com:80'
}
}, false)
runTest('NO_PROXY only overrides HTTP_PROXY if the port matches', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'google.com:1234'
}
}, true)
runTest('NO_PROXY=* should override HTTP_PROXY for all hosts', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : '*'
}
}, false)
runTest('NO_PROXY should override HTTP_PROXY for all subdomains', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'google.com'
},
headers : { host : 'www.google.com' }
}, false)
runTest('NO_PROXY should not override HTTP_PROXY for partial domain matches', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'oogle.com'
}
}, true)
runTest('NO_PROXY with port should not override HTTP_PROXY for partial domain matches', {
env : {
HTTP_PROXY : s.url,
NO_PROXY : 'oogle.com:80'
}
}, true)
runTest('proxy: null should override HTTP_PROXY', {
env : { HTTP_PROXY : s.url },
proxy : null,
timeout : 500
}, false)
runTest('uri auth without proxy auth', {
url : 'http://user:pass@google.com',
proxy : s.url
}, function(t, req, res) {
t.equal(req.headers['proxy-authorization'], undefined)
t.equal(req.headers.authorization, 'Basic dXNlcjpwYXNz')
})
}
tape('cleanup', function(t) {
s.close(function() {
t.end()
})
})