Path: redb-route/tests/redb.Route.Tests.SignalR/SignalRSharedHostTests.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.SignalR/SignalRSharedHostTests.cs
----
using System.Net;
using System.Net.Sockets;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using redb.Route.SignalR;
namespace redb.Route.Tests.SignalR;
///
/// End to end: the hub serves on the SHARED Kestrel (so /hub/ and the
/// REST API can sit on one port behind one proxy), a host-supplied delegate authenticates the
/// handshake and fills UserIdentifier, and MessagePack actually negotiates.
///
public sealed class SignalRSharedHostTests : IAsyncLifetime
{
private readonly SharedHttpServerManager _manager = new();
private readonly HttpClient _http = new();
private readonly List _connections = [];
private SignalRConsumer? _consumer;
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
foreach (var c in _connections) await c.DisposeAsync();
if (_consumer is not null) await _consumer.Stop();
_http.Dispose();
await _manager.DisposeAsync();
}
private static int GetFreePort() => global::redb.Route.Tests.Shared.TestPorts.Next();
private SignalREndpoint CreateEndpoint(SignalRComponent component, int port, string hubPath, string? extraParams = null)
{
var uri = EndpointUriParser.Parse(
$"signalr://127.0.0.1:{port}{hubPath}" + (extraParams is null ? "" : $"?{extraParams}"));
return (SignalREndpoint)component.CreateEndpoint(uri);
}
private async Task ConnectAsync(int port, string hubPath,
Action? configure = null, string? accessToken = null)
{
var builder = new HubConnectionBuilder().WithUrl($"http://127.0.0.1:{port}{hubPath}", o =>
{
if (accessToken is not null)
o.AccessTokenProvider = () => Task.FromResult(accessToken);
});
configure?.Invoke(builder);
var connection = builder.Build();
_connections.Add(connection);
await connection.StartAsync();
return connection;
}
// ── One port for the hub and the REST API ──
[Fact]
public async Task HubAndHttp_ShareOnePort()
{
var port = GetFreePort();
_manager.RegisterRoute("127.0.0.1", port, "/api/health", "GET",
ctx => ctx.Response.WriteAsync("healthy"));
var component = new SignalRComponent { ServerManager = _manager };
var endpoint = CreateEndpoint(component, port, "/chatHub", "inOut=true");
await endpoint.Start();
_consumer = (SignalRConsumer)endpoint.CreateConsumer(new EchoProcessor());
await _consumer.Start();
_manager.ServerCount.Should().Be(1, "the hub and HTTP must share ONE Kestrel");
(await _http.GetStringAsync($"http://127.0.0.1:{port}/api/health")).Should().Be("healthy");
var connection = await ConnectAsync(port, "/chatHub");
var reply = await connection.InvokeAsync