1616
1717#endregion
1818
19+ using System ;
20+ using System . Net . Http ;
21+ using System . Threading . Tasks ;
1922using Greet ;
2023using Grpc . AspNetCore . FunctionalTests . Infrastructure ;
2124using Grpc . Core ;
2225using Grpc . Net . Client ;
23- using Grpc . Tests . Shared ;
26+ using Microsoft . Extensions . DependencyInjection ;
27+ using Microsoft . Extensions . Logging ;
2428using NUnit . Framework ;
25- using System . Net . Http ;
26- using System . Threading . Tasks ;
2729
2830namespace Grpc . AspNetCore . FunctionalTests . Client
2931{
3032 [ TestFixture ]
3133 public class TelemetryTests : FunctionalTestBase
3234 {
33- [ Test ]
34- public async Task InternalHandler_UnaryCall_TelemetryHeaderSentWithRequest ( )
35+ [ TestCase ( ClientType . Channel ) ]
36+ [ TestCase ( ClientType . ClientFactory ) ]
37+ public async Task InternalHandler_UnaryCall_TelemetryHeaderSentWithRequest ( ClientType clientType )
3538 {
36- await TestTelemetryHeaderIsSet ( handler : null ) ;
39+ await TestTelemetryHeaderIsSet ( clientType , handler : null ) ;
3740 }
3841
3942#if NET5_0
40- [ Test ]
41- public async Task SocketsHttpHandler_UnaryCall_TelemetryHeaderSentWithRequest ( )
43+ [ TestCase ( ClientType . Channel ) ]
44+ [ TestCase ( ClientType . ClientFactory ) ]
45+ public async Task Channel_SocketsHttpHandler_UnaryCall_TelemetryHeaderSentWithRequest ( ClientType clientType )
4246 {
43- await TestTelemetryHeaderIsSet ( handler : new SocketsHttpHandler ( ) ) ;
47+ await TestTelemetryHeaderIsSet ( clientType , handler : new SocketsHttpHandler ( ) ) ;
4448 }
4549
46- [ Test ]
47- public async Task SocketsHttpHandlerWrapped_UnaryCall_TelemetryHeaderSentWithRequest ( )
50+ [ TestCase ( ClientType . Channel ) ]
51+ [ TestCase ( ClientType . ClientFactory ) ]
52+ public async Task Channel_SocketsHttpHandlerWrapped_UnaryCall_TelemetryHeaderSentWithRequest ( ClientType clientType )
4853 {
49- await TestTelemetryHeaderIsSet ( handler : new TestDelegatingHandler ( new SocketsHttpHandler ( ) ) ) ;
54+ await TestTelemetryHeaderIsSet ( clientType , handler : new TestDelegatingHandler ( new SocketsHttpHandler ( ) ) ) ;
5055 }
5156
5257 private class TestDelegatingHandler : DelegatingHandler
@@ -57,7 +62,7 @@ public TestDelegatingHandler(HttpMessageHandler innerHandler) : base(innerHandle
5762 }
5863#endif
5964
60- private async Task TestTelemetryHeaderIsSet ( HttpMessageHandler ? handler )
65+ private async Task TestTelemetryHeaderIsSet ( ClientType clientType , HttpMessageHandler ? handler )
6166 {
6267 string ? telemetryHeader = null ;
6368 Task < HelloReply > UnaryTelemetryHeader ( HelloRequest request , ServerCallContext context )
@@ -75,23 +80,58 @@ Task<HelloReply> UnaryTelemetryHeader(HelloRequest request, ServerCallContext co
7580
7681 // Arrange
7782 var method = Fixture . DynamicGrpc . AddUnaryMethod < HelloRequest , HelloReply > ( UnaryTelemetryHeader ) ;
78-
79- var options = new GrpcChannelOptions
80- {
81- LoggerFactory = LoggerFactory ,
82- HttpHandler = handler
83- } ;
84-
85- // Want to test the behavior of the default, internally created handler.
86- // Only supply the URL to a manually created GrpcChannel.
87- var channel = GrpcChannel . ForAddress ( Fixture . GetUrl ( TestServerEndpointName . Http2 ) , options ) ;
88- var client = TestClientFactory . Create ( channel , method ) ;
83+ var client = CreateClient ( clientType , method , handler ) ;
8984
9085 // Act
9186 await client . UnaryCall ( new HelloRequest ( ) ) ;
9287
9388 // Assert
9489 Assert . IsNotNull ( telemetryHeader ) ;
9590 }
91+
92+ private TestClient < HelloRequest , HelloReply > CreateClient ( ClientType clientType , Method < HelloRequest , HelloReply > method , HttpMessageHandler ? handler )
93+ {
94+ switch ( clientType )
95+ {
96+ case ClientType . Channel :
97+ {
98+ var options = new GrpcChannelOptions
99+ {
100+ LoggerFactory = LoggerFactory ,
101+ HttpHandler = handler
102+ } ;
103+
104+ // Want to test the behavior of the default, internally created handler.
105+ // Only supply the URL to a manually created GrpcChannel.
106+ var channel = GrpcChannel . ForAddress ( Fixture . GetUrl ( TestServerEndpointName . Http2 ) , options ) ;
107+ return TestClientFactory . Create ( channel , method ) ;
108+ }
109+ case ClientType . ClientFactory :
110+ {
111+ var serviceCollection = new ServiceCollection ( ) ;
112+ serviceCollection . AddSingleton < ILoggerFactory > ( LoggerFactory ) ;
113+ serviceCollection
114+ . AddGrpcClient < TestClient < HelloRequest , HelloReply > > ( options =>
115+ {
116+ options . Address = Fixture . GetUrl ( TestServerEndpointName . Http2 ) ;
117+ } )
118+ . ConfigureGrpcClientCreator ( invoker =>
119+ {
120+ return TestClientFactory . Create ( invoker , method ) ;
121+ } ) ;
122+ var services = serviceCollection . BuildServiceProvider ( ) ;
123+
124+ return services . GetRequiredService < TestClient < HelloRequest , HelloReply > > ( ) ;
125+ }
126+ default :
127+ throw new InvalidOperationException ( "Unexpected value." ) ;
128+ }
129+ }
130+
131+ public enum ClientType
132+ {
133+ Channel ,
134+ ClientFactory
135+ }
96136 }
97137}
0 commit comments