From 44c7733f72aaf661ff3bf1605c56ff5df4dc6266 Mon Sep 17 00:00:00 2001 From: Huib Piguillet Date: Wed, 21 Oct 2020 18:05:08 +0200 Subject: [PATCH] Moved string literals to constants --- .../Sdk/Api/RequestSender/ApiRequestSender.cs | 4 +- .../Api/RequestSender/HttpRequestSender.cs | 2 +- .../Api/RequestSender/ApiRequestSenderTest.cs | 165 ++++++++---------- 3 files changed, 79 insertions(+), 92 deletions(-) diff --git a/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs b/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs index a1774e8..e32903e 100644 --- a/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs +++ b/Bynder/Sdk/Api/RequestSender/ApiRequestSender.cs @@ -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"; + /// /// Initializes a new instance of the class. /// @@ -138,7 +140,7 @@ private async Task RefreshToken() { Authenticated = false, Query = query, - Path = $"/v6/authentication/oauth2/token", + Path = TokenPath, HTTPMethod = HttpMethod.Post }; diff --git a/Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs b/Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs index 2794f8d..3de9ecf 100644 --- a/Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs +++ b/Bynder/Sdk/Api/RequestSender/HttpRequestSender.cs @@ -20,7 +20,7 @@ internal class HttpRequestSender : IHttpRequestSender /// 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()}"; } } /// diff --git a/Bynder/Test/Api/RequestSender/ApiRequestSenderTest.cs b/Bynder/Test/Api/RequestSender/ApiRequestSenderTest.cs index d10ce27..8d98bdc 100644 --- a/Bynder/Test/Api/RequestSender/ApiRequestSenderTest.cs +++ b/Bynder/Test/Api/RequestSender/ApiRequestSenderTest.cs @@ -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 httpSenderMock; - private StubQuery query; + private Mock _httpSenderMock; + private StubQuery _query; public ApiRequestSenderTest() { - httpSenderMock = new Mock(); - httpSenderMock + _httpSenderMock = new Mock(); + _httpSenderMock .Setup(sender => sender.SendHttpRequest(It.IsAny())) .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() - { - Path = "/fake/api", - HTTPMethod = HttpMethod.Post, - Query = query - }; - - await apiRequestSender.SendRequestAsync(apiRequest); - - httpSenderMock.Verify(sender => sender.SendHttpRequest( - It.Is(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() - ), Times.Once); - } + await SendRequest(hasValidCredentials: true, httpMethod: HttpMethod.Post); + + _httpSenderMock.Verify(sender => sender.SendHttpRequest( + It.Is(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() + ), Times.Once); } [Fact] public async Task WhenCredentialInvalidTwoRequestsSent() { - using (var apiRequestSender = CreateApiRequestSender(false)) - { - var apiRequest = new ApiRequest() - { - Path = "/fake/api", - HTTPMethod = HttpMethod.Get, - Query = query - }; - - await apiRequestSender.SendRequestAsync(apiRequest); - - httpSenderMock.Verify(sender => sender.SendHttpRequest( - It.Is( - req => req.RequestUri.PathAndQuery.Contains("/oauth2/token") - && req.Method == HttpMethod.Post - ) - )); - - httpSenderMock.Verify(sender => sender.SendHttpRequest( - It.Is( - 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() - ), Times.Exactly(2)); - } + await SendRequest(hasValidCredentials: false, httpMethod: HttpMethod.Get); + + _httpSenderMock.Verify(sender => sender.SendHttpRequest( + It.Is( + req => req.RequestUri.PathAndQuery == ApiRequestSender.TokenPath + && req.Method == HttpMethod.Post + ) + )); + + _httpSenderMock.Verify(sender => sender.SendHttpRequest( + It.Is( + 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() + ), Times.Exactly(2)); } [Fact] public async Task WhenRequestIsGetThenParametersAreAddedToUrl() { - using (var apiRequestSender = CreateApiRequestSender(true)) - { - var apiRequest = new ApiRequest + await SendRequest(hasValidCredentials: true, httpMethod: HttpMethod.Get); + + _httpSenderMock.Verify(sender => sender.SendHttpRequest( + It.Is( + 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() + ), Times.Once); + } + + private async Task SendRequest(bool hasValidCredentials, HttpMethod httpMethod) + { + await CreateApiRequestSender(hasValidCredentials).SendRequestAsync( + new ApiRequest() { - Path = "/fake/api", - HTTPMethod = HttpMethod.Get, - Query = query - }; - - await apiRequestSender.SendRequestAsync(apiRequest); - - httpSenderMock.Verify(sender => sender.SendHttpRequest( - It.Is( - 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() - ), Times.Once); - } + Path = _path, + HTTPMethod = httpMethod, + Query = _query + } + ); } private IApiRequestSender CreateApiRequestSender(bool hasValidCredentials) @@ -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(); credentialsMock .Setup(mock => mock.AreValid()) - .Returns(valid); + .Returns(isValid); credentialsMock .SetupGet(mock => mock.AccessToken) - .Returns(AccessToken); + .Returns(_accessToken); credentialsMock .SetupGet(mock => mock.TokenType)