Skip to content

Commit

Permalink
Merge pull request #80 from seegno/support/reintroduce-authorization-…
Browse files Browse the repository at this point in the history
…header

Reintroduce authorization header to be overridden
  • Loading branch information
pgom committed Feb 12, 2016
2 parents beb1d54 + 923fce9 commit 946048b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/interceptors/oauth-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function(config) {
config.headers = config.headers || {};

// Inject `Authorization` header.
if (OAuthToken.getAuthorizationHeader()) {
config.headers = config.headers || {};
if (!config.headers.hasOwnProperty('Authorization') && OAuthToken.getAuthorizationHeader()) {
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/interceptors/oauth-interceptor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ describe('oauthInterceptor', function() {
$httpBackend.flush();
}));

it('should not inject `Authorization` header if it already exists', inject(function($http, $httpBackend, OAuthToken) {
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });

$httpBackend.expectGET('https://website.com', function(headers) {
headers.Authorization = undefined;

return headers;
}).respond(200);

$http.get('https://website.com').then(function(response) {
response.config.headers.should.have.property('Authorization');
(undefined === response.config.headers.Authorization).should.be.true;
}).catch(function() {
should.fail();
});

$httpBackend.flush();

$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));

it('should remove `token` if an `invalid_request` error occurs', inject(function($http, $httpBackend, OAuthToken) {
sinon.spy(OAuthToken, 'removeToken');

Expand Down

0 comments on commit 946048b

Please sign in to comment.