Skip to content

Commit

Permalink
Fix/empty post request (#971)
Browse files Browse the repository at this point in the history
* add test for empty post request on httpClientWrapper

* convert test method to not have async in signature

* add post method to http wrapper mock call

* add valid url to httpClientWrapperTest

* use AreEquals instead of Equals

* introduce httpClientWrapper fix
  • Loading branch information
wboereboom authored Jan 16, 2024
1 parent ff88ecb commit f98bfe8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Adyen.Test/HttpClientWrapperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Adyen.HttpClient;
using Adyen.Model.Management;
using Adyen.Model.TerminalApi;
using Adyen.Service.Management;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;

namespace Adyen.Test
{
[TestClass]
public class HttpClientWrapperTest : BaseTest
{
[TestMethod]
public void EmptyRequestBodyPostTest()
{
var mockHttpMessageHandler = new MockHttpMessageHandler("{}", System.Net.HttpStatusCode.OK);
var httpClient = new System.Net.Http.HttpClient(mockHttpMessageHandler);
var httpClientWrapper = new HttpClientWrapper(MockPaymentData.CreateConfigApiKeyBasedMock(), httpClient);
var _ = httpClientWrapper.Request("https://test.com/testpath", null, null, HttpMethod.Post);
Assert.AreEqual("{}", mockHttpMessageHandler.Input);
}

}
}
34 changes: 34 additions & 0 deletions Adyen.Test/mockHttpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public class MockHttpMessageHandler : HttpMessageHandler
{
private readonly string _response;
private readonly HttpStatusCode _statusCode;

public string Input { get; private set; }
public int NumberOfCalls { get; private set; }

public MockHttpMessageHandler(string response, HttpStatusCode statusCode)
{
_response = response;
_statusCode = statusCode;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
NumberOfCalls++;
if (request.Content != null) // Could be a GET-request without a body
{
Input = await request.Content.ReadAsStringAsync();
}
return new HttpResponseMessage
{
StatusCode = _statusCode,
Content = new StringContent(_response)
};
}
}
2 changes: 1 addition & 1 deletion Adyen/HttpClient/HttpClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<string> RequestAsync(string endpoint, string requestBody, Requ
{
if (requestBody == null && (httpMethod == HttpMethod.Post || httpMethod == new HttpMethod("PATCH")))
{
requestBody = "";
requestBody = "{}";
}

using (var request = GetHttpRequestMessage(endpoint, requestBody, requestOptions, httpMethod))
Expand Down

0 comments on commit f98bfe8

Please sign in to comment.