94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using System.Security.Claims;
|
|
using AyCode.Core.Extensions;
|
|
using AyCode.Core.Helpers;
|
|
using AyCode.Core.Tests.TestModels;
|
|
using AyCode.Models.Server.DynamicMethods;
|
|
using AyCode.Services.Server.SignalRs;
|
|
using AyCode.Services.SignalRs;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace AyCode.Services.Server.Tests.SignalRs;
|
|
|
|
/// <summary>
|
|
/// Testable SignalR hub that overrides infrastructure dependencies.
|
|
/// Enables unit testing without SignalR server or mocks.
|
|
/// </summary>
|
|
public class TestableSignalRHub2 : AcWebSignalRHubBase<TestSignalRTags, TestLogger>
|
|
{
|
|
private IAcSignalRHubItemServer _callerClient;
|
|
|
|
#region Test Configuration
|
|
|
|
/// <summary>
|
|
/// Simulated connection ID
|
|
/// </summary>
|
|
public string TestConnectionId { get; set; } = "test-connection-id";
|
|
|
|
/// <summary>
|
|
/// Simulated user identifier
|
|
/// </summary>
|
|
public string? TestUserIdentifier { get; set; } = "test-user-id";
|
|
|
|
/// <summary>
|
|
/// Simulated connection aborted state
|
|
/// </summary>
|
|
public bool TestIsConnectionAborted { get; set; } = false;
|
|
|
|
/// <summary>
|
|
/// Simulated ClaimsPrincipal (optional)
|
|
/// </summary>
|
|
public ClaimsPrincipal? TestUser { get; set; }
|
|
|
|
#endregion
|
|
|
|
public TestableSignalRHub2()
|
|
: base(new ConfigurationBuilder().Build(), new TestLogger())
|
|
{
|
|
}
|
|
|
|
public TestableSignalRHub2(IConfiguration configuration, TestLogger logger)
|
|
: base(configuration, logger)
|
|
{
|
|
}
|
|
|
|
#region Public Test Entry Points
|
|
|
|
/// <summary>
|
|
/// Register a service with SignalR-attributed methods
|
|
/// </summary>
|
|
public void RegisterService(object service, IAcSignalRHubItemServer callerClient)
|
|
{
|
|
_callerClient = callerClient;
|
|
DynamicMethodCallModels.Add(new AcDynamicMethodCallModel<SignalRAttribute>(service));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the serializer type for testing Binary vs JSON serialization.
|
|
/// </summary>
|
|
public void SetSerializerType(AcSerializerOptions acSerializerOptions)
|
|
{
|
|
SerializerOptions = acSerializerOptions;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Overridden Context Accessors
|
|
|
|
protected override string GetConnectionId() => TestConnectionId;
|
|
|
|
protected override bool IsConnectionAborted() => TestIsConnectionAborted;
|
|
|
|
protected override string? GetUserIdentifier() => TestUserIdentifier;
|
|
|
|
protected override ClaimsPrincipal? GetUser() => TestUser;
|
|
|
|
#endregion
|
|
|
|
#region Overridden Response Methods (capture messages for testing)
|
|
|
|
protected override Task ResponseToCaller(int messageTag, ISignalRMessage message, int? requestId)
|
|
=> SendMessageToClient(_callerClient, messageTag, message, requestId);
|
|
|
|
#endregion
|
|
}
|