From 76cb7eeba9c14fec8069eca097bea3cfd21891e2 Mon Sep 17 00:00:00 2001 From: Andrew Crites Date: Sun, 28 Feb 2016 22:42:02 -0500 Subject: [PATCH 1/2] Fixing 245: Parse basic auth from URL and submit with http adapter --- lib/adapters/http.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 395dd1c..de68369 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -46,6 +46,12 @@ module.exports = function httpAdapter(resolve, reject, config) { // Parse url var parsed = url.parse(config.url); + if (!auth && parsed.auth) { + var urlAuth = parsed.auth.split(":"); + var urlUsername = urlAuth[0] || ""; + var urlPassword = urlAuth[1] || ""; + auth = urlUsername + ':' + urlPassword; + } var options = { hostname: parsed.hostname, port: parsed.port, @@ -53,7 +59,7 @@ module.exports = function httpAdapter(resolve, reject, config) { method: config.method, headers: headers, agent: config.agent, - auth: auth + auth: auth || parsed.auth, }; // Create the request From 116f5fca886b5fd9733c1d1548c5f76e220efb21 Mon Sep 17 00:00:00 2001 From: Andrew Crites Date: Sun, 28 Feb 2016 22:51:20 -0500 Subject: [PATCH 2/2] Fixing 245: Formatting and adding tests --- lib/adapters/http.js | 10 +++++----- test/unit/adapters/http.js | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index de68369..c404679 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -47,10 +47,10 @@ module.exports = function httpAdapter(resolve, reject, config) { // Parse url var parsed = url.parse(config.url); if (!auth && parsed.auth) { - var urlAuth = parsed.auth.split(":"); - var urlUsername = urlAuth[0] || ""; - var urlPassword = urlAuth[1] || ""; - auth = urlUsername + ':' + urlPassword; + var urlAuth = parsed.auth.split(':'); + var urlUsername = urlAuth[0] || ''; + var urlPassword = urlAuth[1] || ''; + auth = urlUsername + ':' + urlPassword; } var options = { hostname: parsed.hostname, @@ -59,7 +59,7 @@ module.exports = function httpAdapter(resolve, reject, config) { method: config.method, headers: headers, agent: config.agent, - auth: auth || parsed.auth, + auth: auth }; // Create the request diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 27e0080..7f55083 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -113,5 +113,19 @@ module.exports = { test.done(); }); }); - } + }, + + testBasicAuth: function (test) { + server = http.createServer(function (req, res) { + console.log(req.headers); + res.end(req.headers.authorization); + }).listen(4444, function () { + var user = 'foo'; + axios.get('http://' + user + '@localhost:4444/').then(function (res) { + var base64 = new Buffer(user + ':', 'utf8').toString('base64'); + test.equal(res.data, 'Basic ' + base64); + test.done(); + }); + }); + }, };