28 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.SignalR.Client;
namespace BuildFlow.IM.Integration.Tests;
/// <summary>
/// SignalR 客户端构造助手。统一注入 TestServer 的 handler,使客户端连接走内存测试通道而非真实网络。
/// </summary>
public static class HubConnectionFactory
{
/// <summary>
/// 构造一个已携带 JWT(query)的 ChatHub 连接(尚未 Start)。
/// 调用方负责 <c>await conn.StartAsync()</c> 与用后 <c>DisposeAsync</c>。
/// 不启用自动重连:测试需要可预期的连接生命周期,断开即断开,便于验证 OnDisconnected 钩子。
/// </summary>
public static HubConnection Create(ImServerFixture fixture, Guid userId, string userName)
{
var token = fixture.IssueToken(userId, userName);
var handler = fixture.CreateMessageHandler();
return new HubConnectionBuilder()
.WithUrl($"{fixture.BaseAddress}hubs/chat?access_token={token}", options =>
{
// 关键:复用 TestServer handler,绕过真实 TCP,连接走内存管道
options.HttpMessageHandlerFactory = _ => handler;
})
.Build();
}
}