Skip to content

Commit 00f7601

Browse files
authored
Add unit test for client factory and HTTP/3 (#1487)
1 parent 763d3b9 commit 00f7601

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/FunctionalTests/Client/ClientFactoryTests.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
#endregion
1818

19+
using System.Net;
1920
using System.Net.Http;
2021
using System.Net.Security;
22+
using System.Threading;
2123
using System.Threading.Tasks;
2224
using Greet;
2325
using Grpc.AspNetCore.FunctionalTests.Infrastructure;
@@ -81,5 +83,66 @@ Task<HelloReply> UnaryCall(HelloRequest request, ServerCallContext context)
8183
// Assert 2
8284
Assert.AreEqual("Hello world", response2.Message);
8385
}
86+
87+
#if NET6_0
88+
[Test]
89+
[RequireHttp3]
90+
public async Task ClientFactory_Http3_Success()
91+
{
92+
// Arrange
93+
Task<HelloReply> UnaryCall(HelloRequest request, ServerCallContext context)
94+
{
95+
return Task.FromResult(new HelloReply { Message = $"Hello {request.Name}" });
96+
}
97+
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryCall);
98+
99+
var serviceCollection = new ServiceCollection();
100+
serviceCollection.AddSingleton<ILoggerFactory>(LoggerFactory);
101+
serviceCollection
102+
.AddGrpcClient<TestClient<HelloRequest, HelloReply>>(options =>
103+
{
104+
options.Address = Fixture.GetUrl(TestServerEndpointName.Http3WithTls);
105+
})
106+
.ConfigureGrpcClientCreator(invoker =>
107+
{
108+
return TestClientFactory.Create(invoker, method);
109+
})
110+
.AddHttpMessageHandler(() => new Http3Handler())
111+
.ConfigurePrimaryHttpMessageHandler(() =>
112+
{
113+
return new SocketsHttpHandler
114+
{
115+
SslOptions = new SslClientAuthenticationOptions
116+
{
117+
RemoteCertificateValidationCallback = (____, ___, __, _) => true
118+
}
119+
};
120+
});
121+
var services = serviceCollection.BuildServiceProvider();
122+
123+
// Act
124+
var client1 = services.GetRequiredService<TestClient<HelloRequest, HelloReply>>();
125+
var call1 = client1.UnaryCall(new HelloRequest { Name = "world" });
126+
var response1 = await call1.ResponseAsync.DefaultTimeout();
127+
128+
// Assert
129+
Assert.AreEqual("Hello world", response1.Message);
130+
}
131+
132+
private class Http3Handler : DelegatingHandler
133+
{
134+
public Http3Handler() { }
135+
public Http3Handler(HttpMessageHandler innerHandler) : base(innerHandler) { }
136+
137+
protected override Task<HttpResponseMessage> SendAsync(
138+
HttpRequestMessage request, CancellationToken cancellationToken)
139+
{
140+
request.Version = HttpVersion.Version30;
141+
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;
142+
143+
return base.SendAsync(request, cancellationToken);
144+
}
145+
}
146+
#endif
84147
}
85148
}

0 commit comments

Comments
 (0)