@@ -779,36 +779,59 @@ describe('$http', function() {
779
779
780
780
describe ( 'cache' , function ( ) {
781
781
782
+ var cache ;
783
+
784
+ beforeEach ( inject ( function ( $cacheFactory ) {
785
+ cache = $cacheFactory ( 'testCache' ) ;
786
+ } ) ) ;
787
+
782
788
function doFirstCacheRequest ( method , respStatus , headers ) {
783
789
$httpBackend . expect ( method || 'GET' , '/url' ) . respond ( respStatus || 200 , 'content' , headers ) ;
784
- $http ( { method : method || 'GET' , url : '/url' , cache : true } ) ;
790
+ $http ( { method : method || 'GET' , url : '/url' , cache : cache } ) ;
785
791
$httpBackend . flush ( ) ;
786
792
}
787
793
788
- it ( 'should cache GET request' , function ( ) {
794
+ it ( 'should cache GET request when cache is provided ' , function ( ) {
789
795
doFirstCacheRequest ( ) ;
790
796
791
- $http ( { method : 'get' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
797
+ $http ( { method : 'get' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
792
798
$browser . defer . flush ( ) ;
793
799
794
800
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
795
801
expect ( callback . mostRecentCall . args [ 0 ] ) . toBe ( 'content' ) ;
796
802
} ) ;
797
803
798
804
805
+ it ( 'should not cache when cache is not provided' , function ( ) {
806
+ doFirstCacheRequest ( ) ;
807
+
808
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( ) ;
809
+ $http ( { method : 'GET' , url : '/url' } ) ;
810
+ } ) ;
811
+
812
+
813
+ it ( 'should perform request when cache cleared' , function ( ) {
814
+ doFirstCacheRequest ( ) ;
815
+
816
+ cache . removeAll ( ) ;
817
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( ) ;
818
+ $http ( { method : 'GET' , url : '/url' , cache : cache } ) ;
819
+ } ) ;
820
+
821
+
799
822
it ( 'should always call callback asynchronously' , function ( ) {
800
823
doFirstCacheRequest ( ) ;
801
- $http ( { method : 'get' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
824
+ $http ( { method : 'get' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
802
825
803
- expect ( callback ) . not . toHaveBeenCalledOnce ( ) ;
826
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
804
827
} ) ;
805
828
806
829
807
830
it ( 'should not cache POST request' , function ( ) {
808
831
doFirstCacheRequest ( 'POST' ) ;
809
832
810
833
$httpBackend . expect ( 'POST' , '/url' ) . respond ( 'content2' ) ;
811
- $http ( { method : 'POST' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
834
+ $http ( { method : 'POST' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
812
835
$httpBackend . flush ( ) ;
813
836
814
837
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
@@ -820,7 +843,7 @@ describe('$http', function() {
820
843
doFirstCacheRequest ( 'PUT' ) ;
821
844
822
845
$httpBackend . expect ( 'PUT' , '/url' ) . respond ( 'content2' ) ;
823
- $http ( { method : 'PUT' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
846
+ $http ( { method : 'PUT' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
824
847
$httpBackend . flush ( ) ;
825
848
826
849
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
@@ -832,7 +855,7 @@ describe('$http', function() {
832
855
doFirstCacheRequest ( 'DELETE' ) ;
833
856
834
857
$httpBackend . expect ( 'DELETE' , '/url' ) . respond ( 206 ) ;
835
- $http ( { method : 'DELETE' , url : '/url' , cache : true } ) . on ( '206' , callback ) ;
858
+ $http ( { method : 'DELETE' , url : '/url' , cache : cache } ) . on ( '206' , callback ) ;
836
859
$httpBackend . flush ( ) ;
837
860
838
861
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
@@ -843,7 +866,7 @@ describe('$http', function() {
843
866
doFirstCacheRequest ( 'GET' , 404 ) ;
844
867
845
868
$httpBackend . expect ( 'GET' , '/url' ) . respond ( 'content2' ) ;
846
- $http ( { method : 'GET' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
869
+ $http ( { method : 'GET' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
847
870
$httpBackend . flush ( ) ;
848
871
849
872
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
@@ -858,7 +881,7 @@ describe('$http', function() {
858
881
expect ( headers ( 'server' ) ) . toBe ( 'Apache' ) ;
859
882
} ) ;
860
883
861
- $http ( { method : 'GET' , url : '/url' , cache : true } ) . on ( '200' , callback ) ;
884
+ $http ( { method : 'GET' , url : '/url' , cache : cache } ) . on ( '200' , callback ) ;
862
885
$browser . defer . flush ( ) ;
863
886
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
864
887
} ) ;
@@ -870,10 +893,27 @@ describe('$http', function() {
870
893
expect ( status ) . toBe ( 201 ) ;
871
894
} ) ;
872
895
873
- $http ( { method : 'get' , url : '/url' , cache : true } ) . on ( '2xx' , callback ) ;
896
+ $http ( { method : 'get' , url : '/url' , cache : cache } ) . on ( '2xx' , callback ) ;
874
897
$browser . defer . flush ( ) ;
875
898
expect ( callback ) . toHaveBeenCalledOnce ( ) ;
876
899
} ) ;
900
+
901
+
902
+ it ( 'should use cache even if request fired before first response is back' , function ( ) {
903
+ $httpBackend . expect ( 'GET' , '/url' ) . respond ( 201 , 'fake-response' ) ;
904
+
905
+ callback . andCallFake ( function ( response , status , headers ) {
906
+ expect ( response ) . toBe ( 'fake-response' ) ;
907
+ expect ( status ) . toBe ( 201 ) ;
908
+ } ) ;
909
+
910
+ $http ( { method : 'GET' , url : '/url' , cache : cache } ) . on ( 'always' , callback ) ;
911
+ $http ( { method : 'GET' , url : '/url' , cache : cache } ) . on ( 'always' , callback ) ;
912
+
913
+ $httpBackend . flush ( ) ;
914
+ expect ( callback ) . toHaveBeenCalled ( ) ;
915
+ expect ( callback . callCount ) . toBe ( 2 ) ;
916
+ } ) ;
877
917
} ) ;
878
918
879
919
@@ -903,10 +943,13 @@ describe('$http', function() {
903
943
} ) ;
904
944
905
945
906
- it ( 'should remove the request when served from cache' , function ( ) {
946
+ it ( 'should update pending requests even when served from cache' , function ( ) {
907
947
$httpBackend . when ( 'GET' ) . respond ( 200 ) ;
908
948
909
949
$http ( { method : 'get' , url : '/cached' , cache : true } ) ;
950
+ $http ( { method : 'get' , url : '/cached' , cache : true } ) ;
951
+ expect ( $http . pendingRequests . length ) . toBe ( 2 ) ;
952
+
910
953
$httpBackend . flush ( ) ;
911
954
expect ( $http . pendingRequests . length ) . toBe ( 0 ) ;
912
955
0 commit comments