@@ -344,8 +344,9 @@ private static X509Certificate2 BuildClientCertificate ()
344344 [ Test ]
345345 public async Task HttpContentStreamIsRewoundAfterCancellation ( )
346346 {
347+ const int testPort = 47664 ;
347348 using var listener = new HttpListener ( ) ;
348- listener . Prefixes . Add ( "http://+:47664 /" ) ;
349+ listener . Prefixes . Add ( $ "http://+:{ testPort } /") ;
349350 listener . Start ( ) ;
350351
351352 // Handle the first request - simulate a slow server to allow cancellation
@@ -357,22 +358,24 @@ public async Task HttpContentStreamIsRewoundAfterCancellation ()
357358 while ( ctx . Request . InputStream . Read ( buffer , 0 , buffer . Length ) > 0 ) {
358359 System . Threading . Thread . Sleep ( 100 ) ; // Slow down to allow cancellation
359360 }
360- } catch {
361+ } catch ( Exception ex ) {
361362 // Expected when connection is cancelled
363+ Console . WriteLine ( $ "Exception while reading request body: { ex } ") ;
362364 }
363365 try {
364366 ctx . Response . StatusCode = 200 ;
365367 ctx . Response . Close ( ) ;
366- } catch {
368+ } catch ( Exception ex ) {
367369 // Connection may already be closed
370+ Console . WriteLine ( $ "Exception while closing response: { ex } ") ;
368371 }
369372 } , null ) ;
370373
371374 var tcs = new System . Threading . CancellationTokenSource ( ) ;
372375 tcs . CancelAfter ( 500 ) ; // Cancel after 500ms
373376 var client = new HttpClient ( new AndroidMessageHandler ( ) ) ;
374377 var byc = new ByteArrayContent ( new byte [ 1_000_000 ] ) ; // 1 MB of data
375- var request = new HttpRequestMessage ( HttpMethod . Post , "http://localhost:47664 /" ) { Content = byc } ;
378+ var request = new HttpRequestMessage ( HttpMethod . Post , $ "http://localhost:{ testPort } /") { Content = byc } ;
376379
377380 var stream = await byc . ReadAsStreamAsync ( ) ;
378381 var positionBefore = stream . Position ;
@@ -382,9 +385,10 @@ public async Task HttpContentStreamIsRewoundAfterCancellation ()
382385 try {
383386 await client . SendAsync ( request , tcs . Token ) . ConfigureAwait ( false ) ;
384387 // If we get here without exception, that's also OK for this test
385- } catch ( Exception ) {
388+ } catch ( Exception ex ) {
386389 // Expected - cancellation or connection error
387390 // We catch all exceptions to ensure the test doesn't fail due to unhandled exceptions
391+ Console . WriteLine ( $ "Exception during first request (expected): { ex } ") ;
388392 exceptionThrown = true ;
389393 }
390394
@@ -393,10 +397,9 @@ public async Task HttpContentStreamIsRewoundAfterCancellation ()
393397 var positionAfter = stream2 . Position ;
394398 Assert . AreEqual ( 0 , positionAfter , "Stream position should be 0 after failed request (stream should be rewound)" ) ;
395399
396-
397400 // Only proceed with second request if we actually got an exception (test scenario succeeded)
398401 if ( exceptionThrown ) {
399- var request2 = new HttpRequestMessage ( HttpMethod . Post , "http://localhost:47664 /" ) { Content = byc } ;
402+ var request2 = new HttpRequestMessage ( HttpMethod . Post , $ "http://localhost:{ testPort } /") { Content = byc } ;
400403
401404 // Set up listener for second request
402405 listener . BeginGetContext ( ar => {
@@ -411,7 +414,6 @@ public async Task HttpContentStreamIsRewoundAfterCancellation ()
411414 var stream3 = await byc . ReadAsStreamAsync ( ) ;
412415 var positionFinal = stream3 . Position ;
413416 Assert . AreEqual ( 0 , positionFinal , "Stream position should be 0 after successful request" ) ;
414-
415417 }
416418
417419 listener . Close ( ) ;
0 commit comments