feat(AuthClientTests): Testserver hinzugefügt

This commit is contained in:
Developer 02 2025-02-04 10:11:34 +01:00
parent 5aab46a221
commit 8e979fa14d

View File

@ -1,29 +1,72 @@
using DigitalData.Auth.Abstractions;
using DigitalData.Auth.API.Hubs;
using DigitalData.Auth.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace DigitalData.Auth.Tests.Client;
[TestFixture]
public class AuthClientTests
{
private static readonly string HubUrl = "https://localhost:7192/auth-hub";
private string _hubUrl;
private Func<Action<ClientParams>, ServiceProvider> Build;
private WebApplication? _app = null;
private static int AvailablePort
{
get
{
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
listener.Start();
int port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
[SetUp]
public void Setup()
{
Build = options => new ServiceCollection()
.AddAuthHubClient(options)
.BuildServiceProvider();
// Create and run test server
// Create builder and add SignalR service
var builder = WebApplication.CreateBuilder();
builder.Services.AddSignalR();
// Listen AvailablePort and map hub
var _app = builder.Build();
var url = $"http://localhost:{AvailablePort}";
var hubRoute = "/auth-hub";
_hubUrl = url + hubRoute;
_app.Urls.Add(url);
_app.MapHub<AuthHub>(hubRoute);
_app.Start();
}
[TearDown]
public async Task TearDown()
{
// Stop test server
if (_app is not null)
{
await _app.StopAsync();
await _app.DisposeAsync();
Console.WriteLine("Test server stopped.");
}
}
[Test]
public async Task StartAsync_ShouldConnectSuccessfully()
{
// Arrange
using var provider = Build(opt => opt.Url = HubUrl);
using var provider = Build(opt => opt.Url = _hubUrl);
var client = provider.GetRequiredService<IAuthClient>();
// Act
@ -46,14 +89,14 @@ public class AuthClientTests
string rcv_msg = string.Empty;
// Sender client
using var provider_sender = Build(opt => opt.Url = HubUrl);
using var provider_sender = Build(opt => opt.Url = _hubUrl);
var sender_client = provider_sender.GetRequiredService<IAuthClient>();
await sender_client.TryStartAsync();
// Receiver client
using var provider_receiver = Build(opt =>
{
opt.Url = HubUrl;
opt.Url = _hubUrl;
opt.Events.OnMessageReceived = (user, message, logger) =>
{
rcv_user = user;