@@ -321,6 +321,18 @@ public interface IFragmentApi
321
321
Task QueryAfterFragment ( ) ;
322
322
}
323
323
324
+ public interface ICancellableApi
325
+ {
326
+ [ Get ( "/foo" ) ]
327
+ Task GetWithCancellation ( CancellationToken token = default ) ;
328
+
329
+ [ Get ( "/foo" ) ]
330
+ Task < string > GetWithCancellationAndReturn ( CancellationToken token = default ) ;
331
+
332
+ [ Get ( "/foo" ) ]
333
+ Task GetWithNullableCancellation ( CancellationToken ? token ) ;
334
+ }
335
+
324
336
public class HttpBinGet
325
337
{
326
338
public Dictionary < string , object > Args { get ; set ; }
@@ -2547,6 +2559,50 @@ public async Task ShouldStripQueryAfterFragment()
2547
2559
mockHttp . VerifyNoOutstandingExpectation ( ) ;
2548
2560
}
2549
2561
2562
+ [ Fact ]
2563
+ public async Task TaskShouldCancelWhenRequested ( )
2564
+ {
2565
+ var ctSource = new CancellationTokenSource ( ) ;
2566
+ var token = ctSource . Token ;
2567
+
2568
+ var fixture = RestService . For < ICancellableApi > ( "https://github.com" ) ;
2569
+
2570
+ ctSource . Cancel ( ) ;
2571
+ var task = fixture . GetWithCancellation ( token ) ;
2572
+ await Assert . ThrowsAsync < TaskCanceledException > ( async ( ) => await task ) ;
2573
+ }
2574
+
2575
+ [ Fact ]
2576
+ public async Task TaskResultShouldCancelWhenRequested ( )
2577
+ {
2578
+ var ctSource = new CancellationTokenSource ( ) ;
2579
+ var token = ctSource . Token ;
2580
+
2581
+ var fixture = RestService . For < ICancellableApi > ( "https://github.com" ) ;
2582
+
2583
+ ctSource . Cancel ( ) ;
2584
+ var task = fixture . GetWithCancellationAndReturn ( token ) ;
2585
+ await Assert . ThrowsAsync < TaskCanceledException > ( async ( ) => await task ) ;
2586
+ }
2587
+
2588
+
2589
+ [ Fact ]
2590
+ public async Task NullableCancellationTokenShouldBeIgnored ( )
2591
+ {
2592
+ var mockHttp = new MockHttpMessageHandler ( ) ;
2593
+ var settings = new RefitSettings { HttpMessageHandlerFactory = ( ) => mockHttp , } ;
2594
+
2595
+ mockHttp
2596
+ . Expect ( HttpMethod . Get , "https://github.com/foo" )
2597
+ . Respond ( HttpStatusCode . OK ) ;
2598
+
2599
+ var fixture = RestService . For < ICancellableApi > ( "https://github.com" , settings ) ;
2600
+
2601
+ await fixture . GetWithNullableCancellation ( null ) ;
2602
+
2603
+ mockHttp . VerifyNoOutstandingExpectation ( ) ;
2604
+ }
2605
+
2550
2606
[ Fact ]
2551
2607
public async Task TypeCollisionTest ( )
2552
2608
{
0 commit comments