From 0f63dbf6c7a21e98ddd1758e4631cd967c421749 Mon Sep 17 00:00:00 2001 From: Mayuki Sawatari Date: Tue, 24 Sep 2024 12:31:39 +0900 Subject: [PATCH] Add unit test for #96 --- test/YetAnotherHttpHandler.Test/Http1Test.cs | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/YetAnotherHttpHandler.Test/Http1Test.cs b/test/YetAnotherHttpHandler.Test/Http1Test.cs index 6b5c885..49940ca 100644 --- a/test/YetAnotherHttpHandler.Test/Http1Test.cs +++ b/test/YetAnotherHttpHandler.Test/Http1Test.cs @@ -1,5 +1,7 @@ +using Microsoft.AspNetCore.Hosting; using System.IO.Pipelines; using System.Net; +using System.Security.Cryptography.X509Certificates; using Xunit.Abstractions; namespace _YetAnotherHttpHandler.Test; @@ -153,4 +155,37 @@ public async Task Post_Timeout() // NOTE: .NET's HttpClient will unwrap OperationCanceledException if an HttpRequestException containing OperationCanceledException is thrown. Assert.IsAssignableFrom(ex); } + + [Fact] + public async Task Secure_Get_Ok() + { + // Arrange + using var httpHandler = new Cysharp.Net.Http.YetAnotherHttpHandler() + { + // We need to verify server certificate. + SkipCertificateVerification = false, + RootCertificates = File.ReadAllText("./Certificates/localhost.crt") + }; + var httpClient = new HttpClient(httpHandler); + await using var server = await LaunchServerAsync(TestWebAppServerListenMode.SecureHttp1Only, builder => + { + // Use self-signed certificate for testing purpose. + builder.WebHost.ConfigureKestrel(options => + { + options.ConfigureHttpsDefaults(options => + { + options.ServerCertificate = new X509Certificate2("Certificates/localhost.pfx"); + }); + }); + }); + + // Act + var response = await httpClient.GetAsync($"{server.BaseUri}/"); + var responseBody = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpVersion.Version11, response.Version); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal("__OK__", responseBody); + } } \ No newline at end of file