Compare commits
11 Commits
5aab46a221
...
5f9926e911
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f9926e911 | ||
|
|
319763040c | ||
|
|
e474cf38d4 | ||
|
|
5092890f14 | ||
|
|
27c2c0b4cb | ||
|
|
9d609dd5ac | ||
|
|
360d91353b | ||
|
|
7c5a545926 | ||
|
|
18d7c475ff | ||
|
|
5886e076f4 | ||
|
|
8e979fa14d |
@@ -1,6 +1,6 @@
|
||||
namespace DigitalData.Auth.Abstractions;
|
||||
|
||||
public interface IAuthClient : IAuthClientHandler
|
||||
public interface IAuthClient : IAuthListenHandler, IAuthSenderHandler
|
||||
{
|
||||
bool IsConnected { get; }
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace DigitalData.Auth.Abstractions;
|
||||
|
||||
public interface IAuthClientHandler
|
||||
{
|
||||
Task ReceiveMessageAsync(string user, string message);
|
||||
}
|
||||
6
DigitalData.Auth.Abstractions/IAuthListenHandler.cs
Normal file
6
DigitalData.Auth.Abstractions/IAuthListenHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace DigitalData.Auth.Abstractions;
|
||||
|
||||
public interface IAuthListenHandler
|
||||
{
|
||||
Task ReceiveKeyAsync(string topic, string key);
|
||||
}
|
||||
6
DigitalData.Auth.Abstractions/IAuthSenderHandler.cs
Normal file
6
DigitalData.Auth.Abstractions/IAuthSenderHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace DigitalData.Auth.Abstractions;
|
||||
|
||||
public interface IAuthSenderHandler
|
||||
{
|
||||
Task SendKeyAsync(string topic, string key);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.Client;
|
||||
|
||||
public class AuthClient : IAuthClient
|
||||
public class AuthClient : IAuthClient, IAsyncDisposable
|
||||
{
|
||||
private readonly HubConnection _connection;
|
||||
|
||||
@@ -21,7 +21,7 @@ public class AuthClient : IAuthClient
|
||||
.WithUrl(paramsOptions.Value.Url)
|
||||
.Build();
|
||||
|
||||
_connection.On<string, string>(nameof(ReceiveMessageAsync), ReceiveMessageAsync);
|
||||
_connection.On<string, string>(nameof(ReceiveKeyAsync), ReceiveKeyAsync);
|
||||
|
||||
_logger = logger;
|
||||
|
||||
@@ -61,5 +61,13 @@ public class AuthClient : IAuthClient
|
||||
}
|
||||
}
|
||||
|
||||
public Task ReceiveMessageAsync(string user, string message) => Task.Run(() => _params.Events.OnMessageReceived(user, message, _logger));
|
||||
public Task ReceiveKeyAsync(string user, string message) => Task.Run(() => _params.Events.OnMessageReceived(user, message, _logger));
|
||||
|
||||
public Task SendKeyAsync(string user, string message) => _connection.InvokeAsync(nameof(SendKeyAsync), user, message);
|
||||
|
||||
public virtual async ValueTask DisposeAsync()
|
||||
{
|
||||
await _connection.StopAsync();
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,77 @@
|
||||
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;
|
||||
|
||||
// TODO: The test checks if the services are working. Performance measurement is ignored. Update it to measure performance as well.
|
||||
[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
|
||||
await client.TryStartAsync();
|
||||
await client.StartAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
@@ -42,37 +86,41 @@ public class AuthClientTests
|
||||
public async Task ReceiveMessage_ShouldCallOnMessageReceived()
|
||||
{
|
||||
// Arrange
|
||||
string rcv_user = string.Empty;
|
||||
string rcv_msg = string.Empty;
|
||||
string rcv_topic = string.Empty;
|
||||
string rcv_key = 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();
|
||||
await sender_client.StartAsync();
|
||||
|
||||
// Receiver client
|
||||
using var provider_receiver = Build(opt =>
|
||||
{
|
||||
opt.Url = HubUrl;
|
||||
opt.Events.OnMessageReceived = (user, message, logger) =>
|
||||
opt.Url = _hubUrl;
|
||||
opt.Events.OnMessageReceived = (topic, key, logger) =>
|
||||
{
|
||||
rcv_user = user;
|
||||
rcv_msg = message;
|
||||
rcv_topic = topic;
|
||||
rcv_key = key;
|
||||
};
|
||||
});
|
||||
var client_receiver = provider_receiver.GetRequiredService<IAuthClient>();
|
||||
await client_receiver.StartAsync();
|
||||
|
||||
string user = "user";
|
||||
string message = "message";
|
||||
string topic = "topic";
|
||||
string key = "key";
|
||||
|
||||
// Act
|
||||
await client_receiver.ReceiveMessageAsync(user, message);
|
||||
await sender_client.SendKeyAsync(topic, key);
|
||||
|
||||
// delay fort getting answer
|
||||
await Task.Delay(2000);
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(rcv_user, Is.EqualTo(user));
|
||||
Assert.That(rcv_msg, Is.EqualTo(message));
|
||||
Assert.That(rcv_topic, Is.EqualTo(topic));
|
||||
Assert.That(rcv_key, Is.EqualTo(key));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,14 @@ using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace DigitalData.Auth.API.Hubs;
|
||||
|
||||
public class AuthHub : Hub<IAuthClientHandler>
|
||||
public class AuthHub : Hub<IAuthListenHandler>, IAuthSenderHandler
|
||||
{
|
||||
public async Task SendMessage(string user, string message)
|
||||
=> await Clients.All.ReceiveMessageAsync(user, message);
|
||||
public async Task SendKeyAsync(string user, string message)
|
||||
=> await Clients.All.ReceiveKeyAsync(user, message);
|
||||
|
||||
public async Task SendMessageToCaller(string user, string message)
|
||||
=> await Clients.Caller.ReceiveMessageAsync(user, message);
|
||||
=> await Clients.Caller.ReceiveKeyAsync(user, message);
|
||||
|
||||
public async Task SendMessageToGroup(string user, string message)
|
||||
=> await Clients.Group("Auth.API Consumers").ReceiveMessageAsync(user, message);
|
||||
=> await Clients.Group("Auth.API Consumers").ReceiveKeyAsync(user, message);
|
||||
}
|
||||
Reference in New Issue
Block a user