Skip to content

Commit

Permalink
Moved string literals to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Huib Piguillet committed Oct 21, 2020
1 parent 14914c1 commit 44c7733
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 92 deletions.
4 changes: 3 additions & 1 deletion Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal class ApiRequestSender : IApiRequestSender
private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private IHttpRequestSender _httpSender;

public const string TokenPath = "/v6/authentication/oauth2/token";

/// <summary>
/// Initializes a new instance of the <see cref="T:Sdk.Api.ApiRequestSender"/> class.
/// </summary>
Expand Down Expand Up @@ -138,7 +140,7 @@ private async Task RefreshToken()
{
Authenticated = false,
Query = query,
Path = $"/v6/authentication/oauth2/token",
Path = TokenPath,
HTTPMethod = HttpMethod.Post
};

Expand Down
2 changes: 1 addition & 1 deletion Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class HttpRequestSender : IHttpRequestSender
/// </summary>
public string UserAgent
{
get { return "bynder-c-sharp-sdk/" + Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
get { return $"bynder-c-sharp-sdk/{Assembly.GetExecutingAssembly().GetName().Version.ToString()}"; }
}

/// <summary>
Expand Down
165 changes: 75 additions & 90 deletions Bynder/Test/Api/RequestSender/ApiRequestSenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,117 +15,102 @@ namespace Bynder.Test.Api.RequestSender
{
public class ApiRequestSenderTest
{
private const string AccessToken = "access_token";
private const string _accessToken = "some_access_token";
private const string _authHeader = "Bearer " + _accessToken;
private const string _path = "/fake/api";
private const string _queryValue = "some_query_value";
private const string _queryString = "Item1=" + _queryValue;

private Mock<IHttpRequestSender> httpSenderMock;
private StubQuery query;
private Mock<IHttpRequestSender> _httpSenderMock;
private StubQuery _query;

public ApiRequestSenderTest()
{
httpSenderMock = new Mock<IHttpRequestSender>();
httpSenderMock
_httpSenderMock = new Mock<IHttpRequestSender>();
_httpSenderMock
.Setup(sender => sender.SendHttpRequest(It.IsAny<HttpRequestMessage>()))
.Returns(Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK)));

query = new StubQuery
_query = new StubQuery
{
Item1 = "Value"
Item1 = _queryValue
};
}

[Fact]
public async Task WhenRequestIsPostThenParametersAreAddedToContent()
{
using (var apiRequestSender = CreateApiRequestSender(true))
{
var apiRequest = new ApiRequest<bool>()
{
Path = "/fake/api",
HTTPMethod = HttpMethod.Post,
Query = query
};

await apiRequestSender.SendRequestAsync(apiRequest);

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(req =>
req.RequestUri.PathAndQuery.Contains("/fake/api")
&& req.Method == HttpMethod.Post
&& req.Headers.Authorization.ToString() == $"Bearer {AccessToken}"
&& req.Content.ReadAsStringAsync().Result.Contains("Item1=Value")
)
));

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Once);
}
await SendRequest(hasValidCredentials: true, httpMethod: HttpMethod.Post);

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(req =>
req.RequestUri.PathAndQuery.Contains(_path)
&& req.Method == HttpMethod.Post
&& req.Headers.Authorization.ToString() == _authHeader
&& req.Content.ReadAsStringAsync().Result.Contains(_queryString)
)
));

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Once);
}


[Fact]
public async Task WhenCredentialInvalidTwoRequestsSent()
{
using (var apiRequestSender = CreateApiRequestSender(false))
{
var apiRequest = new ApiRequest<bool>()
{
Path = "/fake/api",
HTTPMethod = HttpMethod.Get,
Query = query
};

await apiRequestSender.SendRequestAsync(apiRequest);

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery.Contains("/oauth2/token")
&& req.Method == HttpMethod.Post
)
));

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery.Contains("/fake/api")
&& req.Method == HttpMethod.Get
&& req.Headers.Authorization.ToString() == $"Bearer {AccessToken}"
&& req.RequestUri.Query.Contains("Item1=Value")
)
));

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Exactly(2));
}
await SendRequest(hasValidCredentials: false, httpMethod: HttpMethod.Get);

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery == ApiRequestSender.TokenPath
&& req.Method == HttpMethod.Post
)
));

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery.Contains(_path)
&& req.Method == HttpMethod.Get
&& req.Headers.Authorization.ToString() == _authHeader
&& req.RequestUri.Query.Contains(_queryString)
)
));

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Exactly(2));
}

[Fact]
public async Task WhenRequestIsGetThenParametersAreAddedToUrl()
{
using (var apiRequestSender = CreateApiRequestSender(true))
{
var apiRequest = new ApiRequest<bool>
await SendRequest(hasValidCredentials: true, httpMethod: HttpMethod.Get);

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery.Contains(_path)
&& req.Method == HttpMethod.Get
&& req.Headers.Authorization.ToString() == _authHeader
&& req.RequestUri.Query.Contains(_queryString)
)
));

_httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Once);
}

private async Task SendRequest(bool hasValidCredentials, HttpMethod httpMethod)
{
await CreateApiRequestSender(hasValidCredentials).SendRequestAsync(
new ApiRequest<bool>()
{
Path = "/fake/api",
HTTPMethod = HttpMethod.Get,
Query = query
};

await apiRequestSender.SendRequestAsync(apiRequest);

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.Is<HttpRequestMessage>(
req => req.RequestUri.PathAndQuery.Contains("/fake/api")
&& req.Method == HttpMethod.Get
&& req.Headers.Authorization.ToString() == $"Bearer {AccessToken}"
&& req.RequestUri.Query.Contains("Item1=Value")
)
));

httpSenderMock.Verify(sender => sender.SendHttpRequest(
It.IsAny<HttpRequestMessage>()
), Times.Once);
}
Path = _path,
HTTPMethod = httpMethod,
Query = _query
}
);
}

private IApiRequestSender CreateApiRequestSender(bool hasValidCredentials)
Expand All @@ -136,21 +121,21 @@ private IApiRequestSender CreateApiRequestSender(bool hasValidCredentials)
BaseUrl = new Uri("https://example.bynder.com"),
},
GetCredentials(hasValidCredentials),
httpSenderMock.Object
_httpSenderMock.Object
);
}

private ICredentials GetCredentials(bool valid)
private ICredentials GetCredentials(bool isValid)
{
var credentialsMock = new Mock<ICredentials>();

credentialsMock
.Setup(mock => mock.AreValid())
.Returns(valid);
.Returns(isValid);

credentialsMock
.SetupGet(mock => mock.AccessToken)
.Returns(AccessToken);
.Returns(_accessToken);

credentialsMock
.SetupGet(mock => mock.TokenType)
Expand Down

0 comments on commit 44c7733

Please sign in to comment.