Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #298 from BlockForks/master
Browse files Browse the repository at this point in the history
Check for success while calling SubmitTransaction
  • Loading branch information
fracek authored Jan 8, 2021
2 parents a3c1b35 + 754045a commit efcdd3c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
61 changes: 61 additions & 0 deletions stellar-dotnet-sdk-test/ServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Moq;
using Moq.Language;
using stellar_dotnet_sdk;
using stellar_dotnet_sdk.federation;
using stellar_dotnet_sdk.responses;
using System;
using System.IO;
Expand Down Expand Up @@ -46,6 +47,15 @@ public void Cleanup()
_server.Dispose();
}

public static HttpResponseMessage ResponseMessage(HttpStatusCode statusCode)
{
return new HttpResponseMessage
{
StatusCode = statusCode,
Content = null
};
}

public static HttpResponseMessage ResponseMessage(HttpStatusCode statusCode, string content)
{
return new HttpResponseMessage
Expand Down Expand Up @@ -155,6 +165,57 @@ public async Task TestSubmitTransactionFail()
Assert.AreEqual(1, ((TransactionResultFailed)result).Results.Count);
}

[TestMethod]
public async Task TestSubmitTransactionEnsureSuccess()
{
var json = File.ReadAllText(Path.Combine("testdata", "serverSuccess.json"));
When().Returns(ResponseMessage(HttpOk, json));

var response = await _server.SubmitTransaction(
BuildTransaction().ToEnvelopeXdrBase64(), new SubmitTransactionOptions { EnsureSuccess = true });
Assert.IsTrue(response.IsSuccess());
Assert.AreEqual(response.Ledger, (uint)826150);
Assert.AreEqual(response.Hash, "2634d2cf5adcbd3487d1df042166eef53830115844fdde1588828667bf93ff42");
Assert.IsNull(response.SubmitTransactionResponseExtras);
}

[TestMethod]
public async Task TestSubmitTransactionEnsureSuccessWithContent()
{
var json = File.ReadAllText(Path.Combine("testdata", "serverFailure.json"));
When().Returns(ResponseMessage(HttpBadRequest, json));

ConnectionErrorException ex = await Assert.ThrowsExceptionAsync<ConnectionErrorException>( async () => {
await _server.SubmitTransaction(BuildTransaction(), new SubmitTransactionOptions { EnsureSuccess = true });
});

Assert.IsTrue(ex.Message.Contains("Status code (BadRequest) is not success."));
}

[TestMethod]
public async Task TestSubmitTransactionEnsureSuccessWithEmptyContent()
{
When().Returns(ResponseMessage(HttpBadRequest, ""));

ConnectionErrorException ex = await Assert.ThrowsExceptionAsync<ConnectionErrorException>( async () => {
await _server.SubmitTransaction(BuildTransaction(), new SubmitTransactionOptions { EnsureSuccess = true });
});

Assert.AreEqual(ex.Message, "Status code (BadRequest) is not success.");
}

[TestMethod]
public async Task TestSubmitTransactionEnsureSuccessWithNullContent()
{
When().Returns(ResponseMessage(HttpBadRequest));

ConnectionErrorException ex = await Assert.ThrowsExceptionAsync<ConnectionErrorException>( async () => {
await _server.SubmitTransaction(BuildTransaction(), new SubmitTransactionOptions { EnsureSuccess = true });
});

Assert.AreEqual(ex.Message, "Status code (BadRequest) is not success.");
}

[TestMethod]
public async Task TestNoSkipMemoRequiredCheck()
{
Expand Down
15 changes: 14 additions & 1 deletion stellar-dotnet-sdk/Server.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using stellar_dotnet_sdk.requests;
using stellar_dotnet_sdk.federation;
using stellar_dotnet_sdk.requests;
using stellar_dotnet_sdk.responses;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -170,6 +171,18 @@ public async Task<SubmitTransactionResponse> SubmitTransaction(string transactio
};

var response = await _httpClient.PostAsync(transactionUri, new FormUrlEncodedContent(paramsPairs.ToArray()));

if (options.EnsureSuccess && !response.IsSuccessStatusCode)
{
string responseString = string.Empty;
if(response.Content != null)
{
responseString = await response.Content.ReadAsStringAsync();
}

throw new ConnectionErrorException( $"Status code ({response.StatusCode}) is not success.{ ( !string.IsNullOrEmpty(responseString) ? " Content: " + responseString : "") }");
}

if (response.Content != null)
{
var responseString = await response.Content.ReadAsStringAsync();
Expand Down
1 change: 1 addition & 0 deletions stellar-dotnet-sdk/SubmitTransactionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class SubmitTransactionOptions
{
public bool SkipMemoRequiredCheck { get; set; }
public bool FeeBumpTransaction { get; set; }
public bool EnsureSuccess { get; set; }
}
}

0 comments on commit efcdd3c

Please sign in to comment.