|
1 | 1 | var axios = require('../../../index'); |
2 | 2 | var http = require('http'); |
| 3 | +var https = require('https'); |
3 | 4 | var net = require('net'); |
4 | 5 | var url = require('url'); |
5 | 6 | var zlib = require('zlib'); |
@@ -489,6 +490,57 @@ describe('supports http with nodejs', function () { |
489 | 490 | }); |
490 | 491 | }); |
491 | 492 |
|
| 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 | + |
492 | 544 | it('should not pass through disabled proxy', function (done) { |
493 | 545 | // set the env variable |
494 | 546 | process.env.http_proxy = 'http://does-not-exists.example.com:4242/'; |
@@ -542,6 +594,56 @@ describe('supports http with nodejs', function () { |
542 | 594 | }); |
543 | 595 | }); |
544 | 596 |
|
| 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 | + |
545 | 647 | it('should not use proxy for domains in no_proxy', function (done) { |
546 | 648 | server = http.createServer(function (req, res) { |
547 | 649 | res.setHeader('Content-Type', 'text/html; charset=UTF-8'); |
|
0 commit comments