mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($http): expose req/resp headers to transform fns
This commit is contained in:
parent
b911e303ec
commit
23f8da7cbb
2 changed files with 62 additions and 19 deletions
|
|
@ -41,17 +41,17 @@ function parseHeaders(headers) {
|
|||
* - if called with single an argument returns a single header value or null
|
||||
* - if called with no arguments returns an object containing all headers.
|
||||
*/
|
||||
function headersGetter(headersString) {
|
||||
var headers = isObject(headersString) ? headersString : undefined;
|
||||
function headersGetter(headers) {
|
||||
var headersObj = isObject(headers) ? headers : undefined;
|
||||
|
||||
return function(name) {
|
||||
if (!headers) headers = parseHeaders(headersString);
|
||||
if (!headersObj) headersObj = parseHeaders(headers);
|
||||
|
||||
if (name) {
|
||||
return headers[lowercase(name)] || null;
|
||||
return headersObj[lowercase(name)] || null;
|
||||
}
|
||||
|
||||
return headers;
|
||||
return headersObj;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -62,16 +62,16 @@ function headersGetter(headersString) {
|
|||
* This function is used for both request and response transforming
|
||||
*
|
||||
* @param {*} data Data to transform.
|
||||
* @param {function|Array.<function>} fns Function or an array of functions.
|
||||
* @param {*=} param Optional parameter to be passed to all transform functions.
|
||||
* @param {function(string=)} headers Http headers getter fn.
|
||||
* @param {(function|Array.<function>)} fns Function or an array of functions.
|
||||
* @returns {*} Transformed data.
|
||||
*/
|
||||
function transformData(data, fns, param) {
|
||||
function transformData(data, headers, fns) {
|
||||
if (isFunction(fns))
|
||||
return fns(data);
|
||||
return fns(data, headers);
|
||||
|
||||
forEach(fns, function(fn) {
|
||||
data = fn(data, param);
|
||||
data = fn(data, headers);
|
||||
});
|
||||
|
||||
return data;
|
||||
|
|
@ -172,10 +172,10 @@ function $HttpProvider() {
|
|||
|
||||
var reqTransformFn = config.transformRequest || $config.transformRequest,
|
||||
respTransformFn = config.transformResponse || $config.transformResponse,
|
||||
reqData = transformData(config.data, reqTransformFn),
|
||||
defHeaders = $config.headers,
|
||||
reqHeaders = extend({'X-XSRF-TOKEN': $browser.cookies()['XSRF-TOKEN']},
|
||||
defHeaders.common, defHeaders[lowercase(config.method)], config.headers),
|
||||
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
|
||||
promise;
|
||||
|
||||
|
||||
|
|
@ -210,7 +210,7 @@ function $HttpProvider() {
|
|||
function transformResponse(response) {
|
||||
// make a copy since the response must be cacheable
|
||||
var resp = extend({}, response, {
|
||||
data: transformData(response.data, respTransformFn, response.headers)
|
||||
data: transformData(response.data, response.headers, respTransformFn)
|
||||
});
|
||||
return (isSuccess(response.status))
|
||||
? resp
|
||||
|
|
|
|||
|
|
@ -567,6 +567,35 @@ describe('$http', function() {
|
|||
$http({method: 'POST', url: '/url', data: 'string-data'});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should have access to request headers', function() {
|
||||
$httpBackend.expect('POST', '/url', 'header1').respond(200);
|
||||
$http.post('/url', 'req', {
|
||||
headers: {h1: 'header1'},
|
||||
transformRequest: function(data, headers) {
|
||||
return headers('h1');
|
||||
}
|
||||
}).success(callback);
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
|
||||
it('should pipeline more functions', function() {
|
||||
function first(d, h) {return d + '-first' + ':' + h('h1')}
|
||||
function second(d) {return uppercase(d)}
|
||||
|
||||
$httpBackend.expect('POST', '/url', 'REQ-FIRST:V1').respond(200);
|
||||
$http.post('/url', 'req', {
|
||||
headers: {h1: 'v1'},
|
||||
transformRequest: [first, second]
|
||||
}).success(callback);
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -625,16 +654,30 @@ describe('$http', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should pipeline more functions', function() {
|
||||
function first(d) {return d + '1';}
|
||||
function second(d) {return d + '2';}
|
||||
|
||||
$httpBackend.expect('POST', '/url').respond('0');
|
||||
$http({method: 'POST', url: '/url', transformResponse: [first, second]}).success(callback);
|
||||
it('should have access to response headers', function() {
|
||||
$httpBackend.expect('GET', '/url').respond(200, 'response', {h1: 'header1'});
|
||||
$http.get('/url', {
|
||||
transformResponse: function(data, headers) {
|
||||
return headers('h1');
|
||||
}
|
||||
}).success(callback);
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
expect(callback.mostRecentCall.args[0]).toBe('012');
|
||||
expect(callback.mostRecentCall.args[0]).toBe('header1');
|
||||
});
|
||||
|
||||
|
||||
it('should pipeline more functions', function() {
|
||||
function first(d, h) {return d + '-first' + ':' + h('h1')}
|
||||
function second(d) {return uppercase(d)}
|
||||
|
||||
$httpBackend.expect('POST', '/url').respond(200, 'resp', {h1: 'v1'});
|
||||
$http.post('/url', '', {transformResponse: [first, second]}).success(callback);
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(callback).toHaveBeenCalledOnce();
|
||||
expect(callback.mostRecentCall.args[0]).toBe('RESP-FIRST:V1');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue