Path: redb-route/tests/redb.Route.Tests.Hosting/SharedHostTlsTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Hosting/SharedHostTlsTests.cs ---- using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using AwesomeAssertions; using Microsoft.AspNetCore.Http; using redb.Route.Http; using Xunit; namespace redb.Route.Tests.Hosting; /// /// The invariant every comparable stack enforces (nginx, httpd, Jetty, Spring Boot, and Kestrel's /// own UseHttps()): asking for TLS selects the socket, and a certificate that cannot be /// resolved fails the bind. Nobody flips the socket back to plaintext — this host used to, and /// then reported https:// for it. /// /// Where the certificate comes from is a separate question: the endpoint, a named connection /// factory, or the host-wide default configured here (the shape Camel's SSLContextParameters and /// Spring Boot's SSL bundles have). Only "nowhere" is an error. /// /// public sealed class SharedHostTlsTests : IAsyncLifetime { private readonly List _managers = []; private readonly List _tempFiles = []; public Task InitializeAsync() => Task.CompletedTask; public async Task DisposeAsync() { foreach (var m in _managers) await m.DisposeAsync(); foreach (var f in _tempFiles) if (File.Exists(f)) File.Delete(f); } private SharedHttpServerManager Manager(HttpHostingOptions? options = null) { var manager = new SharedHttpServerManager(options); _managers.Add(manager); return manager; } private static int GetFreePort() => global::redb.Route.Tests.Shared.TestPorts.Next(); private static HttpClient TrustingClient() => new(new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator, }); [Fact] public async Task SslWithoutAnyCertificate_RefusesToBind() { var port = GetFreePort(); var manager = Manager(); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true); var act = () => manager.EnsureStarted("127.0.0.1", port); (await act.Should().ThrowAsync( "a plain socket under an https:// sign is not \"TLS off\", it is a silent downgrade")) .WithMessage("*certificate*"); } [Fact] public async Task SslWithoutAnyCertificate_DoesNotLeaveAPlaintextPortOpen() { var port = GetFreePort(); var manager = Manager(); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true); var start = () => manager.EnsureStarted("127.0.0.1", port); await start.Should().ThrowAsync(); using var client = new HttpClient(); var plain = () => client.GetStringAsync($"http://127.0.0.1:{port}/x"); await plain.Should().ThrowAsync("the port must not have opened at all"); } [Fact] public async Task EndpointCertificate_TerminatesTls() { var port = GetFreePort(); var manager = Manager(); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true, sslCertPath: WritePfx()); await manager.EnsureStarted("127.0.0.1", port); using var client = TrustingClient(); (await client.GetStringAsync($"https://127.0.0.1:{port}/x")).Should().Be("x"); } [Fact] public async Task HostDefaultCertificate_IsUsedWhenTheEndpointHasNone() { var port = GetFreePort(); var options = new HttpHostingOptions(); options.Tls.DefaultCertificatePath = WritePfx(); var manager = Manager(options); // No certificate on the route: it resolves from the host, the way a Camel endpoint resolves // from global SSLContextParameters. manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true); await manager.EnsureStarted("127.0.0.1", port); using var client = TrustingClient(); (await client.GetStringAsync($"https://127.0.0.1:{port}/x")).Should().Be("x"); } [Fact] public async Task HostDefaultCertificate_CanBeAnInstance() { var port = GetFreePort(); var options = new HttpHostingOptions(); options.Tls.DefaultCertificate = LoadPfx(WritePfx()); var manager = Manager(options); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true); await manager.EnsureStarted("127.0.0.1", port); using var client = TrustingClient(); (await client.GetStringAsync($"https://127.0.0.1:{port}/x")).Should().Be("x"); } [Fact] public async Task EndpointCertificate_WinsOverTheHostDefault() { var port = GetFreePort(); var options = new HttpHostingOptions(); options.Tls.DefaultCertificatePath = WritePfx("host-default"); var manager = Manager(options); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x"), ssl: true, sslCertPath: WritePfx("endpoint")); await manager.EnsureStarted("127.0.0.1", port); string? subject = null; using var handler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (_, cert, _, _) => { subject = cert?.Subject; return true; }, }; using var client = new HttpClient(handler); await client.GetStringAsync($"https://127.0.0.1:{port}/x"); subject.Should().Contain("endpoint", "the route's explicit certificate overrides the host default"); } [Fact] public async Task PlainListener_IsUnaffectedByAHostDefault() { var options = new HttpHostingOptions(); options.Tls.DefaultCertificatePath = WritePfx(); var port = GetFreePort(); var manager = Manager(options); manager.RegisterRoute("127.0.0.1", port, "/x", "GET", ctx => ctx.Response.WriteAsync("x")); // ssl was and stays an explicit decision: the default certificate turns nothing on by itself. var act = () => manager.EnsureStarted("127.0.0.1", port); // The assertion used to be fire-and-forget (no await): it raced the actual bind. await act.Should().NotThrowAsync(); manager.GetBaseUrl("127.0.0.1", port).Should().StartWith("http://"); } // ── Helpers ── private string WritePfx(string cn = "localhost") { using var rsa = RSA.Create(2048); var request = new CertificateRequest($"CN={cn}", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false)); var san = new SubjectAlternativeNameBuilder(); san.AddDnsName("localhost"); san.AddIpAddress(IPAddress.Loopback); request.CertificateExtensions.Add(san.Build()); using var cert = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1)); var path = Path.Combine(Path.GetTempPath(), $"redb-host-{Guid.NewGuid():N}.pfx"); File.WriteAllBytes(path, cert.Export(X509ContentType.Pfx)); _tempFiles.Add(path); return path; } private static X509Certificate2 LoadPfx(string path) => X509CertificateLoader.LoadPkcs12FromFile(path, null); }