@@ -1364,35 +1364,158 @@ describe('resource', function() {
1364
1364
/ ^ \[ \$ r e s o u r c e : b a d c f g \] E r r o r i n r e s o u r c e c o n f i g u r a t i o n f o r a c t i o n ` g e t ` \. E x p e c t e d r e s p o n s e t o c o n t a i n a n o b j e c t b u t g o t a n a r r a y \( R e q u e s t : G E T \/ C u s t o m e r \/ 1 2 3 \) /
1365
1365
) ;
1366
1366
} ) ;
1367
+ } ) ;
1368
+
1369
+ describe ( 'resource wrt cancelling requests' , function ( ) {
1370
+ var httpSpy ;
1371
+ var $httpBackend ;
1372
+ var $resource ;
1373
+
1374
+ beforeEach ( module ( 'ngResource' , function ( $provide ) {
1375
+ $provide . decorator ( '$http' , function ( $delegate ) {
1376
+ httpSpy = jasmine . createSpy ( '$http' ) . andCallFake ( $delegate ) ;
1377
+ return httpSpy ;
1378
+ } ) ;
1379
+ } ) ) ;
1380
+
1381
+ beforeEach ( inject ( function ( _$httpBackend_ , _$resource_ ) {
1382
+ $httpBackend = _$httpBackend_ ;
1383
+ $resource = _$resource_ ;
1384
+ } ) ) ;
1385
+
1386
+ it ( 'should accept numeric timeouts in actions and pass them to $http' , function ( ) {
1387
+ $httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1388
+
1389
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1390
+ get : {
1391
+ method : 'GET' ,
1392
+ timeout : 10000
1393
+ }
1394
+ } ) ;
1395
+
1396
+ CreditCard . get ( ) ;
1397
+ $httpBackend . flush ( ) ;
1398
+
1399
+ expect ( httpSpy ) . toHaveBeenCalledOnce ( ) ;
1400
+ expect ( httpSpy . calls [ 0 ] . args [ 0 ] . timeout ) . toBe ( 10000 ) ;
1401
+ } ) ;
1402
+
1403
+ it ( 'should delete non-numeric timeouts in actions and log a $debug message' ,
1404
+ inject ( function ( $log , $q ) {
1405
+ spyOn ( $log , 'debug' ) ;
1406
+ $httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1407
+
1408
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1409
+ get : {
1410
+ method : 'GET' ,
1411
+ timeout : $q . defer ( ) . promise
1412
+ }
1413
+ } ) ;
1414
+
1415
+ CreditCard . get ( ) ;
1416
+ $httpBackend . flush ( ) ;
1417
+
1418
+ expect ( httpSpy ) . toHaveBeenCalledOnce ( ) ;
1419
+ expect ( httpSpy . calls [ 0 ] . args [ 0 ] . timeout ) . toBeUndefined ( ) ;
1420
+ expect ( $log . debug ) . toHaveBeenCalledOnceWith ( 'ngResource:\n' +
1421
+ ' Only numeric values are allowed as `timeout`.\n' +
1422
+ ' Promises are not supported in $resource, because the same value has to ' +
1423
+ 'be re-used for multiple requests. If you are looking for a way to cancel ' +
1424
+ 'requests, you should use the `cancellable` option.' ) ;
1425
+ } )
1426
+ ) ;
1427
+
1428
+ it ( 'should not create a `$cancelRequest` method for instance calls' , function ( ) {
1429
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1430
+ save1 : {
1431
+ method : 'POST' ,
1432
+ cancellable : false
1433
+ } ,
1434
+ save2 : {
1435
+ method : 'POST' ,
1436
+ cancellable : true
1437
+ }
1438
+ } ) ;
1439
+
1440
+ var creditCard = new CreditCard ( ) ;
1441
+
1442
+ var promise1 = creditCard . $save1 ( ) ;
1443
+ expect ( promise1 . $cancelRequest ) . toBeUndefined ( ) ;
1444
+ expect ( creditCard . $cancelRequest ) . toBeUndefined ( ) ;
1445
+
1446
+ var promise2 = creditCard . $save2 ( ) ;
1447
+ expect ( promise2 . $cancelRequest ) . toBeUndefined ( ) ;
1448
+ expect ( creditCard . $cancelRequest ) . toBeUndefined ( ) ;
1449
+ } ) ;
1450
+
1451
+ it ( 'should always create a (possibly noop) `$cancelRequest` method for non-instance calls' ,
1452
+ function ( ) {
1453
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1454
+ get1 : {
1455
+ method : 'GET' ,
1456
+ cancellable : false
1457
+ } ,
1458
+ get2 : {
1459
+ method : 'GET' ,
1460
+ cancellable : true
1461
+ }
1462
+ } ) ;
1367
1463
1368
- it ( 'should cancel the request if timeout promise is resolved' , function ( ) {
1369
- var canceler = $q . defer ( ) ;
1464
+ var creditCard1 = CreditCard . get1 ( ) ;
1465
+ var creditCard2 = CreditCard . get2 ( ) ;
1370
1466
1371
- $httpBackend . when ( 'GET' , '/CreditCard' ) . respond ( { data : '123' } ) ;
1467
+ expect ( creditCard1 . $cancelRequest ) . toBe ( noop ) ;
1468
+ expect ( creditCard2 . $cancelRequest ) . toBeDefined ( ) ;
1469
+ }
1470
+ ) ;
1372
1471
1472
+ it ( 'should not make the request cancellable if there is a timeout' , function ( ) {
1373
1473
var CreditCard = $resource ( '/CreditCard' , { } , {
1374
- query : {
1474
+ get : {
1375
1475
method : 'GET' ,
1376
- timeout : canceler . promise
1476
+ timeout : 10000 ,
1477
+ cancellable : true
1377
1478
}
1378
1479
} ) ;
1379
1480
1380
- CreditCard . query ( ) ;
1481
+ var creditCard = CreditCard . get ( ) ;
1381
1482
1382
- canceler . resolve ( ) ;
1383
- expect ( $httpBackend . flush ) . toThrow ( new Error ( "No pending request to flush !" ) ) ;
1483
+ expect ( creditCard . $cancelRequest ) . toBe ( noop ) ;
1484
+ } ) ;
1485
+
1486
+ it ( 'should cancel the request (if cancellable), when calling `$cancelRequest`' , function ( ) {
1487
+ $httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1384
1488
1385
- canceler = $q . defer ( ) ;
1386
- CreditCard = $resource ( '/CreditCard' , { } , {
1387
- query : {
1489
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1490
+ get : {
1388
1491
method : 'GET' ,
1389
- timeout : canceler . promise
1492
+ cancellable : true
1390
1493
}
1391
1494
} ) ;
1392
1495
1393
- CreditCard . query ( ) ;
1496
+ CreditCard . get ( ) . $cancelRequest ( ) ;
1497
+ expect ( $httpBackend . flush ) . toThrow ( new Error ( 'No pending request to flush !' ) ) ;
1498
+
1499
+ CreditCard . get ( ) ;
1394
1500
expect ( $httpBackend . flush ) . not . toThrow ( ) ;
1395
1501
} ) ;
1396
1502
1503
+ it ( 'should reset `$cancelRequest` after the response arrives' , function ( ) {
1504
+ $httpBackend . whenGET ( '/CreditCard' ) . respond ( { } ) ;
1505
+
1506
+ var CreditCard = $resource ( '/CreditCard' , { } , {
1507
+ get : {
1508
+ method : 'GET' ,
1509
+ cancellable : true
1510
+ }
1511
+ } ) ;
1512
+
1513
+ var creditCard = CreditCard . get ( ) ;
1514
+
1515
+ expect ( creditCard . $cancelRequest ) . not . toBe ( noop ) ;
1397
1516
1517
+ $httpBackend . flush ( ) ;
1518
+
1519
+ expect ( creditCard . $cancelRequest ) . toBe ( noop ) ;
1520
+ } ) ;
1398
1521
} ) ;
0 commit comments