3
3
describe ( 'resource' , function ( ) {
4
4
5
5
describe ( 'basic usage' , function ( ) {
6
- var $resource , CreditCard , callback , $httpBackend , resourceProvider ;
6
+ var $resource , CreditCard , callback , $httpBackend , resourceProvider , $q ;
7
7
8
8
beforeEach ( module ( 'ngResource' ) ) ;
9
9
@@ -14,6 +14,7 @@ describe('basic usage', function() {
14
14
beforeEach ( inject ( function ( $injector ) {
15
15
$httpBackend = $injector . get ( '$httpBackend' ) ;
16
16
$resource = $injector . get ( '$resource' ) ;
17
+ $q = $injector . get ( '$q' ) ;
17
18
CreditCard = $resource ( '/CreditCard/:id:verb' , { id :'@id.key' } , {
18
19
charge :{
19
20
method :'post' ,
@@ -1083,6 +1084,187 @@ describe('basic usage', function() {
1083
1084
} ) ;
1084
1085
1085
1086
1087
+ describe ( 'requestInterceptor' , function ( ) {
1088
+ var rejectReason = { 'lol' :'cat' } ;
1089
+ var successSpy , failureSpy ;
1090
+
1091
+ beforeEach ( function ( ) {
1092
+ successSpy = jasmine . createSpy ( 'successSpy' ) ;
1093
+ failureSpy = jasmine . createSpy ( 'failureSpy' ) ;
1094
+ } ) ;
1095
+
1096
+ it ( 'should allow per action request interceptor that gets full configuration' , function ( ) {
1097
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1098
+ query : {
1099
+ method : 'get' ,
1100
+ isArray : true ,
1101
+ interceptor : {
1102
+ request : function ( httpConfig ) {
1103
+ callback ( httpConfig ) ;
1104
+ return httpConfig ;
1105
+ }
1106
+ }
1107
+ }
1108
+ } ) ;
1109
+
1110
+ $httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id : 1 } ] ) ;
1111
+
1112
+ var resource = CreditCard . query ( ) ;
1113
+ resource . $promise . then ( successSpy , failureSpy ) ;
1114
+
1115
+ $httpBackend . flush ( ) ;
1116
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
1117
+ expect ( successSpy ) . toHaveBeenCalledOnce ( ) ;
1118
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1119
+
1120
+ expect ( callback ) . toHaveBeenCalledWith ( {
1121
+ 'method' : 'get' ,
1122
+ 'url' : '/CreditCard'
1123
+ } ) ;
1124
+ } ) ;
1125
+
1126
+ it ( 'should call $http with the value returned from requestInterceptor' , function ( ) {
1127
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1128
+ query : {
1129
+ method : 'get' ,
1130
+ isArray : true ,
1131
+ interceptor : {
1132
+ request : function ( httpConfig ) {
1133
+ httpConfig . url = '/DebitCard' ;
1134
+ return httpConfig ;
1135
+ }
1136
+ }
1137
+ }
1138
+ } ) ;
1139
+
1140
+ $httpBackend . expect ( 'GET' , '/DebitCard' ) . respond ( [ { id : 1 } ] ) ;
1141
+
1142
+ var resource = CreditCard . query ( ) ;
1143
+ resource . $promise . then ( successSpy , failureSpy ) ;
1144
+
1145
+ $httpBackend . flush ( ) ;
1146
+ expect ( successSpy ) . toHaveBeenCalledOnce ( ) ;
1147
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1148
+ } ) ;
1149
+
1150
+ it ( 'should abort the operation if the requestInterceptor rejects the operation' , function ( ) {
1151
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1152
+ query : {
1153
+ method : 'get' ,
1154
+ isArray : true ,
1155
+ interceptor : {
1156
+ request : function ( ) {
1157
+ return $q . reject ( rejectReason ) ;
1158
+ }
1159
+ }
1160
+ }
1161
+ } ) ;
1162
+
1163
+ var resource = CreditCard . query ( ) ;
1164
+ resource . $promise . then ( successSpy , failureSpy ) ;
1165
+
1166
+ // Make sure all promises resolve.
1167
+ $rootScope . $apply ( ) ;
1168
+
1169
+ // Ensure the resource promise was rejected
1170
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1171
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1172
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
1173
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
1174
+
1175
+ // Ensure that no requests were made.
1176
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1177
+ } ) ;
1178
+
1179
+ it ( 'should call requestErrorInterceptor if requestInterceptor rejects the operation' , function ( ) {
1180
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1181
+ query : {
1182
+ method : 'get' ,
1183
+ isArray : true ,
1184
+ interceptor : {
1185
+ request : function ( ) {
1186
+ return $q . reject ( rejectReason ) ;
1187
+ } ,
1188
+ requestError : function ( rejection ) {
1189
+ callback ( rejection ) ;
1190
+ return $q . reject ( rejection ) ;
1191
+ }
1192
+ }
1193
+ }
1194
+ } ) ;
1195
+
1196
+ var resource = CreditCard . query ( ) ;
1197
+ resource . $promise . then ( successSpy , failureSpy ) ;
1198
+ $rootScope . $digest ( ) ;
1199
+
1200
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
1201
+ expect ( callback ) . toHaveBeenCalledWith ( rejectReason ) ;
1202
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1203
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
1204
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
1205
+
1206
+ // Ensure that no requests were made.
1207
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1208
+ } ) ;
1209
+
1210
+ it ( 'should abort the operation if a requestErrorInterceptor rejects the operation' , function ( ) {
1211
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1212
+ query : {
1213
+ method : 'get' ,
1214
+ isArray : true ,
1215
+ interceptor : {
1216
+ request : function ( ) {
1217
+ return $q . reject ( rejectReason ) ;
1218
+ } ,
1219
+ requestError : function ( rejection ) {
1220
+ return $q . reject ( rejection ) ;
1221
+ }
1222
+ }
1223
+ }
1224
+ } ) ;
1225
+
1226
+ var resource = CreditCard . query ( ) ;
1227
+ resource . $promise . then ( successSpy , failureSpy ) ;
1228
+ $rootScope . $apply ( ) ;
1229
+
1230
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1231
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1232
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
1233
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
1234
+
1235
+ // Ensure that no requests were made.
1236
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1237
+ } ) ;
1238
+
1239
+ it ( 'should continue the operation if a requestErrorInterceptor rescues it' , function ( ) {
1240
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1241
+ query : {
1242
+ method : 'get' ,
1243
+ isArray : true ,
1244
+ interceptor : {
1245
+ request : function ( httpConfig ) {
1246
+ return $q . reject ( httpConfig ) ;
1247
+ } ,
1248
+ requestError : function ( httpConfig ) {
1249
+ return $q . resolve ( httpConfig ) ;
1250
+ }
1251
+ }
1252
+ }
1253
+ } ) ;
1254
+
1255
+ $httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id : 1 } ] ) ;
1256
+
1257
+ var resource = CreditCard . query ( ) ;
1258
+ resource . $promise . then ( successSpy , failureSpy ) ;
1259
+ $httpBackend . flush ( ) ;
1260
+
1261
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1262
+ expect ( successSpy ) . toHaveBeenCalledOnce ( ) ;
1263
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1264
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1265
+ } ) ;
1266
+ } ) ;
1267
+
1086
1268
it ( 'should allow per action response interceptor that gets full response' , function ( ) {
1087
1269
CreditCard = $resource ( '/CreditCard' , { } , {
1088
1270
query : {
@@ -1537,6 +1719,7 @@ describe('extra params', function() {
1537
1719
var $http ;
1538
1720
var $httpBackend ;
1539
1721
var $resource ;
1722
+ var $rootScope ;
1540
1723
1541
1724
beforeEach ( module ( 'ngResource' ) ) ;
1542
1725
@@ -1546,10 +1729,11 @@ describe('extra params', function() {
1546
1729
} ) ;
1547
1730
} ) ) ;
1548
1731
1549
- beforeEach ( inject ( function ( _$http_ , _$httpBackend_ , _$resource_ ) {
1732
+ beforeEach ( inject ( function ( _$http_ , _$httpBackend_ , _$resource_ , _$rootScope_ ) {
1550
1733
$http = _$http_ ;
1551
1734
$httpBackend = _$httpBackend_ ;
1552
1735
$resource = _$resource_ ;
1736
+ $rootScope = _$rootScope_ ;
1553
1737
} ) ) ;
1554
1738
1555
1739
afterEach ( function ( ) {
@@ -1563,6 +1747,7 @@ describe('extra params', function() {
1563
1747
var R = $resource ( '/:foo' ) ;
1564
1748
R . get ( { foo : 'bar' , baz : 'qux' } ) ;
1565
1749
1750
+ $rootScope . $digest ( ) ;
1566
1751
expect ( $http ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { params : { baz : 'qux' } } ) ) ;
1567
1752
} ) ;
1568
1753
@@ -1577,7 +1762,7 @@ describe('extra params', function() {
1577
1762
} ) ;
1578
1763
1579
1764
describe ( 'errors' , function ( ) {
1580
- var $httpBackend , $resource , $q ;
1765
+ var $httpBackend , $resource , $q , $rootScope ;
1581
1766
1582
1767
beforeEach ( module ( function ( $exceptionHandlerProvider ) {
1583
1768
$exceptionHandlerProvider . mode ( 'log' ) ;
@@ -1589,6 +1774,7 @@ describe('errors', function() {
1589
1774
$httpBackend = $injector . get ( '$httpBackend' ) ;
1590
1775
$resource = $injector . get ( '$resource' ) ;
1591
1776
$q = $injector . get ( '$q' ) ;
1777
+ $rootScope = $injector . get ( '$rootScope' ) ;
1592
1778
} ) ) ;
1593
1779
1594
1780
@@ -1721,6 +1907,82 @@ describe('handling rejections', function() {
1721
1907
expect ( $exceptionHandler . errors [ 0 ] ) . toMatch ( / ^ P o s s i b l y u n h a n d l e d r e j e c t i o n / ) ;
1722
1908
} )
1723
1909
) ;
1910
+
1911
+
1912
+ describe ( 'requestInterceptor' , function ( ) {
1913
+ var rejectReason = { 'lol' :'cat' } ;
1914
+ var $q , $rootScope ;
1915
+ var successSpy , failureSpy , callback ;
1916
+
1917
+ beforeEach ( inject ( function ( _$q_ , _$rootScope_ ) {
1918
+ $q = _$q_ ;
1919
+ $rootScope = _$rootScope_ ;
1920
+
1921
+ successSpy = jasmine . createSpy ( 'successSpy' ) ;
1922
+ failureSpy = jasmine . createSpy ( 'failureSpy' ) ;
1923
+ callback = jasmine . createSpy ( ) ;
1924
+ } ) ) ;
1925
+
1926
+ it ( 'should call requestErrorInterceptor if requestInterceptor throws an error' , function ( ) {
1927
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1928
+ query : {
1929
+ method : 'get' ,
1930
+ isArray : true ,
1931
+ interceptor : {
1932
+ request : function ( ) {
1933
+ throw rejectReason ;
1934
+ } ,
1935
+ requestError : function ( rejection ) {
1936
+ callback ( rejection ) ;
1937
+ return $q . reject ( rejection ) ;
1938
+ }
1939
+ }
1940
+ }
1941
+ } ) ;
1942
+
1943
+ var resource = CreditCard . query ( ) ;
1944
+ resource . $promise . then ( successSpy , failureSpy ) ;
1945
+ $rootScope . $apply ( ) ;
1946
+
1947
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
1948
+ expect ( callback ) . toHaveBeenCalledWith ( rejectReason ) ;
1949
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1950
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
1951
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
1952
+
1953
+ // Ensure that no requests were made.
1954
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1955
+ } ) ;
1956
+
1957
+ it ( 'should abort the operation if a requestErrorInterceptor throws an exception' , function ( ) {
1958
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1959
+ query : {
1960
+ method : 'get' ,
1961
+ isArray : true ,
1962
+ interceptor : {
1963
+ request : function ( ) {
1964
+ return $q . reject ( ) ;
1965
+ } ,
1966
+ requestError : function ( ) {
1967
+ throw rejectReason ;
1968
+ }
1969
+ }
1970
+ }
1971
+ } ) ;
1972
+
1973
+ var resource = CreditCard . query ( ) ;
1974
+ resource . $promise . then ( successSpy , failureSpy ) ;
1975
+ $rootScope . $apply ( ) ;
1976
+
1977
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1978
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1979
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
1980
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
1981
+
1982
+ // Ensure that no requests were made.
1983
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1984
+ } ) ;
1985
+ } ) ;
1724
1986
} ) ;
1725
1987
1726
1988
describe ( 'cancelling requests' , function ( ) {
@@ -1785,7 +2047,7 @@ describe('cancelling requests', function() {
1785
2047
) ;
1786
2048
1787
2049
it ( 'should use `cancellable` value if passed a non-numeric `timeout` in an action' ,
1788
- inject ( function ( $log , $q ) {
2050
+ inject ( function ( $log , $q , $rootScope ) {
1789
2051
spyOn ( $log , 'debug' ) ;
1790
2052
$httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1791
2053
@@ -1798,6 +2060,7 @@ describe('cancelling requests', function() {
1798
2060
} ) ;
1799
2061
1800
2062
var creditCard = CreditCard . get ( ) ;
2063
+ $rootScope . $digest ( ) ;
1801
2064
expect ( creditCard . $cancelRequest ) . toBeDefined ( ) ;
1802
2065
expect ( httpSpy . calls . argsFor ( 0 ) [ 0 ] . timeout ) . toEqual ( jasmine . any ( $q ) ) ;
1803
2066
expect ( httpSpy . calls . argsFor ( 0 ) [ 0 ] . timeout . then ) . toBeDefined ( ) ;
0 commit comments