Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "edgegrid", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Authorisation process and API helper for Akamai OPEN APIs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -106,1 +106,2 @@ # EdgeGrid for Node.js | ||
* [@mdb](https://github.com/mdb) | ||
* [@ktyacke](https://github.com/ktyacke) |
var request = require('request'), | ||
fs = require('fs'), | ||
url = require('url'), | ||
auth = require('./auth'), | ||
@@ -25,2 +26,3 @@ edgerc = require('./edgerc'), | ||
}, | ||
followRedirect: false, | ||
body: {} | ||
@@ -37,6 +39,22 @@ }); | ||
if (helpers.isRedirect(response.statusCode)) { | ||
this._handleRedirect(response, callback); | ||
return; | ||
} | ||
callback(body, response); | ||
}); | ||
}.bind(this)); | ||
}; | ||
EdgeGrid.prototype._handleRedirect = function(resp, callback) { | ||
var parsedUrl = url.parse(resp.headers['location']); | ||
resp.headers['authorization'] = undefined; | ||
this.request.url = undefined; | ||
this.request.path = parsedUrl.path; | ||
this.auth(this.request); | ||
this.send(callback); | ||
}; | ||
EdgeGrid.prototype._setConfigFromObj = function(obj) { | ||
@@ -43,0 +61,0 @@ if (!obj.path) { |
var fs = require('fs'); | ||
function getGroup(lines, groupName) { | ||
var match = /\[(.*)\]/, | ||
lineMatch, | ||
group; | ||
var match = /\[(.*)\]/, | ||
lineMatch, | ||
group; | ||
lines.forEach(function(line, i) { | ||
lineMatch = line.match(match); | ||
lines.forEach(function(line, i) { | ||
lineMatch = line.match(match); | ||
if (lineMatch && lineMatch[1] === groupName) { | ||
group = lines.slice(i + 1, i + 5); | ||
} | ||
}); | ||
if (lineMatch && lineMatch[1] === groupName) { | ||
group = lines.slice(i + 1, i + 5); | ||
} | ||
}); | ||
return group; | ||
return group; | ||
} | ||
function validatedConfig(config) { | ||
if (config.host.indexOf('https://') > -1) { | ||
return config; | ||
} | ||
if (config.host.indexOf('https://') > -1) { | ||
return config; | ||
} | ||
config.host = 'https://' + config.host; | ||
config.host = 'https://' + config.host; | ||
return config; | ||
return config; | ||
} | ||
function buildObj(configs) { | ||
var result = {}, | ||
keyVal; | ||
var result = {}, | ||
keyVal; | ||
configs.forEach(function(config) { | ||
keyVal = config.split('='); | ||
configs.forEach(function(config) { | ||
// Break string apart at first occurance of equal sign | ||
// character in case the character is found in the value | ||
// strings. | ||
var index = config.indexOf('='), | ||
key = config.substr(0, index) | ||
val = config.substr(index + 1, config.length - index - 1); | ||
result[keyVal[0].trim()] = keyVal[1].trim(); | ||
}); | ||
result[key.trim()] = val.trim(); | ||
}); | ||
return validatedConfig(result); | ||
return validatedConfig(result); | ||
} | ||
module.exports = function(path, conf) { | ||
var edgerc = fs.readFileSync(path).toString().split("\n"), | ||
confGroup = conf || 'default', | ||
confData = getGroup(edgerc, confGroup); | ||
var edgerc = fs.readFileSync(path).toString().split("\n"), | ||
confGroup = conf || 'default', | ||
confData = getGroup(edgerc, confGroup); | ||
if (!confData) { | ||
throw new Error('An error occurred parsing the .edgerc file. You probably specified an invalid group name.'); | ||
} | ||
if (!confData) { | ||
throw new Error('An error occurred parsing the .edgerc file. You probably specified an invalid group name.'); | ||
} | ||
return buildObj(confData); | ||
return buildObj(confData); | ||
}; |
@@ -12,3 +12,9 @@ module.exports = { | ||
return a; | ||
}, | ||
isRedirect: function(statusCode) { | ||
return [ | ||
300, 301, 302, 303, 307 | ||
].indexOf(statusCode) !== -1; | ||
} | ||
}; |
@@ -128,2 +128,3 @@ var assert = require('assert'), | ||
}); | ||
describe('#auth', function() { | ||
@@ -233,3 +234,26 @@ describe('when minimal request options are passed', function() { | ||
}); | ||
describe('when the initial request redirects', function() { | ||
it('correctly follows the redirect and re-signs the request', function(done) { | ||
nock('https://base.com') | ||
.get('/foo') | ||
.reply(302, undefined, { | ||
'Location': 'https://base.com/bar' | ||
}) | ||
.get('/bar') | ||
.reply(200, { | ||
bar: 'bim' | ||
}); | ||
this.api.auth({ | ||
path: '/foo', | ||
}); | ||
this.api.send(function(data, resp) { | ||
assert.equal(JSON.parse(data).bar, 'bim'); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); |
56325
17
988
107