Skip to content

Commit

Permalink
added support for overriding request creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pontus-eliason committed Apr 22, 2024
1 parent 44d6695 commit a9d005e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Client/TrustlyApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class TrustlyApiClient : IDisposable
private readonly JsonRpcSigner _signer;
private readonly JsonRpcValidator _validator = new JsonRpcValidator();

public Func<string, WebRequest> RequestCreator { get; set; }

public event EventHandler<NotificationArgs<AccountNotificationData>> OnAccount;
public event EventHandler<NotificationArgs<CancelNotificationData>> OnCancel;
public event EventHandler<NotificationArgs<CreditNotificationData>> OnCredit;
Expand Down Expand Up @@ -329,6 +331,11 @@ NotificationFailResponseDelegate onFailed
return eventHandler.GetInvocationList().Length;
}

protected virtual WebRequest CreateWebRequest(string url)
{
return WebRequest.Create(url);
}

/// <summary>
/// Sends an HTTP POST to Trustly server.
/// </summary>
Expand All @@ -337,7 +344,7 @@ NotificationFailResponseDelegate onFailed
protected string NewHttpPost(string request)
{
var requestBytes = Encoding.UTF8.GetBytes(request);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(this.Settings.URL);
var httpWebRequest = (this.RequestCreator ?? this.CreateWebRequest)(this.Settings.URL);

httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = requestBytes.Length;
Expand All @@ -353,7 +360,7 @@ protected string NewHttpPost(string request)
}
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var httpResponse = httpWebRequest.GetResponse();

var responseStream = httpResponse.GetResponseStream();
if (responseStream == null)
Expand Down
40 changes: 40 additions & 0 deletions tests/Client.UnitTests/RequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Trustly.Api.Domain.Base;
using Newtonsoft.Json;
using System.Net;

namespace Trustly.Api.Client.Tests
{
Expand Down Expand Up @@ -268,6 +269,45 @@ public void TestDeposit()
Assert.IsFalse(string.IsNullOrEmpty(response.URL));
}

[Test]
public void TestDepositWithCustomProxyClient()
{
var callCount = 0;
var proxyClient = new TrustlyApiClient(TrustlyApiClientSettings.ForDefaultTest())
{
RequestCreator = url =>
{
callCount++;
var request = WebRequest.Create(url);
request.Proxy = new WebProxy();
return request;
}
};

var response = proxyClient.Deposit(new Trustly.Api.Domain.Requests.DepositRequestData
{
NotificationURL = "https://fake.test.notification.trustly.com",
MessageID = Guid.NewGuid().ToString(),
EndUserID = "pontus.eliason@trustly.com",
Attributes = new Trustly.Api.Domain.Requests.DepositRequestDataAttributes
{
Amount = "100.00",
Firstname = "John",
Lastname = "Doe",
Email = "pontus.eliason@trustly.com",
Currency = "EUR",
Country = "SE",
Locale = "sv_SE",
ShopperStatement = "Trustly Test Deposit"
}
});

Assert.NotNull(response);
Assert.IsFalse(string.IsNullOrEmpty(response.URL));
Assert.AreEqual(1, callCount);
}

[Test]
public void TestGetWithdrawals()
{
Expand Down

0 comments on commit a9d005e

Please sign in to comment.