Skip to content

Commit

Permalink
[tests] https test pass, fix #511. Exposed the rejectUnauthorized flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cronopio committed Nov 7, 2013
1 parent a2b1f0a commit fd42dce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ proxy.createProxyServer = proxy.createServer = function createProxyServer(option
* ssl : <object to be passed to https.createServer()>
* ws : <true/false, if you want to proxy websockets>
* xfwd : <true/false, adds x-forward headers>
* secure : <true/false, verify SSL certificate>
* }
*
* NOTE: `options.ws` and `options.ssl` are optional.
Expand Down
5 changes: 5 additions & 0 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
extend(outgoing.headers, options.headers);
}

if (options[forward || 'target'].protocol == 'https:') {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
}


outgoing.agent = options.agent || false;
outgoing.path = req.url;

Expand Down
32 changes: 31 additions & 1 deletion test/lib-https-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Object.defineProperty(gen, 'port', {
});

describe('lib/http-proxy.js', function() {
describe('#createProxyServer using HTTPS', function() {
describe('HTTPS #createProxyServer', function() {
describe('HTTPS to HTTP', function () {
it('should proxy the request en send back the response', function (done) {
var ports = { source: gen.port, proxy: gen.port };
Expand Down Expand Up @@ -79,6 +79,8 @@ describe('lib/http-proxy.js', function() {

var proxy = httpProxy.createProxyServer({
target: 'https://127.0.0.1:' + ports.source,
// Allow to use SSL self signed
secure: false
}).listen(ports.proxy);

http.request({
Expand All @@ -100,5 +102,33 @@ describe('lib/http-proxy.js', function() {
}).end();
})
})
describe('HTTPS not allow SSL self signed', function () {
it('should fail with error', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var source = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
}).listen(ports.source);

var proxy = httpProxy.createProxyServer({
target: 'https://127.0.0.1:' + ports.source,
secure: true
});

proxy.listen(ports.proxy);

proxy.on('error', function (err, req, res) {
expect(err).to.be.an(Error);
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
done();
})

http.request({
hostname: '127.0.0.1',
port: ports.proxy,
method: 'GET'
}).end();
})
})
});
});

0 comments on commit fd42dce

Please sign in to comment.