Skip to content

Commit 1928c4b

Browse files
committed
Use HttpClientTestServer
1 parent e36a73c commit 1928c4b

19 files changed

+139
-368
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "external/HttpClientTestServer"]
2+
path = external/HttpClientTestServer
3+
url = https://github.com/Cysharp/HttpClientTestServer.git

YetAnotherHttpHandler.slnx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
<Project Path="test/YetAnotherHttpHandler.StandaloneTestServer/YetAnotherHttpHandler.StandaloneTestServer.csproj" />
1818
<Project Path="test/YetAnotherHttpHandler.Test/YetAnotherHttpHandler.Test.csproj" />
1919
</Folder>
20+
<Folder Name="/tests/external/">
21+
<Project Path="external/HttpClientTestServer/src/HttpClientTestServer.Abstractions/HttpClientTestServer.Abstractions.csproj" />
22+
<Project Path="external/HttpClientTestServer/src/HttpClientTestServer.Launcher.Container/HttpClientTestServer.Launcher.Container.csproj" />
23+
<Project Path="external/HttpClientTestServer/src/HttpClientTestServer/HttpClientTestServer.csproj" />
24+
</Folder>
2025
<Project Path="src/YetAnotherHttpHandler/YetAnotherHttpHandler.csproj" />
2126
</Solution>

external/HttpClientTestServer

Submodule HttpClientTestServer added at 7eec46c

test/YetAnotherHttpHandler.Test/BackPressureTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public async Task FlushTest(int size)
1515
{
1616
using var httpHandler = new YetAnotherHttpHandler();
1717
using var client = new HttpClient(httpHandler);
18-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>(TestWebAppServerListenMode.InsecureHttp1Only);
18+
await using var server = await LaunchServerAsync(TestServerListenMode.InsecureHttp1Only);
1919

2020
var numRequests = Environment.ProcessorCount + 1; // more than the number of tokio worker threads
2121

@@ -36,4 +36,4 @@ public async Task FlushTest(int size)
3636

3737
foreach (var stream in pendingStreams) await stream.DisposeAsync();
3838
}
39-
}
39+
}

test/YetAnotherHttpHandler.Test/ClientCertificateTest.cs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System.Security.Cryptography.X509Certificates;
22
using Cysharp.Net.Http;
3-
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.AspNetCore.Hosting;
5-
using Microsoft.AspNetCore.Server.Kestrel.Https;
3+
using HttpClientTestServer;
64

75
namespace _YetAnotherHttpHandler.Test;
86

@@ -12,34 +10,14 @@ public ClientCertificateTest(ITestOutputHelper testOutputHelper) : base(testOutp
1210
{
1311
}
1412

15-
protected Task<TestWebAppServer> LaunchServerAsync<T>(Action<WebApplicationBuilder>? configure = null)
16-
where T : ITestServerBuilder
17-
{
18-
return LaunchServerAsync<T>(TestWebAppServerListenMode.SecureHttp1AndHttp2, builder =>
19-
{
20-
// Use self-signed certificate for testing purpose.
21-
builder.WebHost.ConfigureKestrel(options =>
22-
{
23-
options.ConfigureHttpsDefaults(options =>
24-
{
25-
options.ServerCertificate = new X509Certificate2("Certificates/localhost.pfx");
26-
options.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
27-
options.ClientCertificateValidation = (certificate2, chain, policyError) =>
28-
{
29-
return certificate2.Subject == "CN=client.example.com";
30-
};
31-
});
32-
});
33-
34-
configure?.Invoke(builder);
35-
});
36-
}
13+
protected Task<ITestServer> LaunchServerAsync()
14+
=> LaunchServerAsync(new TestServerOptions(ListenHttpProtocols.Http1AndHttp2, isSecure: true) { EnableClientCertificateValidation = true });
3715

3816
[Fact]
3917
public async Task NotSet()
4018
{
4119
// Arrange
42-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
20+
await using var server = await LaunchServerAsync();
4321
using var httpHandler = new YetAnotherHttpHandler()
4422
{
4523
//ClientAuthCertificates = File.ReadAllText("./Certificates/client.crt"),
@@ -61,7 +39,7 @@ public async Task NotSet()
6139
public async Task UseClientCertificate()
6240
{
6341
// Arrange
64-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
42+
await using var server = await LaunchServerAsync();
6543
using var httpHandler = new YetAnotherHttpHandler()
6644
{
6745
ClientAuthCertificates = File.ReadAllText("./Certificates/client.crt"),
@@ -78,12 +56,12 @@ public async Task UseClientCertificate()
7856
// Assert
7957
Assert.Equal("__OK__", result);
8058
}
81-
59+
8260
[Fact]
8361
public async Task Invalid()
8462
{
8563
// Arrange
86-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
64+
await using var server = await LaunchServerAsync();
8765
using var httpHandler = new YetAnotherHttpHandler()
8866
{
8967
ClientAuthCertificates = File.ReadAllText("./Certificates/client_unknown.crt"), // CN=unknown.example.com
@@ -104,7 +82,7 @@ public async Task Invalid()
10482
public async Task Reference_SocketHttpHandler_NotSet()
10583
{
10684
// Arrange
107-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
85+
await using var server = await LaunchServerAsync();
10886
using var httpHandler = new SocketsHttpHandler();
10987
httpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true;
11088
var httpClient = new HttpClient(httpHandler);
@@ -121,7 +99,7 @@ public async Task Reference_SocketHttpHandler_NotSet()
12199
public async Task Reference_SocketHttpHandler_UseClientCertificate()
122100
{
123101
// Arrange
124-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
102+
await using var server = await LaunchServerAsync();
125103
using var httpHandler = new SocketsHttpHandler();
126104
httpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true;
127105
httpHandler.SslOptions.ClientCertificates = new X509CertificateCollection()
@@ -143,7 +121,7 @@ public async Task Reference_SocketHttpHandler_UseClientCertificate()
143121
public async Task Reference_SocketHttpHandler_Invalid()
144122
{
145123
// Arrange
146-
await using var server = await LaunchServerAsync<TestServerForHttp1AndHttp2>();
124+
await using var server = await LaunchServerAsync();
147125
using var httpHandler = new SocketsHttpHandler();
148126
httpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true;
149127
httpHandler.SslOptions.ClientCertificates = new X509CertificateCollection()
@@ -159,4 +137,4 @@ public async Task Reference_SocketHttpHandler_Invalid()
159137
// Assert
160138
Assert.IsType<HttpRequestException>(ex);
161139
}
162-
}
140+
}

test/YetAnotherHttpHandler.Test/Helpers/TestServerHelper.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/YetAnotherHttpHandler.Test/Helpers/TestWebAppServer.cs

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)