Skip to content

Commit 99b6c27

Browse files
committed
feat: Add IApiToolkitClientFactory interface and implementation
- 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
1 parent 837544b commit 99b6c27

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

ApiToolkitTests.cs

+74-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using Microsoft.AspNetCore.Hosting;
1111
using Microsoft.Extensions.Hosting;
1212
using Microsoft.AspNetCore.Builder;
13-
13+
using Moq;
14+
using System.Net.Http;
15+
using Microsoft.Extensions.DependencyInjection;
1416
public class RedactTests
1517
{
1618
[Test]
@@ -208,3 +210,74 @@ public async Task MiddlewareTest_ReturnsNotFoundForRequest()
208210
}
209211
}
210212

213+
214+
public class ApiToolkitClientFactoryTests
215+
{
216+
private Mock<IHttpClientFactory> _httpClientFactoryMock;
217+
private Mock<IServiceProvider> _serviceProviderMock;
218+
private Mock<ObservingHandler> _observingHandlerMock;
219+
private IApiToolkitClientFactory _apiToolkitClientFactory;
220+
221+
[SetUp]
222+
public void SetUp()
223+
{
224+
_httpClientFactoryMock = new Mock<IHttpClientFactory>();
225+
_serviceProviderMock = new Mock<IServiceProvider>();
226+
_observingHandlerMock = new Mock<ObservingHandler>();
227+
228+
// Set up the service provider to return the observing handler mock
229+
_serviceProviderMock
230+
.Setup(sp => sp.GetService(typeof(ObservingHandler)))
231+
.Returns(_observingHandlerMock.Object);
232+
233+
// Initialize the ApiToolkitClientFactory with mocks
234+
_apiToolkitClientFactory = new ApiToolkitClientFactory(_httpClientFactoryMock.Object, _serviceProviderMock.Object);
235+
}
236+
237+
[Test]
238+
public void CreateClient_ShouldReturnHttpClient_WithConfiguredHandler()
239+
{
240+
// Arrange
241+
var options = new ATOptions
242+
{
243+
PathWildCard = "/posts/{id}",
244+
RedactHeaders = new List<string> { "User-Agent" },
245+
RedactRequestBody = new List<string> { "$.user.password" },
246+
RedactResponseBody = new List<string> { "$.user.data.email" }
247+
};
248+
249+
// Act
250+
var client = _apiToolkitClientFactory.CreateClient(options);
251+
252+
// Assert
253+
Assert.NotNull(client);
254+
_observingHandlerMock.Verify(h => h.SetOptions(options), Times.Once);
255+
}
256+
257+
[Test]
258+
public void CreateClient_ShouldUseObservingHandler_FromServiceProvider()
259+
{
260+
// Act
261+
var client = _apiToolkitClientFactory.CreateClient(new ATOptions());
262+
263+
// Assert
264+
_serviceProviderMock.Verify(sp => sp.GetService(typeof(ObservingHandler)), Times.Once);
265+
}
266+
267+
[Test]
268+
public void CreateClient_ShouldThrowException_WhenHandlerNotRegistered()
269+
{
270+
// Arrange
271+
var invalidServiceProvider = new Mock<IServiceProvider>();
272+
invalidServiceProvider.Setup(sp => sp.GetService(typeof(ObservingHandler)))
273+
.Returns(null); // Simulate missing handler registration
274+
275+
var factory = new ApiToolkitClientFactory(_httpClientFactoryMock.Object, invalidServiceProvider.Object);
276+
277+
// Act & Assert
278+
Assert.Throws<NullReferenceException>(() => factory.CreateClient(new ATOptions()));
279+
}
280+
}
281+
282+
283+

apitoolkit-dotnet.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.5" />
2929
<!-- Deprecated -->
3030
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
31+
<PackageReference Include="Moq" Version="4.20.70" />
3132
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3233
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
3334
<PackageReference Include="NUnit" Version="3.13.3" />

0 commit comments

Comments
 (0)