From 7298140648f414c5e3831c0764b567ef586dd585 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 21 May 2026 09:28:57 +0200 Subject: [PATCH] Add tests for ReCClient dependency injection setup Added a new `DependencyInjectionTests` class to validate the dependency injection setup for the `ReCClient` class. - Added tests to ensure `ReCClient` can be resolved when registered with a base URL or custom HTTP client configuration. - Verified default options are registered when no callback is supplied and that options callbacks are applied correctly. - Added tests to validate behavior when `LogSuccessfulRequests` is enabled, including scenarios with and without a registered logger. - Included necessary `using` directives for DI, logging, options, HTTP client, and the `ReC.Client` namespace. --- .../Client/DependencyInjectionTests.cs | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/ReC.Tests/Client/DependencyInjectionTests.cs diff --git a/tests/ReC.Tests/Client/DependencyInjectionTests.cs b/tests/ReC.Tests/Client/DependencyInjectionTests.cs new file mode 100644 index 0000000..52c52b3 --- /dev/null +++ b/tests/ReC.Tests/Client/DependencyInjectionTests.cs @@ -0,0 +1,115 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using ReC.Client; +using System.Net.Http; + +namespace ReC.Tests.Client; + +[TestFixture] +public class DependencyInjectionTests +{ + [Test] + public void AddRecClient_with_base_url_registers_resolvable_client() + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddRecClient("https://example.invalid/"); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + + var client = scope.ServiceProvider.GetRequiredService(); + + Assert.That(client, Is.Not.Null); + Assert.That(client.RecActions, Is.Not.Null); + Assert.That(client.Results, Is.Not.Null); + Assert.That(client.Profiles, Is.Not.Null); + Assert.That(client.EndpointAuth, Is.Not.Null); + Assert.That(client.EndpointParams, Is.Not.Null); + Assert.That(client.Endpoints, Is.Not.Null); + Assert.That(client.Common, Is.Not.Null); + } + + [Test] + public void AddRecClient_with_configure_client_registers_resolvable_client() + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddRecClient(http => + { + http.BaseAddress = new Uri("https://example.invalid/"); + http.Timeout = TimeSpan.FromSeconds(5); + }); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + + var client = scope.ServiceProvider.GetRequiredService(); + + Assert.That(client, Is.Not.Null); + } + + [Test] + public void AddRecClient_registers_default_options_when_no_callback_supplied() + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddRecClient("https://example.invalid/"); + + using var provider = services.BuildServiceProvider(); + + var options = provider.GetRequiredService>().Value; + + Assert.That(options, Is.Not.Null); + Assert.That(options.LogSuccessfulRequests, Is.True, "Default value of LogSuccessfulRequests should be true."); + } + + [Test] + public void AddRecClient_applies_options_callback() + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true); + + using var provider = services.BuildServiceProvider(); + + var options = provider.GetRequiredService>().Value; + + Assert.That(options.LogSuccessfulRequests, Is.True); + } + + [Test] + public void ReCClient_constructor_throws_when_LogSuccessfulRequests_enabled_but_logger_is_null() + { + // Microsoft.Extensions.Http always registers an ILoggerFactory, so the + // "logger == null" branch can only be exercised by invoking the constructor directly. + var services = new ServiceCollection(); + services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true); + + using var provider = services.BuildServiceProvider(); + var httpClientFactory = provider.GetRequiredService(); + var options = provider.GetRequiredService>(); + + var ex = Assert.Throws( + () => new ReCClient(httpClientFactory, options, logger: null)); + + Assert.That(ex!.Message, Does.Contain(nameof(ReCClientOptions.LogSuccessfulRequests))); + Assert.That(ex.Message, Does.Contain("ILogger")); + } + + [Test] + public void ReCClient_resolves_when_LogSuccessfulRequests_enabled_and_logger_registered() + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddRecClient("https://example.invalid/", opt => opt.LogSuccessfulRequests = true); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + + var client = scope.ServiceProvider.GetRequiredService(); + + Assert.That(client, Is.Not.Null); + } +}