From d84ef820f13134dffda5994a1208dfee5086dc35 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 26 Jun 2024 16:57:30 +0200 Subject: [PATCH] =?UTF-8?q?GetCookies-Methode=20hinzugef=C3=BCgt.=20Test?= =?UTF-8?q?=20f=C3=BCr=20http-Dienst=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Client/IBaseHttpClientService.cs | 6 +- .../BaseHttpClientService.cs | 4 +- DigitalData.Core.Client/DIExtensions.cs | 4 +- .../Client/BaseHttpClientServiceTest.cs | 127 ++++++++++++++++++ .../DigitalData.Core.Tests.csproj | 4 + ...igitalData.Core.Tests.csproj.nuget.g.props | 2 +- 6 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs diff --git a/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs b/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs index 1d76d72..bfbde35 100644 --- a/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs +++ b/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs @@ -1,9 +1,13 @@ -namespace DigitalData.Core.Abstractions.Client +using System.Net; + +namespace DigitalData.Core.Abstractions.Client { public interface IBaseHttpClientService { public string Uri { get; init; } + public CookieCollection GetCookies(string route = ""); + Task FetchAsync( string route = "", HttpMethod? method = null, diff --git a/DigitalData.Core.Client/BaseHttpClientService.cs b/DigitalData.Core.Client/BaseHttpClientService.cs index 0664e7d..b48ab6c 100644 --- a/DigitalData.Core.Client/BaseHttpClientService.cs +++ b/DigitalData.Core.Client/BaseHttpClientService.cs @@ -8,7 +8,7 @@ namespace DigitalData.Core.Client public class BaseHttpClientService : IBaseHttpClientService { protected readonly HttpClient _client; - protected readonly CookieContainer _cookies; + protected readonly CookieContainer _cookies; [StringSyntax("Uri")] public string Uri { get; init; } @@ -20,6 +20,8 @@ namespace DigitalData.Core.Client Uri = clientOptions.Value.Uri; } + public CookieCollection GetCookies(string route = "") => _cookies.GetCookies(uri: new Uri(Uri + route)); + public async Task FetchAsync( string route = "", HttpMethod? method = null, diff --git a/DigitalData.Core.Client/DIExtensions.cs b/DigitalData.Core.Client/DIExtensions.cs index e9c654c..a328ddf 100644 --- a/DigitalData.Core.Client/DIExtensions.cs +++ b/DigitalData.Core.Client/DIExtensions.cs @@ -17,7 +17,7 @@ namespace DigitalData.Core.Client return services; } - public static IServiceCollection AddHttpClientService(this IServiceCollection services, Action? clientOptions = null, bool setAsDefault = false) + public static IServiceCollection AddHttpClientService(this IServiceCollection services, Action? clientOptions = null, bool setAsDefaultBase = false) where TClientOptions : HttpClientOptions, new() { services.TryAddSingleton(); @@ -25,7 +25,7 @@ namespace DigitalData.Core.Client services.AddSingleton, HttpClientService>(); services.Configure(clientOptions ?? (_ => { })); - if (setAsDefault) + if (setAsDefaultBase) services.AddSingleton>(); return services; diff --git a/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs b/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs new file mode 100644 index 0000000..aca2a5f --- /dev/null +++ b/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs @@ -0,0 +1,127 @@ +using DigitalData.Core.Client; +using Microsoft.Extensions.Options; +using Moq; +using Moq.Protected; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace DigitalData.Core.Tests +{ + [TestFixture] + public class BaseHttpClientServiceTests + { + private Mock _messageHandlerMock; + private HttpClient _httpClient; + private CookieContainer _cookieContainer; + private Mock> _optionsMock; + private BaseHttpClientService _service; + + [SetUp] + public void SetUp() + { + _messageHandlerMock = new Mock(MockBehavior.Strict); + _httpClient = new HttpClient(_messageHandlerMock.Object); + _cookieContainer = new CookieContainer(); + _optionsMock = new Mock>(); + _optionsMock.Setup(o => o.Value).Returns(new HttpClientOptions { Uri = "https://example.com" }); + + _service = new BaseHttpClientService(_httpClient, _cookieContainer, _optionsMock.Object); + } + + [Test] + public void GetCookies_ShouldReturnCookies() + { + // Arrange + var uri = new Uri("https://example.com/test"); + _cookieContainer.Add(uri, new Cookie("test", "value")); + + // Act + var cookies = _service.GetCookies("/test"); + + // Assert + Assert.AreEqual(1, cookies.Count); + Assert.AreEqual("test", cookies[0].Name); + Assert.AreEqual("value", cookies[0].Value); + } + + [Test] + public async Task FetchAsync_ShouldSendRequestWithMethodAndBody() + { + // Arrange + var responseMessage = new HttpResponseMessage(HttpStatusCode.OK); + _messageHandlerMock + .Protected() + .Setup>( + "SendAsync", + ItExpr.IsAny(), + ItExpr.IsAny() + ) + .ReturnsAsync(responseMessage); + + var bodyContent = new StringContent("test body"); + + // Act + var response = await _service.FetchAsync("/test", HttpMethod.Post, body: bodyContent); + + // Assert + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + _messageHandlerMock.Protected().Verify( + "SendAsync", + Times.Once(), + ItExpr.Is(req => + req.Method == HttpMethod.Post && + req.RequestUri == new Uri("https://example.com/test") && + req.Content == bodyContent), + ItExpr.IsAny() + ); + } + + [Test] + public async Task FetchAsync_ShouldSendRequestWithForm() + { + // Arrange + var responseMessage = new HttpResponseMessage(HttpStatusCode.OK); + _messageHandlerMock + .Protected() + .Setup>( + "SendAsync", + ItExpr.IsAny(), + ItExpr.IsAny() + ) + .ReturnsAsync(responseMessage); + + var formData = new Dictionary { { "key", "value" } }; + + // Act + var response = await _service.FetchAsync("/test", HttpMethod.Post, form: formData); + + // Assert + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + _messageHandlerMock.Protected().Verify( + "SendAsync", + Times.Once(), + ItExpr.Is(req => + req.Method == HttpMethod.Post && + req.RequestUri == new Uri("https://example.com/test") && + req.Content.Headers.ContentType.MediaType == "application/x-www-form-urlencoded"), + ItExpr.IsAny() + ); + } + + [Test] + public async Task FetchAsync_ShouldThrowException_WhenBothBodyAndFormAreSet() + { + // Arrange + var bodyContent = new StringContent("test body"); + var formData = new Dictionary { { "key", "value" } }; + + // Act & Assert + Assert.ThrowsAsync(() => _service.FetchAsync("/test", HttpMethod.Post, body: bodyContent, form: formData)); + } + } +} diff --git a/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj b/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj index 7d99120..7bb0364 100644 --- a/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj +++ b/DigitalData.Core.Tests/DigitalData.Core.Tests.csproj @@ -20,8 +20,12 @@ + + + + diff --git a/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.g.props b/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.g.props index 05a5562..37b4a46 100644 --- a/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.g.props +++ b/DigitalData.Core.Tests/obj/DigitalData.Core.Tests.csproj.nuget.g.props @@ -19,9 +19,9 @@ + C:\Users\tekh\.nuget\packages\nunit.analyzers\3.5.0 - C:\Users\tekh\.nuget\packages\newtonsoft.json\9.0.1 \ No newline at end of file