Spotify.NET/SpotifyAPI.Web.Tests/Http/APIConnectorTest.cs
Sascha Kiefer b8a2190168
feat: allow to pass cancellation token to requests (#813)
* feat: implements markets API

* fix: use correct constructor name

* feat: allow to pass a cancellation token

* pass cancellation token

* pass cancellation token only >NETSTANDARD2_1

Co-authored-by: Jonas Dellinger <jonas@dellinger.dev>
2022-11-18 12:30:09 +01:00

102 lines
3.8 KiB
C#

using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web.Tests
{
[TestFixture]
public class APIConnectorTest
{
[Test]
public async Task RetryHandler_IsUsed()
{
var apiResponse = new Mock<IAPIResponse<string>>();
apiResponse.SetupGet(a => a.Body).Returns("Hello World");
var response = new Mock<IResponse>();
response.SetupGet(r => r.ContentType).Returns("application/json");
response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
response.SetupGet(r => r.Body).Returns("\"Hello World\"");
var authenticator = new Mock<IAuthenticator>();
var serializer = new Mock<IJSONSerializer>();
serializer.Setup(s => s.DeserializeResponse<string>(It.IsAny<IResponse>())).Returns(apiResponse.Object);
var httpClient = new Mock<IHTTPClient>();
var retryHandler = new Mock<IRetryHandler>();
retryHandler.Setup(r =>
r.HandleRetry(
It.IsAny<IRequest>(),
It.IsAny<IResponse>(),
It.IsAny<IRetryHandler.RetryFunc>(),
It.IsAny<CancellationToken>()
)
).Returns(Task.FromResult(response.Object));
var apiConnector = new APIConnector(
new Uri("https://spotify.com"),
authenticator.Object,
serializer.Object,
httpClient.Object,
retryHandler.Object,
null
);
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get).ConfigureAwait(false);
authenticator.Verify(a => a.Apply(It.IsAny<IRequest>(), It.IsAny<IAPIConnector>()), Times.Once);
httpClient.Verify(h => h.DoRequest(It.IsAny<IRequest>(), It.IsAny<CancellationToken>()), Times.Once);
serializer.Verify(s => s.DeserializeResponse<string>(response.Object), Times.Once);
}
[Test]
public async Task RetryHandler_CanRetry()
{
var apiResponse = new Mock<IAPIResponse<string>>();
apiResponse.SetupGet(a => a.Body).Returns("Hello World");
var response = new Mock<IResponse>();
response.SetupGet(r => r.ContentType).Returns("application/json");
response.SetupGet(r => r.StatusCode).Returns(HttpStatusCode.OK);
response.SetupGet(r => r.Body).Returns("\"Hello World\"");
var authenticator = new Mock<IAuthenticator>();
var serializer = new Mock<IJSONSerializer>();
serializer.Setup(s => s.DeserializeResponse<string>(It.IsAny<IResponse>())).Returns(apiResponse.Object);
var httpClient = new Mock<IHTTPClient>();
httpClient.Setup(h => h.DoRequest(It.IsAny<IRequest>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(response.Object));
var retryHandler = new Mock<IRetryHandler>();
retryHandler.Setup(r =>
r.HandleRetry(
It.IsAny<IRequest>(),
It.IsAny<IResponse>(),
It.IsAny<IRetryHandler.RetryFunc>(),
It.IsAny<CancellationToken>()
)
).Returns((IRequest request, IResponse _, IRetryHandler.RetryFunc retry, CancellationToken ct)
=> retry(request, ct));
var apiConnector = new APIConnector(
new Uri("https://spotify.com"),
authenticator.Object,
serializer.Object,
httpClient.Object,
retryHandler.Object,
null
);
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get).ConfigureAwait(false);
serializer.Verify(s => s.SerializeRequest(It.IsAny<IRequest>()), Times.Once);
authenticator.Verify(a => a.Apply(It.IsAny<IRequest>(), It.IsAny<IAPIConnector>()), Times.Exactly(2));
httpClient.Verify(h => h.DoRequest(It.IsAny<IRequest>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
serializer.Verify(s => s.DeserializeResponse<string>(response.Object), Times.Once);
}
}
}