2020-05-02 18:57:31 +01:00
|
|
|
using System;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.Http;
|
|
|
|
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>(),
|
2020-06-03 16:44:13 +01:00
|
|
|
It.IsAny<IRetryHandler.RetryFunc>()
|
2020-05-02 18:57:31 +01:00
|
|
|
)
|
|
|
|
).Returns(Task.FromResult(response.Object));
|
|
|
|
|
|
|
|
var apiConnector = new APIConnector(
|
|
|
|
new Uri("https://spotify.com"),
|
|
|
|
authenticator.Object,
|
|
|
|
serializer.Object,
|
|
|
|
httpClient.Object,
|
2020-05-03 12:00:50 +01:00
|
|
|
retryHandler.Object,
|
|
|
|
null
|
2020-05-02 18:57:31 +01:00
|
|
|
);
|
2020-05-05 04:26:37 +01:00
|
|
|
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get).ConfigureAwait(false);
|
2020-05-02 18:57:31 +01:00
|
|
|
|
2020-05-13 22:49:54 +01:00
|
|
|
authenticator.Verify(a => a.Apply(It.IsAny<IRequest>(), It.IsAny<IAPIConnector>()), Times.Once);
|
2020-05-02 18:57:31 +01:00
|
|
|
httpClient.Verify(h => h.DoRequest(It.IsAny<IRequest>()), 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>())).Returns(Task.FromResult(response.Object));
|
|
|
|
|
|
|
|
var retryHandler = new Mock<IRetryHandler>();
|
|
|
|
retryHandler.Setup(r =>
|
|
|
|
r.HandleRetry(
|
|
|
|
It.IsAny<IRequest>(),
|
|
|
|
It.IsAny<IResponse>(),
|
2020-06-03 16:44:13 +01:00
|
|
|
It.IsAny<IRetryHandler.RetryFunc>()
|
2020-05-02 18:57:31 +01:00
|
|
|
)
|
2020-06-03 16:44:13 +01:00
|
|
|
).Returns((IRequest request, IResponse response, IRetryHandler.RetryFunc retry) => retry(request));
|
2020-05-02 18:57:31 +01:00
|
|
|
|
|
|
|
var apiConnector = new APIConnector(
|
|
|
|
new Uri("https://spotify.com"),
|
|
|
|
authenticator.Object,
|
|
|
|
serializer.Object,
|
|
|
|
httpClient.Object,
|
2020-05-03 12:00:50 +01:00
|
|
|
retryHandler.Object,
|
|
|
|
null
|
2020-05-02 18:57:31 +01:00
|
|
|
);
|
2020-05-05 04:26:37 +01:00
|
|
|
await apiConnector.SendAPIRequest<string>(new Uri("/me", UriKind.Relative), HttpMethod.Get).ConfigureAwait(false);
|
2020-05-02 18:57:31 +01:00
|
|
|
|
|
|
|
serializer.Verify(s => s.SerializeRequest(It.IsAny<IRequest>()), Times.Once);
|
2020-05-13 22:49:54 +01:00
|
|
|
authenticator.Verify(a => a.Apply(It.IsAny<IRequest>(), It.IsAny<IAPIConnector>()), Times.Exactly(2));
|
2020-05-02 18:57:31 +01:00
|
|
|
httpClient.Verify(h => h.DoRequest(It.IsAny<IRequest>()), Times.Exactly(2));
|
|
|
|
serializer.Verify(s => s.DeserializeResponse<string>(response.Object), Times.Once);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|