Skip to content

Commit 29f3cf0

Browse files
author
ladeak
committed
Using custom cert for tests
1 parent bb7b174 commit 29f3cf0

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

src/CHttpServer/CHttpServer/HostExtensions.cs

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public static IWebHostBuilder UseCHttpServer(this IWebHostBuilder hostBuilder, A
5454
hostBuilder.ConfigureServices(services =>
5555
{
5656
services.TryAddSingleton<IConnectionListenerFactory, SocketTransportFactory>();
57-
//services.AddSingleton<IHttpsConfigurationService, HttpsConfigurationService>();
58-
//services.AddSingleton<HttpsConfigurationService.IInitializer, HttpsConfigurationService.Initializer>();
5957
services.Configure<CHttpServerOptions>(configure);
6058
services.AddSingleton<IServer, CHttpServerImpl>();
6159
});

tests/CHttp.Tests/HttpServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static WebApplication CreateHostBuilder(RequestDelegate? requestDelegate
2222
{
2323
kestrel.ListenAnyIP(port, options =>
2424
{
25-
options.UseHttps(new X509Certificate2("testCert.pfx", "testPassword"));
25+
options.UseHttps(X509CertificateLoader.LoadPkcs12FromFile("testCert.pfx", "testPassword"));
2626
options.Protocols = protocol ?? HttpProtocols.Http3;
2727
});
2828
configureKestrel?.Invoke(kestrel);

tests/CHttpServer.Tests/CHttpServer.Tests.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
1111
</PropertyGroup>
1212

13+
<ItemGroup>
14+
<None Include="..\CHttp.Tests\testCert.pfx" Link="testCert.pfx">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
1319
<ItemGroup>
1420
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
1521
<PackageReference Include="coverlet.collector" Version="6.0.2" />

tests/CHttpServer.Tests/ChttpServerIntegrationTests.cs

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System.Collections.Specialized;
2-
using System.Net;
3-
using System.Net.Http;
1+
using System.Net;
42
using System.Net.Http.Json;
5-
using System.Numerics;
6-
using System.Runtime.Intrinsics;
7-
using System.Runtime.Intrinsics.X86;
3+
using System.Security.Cryptography.X509Certificates;
84
using System.Text;
95
using Microsoft.AspNetCore.Builder;
106
using Microsoft.AspNetCore.Hosting;
@@ -26,10 +22,20 @@ public ChttpServerIntegrationTests(TestServer testServer)
2622
_server.RunAsync();
2723
}
2824

25+
private HttpClient CreateClient()
26+
{
27+
var handler = new HttpClientHandler
28+
{
29+
// Matching testCert.pfx
30+
ServerCertificateCustomValidationCallback = (message, certificate, chain, sslPolicyErrors) => certificate?.Issuer == "CN=localhost"
31+
};
32+
return new HttpClient(handler);
33+
}
34+
2935
[Fact]
3036
public async Task Get_NoContent()
3137
{
32-
var client = new HttpClient();
38+
var client = CreateClient();
3339
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/nocontent") { Version = HttpVersion.Version20 };
3440
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
3541
Assert.True(response.IsSuccessStatusCode);
@@ -38,7 +44,7 @@ public async Task Get_NoContent()
3844
[Fact]
3945
public async Task Get_Content()
4046
{
41-
var client = new HttpClient();
47+
var client = CreateClient();
4248
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
4349
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
4450
var content = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
@@ -49,7 +55,7 @@ public async Task Get_Content()
4955
[Fact]
5056
public async Task Get_NoStatusCode()
5157
{
52-
var client = new HttpClient();
58+
var client = CreateClient();
5359
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/nostatuscode") { Version = HttpVersion.Version20 };
5460
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
5561
Assert.True(response.IsSuccessStatusCode);
@@ -58,7 +64,7 @@ public async Task Get_NoStatusCode()
5864
[Fact]
5965
public async Task TestPost()
6066
{
61-
var client = new HttpClient();
67+
var client = CreateClient();
6268
var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:7222/post") { Version = HttpVersion.Version20, Content = JsonContent.Create(new WeatherForecast(new DateOnly(2025, 01, 01), 22, "sunny")) };
6369
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
6470
Assert.True(response.IsSuccessStatusCode);
@@ -67,7 +73,7 @@ public async Task TestPost()
6773
[Fact]
6874
public async Task HttpContext_WritesResponse()
6975
{
70-
var client = new HttpClient();
76+
var client = CreateClient();
7177
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/httpcontext") { Version = HttpVersion.Version20 };
7278
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
7379
Assert.True(response.IsSuccessStatusCode);
@@ -78,7 +84,7 @@ public async Task HttpContext_WritesResponse()
7884
[Fact]
7985
public async Task HttpContext_StreamsResponse()
8086
{
81-
var client = new HttpClient();
87+
var client = CreateClient();
8288
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/stream") { Version = HttpVersion.Version20 };
8389
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
8490
Assert.True(response.IsSuccessStatusCode);
@@ -95,7 +101,7 @@ await content.ReadExactlyAsync(buffer.AsMemory(), TestContext.Current.Cancellati
95101
[Fact]
96102
public async Task HttpContext_DoubleWrite()
97103
{
98-
var client = new HttpClient();
104+
var client = CreateClient();
99105
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/stream") { Version = HttpVersion.Version20 };
100106
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
101107
Assert.True(response.IsSuccessStatusCode);
@@ -106,7 +112,7 @@ public async Task HttpContext_DoubleWrite()
106112
[Fact]
107113
public async Task IAsyncEnumerable()
108114
{
109-
var client = new HttpClient();
115+
var client = CreateClient();
110116
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/iasyncenumerable") { Version = HttpVersion.Version20 };
111117
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
112118
Assert.True(response.IsSuccessStatusCode);
@@ -119,7 +125,7 @@ public async Task IAsyncEnumerable()
119125
[Fact]
120126
public async Task Get_Content_TwiceSerial_SameConnection()
121127
{
122-
var client = new HttpClient();
128+
var client = CreateClient();
123129
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
124130
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
125131
var content = await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken);
@@ -136,7 +142,7 @@ public async Task Get_Content_TwiceSerial_SameConnection()
136142
[Fact]
137143
public async Task Get_Content_TwoParallelRequests_SameConnection()
138144
{
139-
var client = new HttpClient();
145+
var client = CreateClient();
140146
var request0 = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
141147
var request1 = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
142148

@@ -160,7 +166,7 @@ public async Task Get_Content_ParallelRequests_NewConnection()
160166
{
161167
for (int i = 0; i < 10; i++)
162168
{
163-
var client = new HttpClient();
169+
var client = CreateClient();
164170
var request0 = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
165171
var request1 = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/content") { Version = HttpVersion.Version20 };
166172

@@ -183,7 +189,7 @@ public async Task Get_Content_ParallelRequests_NewConnection()
183189
[Fact]
184190
public async Task Header_And_Trailers()
185191
{
186-
var client = new HttpClient();
192+
var client = CreateClient();
187193
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:7222/headerstrailers") { Version = HttpVersion.Version20 };
188194
request.Headers.Add("x-custom", "custom-header-value");
189195
request.Headers.Accept.Add(new("application/json"));
@@ -204,7 +210,7 @@ public Task RunAsync()
204210
if (_app != null)
205211
return Task.CompletedTask;
206212
var builder = WebApplication.CreateBuilder();
207-
builder.WebHost.UseCHttpServer(o => { o.Port = 7222; });
213+
builder.WebHost.UseCHttpServer(o => { o.Port = 7222; o.Certificate = X509CertificateLoader.LoadPkcs12FromFile("testCert.pfx", "testPassword"); });
208214

209215
// Use Kestrel:
210216
//builder.WebHost.UseKestrel(o =>

0 commit comments

Comments
 (0)