Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 51ac356

Browse files
committed
feat(http): add config parameter to headers functions
1 parent addfc56 commit 51ac356

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/ng/http.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ function $HttpProvider() {
487487
* - **data** – `{string|Object}` – Data to be sent as the request message data.
488488
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
489489
* HTTP headers to send to the server. If the return value of a function is null, the
490-
* header will not be sent.
490+
* header will not be sent. Functions takes a single argument – an object with all the
491+
* information about the request.
491492
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
492493
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
493494
* - **transformRequest** –
@@ -717,15 +718,15 @@ function $HttpProvider() {
717718
}
718719

719720
// execute if header value is a function for merged headers
720-
execHeaders(reqHeaders);
721+
execHeaders(reqHeaders, config);
721722
return reqHeaders;
722723

723-
function execHeaders(headers) {
724+
function execHeaders(headers, data) {
724725
var headerContent;
725726

726727
forEach(headers, function(headerFn, header) {
727728
if (isFunction(headerFn)) {
728-
headerContent = headerFn();
729+
headerContent = headerFn(data);
729730
if (headerContent != null) {
730731
headers[header] = headerContent;
731732
} else {

test/ng/httpSpec.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -755,17 +755,19 @@ describe('$http', function() {
755755
}));
756756

757757
it('should send execute result if header value is function', inject(function() {
758-
var headerConfig = {'Accept': function() { return 'Rewritten'; }};
758+
var headerConfig = {'Accept': function(config) { return 'Rewritten-' + config.method; }};
759759

760-
function checkHeaders(headers) {
761-
return headers['Accept'] == 'Rewritten';
760+
function checkHeaders(method) {
761+
return function(headers) {
762+
return headers['Accept'] == 'Rewritten-' + method;
763+
};
762764
}
763765

764-
$httpBackend.expect('GET', '/url', undefined, checkHeaders).respond('');
765-
$httpBackend.expect('POST', '/url', undefined, checkHeaders).respond('');
766-
$httpBackend.expect('PUT', '/url', undefined, checkHeaders).respond('');
767-
$httpBackend.expect('PATCH', '/url', undefined, checkHeaders).respond('');
768-
$httpBackend.expect('DELETE', '/url', undefined, checkHeaders).respond('');
766+
$httpBackend.expect('GET', '/url', undefined, checkHeaders('GET')).respond('');
767+
$httpBackend.expect('POST', '/url', undefined, checkHeaders('POST')).respond('');
768+
$httpBackend.expect('PUT', '/url', undefined, checkHeaders('PUT')).respond('');
769+
$httpBackend.expect('PATCH', '/url', undefined, checkHeaders('PATCH')).respond('');
770+
$httpBackend.expect('DELETE', '/url', undefined, checkHeaders('DELETE')).respond('');
769771

770772
$http({url: '/url', method: 'GET', headers: headerConfig});
771773
$http({url: '/url', method: 'POST', headers: headerConfig});

0 commit comments

Comments
 (0)