Skip to content

Commit 449eaa2

Browse files
improvements on chieffancypants#133
1 parent 5a77fcd commit 449eaa2

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

src/loading-bar.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
107107
},
108108

109109
'response': function(response) {
110+
if (!response || !response.config) {
111+
$log.error('Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50');
112+
return response;
113+
}
114+
110115
if (!response.config.ignoreLoadingBar && !isCached(response.config)) {
111116
reqsCompleted++;
112117
$rootScope.$broadcast('cfpLoadingBar:loaded', {url: response.config.url, result: response});
@@ -120,9 +125,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
120125
},
121126

122127
'responseError': function(rejection) {
123-
if (!rejection.config) {
124-
$log.error('Other interceptors are not returning config object \n https://github.com/chieffancypants/angular-loading-bar/pull/50');
128+
if (!rejection || !rejection.config) {
129+
$log.error('Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50');
130+
return $q.reject(rejection);
125131
}
132+
126133
if (!rejection.config.ignoreLoadingBar && !isCached(rejection.config)) {
127134
reqsCompleted++;
128135
$rootScope.$broadcast('cfpLoadingBar:loaded', {url: rejection.config.url, result: rejection});

test/loading-bar-interceptor.coffee

+59-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ describe 'loadingBarInterceptor Service', ->
6464
$httpBackend.verifyNoOutstandingRequest()
6565
$timeout.flush() # loading bar is animated, so flush timeout
6666

67-
6867
it 'should not increment if the response is cached using $http.defaults.cache', inject (cfpLoadingBar, $cacheFactory) ->
6968
$http.defaults.cache = $cacheFactory('loading-bar')
7069
$httpBackend.expectGET(endpoint).respond response
@@ -88,7 +87,6 @@ describe 'loadingBarInterceptor Service', ->
8887
$httpBackend.verifyNoOutstandingRequest()
8988
$timeout.flush() # loading bar is animated, so flush timeout
9089

91-
9290
it 'should not increment if the response is cached', inject (cfpLoadingBar) ->
9391
$httpBackend.expectGET(endpoint).respond response
9492
$http.get(endpoint, cache: true).then (data) ->
@@ -160,6 +158,7 @@ describe 'loadingBarInterceptor Service', ->
160158
expect(cfpLoadingBar.status()).toBe 1
161159
$timeout.flush()
162160

161+
163162
it 'should increment the loading bar when not all requests have been recieved', inject (cfpLoadingBar) ->
164163
$httpBackend.expectGET(endpoint).respond response
165164
$httpBackend.expectGET(endpoint).respond response
@@ -178,7 +177,6 @@ describe 'loadingBarInterceptor Service', ->
178177
expect(cfpLoadingBar.status()).toBe 1
179178
$timeout.flush() # loading bar is animated, so flush timeout
180179

181-
182180
it 'should count http errors as responses so the loading bar can complete', inject (cfpLoadingBar) ->
183181
# $httpBackend.expectGET(endpoint).respond response
184182
$httpBackend.expectGET(endpoint).respond 401
@@ -196,8 +194,6 @@ describe 'loadingBarInterceptor Service', ->
196194

197195
$timeout.flush()
198196

199-
200-
201197
it 'should insert the loadingbar into the DOM when a request is sent', inject (cfpLoadingBar) ->
202198
$httpBackend.expectGET(endpoint).respond response
203199
$httpBackend.expectGET(endpoint).respond response
@@ -343,7 +339,6 @@ describe 'loadingBarInterceptor Service', ->
343339
cfpLoadingBar.complete()
344340
$timeout.flush()
345341

346-
347342
it 'should not set the status if the loading bar has not yet been started', inject (cfpLoadingBar) ->
348343
cfpLoadingBar.set(0.5)
349344
expect(cfpLoadingBar.status()).toBe 0
@@ -493,3 +488,61 @@ describe 'LoadingBar only', ->
493488

494489
expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false
495490

491+
492+
describe 'Interceptor tests', ->
493+
provider = $http = $httpBackend = $log = null
494+
endpoint = '/service'
495+
response = {message:'OK'}
496+
497+
describe 'Success response', ->
498+
499+
beforeEach ->
500+
module 'chieffancypants.loadingBar', ($httpProvider) ->
501+
provider = $httpProvider
502+
provider.interceptors.push ->
503+
response: (resp) ->
504+
return null
505+
return
506+
507+
inject (_$http_, _$httpBackend_, _$log_) ->
508+
$http = _$http_
509+
$httpBackend = _$httpBackend_
510+
$log = _$log_
511+
512+
513+
it 'should detect poorly implemented interceptors and warn accordingly', ->
514+
expect($log.error.logs.length).toBe 0
515+
516+
$httpBackend.expectGET(endpoint).respond 204
517+
$http.get(endpoint)
518+
$httpBackend.flush()
519+
520+
expect($log.error.logs.length).toBe 1
521+
expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50']
522+
523+
describe 'Error response', ->
524+
525+
beforeEach ->
526+
module 'chieffancypants.loadingBar', ($httpProvider) ->
527+
provider = $httpProvider
528+
provider.interceptors.push ($q) ->
529+
responseError: (resp) ->
530+
delete resp.config
531+
$q.reject(resp);
532+
return
533+
534+
inject (_$http_, _$httpBackend_, _$log_) ->
535+
$http = _$http_
536+
$httpBackend = _$httpBackend_
537+
$log = _$log_
538+
539+
540+
it 'should detect poorly implemented interceptors and warn accordingly', ->
541+
expect($log.error.logs.length).toBe 0
542+
543+
$httpBackend.expectGET(endpoint).respond 500
544+
$http.get(endpoint)
545+
$httpBackend.flush()
546+
547+
expect($log.error.logs.length).toBe 1
548+
expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50']

0 commit comments

Comments
 (0)