Skip to content

Commit e426910

Browse files
Protocol not parsed when setting proxy config from env vars (#3070)
* Fixing proxy protocol config when parsed from env vars * Adding instructions to specify proxy protocol when setting proxy config * Moved HTTPS proxy config instruction closer to example * Clear https_proxy env var so as to not impact other tests
1 parent c7329fe commit e426910

File tree

5 files changed

+151
-3
lines changed

5 files changed

+151
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ These are the available config options for making requests. Only the `url` is re
422422
httpAgent: new http.Agent({ keepAlive: true }),
423423
httpsAgent: new https.Agent({ keepAlive: true }),
424424

425-
// `proxy` defines the hostname and port of the proxy server.
425+
// `proxy` defines the hostname, port, and protocol of the proxy server.
426426
// You can also define your proxy using the conventional `http_proxy` and
427427
// `https_proxy` environment variables. If you are using environment variables
428428
// for your proxy configuration, you can also define a `no_proxy` environment
@@ -432,7 +432,9 @@ These are the available config options for making requests. Only the `url` is re
432432
// supplies credentials.
433433
// This will set an `Proxy-Authorization` header, overwriting any existing
434434
// `Proxy-Authorization` custom headers you have set using `headers`.
435+
// If the proxy server uses HTTPS, then you must set the protocol to `https`.
435436
proxy: {
437+
protocol: 'https',
436438
host: '127.0.0.1',
437439
port: 9000,
438440
auth: {

lib/adapters/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ module.exports = function httpAdapter(config) {
151151
});
152152
}
153153

154-
155154
if (shouldProxy) {
156155
proxy = {
157156
host: parsedProxyUrl.hostname,
158-
port: parsedProxyUrl.port
157+
port: parsedProxyUrl.port,
158+
protocol: parsedProxyUrl.protocol
159159
};
160160

161161
if (parsedProxyUrl.auth) {

test/unit/adapters/cert.pem

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICpDCCAYwCCQDbqELLwgbPdDANBgkqhkiG9w0BAQUFADAUMRIwEAYDVQQDDAls
3+
b2NhbGhvc3QwHhcNMjAwNjI2MjIxMTQ3WhcNNDcxMTExMjIxMTQ3WjAUMRIwEAYD
4+
VQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD6
5+
Ogt99/dZ0UgbCuVV1RZ9n28Ov3DzrJCkjperQoXomIq3Fr4RUI1a2rwe3mtl3UzE
6+
1IVZVvWPGdEsEQHwXfAsP/jFGTwI3HDyOhcqzFQSKsjvqJWYkOOb+2r3SBrFlRZW
7+
09k/3lC+hx2XtuuG68u4Xgn3AlUvm2vplgCN7eiYcGeNwVuf2eHdOqTRTqiYCZLi
8+
T8GtdYMDXOrwsGZs/jUKd9U0ar/lqwMhmw07yzlVDM2MWM2tyq/asQ7Sf7vuoMFu
9+
oAtDJ3E+bK1k/7SNhdyP4RonhyUCkWG+mzoKDS1qgXroTiQSDUksAvOCTcj8BNIT
10+
ee+Lcn9FaTKNJiKiU9q/AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFi5ZpaUj+mU
11+
dsgOka+j2/njgNXux3cOjhm7z/N7LeTuDENAOrYa5b+j5JX/YM7RKHrkbXHsQbfs
12+
GB3ufH6QhSiCd/AdsXp/TbCE/8gdq8ykkjwVP1bvBle9oPH7x1aO/WP/odsepYUv
13+
o9aOZW4iNQVmwamU62ezglf3QD7HPeE4LnZueaFtuzRoC+aWT9v0MIeUPJLe3WDQ
14+
FEySwUuthMDJEv92/TeK0YOiunmseCu2mvdiDj6E3C9xa5q2DWgl+msu7+bPgvYO
15+
GuWaoNeQQGk7ebBO3Hk3IyaGx6Cbd8ty+YaZW7dUT+m7KCs1VkxdcDMjZJVWiJy4
16+
4HcEcKboG4Y=
17+
-----END CERTIFICATE-----

test/unit/adapters/http.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var axios = require('../../../index');
22
var http = require('http');
3+
var https = require('https');
34
var net = require('net');
45
var url = require('url');
56
var zlib = require('zlib');
@@ -489,6 +490,57 @@ describe('supports http with nodejs', function () {
489490
});
490491
});
491492

493+
it('should support HTTPS proxies', function (done) {
494+
var options = {
495+
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
496+
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
497+
};
498+
499+
server = https.createServer(options, function (req, res) {
500+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
501+
res.end('12345');
502+
}).listen(4444, function () {
503+
proxy = https.createServer(options, function (request, response) {
504+
var parsed = url.parse(request.url);
505+
var opts = {
506+
host: parsed.hostname,
507+
port: parsed.port,
508+
path: parsed.path,
509+
protocol: parsed.protocol,
510+
rejectUnauthorized: false
511+
};
512+
513+
https.get(opts, function (res) {
514+
var body = '';
515+
res.on('data', function (data) {
516+
body += data;
517+
});
518+
res.on('end', function () {
519+
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
520+
response.end(body + '6789');
521+
});
522+
});
523+
}).listen(4000, function () {
524+
axios.get('https://localhost:4444/', {
525+
proxy: {
526+
host: 'localhost',
527+
port: 4000,
528+
protocol: 'https'
529+
},
530+
httpsAgent: new https.Agent({
531+
rejectUnauthorized: false
532+
})
533+
}).then(function (res) {
534+
assert.equal(res.data, '123456789', 'should pass through proxy');
535+
done();
536+
}).catch(function (err) {
537+
assert.fail(err);
538+
done()
539+
});
540+
});
541+
});
542+
});
543+
492544
it('should not pass through disabled proxy', function (done) {
493545
// set the env variable
494546
process.env.http_proxy = 'http://does-not-exists.example.com:4242/';
@@ -542,6 +594,56 @@ describe('supports http with nodejs', function () {
542594
});
543595
});
544596

597+
it('should support HTTPS proxy set via env var', function (done) {
598+
var options = {
599+
key: fs.readFileSync(path.join(__dirname, 'key.pem')),
600+
cert: fs.readFileSync(path.join(__dirname, 'cert.pem'))
601+
};
602+
603+
server = https.createServer(options, function (req, res) {
604+
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
605+
res.end('12345');
606+
}).listen(4444, function () {
607+
proxy = https.createServer(options, function (request, response) {
608+
var parsed = url.parse(request.url);
609+
var opts = {
610+
host: parsed.hostname,
611+
port: parsed.port,
612+
path: parsed.path,
613+
protocol: parsed.protocol,
614+
rejectUnauthorized: false
615+
};
616+
617+
https.get(opts, function (res) {
618+
var body = '';
619+
res.on('data', function (data) {
620+
body += data;
621+
});
622+
res.on('end', function () {
623+
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
624+
response.end(body + '6789');
625+
});
626+
});
627+
}).listen(4000, function () {
628+
process.env.https_proxy = 'https://localhost:4000/';
629+
630+
axios.get('https://localhost:4444/', {
631+
httpsAgent: new https.Agent({
632+
rejectUnauthorized: false
633+
})
634+
}).then(function (res) {
635+
assert.equal(res.data, '123456789', 'should pass through proxy');
636+
done();
637+
}).catch(function (err) {
638+
assert.fail(err);
639+
done()
640+
}).finally(function () {
641+
process.env.https_proxy = ''
642+
});
643+
});
644+
});
645+
});
646+
545647
it('should not use proxy for domains in no_proxy', function (done) {
546648
server = http.createServer(function (req, res) {
547649
res.setHeader('Content-Type', 'text/html; charset=UTF-8');

test/unit/adapters/key.pem

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIEogIBAAKCAQEA+joLfff3WdFIGwrlVdUWfZ9vDr9w86yQpI6Xq0KF6JiKtxa+
3+
EVCNWtq8Ht5rZd1MxNSFWVb1jxnRLBEB8F3wLD/4xRk8CNxw8joXKsxUEirI76iV
4+
mJDjm/tq90gaxZUWVtPZP95Qvocdl7brhuvLuF4J9wJVL5tr6ZYAje3omHBnjcFb
5+
n9nh3Tqk0U6omAmS4k/BrXWDA1zq8LBmbP41CnfVNGq/5asDIZsNO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zSSkrmVPwN
22+
APfskSpxP8qPXpai8z4gDz47NtG2q/DOqIKWrtHwnF4iGibjwxFzdTz+dA/MR0r9
23+
P8QcbHIMy7/2lbK/B5JWYQDC5h28qs5pz8tqKZLyMqCfOiDWhX9f/zbBrxPw8KqR
24+
q0ylAoGAL/0kemA/Tmxpwmp0S0oCqnA4gbCgS7qnApxB09xTewc/tuvraXc3Mzea
25+
EvqDXLXK0R7O4E3vo0Mr23SodRVlFPevsmUUJLPJMJcxdfnSJgX+qE/UC8Ux+UMi
26+
eYufYRDYSslfL2rt9D7abnnbqSfsHymJKukWpElIgJTklQUru4k=
27+
-----END RSA PRIVATE KEY-----

0 commit comments

Comments
 (0)