From 449eaa262b870adb71689d56e5788e716fbe8435 Mon Sep 17 00:00:00 2001 From: Wes Cruver Date: Tue, 4 Nov 2014 11:31:31 -0800 Subject: [PATCH] improvements on #133 --- src/loading-bar.js | 11 ++++- test/loading-bar-interceptor.coffee | 65 ++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/loading-bar.js b/src/loading-bar.js index 26c792a..607a7ea 100644 --- a/src/loading-bar.js +++ b/src/loading-bar.js @@ -107,6 +107,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) }, 'response': function(response) { + if (!response || !response.config) { + $log.error('Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50'); + return response; + } + if (!response.config.ignoreLoadingBar && !isCached(response.config)) { reqsCompleted++; $rootScope.$broadcast('cfpLoadingBar:loaded', {url: response.config.url, result: response}); @@ -120,9 +125,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) }, 'responseError': function(rejection) { - if (!rejection.config) { - $log.error('Other interceptors are not returning config object \n https://github.com/chieffancypants/angular-loading-bar/pull/50'); + if (!rejection || !rejection.config) { + $log.error('Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50'); + return $q.reject(rejection); } + if (!rejection.config.ignoreLoadingBar && !isCached(rejection.config)) { reqsCompleted++; $rootScope.$broadcast('cfpLoadingBar:loaded', {url: rejection.config.url, result: rejection}); diff --git a/test/loading-bar-interceptor.coffee b/test/loading-bar-interceptor.coffee index 6bd8e77..8f38281 100644 --- a/test/loading-bar-interceptor.coffee +++ b/test/loading-bar-interceptor.coffee @@ -64,7 +64,6 @@ describe 'loadingBarInterceptor Service', -> $httpBackend.verifyNoOutstandingRequest() $timeout.flush() # loading bar is animated, so flush timeout - it 'should not increment if the response is cached using $http.defaults.cache', inject (cfpLoadingBar, $cacheFactory) -> $http.defaults.cache = $cacheFactory('loading-bar') $httpBackend.expectGET(endpoint).respond response @@ -88,7 +87,6 @@ describe 'loadingBarInterceptor Service', -> $httpBackend.verifyNoOutstandingRequest() $timeout.flush() # loading bar is animated, so flush timeout - it 'should not increment if the response is cached', inject (cfpLoadingBar) -> $httpBackend.expectGET(endpoint).respond response $http.get(endpoint, cache: true).then (data) -> @@ -160,6 +158,7 @@ describe 'loadingBarInterceptor Service', -> expect(cfpLoadingBar.status()).toBe 1 $timeout.flush() + it 'should increment the loading bar when not all requests have been recieved', inject (cfpLoadingBar) -> $httpBackend.expectGET(endpoint).respond response $httpBackend.expectGET(endpoint).respond response @@ -178,7 +177,6 @@ describe 'loadingBarInterceptor Service', -> expect(cfpLoadingBar.status()).toBe 1 $timeout.flush() # loading bar is animated, so flush timeout - it 'should count http errors as responses so the loading bar can complete', inject (cfpLoadingBar) -> # $httpBackend.expectGET(endpoint).respond response $httpBackend.expectGET(endpoint).respond 401 @@ -196,8 +194,6 @@ describe 'loadingBarInterceptor Service', -> $timeout.flush() - - it 'should insert the loadingbar into the DOM when a request is sent', inject (cfpLoadingBar) -> $httpBackend.expectGET(endpoint).respond response $httpBackend.expectGET(endpoint).respond response @@ -343,7 +339,6 @@ describe 'loadingBarInterceptor Service', -> cfpLoadingBar.complete() $timeout.flush() - it 'should not set the status if the loading bar has not yet been started', inject (cfpLoadingBar) -> cfpLoadingBar.set(0.5) expect(cfpLoadingBar.status()).toBe 0 @@ -493,3 +488,61 @@ describe 'LoadingBar only', -> expect(isLoadingBarInjected($document.find(cfpLoadingBar.parentSelector))).toBe false + +describe 'Interceptor tests', -> + provider = $http = $httpBackend = $log = null + endpoint = '/service' + response = {message:'OK'} + + describe 'Success response', -> + + beforeEach -> + module 'chieffancypants.loadingBar', ($httpProvider) -> + provider = $httpProvider + provider.interceptors.push -> + response: (resp) -> + return null + return + + inject (_$http_, _$httpBackend_, _$log_) -> + $http = _$http_ + $httpBackend = _$httpBackend_ + $log = _$log_ + + + it 'should detect poorly implemented interceptors and warn accordingly', -> + expect($log.error.logs.length).toBe 0 + + $httpBackend.expectGET(endpoint).respond 204 + $http.get(endpoint) + $httpBackend.flush() + + expect($log.error.logs.length).toBe 1 + expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in response:\n https://github.com/chieffancypants/angular-loading-bar/pull/50'] + + describe 'Error response', -> + + beforeEach -> + module 'chieffancypants.loadingBar', ($httpProvider) -> + provider = $httpProvider + provider.interceptors.push ($q) -> + responseError: (resp) -> + delete resp.config + $q.reject(resp); + return + + inject (_$http_, _$httpBackend_, _$log_) -> + $http = _$http_ + $httpBackend = _$httpBackend_ + $log = _$log_ + + + it 'should detect poorly implemented interceptors and warn accordingly', -> + expect($log.error.logs.length).toBe 0 + + $httpBackend.expectGET(endpoint).respond 500 + $http.get(endpoint) + $httpBackend.flush() + + expect($log.error.logs.length).toBe 1 + expect($log.error.logs).toContain ['Broken interceptor detected: Config object not supplied in rejection:\n https://github.com/chieffancypants/angular-loading-bar/pull/50']