|
10 | 10 | using Microsoft.AspNetCore.Hosting;
|
11 | 11 | using Microsoft.Extensions.Hosting;
|
12 | 12 | using Microsoft.AspNetCore.Builder;
|
13 |
| - |
| 13 | +using Moq; |
| 14 | +using System.Net.Http; |
| 15 | +using Microsoft.Extensions.DependencyInjection; |
14 | 16 | public class RedactTests
|
15 | 17 | {
|
16 | 18 | [Test]
|
@@ -208,3 +210,74 @@ public async Task MiddlewareTest_ReturnsNotFoundForRequest()
|
208 | 210 | }
|
209 | 211 | }
|
210 | 212 |
|
| 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 | + |
0 commit comments