feat(AuthClientTests): Testserver hinzugefügt
This commit is contained in:
parent
5aab46a221
commit
8e979fa14d
@ -1,29 +1,72 @@
|
|||||||
using DigitalData.Auth.Abstractions;
|
using DigitalData.Auth.Abstractions;
|
||||||
|
using DigitalData.Auth.API.Hubs;
|
||||||
using DigitalData.Auth.Client;
|
using DigitalData.Auth.Client;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace DigitalData.Auth.Tests.Client;
|
namespace DigitalData.Auth.Tests.Client;
|
||||||
|
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class AuthClientTests
|
public class AuthClientTests
|
||||||
{
|
{
|
||||||
private static readonly string HubUrl = "https://localhost:7192/auth-hub";
|
private string _hubUrl;
|
||||||
|
|
||||||
private Func<Action<ClientParams>, ServiceProvider> Build;
|
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]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
Build = options => new ServiceCollection()
|
Build = options => new ServiceCollection()
|
||||||
.AddAuthHubClient(options)
|
.AddAuthHubClient(options)
|
||||||
.BuildServiceProvider();
|
.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]
|
[Test]
|
||||||
public async Task StartAsync_ShouldConnectSuccessfully()
|
public async Task StartAsync_ShouldConnectSuccessfully()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
using var provider = Build(opt => opt.Url = HubUrl);
|
using var provider = Build(opt => opt.Url = _hubUrl);
|
||||||
var client = provider.GetRequiredService<IAuthClient>();
|
var client = provider.GetRequiredService<IAuthClient>();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@ -46,14 +89,14 @@ public class AuthClientTests
|
|||||||
string rcv_msg = string.Empty;
|
string rcv_msg = string.Empty;
|
||||||
|
|
||||||
// Sender client
|
// 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>();
|
var sender_client = provider_sender.GetRequiredService<IAuthClient>();
|
||||||
await sender_client.TryStartAsync();
|
await sender_client.TryStartAsync();
|
||||||
|
|
||||||
// Receiver client
|
// Receiver client
|
||||||
using var provider_receiver = Build(opt =>
|
using var provider_receiver = Build(opt =>
|
||||||
{
|
{
|
||||||
opt.Url = HubUrl;
|
opt.Url = _hubUrl;
|
||||||
opt.Events.OnMessageReceived = (user, message, logger) =>
|
opt.Events.OnMessageReceived = (user, message, logger) =>
|
||||||
{
|
{
|
||||||
rcv_user = user;
|
rcv_user = user;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user