diff --git a/stellar-dotnet-sdk-test/ServerTest.cs b/stellar-dotnet-sdk-test/ServerTest.cs index af666644a..c1cc8fe6d 100644 --- a/stellar-dotnet-sdk-test/ServerTest.cs +++ b/stellar-dotnet-sdk-test/ServerTest.cs @@ -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; @@ -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 @@ -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( 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( 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( async () => { + await _server.SubmitTransaction(BuildTransaction(), new SubmitTransactionOptions { EnsureSuccess = true }); + }); + + Assert.AreEqual(ex.Message, "Status code (BadRequest) is not success."); + } + [TestMethod] public async Task TestNoSkipMemoRequiredCheck() { diff --git a/stellar-dotnet-sdk/Server.cs b/stellar-dotnet-sdk/Server.cs index 82f4f18af..cdda102a9 100644 --- a/stellar-dotnet-sdk/Server.cs +++ b/stellar-dotnet-sdk/Server.cs @@ -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; @@ -170,6 +171,18 @@ public async Task 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(); diff --git a/stellar-dotnet-sdk/SubmitTransactionOptions.cs b/stellar-dotnet-sdk/SubmitTransactionOptions.cs index acd81dbd5..9b26a80f5 100644 --- a/stellar-dotnet-sdk/SubmitTransactionOptions.cs +++ b/stellar-dotnet-sdk/SubmitTransactionOptions.cs @@ -4,5 +4,6 @@ public class SubmitTransactionOptions { public bool SkipMemoRequiredCheck { get; set; } public bool FeeBumpTransaction { get; set; } + public bool EnsureSuccess { get; set; } } }