@@ -29,6 +29,8 @@ public sealed class RequestContext
29
29
30
30
public RequestContext ( Configuration config , HttpClient httpClient , Random random , CancellationToken globalToken , int taskNum )
31
31
{
32
+ Debug . Assert ( httpClient . BaseAddress != null ) ;
33
+
32
34
_random = random ;
33
35
_client = httpClient ;
34
36
_globalToken = globalToken ;
@@ -47,7 +49,7 @@ public RequestContext(Configuration config, HttpClient httpClient, Random random
47
49
public int MaxRequestHeaderCount => _config . MaxRequestHeaderCount ;
48
50
public int MaxRequestHeaderTotalSize => _config . MaxRequestHeaderTotalSize ;
49
51
public int MaxContentLength => _config . MaxContentLength ;
50
- public Uri BaseAddress => _client . BaseAddress ;
52
+ public Uri BaseAddress => _client . BaseAddress ! ;
51
53
52
54
// HttpClient.SendAsync() wrapper that wires randomized cancellation
53
55
public async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , HttpCompletionOption httpCompletion = HttpCompletionOption . ResponseContentRead , CancellationToken ? token = null )
@@ -190,7 +192,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
190
192
using HttpResponseMessage m = await ctx . SendAsync ( req ) ;
191
193
192
194
ValidateStatusCode ( m ) ;
193
- ValidateServerContent ( await m . Content . ReadAsStringAsync ( ) , expectedLength ) ;
195
+ ValidateServerContent ( await m . Content ! . ReadAsStringAsync ( ) , expectedLength ) ;
194
196
} ) ,
195
197
196
198
( "GET Partial" ,
@@ -202,7 +204,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
202
204
203
205
ValidateStatusCode ( m ) ;
204
206
205
- using ( Stream s = await m . Content . ReadAsStreamAsync ( ) )
207
+ using ( Stream s = await m . Content ! . ReadAsStreamAsync ( ) )
206
208
{
207
209
s . ReadByte ( ) ; // read single byte from response and throw the rest away
208
210
}
@@ -219,22 +221,21 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
219
221
220
222
ValidateStatusCode ( res ) ;
221
223
222
- await res . Content . ReadAsStringAsync ( ) ;
224
+ await res . Content ! . ReadAsStringAsync ( ) ;
223
225
224
226
bool isValidChecksum = ValidateServerChecksum ( res . Headers , expectedChecksum ) ;
225
- string GetFailureDetails ( ) => isValidChecksum ? "server checksum matches client checksum" : "server checksum mismatch" ;
227
+ string failureDetails = isValidChecksum ? "server checksum matches client checksum" : "server checksum mismatch" ;
226
228
227
229
// Validate that request headers are being echoed
228
230
foreach ( KeyValuePair < string , IEnumerable < string > > reqHeader in req . Headers )
229
231
{
230
- if ( ! res . Headers . TryGetValues ( reqHeader . Key , out IEnumerable < string > values ) )
232
+ if ( ! res . Headers . TryGetValues ( reqHeader . Key , out IEnumerable < string > ? values ) )
231
233
{
232
- throw new Exception ( $ "Expected response header name { reqHeader . Key } missing. { GetFailureDetails ( ) } ") ;
234
+ throw new Exception ( $ "Expected response header name { reqHeader . Key } missing. { failureDetails } ") ;
233
235
}
234
236
else if ( ! reqHeader . Value . SequenceEqual ( values ) )
235
237
{
236
- string FmtValues ( IEnumerable < string > values ) => string . Join ( ", " , values . Select ( x => $ "\" { x } \" ") ) ;
237
- throw new Exception ( $ "Unexpected values for header { reqHeader . Key } . Expected { FmtValues ( reqHeader . Value ) } but got { FmtValues ( values ) } . { GetFailureDetails ( ) } ") ;
238
+ throw new Exception ( $ "Unexpected values for header { reqHeader . Key } . Expected { FmtValues ( reqHeader . Value ) } but got { FmtValues ( values ) } . { failureDetails } ") ;
238
239
}
239
240
}
240
241
@@ -243,14 +244,13 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
243
244
{
244
245
foreach ( KeyValuePair < string , IEnumerable < string > > reqHeader in req . Headers )
245
246
{
246
- if ( ! res . TrailingHeaders . TryGetValues ( reqHeader . Key + "-trailer" , out IEnumerable < string > values ) )
247
+ if ( ! res . TrailingHeaders . TryGetValues ( reqHeader . Key + "-trailer" , out IEnumerable < string > ? values ) )
247
248
{
248
- throw new Exception ( $ "Expected trailing header name { reqHeader . Key } -trailer missing. { GetFailureDetails ( ) } ") ;
249
+ throw new Exception ( $ "Expected trailing header name { reqHeader . Key } -trailer missing. { failureDetails } ") ;
249
250
}
250
251
else if ( ! reqHeader . Value . SequenceEqual ( values ) )
251
252
{
252
- string FmtValues ( IEnumerable < string > values ) => string . Join ( ", " , values . Select ( x => $ "\" { x } \" ") ) ;
253
- throw new Exception ( $ "Unexpected values for trailing header { reqHeader . Key } -trailer. Expected { FmtValues ( reqHeader . Value ) } but got { FmtValues ( values ) } . { GetFailureDetails ( ) } ") ;
253
+ throw new Exception ( $ "Unexpected values for trailing header { reqHeader . Key } -trailer. Expected { FmtValues ( reqHeader . Value ) } but got { FmtValues ( values ) } . { failureDetails } ") ;
254
254
}
255
255
}
256
256
}
@@ -260,6 +260,8 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
260
260
// Should not reach this block unless there's a bug in checksum validation logic. Do throw now
261
261
throw new Exception ( "server checksum mismatch" ) ;
262
262
}
263
+
264
+ static string FmtValues ( IEnumerable < string > values ) => string . Join ( ", " , values . Select ( x => $ "\" { x } \" ") ) ;
263
265
} ) ,
264
266
265
267
( "GET Parameters" ,
@@ -271,7 +273,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
271
273
using HttpResponseMessage m = await ctx . SendAsync ( req ) ;
272
274
273
275
ValidateStatusCode ( m ) ;
274
- ValidateContent ( expectedResponse , await m . Content . ReadAsStringAsync ( ) , $ "Uri: { uri } ") ;
276
+ ValidateContent ( expectedResponse , await m . Content ! . ReadAsStringAsync ( ) , $ "Uri: { uri } ") ;
275
277
} ) ,
276
278
277
279
( "GET Aborted" ,
@@ -330,7 +332,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
330
332
331
333
ValidateStatusCode ( m ) ;
332
334
string checksumMessage = ValidateServerChecksum ( m . Headers , checksum ) ? "server checksum matches client checksum" : "server checksum mismatch" ;
333
- ValidateContent ( content , await m . Content . ReadAsStringAsync ( ) , checksumMessage ) ;
335
+ ValidateContent ( content , await m . Content ! . ReadAsStringAsync ( ) , checksumMessage ) ;
334
336
} ) ,
335
337
336
338
( "POST Multipart Data" ,
@@ -344,7 +346,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
344
346
345
347
ValidateStatusCode ( m ) ;
346
348
string checksumMessage = ValidateServerChecksum ( m . Headers , checksum ) ? "server checksum matches client checksum" : "server checksum mismatch" ;
347
- ValidateContent ( formData . expected , await m . Content . ReadAsStringAsync ( ) , checksumMessage ) ;
349
+ ValidateContent ( formData . expected , await m . Content ! . ReadAsStringAsync ( ) , checksumMessage ) ;
348
350
} ) ,
349
351
350
352
( "POST Duplex" ,
@@ -357,7 +359,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
357
359
using HttpResponseMessage m = await ctx . SendAsync ( req , HttpCompletionOption . ResponseHeadersRead ) ;
358
360
359
361
ValidateStatusCode ( m ) ;
360
- string response = await m . Content . ReadAsStringAsync ( ) ;
362
+ string response = await m . Content ! . ReadAsStringAsync ( ) ;
361
363
362
364
string checksumMessage = ValidateServerChecksum ( m . TrailingHeaders , checksum , required : false ) ? "server checksum matches client checksum" : "server checksum mismatch" ;
363
365
ValidateContent ( content , await m . Content . ReadAsStringAsync ( ) , checksumMessage ) ;
@@ -374,7 +376,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
374
376
using HttpResponseMessage m = await ctx . SendAsync ( req , HttpCompletionOption . ResponseHeadersRead ) ;
375
377
376
378
ValidateStatusCode ( m ) ;
377
- string response = await m . Content . ReadAsStringAsync ( ) ;
379
+ string response = await m . Content ! . ReadAsStringAsync ( ) ;
378
380
379
381
// trailing headers not supported for all servers, so do not require checksums
380
382
bool isValidChecksum = ValidateServerChecksum ( m . TrailingHeaders , checksum , required : false ) ;
@@ -414,7 +416,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
414
416
415
417
ValidateStatusCode ( m ) ;
416
418
string checksumMessage = ValidateServerChecksum ( m . Headers , checksum ) ? "server checksum matches client checksum" : "server checksum mismatch" ;
417
- ValidateContent ( content , await m . Content . ReadAsStringAsync ( ) , checksumMessage ) ;
419
+ ValidateContent ( content , await m . Content ! . ReadAsStringAsync ( ) , checksumMessage ) ;
418
420
} ) ,
419
421
420
422
( "HEAD" ,
@@ -426,7 +428,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
426
428
427
429
ValidateStatusCode ( m ) ;
428
430
429
- if ( m . Content . Headers . ContentLength != expectedLength )
431
+ if ( m . Content ! . Headers . ContentLength != expectedLength )
430
432
{
431
433
throw new Exception ( $ "Expected { expectedLength } , got { m . Content . Headers . ContentLength } ") ;
432
434
}
@@ -444,7 +446,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
444
446
445
447
ValidateStatusCode ( m ) ;
446
448
447
- string r = await m . Content . ReadAsStringAsync ( ) ;
449
+ string r = await m . Content ! . ReadAsStringAsync ( ) ;
448
450
if ( r != "" ) throw new Exception ( $ "Got unexpected response: { r } ") ;
449
451
} ) ,
450
452
@@ -458,7 +460,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
458
460
459
461
ValidateStatusCode ( m ) ;
460
462
461
- string r = await m . Content . ReadAsStringAsync ( ) ;
463
+ string r = await m . Content ! . ReadAsStringAsync ( ) ;
462
464
if ( r != "" ) throw new Exception ( $ "Got unexpected response: { r } ") ;
463
465
} ) ,
464
466
@@ -470,7 +472,7 @@ public static (string name, Func<RequestContext, Task> operation)[] Operations =
470
472
using HttpResponseMessage m = await ctx . SendAsync ( req ) ;
471
473
472
474
ValidateStatusCode ( m ) ;
473
- ValidateServerContent ( await m . Content . ReadAsStringAsync ( ) , expectedLength ) ;
475
+ ValidateServerContent ( await m . Content ! . ReadAsStringAsync ( ) , expectedLength ) ;
474
476
} ) ,
475
477
} ;
476
478
@@ -573,7 +575,7 @@ private static (string, MultipartContent) GetMultipartContent(RequestContext cli
573
575
574
576
private static bool ValidateServerChecksum ( HttpResponseHeaders headers , ulong expectedChecksum , bool required = true )
575
577
{
576
- if ( headers . TryGetValues ( "crc32" , out IEnumerable < string > values ) &&
578
+ if ( headers . TryGetValues ( "crc32" , out IEnumerable < string > ? values ) &&
577
579
uint . TryParse ( values . First ( ) , out uint serverChecksum ) )
578
580
{
579
581
return serverChecksum == expectedChecksum ;
@@ -595,7 +597,7 @@ private sealed class StringDuplexContent : HttpContent
595
597
596
598
public StringDuplexContent ( string value ) => _data = Encoding . UTF8 . GetBytes ( value ) ;
597
599
598
- protected override Task SerializeToStreamAsync ( Stream stream , TransportContext context ) =>
600
+ protected override Task SerializeToStreamAsync ( Stream stream , TransportContext ? context ) =>
599
601
stream . WriteAsync ( _data , 0 , _data . Length ) ;
600
602
601
603
protected override bool TryComputeLength ( out long length )
@@ -612,7 +614,7 @@ private sealed class ByteAtATimeNoLengthContent : HttpContent
612
614
613
615
public ByteAtATimeNoLengthContent ( byte [ ] buffer ) => _buffer = buffer ;
614
616
615
- protected override async Task SerializeToStreamAsync ( Stream stream , TransportContext context )
617
+ protected override async Task SerializeToStreamAsync ( Stream stream , TransportContext ? context )
616
618
{
617
619
for ( int i = 0 ; i < _buffer . Length ; i ++ )
618
620
{
0 commit comments