@@ -923,6 +923,77 @@ describe('$http', function() {
923
923
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
924
924
} )
925
925
) ;
926
+
927
+ describe ( '$http.defaults.cache' , function ( ) {
928
+
929
+ it ( 'should be undefined by default' , function ( ) {
930
+ expect ( $http . defaults . cache ) . toBeUndefined ( )
931
+ } ) ;
932
+
933
+ it ( 'should cache requests when no cache given in request config' , function ( ) {
934
+ $http . defaults . cache = cache ;
935
+
936
+ // First request fills the cache from server response.
937
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 200 , 'content' ) ;
938
+ $http ( { method : 'GET' , url : '/url' } ) ; // Notice no cache given in config.
939
+ $httpBackend . flush ( ) ;
940
+
941
+ // Second should be served from cache, without sending request to server.
942
+ $http ( { method : 'get' , url : '/url' } ) . success ( callback ) ;
943
+ $rootScope . $digest ( ) ;
944
+
945
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
946
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toBe ( 'content' ) ;
947
+
948
+ // Invalidate cache entry.
949
+ $http . defaults . cache . remove ( "/url" ) ;
950
+
951
+ // After cache entry removed, a request should be sent to server.
952
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 200 , 'content' ) ;
953
+ $http ( { method : 'GET' , url : '/url' } ) ;
954
+ $httpBackend . flush ( ) ;
955
+ } ) ;
956
+
957
+ it ( 'should have less priority than explicitly given cache' , inject ( function ( $cacheFactory ) {
958
+ var localCache = $cacheFactory ( 'localCache' ) ;
959
+ $http . defaults . cache = cache ;
960
+
961
+ // Fill local cache.
962
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 200 , 'content-local-cache' ) ;
963
+ $http ( { method : 'GET' , url : '/url' , cache : localCache } ) ;
964
+ $httpBackend . flush ( ) ;
965
+
966
+ // Fill default cache.
967
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 200 , 'content-default-cache' ) ;
968
+ $http ( { method : 'GET' , url : '/url' } ) ;
969
+ $httpBackend . flush ( ) ;
970
+
971
+ // Serve request from default cache when no local given.
972
+ $http ( { method : 'get' , url : '/url' } ) . success ( callback ) ;
973
+ $rootScope . $digest ( ) ;
974
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
975
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toBe ( 'content-default-cache' ) ;
976
+ callback . reset ( ) ;
977
+
978
+ // Serve request from local cache when it is given (but default filled too).
979
+ $http ( { method : 'get' , url : '/url' , cache : localCache } ) . success ( callback ) ;
980
+ $rootScope . $digest ( ) ;
981
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
982
+ expect ( callback . mostRecentCall . args [ 0 ] ) . toBe ( 'content-local-cache' ) ;
983
+ } ) ) ;
984
+
985
+ it ( 'should be skipped if {cache: false} is passed in request config' , function ( ) {
986
+ $http . defaults . cache = cache ;
987
+
988
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 200 , 'content' ) ;
989
+ $http ( { method : 'GET' , url : '/url' } ) ;
990
+ $httpBackend . flush ( ) ;
991
+
992
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( ) ;
993
+ $http ( { method : 'GET' , url : '/url' , cache : false } ) ;
994
+ $httpBackend . flush ( ) ;
995
+ } ) ;
996
+ } ) ;
926
997
} ) ;
927
998
928
999
0 commit comments