Skip to content

Commit

Permalink
Add integration test to verify setting a custom idempotency key works
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt committed Mar 29, 2024
1 parent fd3eb80 commit 2673ad7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
20 changes: 20 additions & 0 deletions tests/Mollie.Tests.Integration/Api/PaymentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public async Task CanCreateDefaultPaymentWithOnlyRequiredFields() {
result.Description.Should().Be(paymentRequest.Description);
result.RedirectUrl.Should().Be(paymentRequest.RedirectUrl);
}

[DefaultRetryFact]
public async Task CanCreateDefaultPaymentWithCustomIdempotencyKey() {
// Given: we create a payment request with only the required parameters
PaymentRequest paymentRequest = new PaymentRequest() {
Amount = new Amount(Currency.EUR, "100.00"),
Description = "Description",
RedirectUrl = DefaultRedirectUrl
};

// When: We send the payment request to Mollie
using (_paymentClient.WithIdempotencyKey("my-idempotency-key"))
{
PaymentResponse firstAttempt = await _paymentClient.CreatePaymentAsync(paymentRequest);
PaymentResponse secondAttempt = await _paymentClient.CreatePaymentAsync(paymentRequest);

// Then: Make sure the responses have the same payment Id
firstAttempt.Id.Should().Be(secondAttempt.Id);
}
}

[DefaultRetryFact]
public async Task CanCreateDefaultPaymentWithAllFields() {
Expand Down
14 changes: 11 additions & 3 deletions tests/Mollie.Tests.Unit/Client/PaymentClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ public async Task CreatePaymentAsync_WithCustomIdempotencyKey_CustomIdemPotencyK
Description = "Description",
RedirectUrl = "http://www.mollie.com"
};
const string customIdempotencyKey = "my-idempotency-key";
const string customIdempotencyKey1 = "my-idempotency-key-1";
const string customIdempotencyKey2 = "my-idempotency-key-2";
const string jsonToReturnInMockResponse = defaultPaymentJsonResponse;
var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Post, $"{BaseMollieClient.ApiEndPoint}*")
.WithHeaders("Idempotency-Key", customIdempotencyKey)
.WithHeaders("Idempotency-Key", customIdempotencyKey1)
.Respond("application/json", jsonToReturnInMockResponse);
mockHttp.Expect(HttpMethod.Post, $"{BaseMollieClient.ApiEndPoint}*")
.WithHeaders("Idempotency-Key", customIdempotencyKey2)
.Respond("application/json", jsonToReturnInMockResponse);
HttpClient httpClient = mockHttp.ToHttpClient();
PaymentClient paymentClient = new PaymentClient("abcde", httpClient);

// Act
using (paymentClient.WithIdempotencyKey(customIdempotencyKey))
using (paymentClient.WithIdempotencyKey(customIdempotencyKey1))
{
await paymentClient.CreatePaymentAsync(paymentRequest);
}
using (paymentClient.WithIdempotencyKey(customIdempotencyKey2))
{
await paymentClient.CreatePaymentAsync(paymentRequest);
}
Expand Down

0 comments on commit 2673ad7

Please sign in to comment.