|
1 | 1 | package com.soneso.stellar.sdk.unitTests.horizon |
2 | 2 |
|
3 | 3 | import com.soneso.stellar.sdk.horizon.HorizonServer |
| 4 | +import com.soneso.stellar.sdk.horizon.exceptions.BadRequestException |
| 5 | +import com.soneso.stellar.sdk.horizon.exceptions.BadResponseException |
4 | 6 | import com.soneso.stellar.sdk.horizon.exceptions.ConnectionErrorException |
| 7 | +import com.soneso.stellar.sdk.horizon.exceptions.UnknownResponseException |
5 | 8 | import io.ktor.client.* |
6 | 9 | import io.ktor.client.engine.mock.* |
7 | 10 | import io.ktor.client.plugins.contentnegotiation.* |
@@ -112,6 +115,133 @@ class HorizonServerMethodsTest { |
112 | 115 | server.close() |
113 | 116 | } |
114 | 117 |
|
| 118 | + @Test |
| 119 | + fun testSubmitTransactionAsync_engineThrowable_wrappedAsConnectionErrorException() = runTest { |
| 120 | + // Given: the async submit path with a client reporting a connectivity failure as a |
| 121 | + // non-Exception Throwable (the Kotlin/JS HTTP engine reports these as kotlin.Error) |
| 122 | + val throwingClient = createThrowingMockClient(Error("Fail to fetch")) |
| 123 | + val server = HorizonServer( |
| 124 | + "https://horizon-testnet.stellar.org", |
| 125 | + httpClient = throwingClient, |
| 126 | + submitHttpClient = throwingClient |
| 127 | + ) |
| 128 | + |
| 129 | + // When/Then: the failure surfaces as the documented ConnectionErrorException. |
| 130 | + // Type and message are asserted instead of instance identity because the JVM |
| 131 | + // coroutine machinery copies exceptions for stack-trace recovery. |
| 132 | + val exception = assertFailsWith<ConnectionErrorException> { |
| 133 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 134 | + } |
| 135 | + val cause = assertIs<Error>(exception.cause) |
| 136 | + assertEquals("Fail to fetch", cause.message) |
| 137 | + |
| 138 | + server.close() |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun testSubmitTransactionAsync_engineCancellation_propagates() = runTest { |
| 143 | + // Given: the async submit path with a client that throws a cancellation |
| 144 | + val throwingClient = createThrowingMockClient(CancellationException("cancelled")) |
| 145 | + val server = HorizonServer( |
| 146 | + "https://horizon-testnet.stellar.org", |
| 147 | + httpClient = throwingClient, |
| 148 | + submitHttpClient = throwingClient |
| 149 | + ) |
| 150 | + |
| 151 | + // When/Then: cancellation propagates instead of being wrapped |
| 152 | + assertFailsWith<CancellationException> { |
| 153 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 154 | + } |
| 155 | + |
| 156 | + server.close() |
| 157 | + } |
| 158 | + |
| 159 | + // ========== Submit error-status handling (error body included in the exception) ========== |
| 160 | + |
| 161 | + @Test |
| 162 | + fun testSubmitTransaction_forbidden403_throwsBadRequestWithBody() = runTest { |
| 163 | + val mockClient = createMockClient("forbidden detail", statusCode = HttpStatusCode.Forbidden, contentType = "text/plain", expectedPath = "/transactions") |
| 164 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 165 | + val exception = assertFailsWith<BadRequestException> { |
| 166 | + server.submitTransaction(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 167 | + } |
| 168 | + assertEquals(403, exception.code) |
| 169 | + assertEquals("forbidden detail", exception.body) |
| 170 | + server.close() |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + fun testSubmitTransaction_serverError500_throwsBadResponseWithBody() = runTest { |
| 175 | + val mockClient = createMockClient("server exploded", statusCode = HttpStatusCode.InternalServerError, contentType = "text/plain", expectedPath = "/transactions") |
| 176 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 177 | + val exception = assertFailsWith<BadResponseException> { |
| 178 | + server.submitTransaction(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 179 | + } |
| 180 | + assertEquals(500, exception.code) |
| 181 | + assertEquals("server exploded", exception.body) |
| 182 | + server.close() |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + fun testSubmitTransaction_unknownStatus_throwsUnknownResponseWithBody() = runTest { |
| 187 | + val mockClient = createMockClient("weird", statusCode = HttpStatusCode(600, "Weird"), contentType = "text/plain", expectedPath = "/transactions") |
| 188 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 189 | + val exception = assertFailsWith<UnknownResponseException> { |
| 190 | + server.submitTransaction(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 191 | + } |
| 192 | + assertEquals(600, exception.code) |
| 193 | + assertEquals("weird", exception.body) |
| 194 | + server.close() |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + fun testSubmitTransactionAsync_malformed400_throwsBadRequestWithBody() = runTest { |
| 199 | + val mockClient = createMockClient("not a valid async response", statusCode = HttpStatusCode.BadRequest, contentType = "text/plain", expectedPath = "/transactions_async") |
| 200 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 201 | + val exception = assertFailsWith<BadRequestException> { |
| 202 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 203 | + } |
| 204 | + assertEquals(400, exception.code) |
| 205 | + assertEquals("not a valid async response", exception.body) |
| 206 | + server.close() |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + fun testSubmitTransactionAsync_forbidden403_throwsBadRequestWithBody() = runTest { |
| 211 | + val mockClient = createMockClient("forbidden detail", statusCode = HttpStatusCode.Forbidden, contentType = "text/plain", expectedPath = "/transactions_async") |
| 212 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 213 | + val exception = assertFailsWith<BadRequestException> { |
| 214 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 215 | + } |
| 216 | + assertEquals(403, exception.code) |
| 217 | + assertEquals("forbidden detail", exception.body) |
| 218 | + server.close() |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + fun testSubmitTransactionAsync_serverError500_throwsBadResponseWithBody() = runTest { |
| 223 | + val mockClient = createMockClient("server exploded", statusCode = HttpStatusCode.InternalServerError, contentType = "text/plain", expectedPath = "/transactions_async") |
| 224 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 225 | + val exception = assertFailsWith<BadResponseException> { |
| 226 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 227 | + } |
| 228 | + assertEquals(500, exception.code) |
| 229 | + assertEquals("server exploded", exception.body) |
| 230 | + server.close() |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + fun testSubmitTransactionAsync_unknownStatus_throwsUnknownResponseWithBody() = runTest { |
| 235 | + val mockClient = createMockClient("weird", statusCode = HttpStatusCode(600, "Weird"), contentType = "text/plain", expectedPath = "/transactions_async") |
| 236 | + val server = HorizonServer("https://horizon-testnet.stellar.org", httpClient = mockClient, submitHttpClient = mockClient) |
| 237 | + val exception = assertFailsWith<UnknownResponseException> { |
| 238 | + server.submitTransactionAsync(VALID_ENVELOPE_XDR, skipMemoRequiredCheck = true) |
| 239 | + } |
| 240 | + assertEquals(600, exception.code) |
| 241 | + assertEquals("weird", exception.body) |
| 242 | + server.close() |
| 243 | + } |
| 244 | + |
115 | 245 | // ========== Individual Resource Methods ========== |
116 | 246 |
|
117 | 247 | @Test |
|
0 commit comments