diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 86251aa..23c40f5 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -3,8 +3,10 @@ var utils = require('./../utils'); var buildURL = require('./../helpers/buildURL'); var transformData = require('./../helpers/transformData'); -var http = require('follow-redirects').http; -var https = require('follow-redirects').https; +var http = require('http'); +var https = require('https'); +var httpFollow = require('follow-redirects').http; +var httpsFollow = require('follow-redirects').https; var url = require('url'); var zlib = require('zlib'); var pkg = require('./../../package.json'); @@ -70,8 +72,17 @@ module.exports = function httpAdapter(resolve, reject, config) { options.path = parsed.protocol + '//' + parsed.hostname + options.path; } + var transport; + if (config.maxRedirects === 0) { + transport = parsed.protocol === 'https:' ? https : http; + } else { + if (config.maxRedirects) { + options.maxRedirects = config.maxRedirects; + } + transport = parsed.protocol === 'https:' ? httpsFollow : httpFollow; + } + // Create the request - var transport = parsed.protocol === 'https:' ? https : http; var req = transport.request(options, function handleResponse(res) { if (aborted) return; @@ -101,7 +112,6 @@ module.exports = function httpAdapter(resolve, reject, config) { config: config, request: req }; - if (config.responseType === 'stream') { response.data = stream; settle(resolve, reject, response); diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 9351fcb..54a4c4a 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -79,6 +79,25 @@ module.exports = { }); }, + testNoRedirect: function (test) { + server = http.createServer(function (req, res) { + res.setHeader('Location', '/foo'); + res.statusCode = 302; + res.end(); + }).listen(4444, function () { + axios.get('http://localhost:4444/', { + maxRedirects: 0, + validateStatus: function () { + return true; + } + }).then(function (res) { + test.equal(res.status, 302); + test.equal(res.headers['location'], '/foo'); + test.done(); + }); + }); + }, + testTransparentGunzip: function (test) { var data = { firstName: 'Fred',