Skip to content

Commit

Permalink
Merge pull request #10 from trustly/MX-930_proxy
Browse files Browse the repository at this point in the history
added support for overriding request creation
  • Loading branch information
pontus-eliason authored Apr 22, 2024
2 parents 44d6695 + ed02fa7 commit 5582f1c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ jobs:

runs-on: windows-latest

#strategy:
# matrix:
# dotnet: [ '4.5.x', '5.0.x' ]

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 5.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
id: get_version
uses: battila7/get-version-action@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 5.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
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
6 changes: 3 additions & 3 deletions tests/Client.UnitTests/Client.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461</TargetFrameworks>
<TargetFrameworks>net472</TargetFrameworks>

<IsPackable>false</IsPackable>
<RootNamespace>Trustly.Api.Client.UnitTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />

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 5582f1c

Please sign in to comment.