DigitalData.Core/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs
Developer 02 0c2334cefb refactor(BaseHttpClientService): Der Wert von query params wurde zum nullbaren Objekt, um Flag-Parameter hinzuzufügen.
- Aktualisierte Schnittstelle und Logik unter Berücksichtigung dieser Situation
2024-11-22 14:35:22 +01:00

59 lines
2.0 KiB
C#

using DigitalData.Core.Abstractions.Client;
using DigitalData.Core.Client;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Tests.Client
{
[TestFixture]
public class BaseHttpClientServiceTests
{
private IServiceProvider _serviceProvider;
private IBaseHttpClientService _service;
[SetUp]
public void SetUp()
{
_serviceProvider = new ServiceCollection()
.AddHttpClientService("https://jsonplaceholder.typicode.com", "todos")
.BuildServiceProvider();
_service = _serviceProvider.GetRequiredService<IBaseHttpClientService>();
}
[Test]
public async Task FetchJsonAsync_ShouldReturnJsonResponse_WithCorrectWithPath()
{
// Act
var expectedUserId = (int) await _service.FetchAsync(path: "/1", sendWithCookie: false, saveCookie: false)
.ThenAsync(res => res.Json())
.ThenAsync(todo => todo.userId);
// Assert
Assert.That(expectedUserId, Is.EqualTo(1), "The userId of the fetched JSON object should be 1.");
}
[Test]
public async Task FetchJsonAsync_ShouldReturnJsonResponse_WithQueryParams()
{
var queryParams = new Dictionary<string, object?>
{
{ "id", "1" }
};
// Act
var dyn_id = await _service.FetchAsync(queryParams: queryParams, sendWithCookie: false, saveCookie: false)
.ThenAsync(res => res.JsonList())
.ThenAsync(todo => todo.FirstOrDefault()?.userId);
try
{
Assert.That((int)dyn_id, Is.EqualTo(1), "The userId of the fetched JSON object should be 1.");
}
catch (InvalidCastException)
{
// Handle the case where the cast is not possible
Assert.Fail("The id could not be cast to an integer.");
}
}
}
}