Skip to content

Commit 946048b

Browse files
committed
Merge pull request #80 from seegno/support/reintroduce-authorization-header
Reintroduce authorization header to be overridden
2 parents beb1d54 + 923fce9 commit 946048b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/interceptors/oauth-interceptor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
function oauthInterceptor($q, $rootScope, OAuthToken) {
77
return {
88
request: function(config) {
9+
config.headers = config.headers || {};
10+
911
// Inject `Authorization` header.
10-
if (OAuthToken.getAuthorizationHeader()) {
11-
config.headers = config.headers || {};
12+
if (!config.headers.hasOwnProperty('Authorization') && OAuthToken.getAuthorizationHeader()) {
1213
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
1314
}
1415

test/unit/interceptors/oauth-interceptor.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ describe('oauthInterceptor', function() {
3737
$httpBackend.flush();
3838
}));
3939

40+
it('should not inject `Authorization` header if it already exists', inject(function($http, $httpBackend, OAuthToken) {
41+
OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' });
42+
43+
$httpBackend.expectGET('https://website.com', function(headers) {
44+
headers.Authorization = undefined;
45+
46+
return headers;
47+
}).respond(200);
48+
49+
$http.get('https://website.com').then(function(response) {
50+
response.config.headers.should.have.property('Authorization');
51+
(undefined === response.config.headers.Authorization).should.be.true;
52+
}).catch(function() {
53+
should.fail();
54+
});
55+
56+
$httpBackend.flush();
57+
58+
$httpBackend.verifyNoOutstandingExpectation();
59+
$httpBackend.verifyNoOutstandingRequest();
60+
}));
61+
4062
it('should remove `token` if an `invalid_request` error occurs', inject(function($http, $httpBackend, OAuthToken) {
4163
sinon.spy(OAuthToken, 'removeToken');
4264

0 commit comments

Comments
 (0)