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' ,
@@ -1129,6 +1130,188 @@ describe('basic usage', function() {
1129
1130
} ) ;
1130
1131
1131
1132
1133
+ describe ( 'requestInterceptor' , function ( ) {
1134
+ var rejectReason = { 'lol' :'cat' } ;
1135
+ var successSpy , failureSpy ;
1136
+
1137
+ beforeEach ( function ( ) {
1138
+ successSpy = jasmine . createSpy ( 'successSpy' ) ;
1139
+ failureSpy = jasmine . createSpy ( 'failureSpy' ) ;
1140
+ } ) ;
1141
+
1142
+ it ( 'should allow per action request interceptor that gets full configuration' , function ( ) {
1143
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1144
+ query : {
1145
+ method : 'get' ,
1146
+ isArray : true ,
1147
+ interceptor : {
1148
+ request : function ( httpConfig ) {
1149
+ callback ( httpConfig ) ;
1150
+ return httpConfig ;
1151
+ }
1152
+ }
1153
+ }
1154
+ } ) ;
1155
+
1156
+ $httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id : 1 } ] ) ;
1157
+
1158
+ var resource = CreditCard . query ( ) ;
1159
+ resource . $promise . then ( successSpy , failureSpy ) ;
1160
+
1161
+ $httpBackend . flush ( ) ;
1162
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
1163
+ expect ( successSpy ) . toHaveBeenCalledOnce ( ) ;
1164
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1165
+
1166
+ expect ( callback ) . toHaveBeenCalledWith ( {
1167
+ 'method' : 'get' ,
1168
+ 'url' : '/CreditCard'
1169
+ } ) ;
1170
+ } ) ;
1171
+
1172
+ it ( 'should call $http with the value returned from requestInterceptor' , function ( ) {
1173
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1174
+ query : {
1175
+ method : 'get' ,
1176
+ isArray : true ,
1177
+ interceptor : {
1178
+ request : function ( httpConfig ) {
1179
+ httpConfig . url = '/DebitCard' ;
1180
+ return httpConfig ;
1181
+ }
1182
+ }
1183
+ }
1184
+ } ) ;
1185
+
1186
+ $httpBackend . expect ( 'GET' , '/DebitCard' ) . respond ( [ { id : 1 } ] ) ;
1187
+
1188
+ var resource = CreditCard . query ( ) ;
1189
+ resource . $promise . then ( successSpy , failureSpy ) ;
1190
+
1191
+ $httpBackend . flush ( ) ;
1192
+ expect ( successSpy ) . toHaveBeenCalledOnceWith ( jasmine . arrayContaining ( [
1193
+ jasmine . objectContaining ( { id : 1 } )
1194
+ ] ) ) ;
1195
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1196
+ } ) ;
1197
+
1198
+ it ( 'should abort the operation if the requestInterceptor rejects the operation' , function ( ) {
1199
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1200
+ query : {
1201
+ method : 'get' ,
1202
+ isArray : true ,
1203
+ interceptor : {
1204
+ request : function ( ) {
1205
+ return $q . reject ( rejectReason ) ;
1206
+ }
1207
+ }
1208
+ }
1209
+ } ) ;
1210
+
1211
+ var resource = CreditCard . query ( ) ;
1212
+ resource . $promise . then ( successSpy , failureSpy ) ;
1213
+
1214
+ // Make sure all promises resolve.
1215
+ $rootScope . $apply ( ) ;
1216
+
1217
+ // Ensure the resource promise was rejected
1218
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1219
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1220
+ expect ( failureSpy ) . toHaveBeenCalledOnceWith ( rejectReason ) ;
1221
+
1222
+ // Ensure that no requests were made.
1223
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1224
+ } ) ;
1225
+
1226
+ it ( 'should call requestErrorInterceptor if requestInterceptor rejects the operation' , function ( ) {
1227
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1228
+ query : {
1229
+ method : 'get' ,
1230
+ isArray : true ,
1231
+ interceptor : {
1232
+ request : function ( ) {
1233
+ return $q . reject ( rejectReason ) ;
1234
+ } ,
1235
+ requestError : function ( rejection ) {
1236
+ callback ( rejection ) ;
1237
+ return $q . reject ( rejection ) ;
1238
+ }
1239
+ }
1240
+ }
1241
+ } ) ;
1242
+
1243
+ var resource = CreditCard . query ( ) ;
1244
+ resource . $promise . then ( successSpy , failureSpy ) ;
1245
+ $rootScope . $digest ( ) ;
1246
+
1247
+ expect ( callback ) . toHaveBeenCalledOnceWith ( rejectReason ) ;
1248
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1249
+ expect ( failureSpy ) . toHaveBeenCalledOnceWith ( rejectReason ) ;
1250
+
1251
+ // Ensure that no requests were made.
1252
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1253
+ } ) ;
1254
+
1255
+ it ( 'should abort the operation if a requestErrorInterceptor rejects the operation' , function ( ) {
1256
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1257
+ query : {
1258
+ method : 'get' ,
1259
+ isArray : true ,
1260
+ interceptor : {
1261
+ request : function ( ) {
1262
+ return $q . reject ( rejectReason ) ;
1263
+ } ,
1264
+ requestError : function ( rejection ) {
1265
+ return $q . reject ( rejection ) ;
1266
+ }
1267
+ }
1268
+ }
1269
+ } ) ;
1270
+
1271
+ var resource = CreditCard . query ( ) ;
1272
+ resource . $promise . then ( successSpy , failureSpy ) ;
1273
+ $rootScope . $apply ( ) ;
1274
+
1275
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1276
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
1277
+ expect ( failureSpy ) . toHaveBeenCalledOnceWith ( rejectReason ) ;
1278
+
1279
+ // Ensure that no requests were made.
1280
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1281
+ } ) ;
1282
+
1283
+ it ( 'should continue the operation if a requestErrorInterceptor rescues it' , function ( ) {
1284
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1285
+ query : {
1286
+ method : 'get' ,
1287
+ isArray : true ,
1288
+ interceptor : {
1289
+ request : function ( httpConfig ) {
1290
+ return $q . reject ( httpConfig ) ;
1291
+ } ,
1292
+ requestError : function ( httpConfig ) {
1293
+ return $q . resolve ( httpConfig ) ;
1294
+ }
1295
+ }
1296
+ }
1297
+ } ) ;
1298
+
1299
+ $httpBackend . expect ( 'GET' , '/CreditCard' ) . respond ( [ { id : 1 } ] ) ;
1300
+
1301
+ var resource = CreditCard . query ( ) ;
1302
+ resource . $promise . then ( successSpy , failureSpy ) ;
1303
+ $httpBackend . flush ( ) ;
1304
+
1305
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
1306
+ expect ( successSpy ) . toHaveBeenCalledOnceWith ( jasmine . arrayContaining ( [
1307
+ jasmine . objectContaining ( { id : 1 } )
1308
+ ] ) ) ;
1309
+ expect ( failureSpy ) . not . toHaveBeenCalled ( ) ;
1310
+
1311
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
1312
+ } ) ;
1313
+ } ) ;
1314
+
1132
1315
it ( 'should allow per action response interceptor that gets full response' , function ( ) {
1133
1316
CreditCard = $resource ( '/CreditCard' , { } , {
1134
1317
query : {
@@ -1584,6 +1767,7 @@ describe('extra params', function() {
1584
1767
var $http ;
1585
1768
var $httpBackend ;
1586
1769
var $resource ;
1770
+ var $rootScope ;
1587
1771
1588
1772
beforeEach ( module ( 'ngResource' ) ) ;
1589
1773
@@ -1593,10 +1777,11 @@ describe('extra params', function() {
1593
1777
} ) ;
1594
1778
} ) ) ;
1595
1779
1596
- beforeEach ( inject ( function ( _$http_ , _$httpBackend_ , _$resource_ ) {
1780
+ beforeEach ( inject ( function ( _$http_ , _$httpBackend_ , _$resource_ , _$rootScope_ ) {
1597
1781
$http = _$http_ ;
1598
1782
$httpBackend = _$httpBackend_ ;
1599
1783
$resource = _$resource_ ;
1784
+ $rootScope = _$rootScope_ ;
1600
1785
} ) ) ;
1601
1786
1602
1787
afterEach ( function ( ) {
@@ -1610,6 +1795,7 @@ describe('extra params', function() {
1610
1795
var R = $resource ( '/:foo' ) ;
1611
1796
R . get ( { foo : 'bar' , baz : 'qux' } ) ;
1612
1797
1798
+ $rootScope . $digest ( ) ;
1613
1799
expect ( $http ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { params : { baz : 'qux' } } ) ) ;
1614
1800
} ) ;
1615
1801
@@ -1624,7 +1810,7 @@ describe('extra params', function() {
1624
1810
} ) ;
1625
1811
1626
1812
describe ( 'errors' , function ( ) {
1627
- var $httpBackend , $resource , $q ;
1813
+ var $httpBackend , $resource , $q , $rootScope ;
1628
1814
1629
1815
beforeEach ( module ( function ( $exceptionHandlerProvider ) {
1630
1816
$exceptionHandlerProvider . mode ( 'log' ) ;
@@ -1636,6 +1822,7 @@ describe('errors', function() {
1636
1822
$httpBackend = $injector . get ( '$httpBackend' ) ;
1637
1823
$resource = $injector . get ( '$resource' ) ;
1638
1824
$q = $injector . get ( '$q' ) ;
1825
+ $rootScope = $injector . get ( '$rootScope' ) ;
1639
1826
} ) ) ;
1640
1827
1641
1828
@@ -1838,6 +2025,81 @@ describe('handling rejections', function() {
1838
2025
expect ( $exceptionHandler . errors [ 0 ] ) . toMatch ( / ^ E r r o r : s h o u l d b e c a u g h t / ) ;
1839
2026
}
1840
2027
) ;
2028
+
2029
+ describe ( 'requestInterceptor' , function ( ) {
2030
+ var rejectReason = { 'lol' :'cat' } ;
2031
+ var $q , $rootScope ;
2032
+ var successSpy , failureSpy , callback ;
2033
+
2034
+ beforeEach ( inject ( function ( _$q_ , _$rootScope_ ) {
2035
+ $q = _$q_ ;
2036
+ $rootScope = _$rootScope_ ;
2037
+
2038
+ successSpy = jasmine . createSpy ( 'successSpy' ) ;
2039
+ failureSpy = jasmine . createSpy ( 'failureSpy' ) ;
2040
+ callback = jasmine . createSpy ( ) ;
2041
+ } ) ) ;
2042
+
2043
+ it ( 'should call requestErrorInterceptor if requestInterceptor throws an error' , function ( ) {
2044
+ var CreditCard = $resource ( '/CreditCard' , { } , {
2045
+ query : {
2046
+ method : 'get' ,
2047
+ isArray : true ,
2048
+ interceptor : {
2049
+ request : function ( ) {
2050
+ throw rejectReason ;
2051
+ } ,
2052
+ requestError : function ( rejection ) {
2053
+ callback ( rejection ) ;
2054
+ return $q . reject ( rejection ) ;
2055
+ }
2056
+ }
2057
+ }
2058
+ } ) ;
2059
+
2060
+ var resource = CreditCard . query ( ) ;
2061
+ resource . $promise . then ( successSpy , failureSpy ) ;
2062
+ $rootScope . $apply ( ) ;
2063
+
2064
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
2065
+ expect ( callback ) . toHaveBeenCalledWith ( rejectReason ) ;
2066
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
2067
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
2068
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
2069
+
2070
+ // Ensure that no requests were made.
2071
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
2072
+ } ) ;
2073
+
2074
+ it ( 'should abort the operation if a requestErrorInterceptor throws an exception' , function ( ) {
2075
+ var CreditCard = $resource ( '/CreditCard' , { } , {
2076
+ query : {
2077
+ method : 'get' ,
2078
+ isArray : true ,
2079
+ interceptor : {
2080
+ request : function ( ) {
2081
+ return $q . reject ( ) ;
2082
+ } ,
2083
+ requestError : function ( ) {
2084
+ throw rejectReason ;
2085
+ }
2086
+ }
2087
+ }
2088
+ } ) ;
2089
+
2090
+ var resource = CreditCard . query ( ) ;
2091
+ resource . $promise . then ( successSpy , failureSpy ) ;
2092
+ $rootScope . $apply ( ) ;
2093
+
2094
+ expect ( resource . $resolved ) . toBeTruthy ( ) ;
2095
+ expect ( successSpy ) . not . toHaveBeenCalled ( ) ;
2096
+ expect ( failureSpy ) . toHaveBeenCalledOnce ( ) ;
2097
+ expect ( failureSpy ) . toHaveBeenCalledWith ( rejectReason ) ;
2098
+
2099
+ // Ensure that no requests were made.
2100
+ $httpBackend . verifyNoOutstandingRequest ( ) ;
2101
+ } ) ;
2102
+ } ) ;
1841
2103
} ) ;
1842
2104
1843
2105
describe ( 'cancelling requests' , function ( ) {
@@ -1902,7 +2164,7 @@ describe('cancelling requests', function() {
1902
2164
) ;
1903
2165
1904
2166
it ( 'should use `cancellable` value if passed a non-numeric `timeout` in an action' ,
1905
- inject ( function ( $log , $q ) {
2167
+ inject ( function ( $log , $q , $rootScope ) {
1906
2168
spyOn ( $log , 'debug' ) ;
1907
2169
$httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1908
2170
@@ -1915,6 +2177,7 @@ describe('cancelling requests', function() {
1915
2177
} ) ;
1916
2178
1917
2179
var creditCard = CreditCard . get ( ) ;
2180
+ $rootScope . $digest ( ) ;
1918
2181
expect ( creditCard . $cancelRequest ) . toBeDefined ( ) ;
1919
2182
expect ( httpSpy . calls . argsFor ( 0 ) [ 0 ] . timeout ) . toEqual ( jasmine . any ( $q ) ) ;
1920
2183
expect ( httpSpy . calls . argsFor ( 0 ) [ 0 ] . timeout . then ) . toBeDefined ( ) ;
0 commit comments