1
- using System . Collections . Specialized ;
2
- using System . Net ;
3
- using System . Net . Http ;
1
+ using System . Net ;
4
2
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 ;
8
4
using System . Text ;
9
5
using Microsoft . AspNetCore . Builder ;
10
6
using Microsoft . AspNetCore . Hosting ;
@@ -26,10 +22,20 @@ public ChttpServerIntegrationTests(TestServer testServer)
26
22
_server . RunAsync ( ) ;
27
23
}
28
24
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
+
29
35
[ Fact ]
30
36
public async Task Get_NoContent ( )
31
37
{
32
- var client = new HttpClient ( ) ;
38
+ var client = CreateClient ( ) ;
33
39
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/nocontent" ) { Version = HttpVersion . Version20 } ;
34
40
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
35
41
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -38,7 +44,7 @@ public async Task Get_NoContent()
38
44
[ Fact ]
39
45
public async Task Get_Content ( )
40
46
{
41
- var client = new HttpClient ( ) ;
47
+ var client = CreateClient ( ) ;
42
48
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
43
49
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
44
50
var content = await response . Content . ReadAsStringAsync ( TestContext . Current . CancellationToken ) ;
@@ -49,7 +55,7 @@ public async Task Get_Content()
49
55
[ Fact ]
50
56
public async Task Get_NoStatusCode ( )
51
57
{
52
- var client = new HttpClient ( ) ;
58
+ var client = CreateClient ( ) ;
53
59
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/nostatuscode" ) { Version = HttpVersion . Version20 } ;
54
60
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
55
61
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -58,7 +64,7 @@ public async Task Get_NoStatusCode()
58
64
[ Fact ]
59
65
public async Task TestPost ( )
60
66
{
61
- var client = new HttpClient ( ) ;
67
+ var client = CreateClient ( ) ;
62
68
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" ) ) } ;
63
69
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
64
70
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -67,7 +73,7 @@ public async Task TestPost()
67
73
[ Fact ]
68
74
public async Task HttpContext_WritesResponse ( )
69
75
{
70
- var client = new HttpClient ( ) ;
76
+ var client = CreateClient ( ) ;
71
77
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/httpcontext" ) { Version = HttpVersion . Version20 } ;
72
78
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
73
79
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -78,7 +84,7 @@ public async Task HttpContext_WritesResponse()
78
84
[ Fact ]
79
85
public async Task HttpContext_StreamsResponse ( )
80
86
{
81
- var client = new HttpClient ( ) ;
87
+ var client = CreateClient ( ) ;
82
88
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/stream" ) { Version = HttpVersion . Version20 } ;
83
89
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
84
90
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -95,7 +101,7 @@ await content.ReadExactlyAsync(buffer.AsMemory(), TestContext.Current.Cancellati
95
101
[ Fact ]
96
102
public async Task HttpContext_DoubleWrite ( )
97
103
{
98
- var client = new HttpClient ( ) ;
104
+ var client = CreateClient ( ) ;
99
105
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/stream" ) { Version = HttpVersion . Version20 } ;
100
106
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
101
107
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -106,7 +112,7 @@ public async Task HttpContext_DoubleWrite()
106
112
[ Fact ]
107
113
public async Task IAsyncEnumerable ( )
108
114
{
109
- var client = new HttpClient ( ) ;
115
+ var client = CreateClient ( ) ;
110
116
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/iasyncenumerable" ) { Version = HttpVersion . Version20 } ;
111
117
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
112
118
Assert . True ( response . IsSuccessStatusCode ) ;
@@ -119,7 +125,7 @@ public async Task IAsyncEnumerable()
119
125
[ Fact ]
120
126
public async Task Get_Content_TwiceSerial_SameConnection ( )
121
127
{
122
- var client = new HttpClient ( ) ;
128
+ var client = CreateClient ( ) ;
123
129
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
124
130
var response = await client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , TestContext . Current . CancellationToken ) ;
125
131
var content = await response . Content . ReadAsStringAsync ( TestContext . Current . CancellationToken ) ;
@@ -136,7 +142,7 @@ public async Task Get_Content_TwiceSerial_SameConnection()
136
142
[ Fact ]
137
143
public async Task Get_Content_TwoParallelRequests_SameConnection ( )
138
144
{
139
- var client = new HttpClient ( ) ;
145
+ var client = CreateClient ( ) ;
140
146
var request0 = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
141
147
var request1 = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
142
148
@@ -160,7 +166,7 @@ public async Task Get_Content_ParallelRequests_NewConnection()
160
166
{
161
167
for ( int i = 0 ; i < 10 ; i ++ )
162
168
{
163
- var client = new HttpClient ( ) ;
169
+ var client = CreateClient ( ) ;
164
170
var request0 = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
165
171
var request1 = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/content" ) { Version = HttpVersion . Version20 } ;
166
172
@@ -183,7 +189,7 @@ public async Task Get_Content_ParallelRequests_NewConnection()
183
189
[ Fact ]
184
190
public async Task Header_And_Trailers ( )
185
191
{
186
- var client = new HttpClient ( ) ;
192
+ var client = CreateClient ( ) ;
187
193
var request = new HttpRequestMessage ( HttpMethod . Get , "https://localhost:7222/headerstrailers" ) { Version = HttpVersion . Version20 } ;
188
194
request . Headers . Add ( "x-custom" , "custom-header-value" ) ;
189
195
request . Headers . Accept . Add ( new ( "application/json" ) ) ;
@@ -204,7 +210,7 @@ public Task RunAsync()
204
210
if ( _app != null )
205
211
return Task . CompletedTask ;
206
212
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" ) ; } ) ;
208
214
209
215
// Use Kestrel:
210
216
//builder.WebHost.UseKestrel(o =>
0 commit comments