From b9c33aa64e6c8bf38201714d114579b3040645f6 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Tue, 14 Jul 2015 22:10:56 +0500 Subject: [PATCH] Reselect agent on redirect from http to https Closes #80 --- index.js | 6 +++++- test/test-https.js | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b42a807..21d1714 100644 --- a/index.js +++ b/index.js @@ -148,7 +148,11 @@ function got(url, opts, cb) { } var redirectUrl = urlLib.resolve(url, res.headers.location); - var redirectOpts = objectAssign(opts, urlLib.parse(redirectUrl)); + var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl)); + + if (opts.agent === infinityAgent.http.globalAgent && redirectOpts.protocol === 'https:' && opts.protocol === 'http:') { + redirectOpts.agent = undefined; + } if (proxy) { proxy.emit('redirect', res, redirectOpts); diff --git a/test/test-https.js b/test/test-https.js index 594820e..8e6560d 100644 --- a/test/test-https.js +++ b/test/test-https.js @@ -55,6 +55,14 @@ test('setup', function (t) { }); }); +test('redirects from http to https works', function (t) { + got('http://github.com', function (err, data) { + t.error(err); + t.ok(data); + t.end(); + }); +}); + test('make request to https server', function (t) { got('https://google.com', { strictSSL: true