@@ -1869,6 +1869,155 @@ describe('$http', function() {
1869
1869
} ) ;
1870
1870
1871
1871
1872
+ describe ( '$browser\'s outstandingRequestCount' , function ( ) {
1873
+ var $http ;
1874
+ var $httpBackend ;
1875
+ var $rootScope ;
1876
+ var incOutstandingRequestCountSpy ;
1877
+ var completeOutstandingRequestSpy ;
1878
+
1879
+
1880
+ describe ( 'without interceptors' , function ( ) {
1881
+ beforeEach ( setupServicesAndSpies ) ;
1882
+
1883
+
1884
+ it ( 'should immediately call `$browser.$$incOutstandingRequestCount()`' , function ( ) {
1885
+ expect ( incOutstandingRequestCountSpy ) . not . toHaveBeenCalled ( ) ;
1886
+ $http . get ( '' ) ;
1887
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1888
+ } ) ;
1889
+
1890
+
1891
+ it ( 'should call `$browser.$$completeOutstandingRequest()` upon response' , function ( ) {
1892
+ $httpBackend . when ( 'GET' ) . respond ( 200 ) ;
1893
+
1894
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1895
+ $http . get ( '' ) ;
1896
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1897
+ $httpBackend . flush ( ) ;
1898
+ expect ( completeOutstandingRequestSpy ) . toHaveBeenCalledOnce ( ) ;
1899
+ } ) ;
1900
+
1901
+
1902
+ it ( 'should call `$browser.$$completeOutstandingRequest()` upon error' , function ( ) {
1903
+ $httpBackend . when ( 'GET' ) . respond ( 500 ) ;
1904
+
1905
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1906
+ $http . get ( '' ) ;
1907
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1908
+ $httpBackend . flush ( ) ;
1909
+ expect ( completeOutstandingRequestSpy ) . toHaveBeenCalledOnce ( ) ;
1910
+ } ) ;
1911
+
1912
+
1913
+ it ( 'should properly increment/decrement `outstandingRequestCount` '
1914
+ + 'upon error in transformRequest' ,
1915
+ inject ( function ( $exceptionHandler ) {
1916
+ expect ( incOutstandingRequestCountSpy ) . not . toHaveBeenCalled ( ) ;
1917
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1918
+
1919
+ $http . get ( '' , { transformRequest : function ( ) { throw new Error ( ) ; } } ) ;
1920
+
1921
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1922
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1923
+
1924
+ $rootScope . $digest ( ) ;
1925
+
1926
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1927
+ expect ( completeOutstandingRequestSpy ) . toHaveBeenCalledOnce ( ) ;
1928
+
1929
+ $exceptionHandler . errors = [ ] ;
1930
+ } )
1931
+ ) ;
1932
+ } ) ;
1933
+
1934
+
1935
+ describe ( 'with interceptors' , function ( ) {
1936
+ var requestInterceptorCalled ;
1937
+ var responseInterceptorCalled ;
1938
+
1939
+
1940
+ beforeEach ( module ( function ( $httpProvider ) {
1941
+ requestInterceptorCalled = false ;
1942
+ responseInterceptorCalled = false ;
1943
+
1944
+ $httpProvider . interceptors . push ( function ( $q ) {
1945
+ return {
1946
+ request : function ( config ) {
1947
+ requestInterceptorCalled = true ;
1948
+ return config . _requestError ? $q . reject ( ) : config ;
1949
+ } ,
1950
+ response : function ( ) {
1951
+ responseInterceptorCalled = true ;
1952
+ return $q . reject ( ) ;
1953
+ }
1954
+ } ;
1955
+ } ) ;
1956
+ } ) ) ;
1957
+ beforeEach ( setupServicesAndSpies ) ;
1958
+
1959
+
1960
+ it ( 'should properly increment/decrement `outstandingRequestCount` '
1961
+ + 'upon error in request interceptor' ,
1962
+ function ( ) {
1963
+ expect ( incOutstandingRequestCountSpy ) . not . toHaveBeenCalled ( ) ;
1964
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1965
+ expect ( requestInterceptorCalled ) . toBe ( false ) ;
1966
+
1967
+ $http . get ( '' , { _requestError : true } ) ;
1968
+
1969
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1970
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1971
+ expect ( requestInterceptorCalled ) . toBe ( false ) ;
1972
+
1973
+ $rootScope . $digest ( ) ;
1974
+
1975
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1976
+ expect ( completeOutstandingRequestSpy ) . toHaveBeenCalledOnce ( ) ;
1977
+ expect ( requestInterceptorCalled ) . toBe ( true ) ;
1978
+ }
1979
+ ) ;
1980
+
1981
+
1982
+ it ( 'should properly increment/decrement `outstandingRequestCount` '
1983
+ + 'upon error in response interceptor' ,
1984
+ function ( ) {
1985
+ expect ( incOutstandingRequestCountSpy ) . not . toHaveBeenCalled ( ) ;
1986
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1987
+ expect ( responseInterceptorCalled ) . toBe ( false ) ;
1988
+
1989
+ $httpBackend . when ( 'GET' ) . respond ( 200 ) ;
1990
+ $http . get ( '' , { _requestError : false } ) ;
1991
+
1992
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1993
+ expect ( completeOutstandingRequestSpy ) . not . toHaveBeenCalled ( ) ;
1994
+ expect ( responseInterceptorCalled ) . toBe ( false ) ;
1995
+
1996
+ $httpBackend . flush ( ) ;
1997
+
1998
+ expect ( incOutstandingRequestCountSpy ) . toHaveBeenCalledOnce ( ) ;
1999
+ expect ( completeOutstandingRequestSpy ) . toHaveBeenCalledOnce ( ) ;
2000
+ expect ( responseInterceptorCalled ) . toBe ( true ) ;
2001
+ }
2002
+ ) ;
2003
+ } ) ;
2004
+
2005
+
2006
+ function setupServicesAndSpies ( ) {
2007
+ inject ( function ( $browser , _$http_ , _$httpBackend_ , _$rootScope_ ) {
2008
+ $http = _$http_ ;
2009
+ $httpBackend = _$httpBackend_ ;
2010
+ $rootScope = _$rootScope_ ;
2011
+
2012
+ incOutstandingRequestCountSpy
2013
+ = spyOn ( $browser , '$$incOutstandingRequestCount' ) . andCallThrough ( ) ;
2014
+ completeOutstandingRequestSpy
2015
+ = spyOn ( $browser , '$$completeOutstandingRequest' ) . andCallThrough ( ) ;
2016
+ } ) ;
2017
+ }
2018
+ } ) ;
2019
+
2020
+
1872
2021
it ( 'should pass timeout, withCredentials and responseType' , function ( ) {
1873
2022
var $httpBackend = jasmine . createSpy ( '$httpBackend' ) ;
1874
2023
0 commit comments