diff --git a/src/loading-bar.js b/src/loading-bar.js index 44e092a..c66cc3d 100644 --- a/src/loading-bar.js +++ b/src/loading-bar.js @@ -69,6 +69,11 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) var cache; var defaultCache = $cacheFactory.get('$http'); var defaults = $httpProvider.defaults; + var url = config.url; + + if (config.paramSerializer !== undefined) { + url = buildUrl(config.url, config.paramSerializer(config.params)); + } // Choose the proper cache source. Borrowed from angular: $http service if ((config.cache || defaults.cache) && config.cache !== false && @@ -79,7 +84,7 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) } var cached = cache !== undefined ? - cache.get(config.url) !== undefined : false; + cache.get(url) !== undefined : false; if (config.cached !== undefined && cached !== config.cached) { return config.cached; @@ -88,6 +93,12 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar']) return cached; } + function buildUrl(url, serializedParams) { + if (serializedParams.length > 0) { + url += ((url.indexOf('?') == -1) ? '?' : '&') + serializedParams; + } + return url; + } return { 'request': function(config) { diff --git a/test/loading-bar-interceptor.coffee b/test/loading-bar-interceptor.coffee index 241967f..1d4131f 100644 --- a/test/loading-bar-interceptor.coffee +++ b/test/loading-bar-interceptor.coffee @@ -165,6 +165,47 @@ describe 'loadingBarInterceptor Service', -> expect(cfpLoadingBar.status()).toBe 1 $timeout.flush() + it 'should not increment if the response is cached with different params', inject (cfpLoadingBar) -> + return unless angular.version.major >= 1 && angular.version.minor >= 4 + + $httpBackend.expectGET(endpoint).respond response + $http.get(endpoint, cache: true).then (data) -> + result = data + + expect(cfpLoadingBar.status()).toBe 0 + $timeout.flush() + $timeout.flush() + $httpBackend.flush(1) + expect(cfpLoadingBar.status()).toBe 1 + cfpLoadingBar.complete() # set as complete + $timeout.flush() + flush() + + $httpBackend.verifyNoOutstandingRequest() + + + $httpBackend.expectGET(endpoint + "?q=angular").respond response + $http.get(endpoint, params: { q: 'angular' }, cache: true).then (data) -> + result = data + + $httpBackend.verifyNoOutstandingExpectation() + expect(cfpLoadingBar.status()).toBe 0 + $timeout.flush() + $timeout.flush() + $httpBackend.flush(1) + expect(cfpLoadingBar.status()).toBe 1 + cfpLoadingBar.complete() # set as complete + $timeout.flush() + flush() + + + $http.get(endpoint, params: { q: 'angular' }, cache: true).then (data) -> + result = data + # no need to flush $httpBackend since the response is cached + expect(cfpLoadingBar.status()).toBe 0 + $httpBackend.verifyNoOutstandingRequest() + $timeout.flush() # loading bar is animated, so flush timeout + it 'should increment the loading bar when not all requests have been recieved', inject (cfpLoadingBar) -> $httpBackend.expectGET(endpoint).respond response