@@ -2318,33 +2318,70 @@ await server.AcceptConnectionAsync(
2318
2318
[ ConditionalFact ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
2319
2319
public async Task SendHttpRequest_WhenDefaultMaximumErrorResponseLengthSet_Success ( )
2320
2320
{
2321
- await RemoteExecutor . Invoke ( async ( async ) =>
2321
+ await RemoteExecutor . Invoke ( async isAsync =>
2322
2322
{
2323
2323
TaskCompletionSource tcs = new TaskCompletionSource ( ) ;
2324
2324
await LoopbackServer . CreateClientAndServerAsync (
2325
- async ( uri ) =>
2325
+ async uri =>
2326
2326
{
2327
2327
HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
2328
- HttpWebRequest . DefaultMaximumErrorResponseLength = 5 ;
2329
- var exception =
2330
- await Assert . ThrowsAsync < WebException > ( ( ) => bool . Parse ( async ) ? request . GetResponseAsync ( ) : Task . Run ( ( ) => request . GetResponse ( ) ) ) ;
2328
+ HttpWebRequest . DefaultMaximumErrorResponseLength = 1 ; // 1 KB
2329
+ WebException exception =
2330
+ await Assert . ThrowsAsync < WebException > ( ( ) => bool . Parse ( isAsync ) ? request . GetResponseAsync ( ) : Task . Run ( ( ) => request . GetResponse ( ) ) ) ;
2331
2331
tcs . SetResult ( ) ;
2332
2332
Assert . NotNull ( exception . Response ) ;
2333
- using ( var responseStream = exception . Response . GetResponseStream ( ) )
2333
+ using ( Stream responseStream = exception . Response . GetResponseStream ( ) )
2334
2334
{
2335
- var buffer = new byte [ 10 ] ;
2336
- int readLen = responseStream . Read ( buffer , 0 , buffer . Length ) ;
2337
- Assert . Equal ( 5 , readLen ) ;
2338
- Assert . Equal ( new string ( 'a' , 5 ) , Encoding . UTF8 . GetString ( buffer [ 0 ..readLen ] ) ) ;
2339
- Assert . Equal ( 0 , responseStream . Read ( buffer ) ) ;
2335
+ byte [ ] buffer = new byte [ 10 * 1024 ] ;
2336
+ int totalReadLen = 0 ;
2337
+ int readLen = 0 ;
2338
+ while ( ( readLen = responseStream . Read ( buffer , readLen , buffer . Length - readLen ) ) > 0 )
2339
+ {
2340
+ totalReadLen += readLen ;
2341
+ }
2342
+
2343
+ Assert . Equal ( 1024 , totalReadLen ) ;
2344
+ Assert . Equal ( new string ( 'a' , 1024 ) , Encoding . UTF8 . GetString ( buffer [ 0 ..totalReadLen ] ) ) ;
2340
2345
}
2341
2346
} ,
2342
- async ( server ) =>
2347
+ async server =>
2348
+ {
2349
+ await server . AcceptConnectionAsync (
2350
+ async connection =>
2351
+ {
2352
+ await connection . SendResponseAsync ( statusCode : HttpStatusCode . BadRequest , content : new string ( 'a' , 10 * 1024 ) ) ;
2353
+ await tcs . Task ;
2354
+ } ) ;
2355
+ } ) ;
2356
+ } , IsAsync . ToString ( ) ) . DisposeAsync ( ) ;
2357
+ }
2358
+
2359
+ [ ConditionalFact ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
2360
+ public async Task SendHttpRequest_WhenDefaultMaximumErrorResponseLengthSetToIntMax_DoesNotThrow ( )
2361
+ {
2362
+ await RemoteExecutor . Invoke ( async isAsync =>
2363
+ {
2364
+ TaskCompletionSource tcs = new TaskCompletionSource ( ) ;
2365
+ await LoopbackServer . CreateClientAndServerAsync (
2366
+ async uri =>
2367
+ {
2368
+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
2369
+ HttpWebRequest . DefaultMaximumErrorResponseLength = int . MaxValue ; // KB
2370
+ WebException exception =
2371
+ await Assert . ThrowsAsync < WebException > ( ( ) => bool . Parse ( isAsync ) ? request . GetResponseAsync ( ) : Task . Run ( ( ) => request . GetResponse ( ) ) ) ;
2372
+ tcs . SetResult ( ) ;
2373
+ Assert . NotNull ( exception . Response ) ;
2374
+ using ( Stream responseStream = exception . Response . GetResponseStream ( ) )
2375
+ {
2376
+ Assert . Equal ( 1 , await responseStream . ReadAsync ( new byte [ 1 ] ) ) ;
2377
+ }
2378
+ } ,
2379
+ async server =>
2343
2380
{
2344
2381
await server . AcceptConnectionAsync (
2345
2382
async connection =>
2346
2383
{
2347
- await connection . SendResponseAsync ( statusCode : HttpStatusCode . BadRequest , content : new string ( 'a' , 10 ) ) ;
2384
+ await connection . SendResponseAsync ( statusCode : HttpStatusCode . BadRequest , content : new string ( 'a' , 10 * 1024 ) ) ;
2348
2385
await tcs . Task ;
2349
2386
} ) ;
2350
2387
} ) ;
0 commit comments