Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 335f649

Browse files
committed
test: add a test for Basic auth
1 parent d9fa69c commit 335f649

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,41 @@ describe('HttpProxyAgent', function () {
167167
done();
168168
});
169169
});
170+
it('should send the "Proxy-Authorization" request header', function (done) {
171+
// set a proxy authentication function for this test
172+
proxy.authenticate = function (req, fn) {
173+
// username:password is "foo:bar"
174+
fn(null, req.headers['proxy-authorization'] == 'Basic Zm9vOmJhcg==');
175+
};
176+
177+
// set HTTP "request" event handler for this test
178+
server.once('request', function (req, res) {
179+
res.end(JSON.stringify(req.headers));
180+
});
181+
182+
var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
183+
var proxyOpts = url.parse(proxyUri);
184+
proxyOpts.auth = 'foo:bar';
185+
var agent = new HttpProxyAgent(proxyOpts);
186+
187+
var opts = url.parse('http://127.0.0.1:' + serverPort);
188+
opts.agent = agent;
189+
190+
http.get(opts, function (res) {
191+
var data = '';
192+
res.setEncoding('utf8');
193+
res.on('data', function (b) {
194+
data += b;
195+
});
196+
res.on('end', function () {
197+
data = JSON.parse(data);
198+
assert.equal('127.0.0.1:' + serverPort, data.host);
199+
assert('via' in data);
200+
delete proxy.authenticate;
201+
done();
202+
});
203+
});
204+
});
170205
});
171206

172207
});

0 commit comments

Comments
 (0)