1- using System . Security . Cryptography . X509Certificates ;
1+ using System . Security . Cryptography . X509Certificates ;
22using Cysharp . Net . Http ;
3- using Microsoft . AspNetCore . Builder ;
4- using Microsoft . AspNetCore . Hosting ;
5- using Microsoft . AspNetCore . Server . Kestrel . Https ;
3+ using HttpClientTestServer ;
64
75namespace _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+ }
0 commit comments