提交 c0a91847 编写于 作者: M Matt Zabriskie

Using response headers

上级 514e281a
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -3,6 +3,7 @@ var buildUrl = require('./buildUrl');
var defaults = require('./defaults');
var forEach = require('./forEach');
var merge = require('./merge');
var parseHeaders = require('./parseHeaders');
var transformData = require('./transformData');
var axios = module.exports = function axios(options) {
......@@ -38,7 +39,7 @@ var axios = module.exports = function axios(options) {
options.transformResponse
),
status: request.status,
headers: headers,
headers: parseHeaders(request.getAllResponseHeaders()),
config: options
};
......
'use strict';
var forEach = require('./forEach');
function trim(str) {
return str.replace(/^\s*/, '').replace(/\s*$/, '');
}
/**
* Parse headers into an object
*
* ```
* Date: Wed, 27 Aug 2014 08:58:49 GMT
* Content-Type: application/json
* Connection: keep-alive
* Transfer-Encoding: chunked
* ```
*
* @param {String} headers Headers needing to be parsed
* @returns {Object} Headers parsed into an object
*/
module.exports = function parseHeaders(headers) {
var parsed = {}, key, val, i;
if (!headers) return parsed;
forEach(headers.split('\n'), function(line) {
i = line.indexOf(':');
key = trim(line.substr(0, i)).toLowerCase();
val = trim(line.substr(i + 1));
if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
}
});
return parsed;
};
\ No newline at end of file
var parseHeaders = require('../../lib/parseHeaders');
module.exports = {
testParse: function (test) {
var date = new Date();
var parsed = parseHeaders(
'Date: ' + date.toISOString() + '\n' +
'Content-Type: application/json\n' +
'Connection: keep-alive\n' +
'Transfer-Encoding: chunked'
);
test.equals(parsed['date'], date.toISOString());
test.equals(parsed['content-type'], 'application/json');
test.equals(parsed['connection'], 'keep-alive');
test.equals(parsed['transfer-encoding'], 'chunked');
test.done();
}
};
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册