128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
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<HttpMessageHandler> _messageHandlerMock;
|
|
private HttpClient _httpClient;
|
|
private CookieContainer _cookieContainer;
|
|
private Mock<IOptions<HttpClientOptions>> _optionsMock;
|
|
private BaseHttpClientService _service;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_messageHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
|
|
_httpClient = new HttpClient(_messageHandlerMock.Object);
|
|
_cookieContainer = new CookieContainer();
|
|
_optionsMock = new Mock<IOptions<HttpClientOptions>>();
|
|
_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<Task<HttpResponseMessage>>(
|
|
"SendAsync",
|
|
ItExpr.IsAny<HttpRequestMessage>(),
|
|
ItExpr.IsAny<CancellationToken>()
|
|
)
|
|
.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<HttpRequestMessage>(req =>
|
|
req.Method == HttpMethod.Post &&
|
|
req.RequestUri == new Uri("https://example.com/test") &&
|
|
req.Content == bodyContent),
|
|
ItExpr.IsAny<CancellationToken>()
|
|
);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FetchAsync_ShouldSendRequestWithForm()
|
|
{
|
|
// Arrange
|
|
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
|
|
_messageHandlerMock
|
|
.Protected()
|
|
.Setup<Task<HttpResponseMessage>>(
|
|
"SendAsync",
|
|
ItExpr.IsAny<HttpRequestMessage>(),
|
|
ItExpr.IsAny<CancellationToken>()
|
|
)
|
|
.ReturnsAsync(responseMessage);
|
|
|
|
var formData = new Dictionary<string, string> { { "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<HttpRequestMessage>(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<CancellationToken>()
|
|
);
|
|
}
|
|
|
|
[Test]
|
|
public async Task FetchAsync_ShouldThrowException_WhenBothBodyAndFormAreSet()
|
|
{
|
|
// Arrange
|
|
var bodyContent = new StringContent("test body");
|
|
var formData = new Dictionary<string, string> { { "key", "value" } };
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<InvalidOperationException>(() => _service.FetchAsync("/test", HttpMethod.Post, body: bodyContent, form: formData));
|
|
}
|
|
}
|
|
}
|