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

Commit

Permalink
feat($http): accept function as headers value
Browse files Browse the repository at this point in the history
So we can request with dynamic header value.

module.factory('Res', [
  '$resource'
  '$routeParams'
  'globalConfig'

function($resource, $routeParams, globalConfig) {
  resource('/url/:id', {id: "@id"}, {
    patch: {
      method: 'patch',
      headers: {
        'Authorization': function() {
          return "token " + globalConfig.token;
        }
      }
    }
  });
}]);
  • Loading branch information
bolasblack authored and IgorMinar committed Jul 8, 2013
1 parent 0d124e1 commit a7150f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ function $HttpProvider() {
* - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
* `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
* - **data** – `{string|Object}` – Data to be sent as the request message data.
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
* - **headers** – `{Object}` – Map of strings or functions which return strings representing
* HTTP headers to send to the server. If the return value of a function is null, the header will
* not be sent.
* - **xsrfHeaderName** – `{string}` – Name of HTTP header to populate with the XSRF token.
* - **xsrfCookieName** – `{string}` – Name of cookie containing the XSRF token.
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
Expand Down Expand Up @@ -736,6 +738,10 @@ function $HttpProvider() {

defHeaders = extend({}, defHeaders.common, defHeaders[lowercase(config.method)]);

// execute if header value is function
execHeaders(defHeaders);
execHeaders(reqHeaders);

// using for-in instead of forEach to avoid unecessary iteration after header has been found
defaultHeadersIteration:
for (defHeaderName in defHeaders) {
Expand All @@ -751,6 +757,21 @@ function $HttpProvider() {
}

return reqHeaders;

function execHeaders(headers) {
var headerContent;

forEach(headers, function(headerFn, header) {
if (isFunction(headerFn)) {
headerContent = headerFn();
if (headerContent != null) {
headers[header] = headerContent;
} else {
delete headers[header];
}
}
});
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,28 @@ describe('$http', function() {

$httpBackend.flush();
}));

it('should send execute result if header value is function', inject(function() {
var headerConfig = {'Accept': function() { return 'Rewritten'; }};

function checkHeaders(headers) {
return headers['Accept'] == 'Rewritten';
}

$httpBackend.expect('GET', '/url', undefined, checkHeaders).respond('');
$httpBackend.expect('POST', '/url', undefined, checkHeaders).respond('');
$httpBackend.expect('PUT', '/url', undefined, checkHeaders).respond('');
$httpBackend.expect('PATCH', '/url', undefined, checkHeaders).respond('');
$httpBackend.expect('DELETE', '/url', undefined, checkHeaders).respond('');

$http({url: '/url', method: 'GET', headers: headerConfig});
$http({url: '/url', method: 'POST', headers: headerConfig});
$http({url: '/url', method: 'PUT', headers: headerConfig});
$http({url: '/url', method: 'PATCH', headers: headerConfig});
$http({url: '/url', method: 'DELETE', headers: headerConfig});

$httpBackend.flush();
}));
});


Expand Down

0 comments on commit a7150f1

Please sign in to comment.