@@ -1406,6 +1406,27 @@ await GetFactoryForVersion(UseVersion).CreateClientAndServerAsync(
1406
1406
} ) ;
1407
1407
}
1408
1408
1409
+ [ ConditionalTheory ( typeof ( PlatformDetection ) , nameof ( PlatformDetection . IsNotBrowser ) ) ]
1410
+ [ InlineData ( "A B" , "Foo" , typeof ( FormatException ) ) ] // Invalid header name
1411
+ [ InlineData ( "Content-Length" , "42" , typeof ( InvalidOperationException ) ) ] // Invalid header name for the request headers collection
1412
+ [ InlineData ( "Foo" , "Bar\n Baz" , typeof ( FormatException ) ) ] // Invalid header value
1413
+ public async Task SendAsync_PropagatorInjectsInvalidHeaders_Throws ( string headerName , string headerValue , Type exceptionType )
1414
+ {
1415
+ using Activity parent = new Activity ( "parent" ) ;
1416
+ parent . SetIdFormat ( ActivityIdFormat . W3C ) ;
1417
+ parent . Start ( ) ;
1418
+
1419
+ using var handler = CreateSocketsHttpHandler ( allowAllCertificates : true ) ;
1420
+ handler . ActivityHeadersPropagator = new DelegatingPropagator ( [ headerName ] , ( activity , carrier , setter ) => setter ( carrier , headerName , headerValue ) ) ;
1421
+
1422
+ using var client = new HttpClient ( handler ) ;
1423
+
1424
+ // Url doesn't matter since the request should fail before hitting the network.
1425
+ var request = CreateRequest ( HttpMethod . Get , new Uri ( "https://microsoft.com" ) , UseVersion , exactVersion : true ) ;
1426
+
1427
+ await Assert . ThrowsAsync ( exceptionType , ( ) => client . SendAsync ( TestAsync , request ) ) ;
1428
+ }
1429
+
1409
1430
public static IEnumerable < object [ ] > SocketsHttpHandler_ActivityCreation_MemberData ( )
1410
1431
{
1411
1432
foreach ( var currentActivitySet in new bool [ ] {
@@ -1855,5 +1876,20 @@ private static void AssertHeadersAreInjected(HttpRequestData request, Activity p
1855
1876
var request = CreateRequest ( HttpMethod . Get , uri , Version . Parse ( useVersion ) , exactVersion : true ) ;
1856
1877
return ( request , await client . SendAsync ( bool . Parse ( testAsync ) , request , cancellationToken ) ) ;
1857
1878
}
1879
+
1880
+ private sealed class DelegatingPropagator ( string [ ] fields , Action < Activity ? , object ? , DistributedContextPropagator . PropagatorSetterCallback ? > inject ) : DistributedContextPropagator
1881
+ {
1882
+ public override IReadOnlyCollection < string > Fields => fields ;
1883
+
1884
+ public override IEnumerable < KeyValuePair < string , string ? > > ? ExtractBaggage ( object ? carrier , PropagatorGetterCallback ? getter ) => [ ] ;
1885
+
1886
+ public override void ExtractTraceIdAndState ( object ? carrier , PropagatorGetterCallback ? getter , out string ? traceId , out string ? traceState )
1887
+ {
1888
+ traceId = null ;
1889
+ traceState = null ;
1890
+ }
1891
+
1892
+ public override void Inject ( Activity ? activity , object ? carrier , PropagatorSetterCallback ? setter ) => inject ( activity , carrier , setter ) ;
1893
+ }
1858
1894
}
1859
1895
}
0 commit comments