Skip to content

Commit

Permalink
feat: Add IApiToolkitClientFactory interface and implementation
Browse files Browse the repository at this point in the history
- Added IApiToolkitClientFactory interface to provide an abstraction for creating HttpClient instances with specific ATOptions.
- Implemented ApiToolkitClientFactory class, which leverages HttpClientFactory for centralized management of HttpClient instances.
-Added tests For The IApiToolKitCLientFactory
  • Loading branch information
shepherrrd committed Aug 10, 2024
1 parent 837544b commit 99b6c27
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
75 changes: 74 additions & 1 deletion ApiToolkitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;

using Moq;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
public class RedactTests
{
[Test]
Expand Down Expand Up @@ -208,3 +210,74 @@ public async Task MiddlewareTest_ReturnsNotFoundForRequest()
}
}


public class ApiToolkitClientFactoryTests
{
private Mock<IHttpClientFactory> _httpClientFactoryMock;
private Mock<IServiceProvider> _serviceProviderMock;
private Mock<ObservingHandler> _observingHandlerMock;
private IApiToolkitClientFactory _apiToolkitClientFactory;

[SetUp]
public void SetUp()
{
_httpClientFactoryMock = new Mock<IHttpClientFactory>();
_serviceProviderMock = new Mock<IServiceProvider>();
_observingHandlerMock = new Mock<ObservingHandler>();

// Set up the service provider to return the observing handler mock
_serviceProviderMock
.Setup(sp => sp.GetService(typeof(ObservingHandler)))
.Returns(_observingHandlerMock.Object);

// Initialize the ApiToolkitClientFactory with mocks
_apiToolkitClientFactory = new ApiToolkitClientFactory(_httpClientFactoryMock.Object, _serviceProviderMock.Object);
}

[Test]
public void CreateClient_ShouldReturnHttpClient_WithConfiguredHandler()
{
// Arrange
var options = new ATOptions
{
PathWildCard = "/posts/{id}",
RedactHeaders = new List<string> { "User-Agent" },
RedactRequestBody = new List<string> { "$.user.password" },
RedactResponseBody = new List<string> { "$.user.data.email" }
};

// Act
var client = _apiToolkitClientFactory.CreateClient(options);

// Assert
Assert.NotNull(client);
_observingHandlerMock.Verify(h => h.SetOptions(options), Times.Once);
}

[Test]
public void CreateClient_ShouldUseObservingHandler_FromServiceProvider()
{
// Act
var client = _apiToolkitClientFactory.CreateClient(new ATOptions());

// Assert
_serviceProviderMock.Verify(sp => sp.GetService(typeof(ObservingHandler)), Times.Once);
}

[Test]
public void CreateClient_ShouldThrowException_WhenHandlerNotRegistered()
{
// Arrange
var invalidServiceProvider = new Mock<IServiceProvider>();
invalidServiceProvider.Setup(sp => sp.GetService(typeof(ObservingHandler)))
.Returns(null); // Simulate missing handler registration

var factory = new ApiToolkitClientFactory(_httpClientFactoryMock.Object, invalidServiceProvider.Object);

// Act & Assert
Assert.Throws<NullReferenceException>(() => factory.CreateClient(new ATOptions()));
}
}



1 change: 1 addition & 0 deletions apitoolkit-dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.5" />
<!-- Deprecated -->
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
Expand Down

0 comments on commit 99b6c27

Please sign in to comment.