diff --git a/build.gradle.kts b/build.gradle.kts index e8a2796e6..bc16114fd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,14 +8,20 @@ plugins { id("com.github.ben-manes.versions") version "0.51.0" id("io.freefair.lombok") version "8.10.2" id("com.gradleup.nmcp") version "0.0.9" + kotlin("jvm") version "2.2.0" } group = "network.lightsail" version = "2.0.0-beta0" java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + toolchain { + languageVersion = JavaLanguageVersion.of(8) + } +} + +kotlin { + jvmToolchain(8) } spotless { @@ -24,6 +30,10 @@ spotless { removeUnusedImports() googleJavaFormat() } + kotlin { + target("src/test/kotlin/**/*.kt") + ktfmt("0.56").googleStyle() + } } repositories { @@ -46,6 +56,9 @@ dependencies { testImplementation("org.bouncycastle:bcpkix-jdk18on:1.79") // mock https testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.11.3") testRuntimeOnly("org.junit.platform:junit-platform-launcher") + + testImplementation("io.kotest:kotest-runner-junit5:5.9.1") + testImplementation("io.kotest:kotest-assertions-core:5.9.1") } tasks { @@ -197,4 +210,4 @@ nmcp { // or if you want to publish automatically // publicationType = "AUTOMATIC" } -} \ No newline at end of file +} diff --git a/src/test/java/org/stellar/sdk/federation/FederationTest.java b/src/test/java/org/stellar/sdk/federation/FederationTest.java deleted file mode 100644 index 6d32beca4..000000000 --- a/src/test/java/org/stellar/sdk/federation/FederationTest.java +++ /dev/null @@ -1,359 +0,0 @@ -package org.stellar.sdk.federation; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThrows; - -import java.io.IOException; -import java.net.URLEncoder; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.X509TrustManager; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import org.junit.Test; -import org.stellar.sdk.SslCertificateUtils; - -public class FederationTest { - @Test - public void testResolveAddressSuccess() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String successResponse = - "{\"stellar_address\":\"bob*" - + domain - + "\",\"account_id\":\"GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse)); - - FederationResponse response = new Federation(client).resolveAddress("bob*" + domain); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = "/federation?type=name&q=bob*" + URLEncoder.encode(domain, "UTF-8"); - assertEquals(expectedPath, federationRequest.getPath()); - - assertEquals(response.getStellarAddress(), "bob*" + domain); - assertEquals( - response.getAccountId(), "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"); - assertNull(response.getMemoType()); - assertNull(response.getMemo()); - - mockWebServer.close(); - } - - @Test - public void testResolveAddressSuccessWithMemo() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String successResponse = - "{\"stellar_address\":\"bob*" - + domain - + "\",\"account_id\":\"GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY\"" - + ", \"memo_type\": \"text\", \"memo\": \"test federation\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse)); - - FederationResponse response = new Federation(client).resolveAddress("bob*" + domain); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = "/federation?type=name&q=bob*" + URLEncoder.encode(domain, "UTF-8"); - assertEquals(expectedPath, federationRequest.getPath()); - - assertEquals(response.getStellarAddress(), "bob*" + domain); - assertEquals( - response.getAccountId(), "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"); - assertEquals(response.getMemoType(), "text"); - assertEquals(response.getMemo(), "test federation"); - mockWebServer.close(); - } - - @Test - public void testResolveAddressNotFound() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String notFoundResponse = "{\"code\":\"not_found\",\"message\":\"Account not found\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody(notFoundResponse)); - - assertThrows( - org.stellar.sdk.federation.exception.NotFoundException.class, - () -> new Federation(client).resolveAddress("bob*" + domain)); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = "/federation?type=name&q=bob*" + URLEncoder.encode(domain, "UTF-8"); - assertEquals(expectedPath, federationRequest.getPath()); - - mockWebServer.close(); - } - - @Test - public void testResolveAccountIdSuccess() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String successResponse = - "{\"stellar_address\":\"bob*" - + domain - + "\",\"account_id\":\"GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse)); - - FederationResponse response = - new Federation(client) - .resolveAccountId("GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY", domain); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = - "/federation?type=id&q=GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"; - assertEquals(expectedPath, federationRequest.getPath()); - - assertEquals( - response.getAccountId(), "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"); - assertNull(response.getMemoType()); - assertNull(response.getMemo()); - mockWebServer.close(); - } - - @Test - public void testResolveAccountIdSuccessWithMemo() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String successResponse = - "{\"stellar_address\":\"bob*" - + domain - + "\",\"account_id\":\"GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY\"" - + ", \"memo_type\": \"text\", \"memo\": \"test federation\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(successResponse)); - FederationResponse response = - new Federation(client) - .resolveAccountId("GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY", domain); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = - "/federation?type=id&q=GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"; - assertEquals(expectedPath, federationRequest.getPath()); - - assertEquals(response.getStellarAddress(), "bob*" + domain); - assertEquals( - response.getAccountId(), "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"); - assertEquals(response.getMemoType(), "text"); - assertEquals(response.getMemo(), "test federation"); - mockWebServer.close(); - } - - @Test - public void testResolveAccountIdNotFound() throws IOException, InterruptedException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"https://" + domain + "/federation\""; - String notFoundResponse = "{\"code\":\"not_found\",\"message\":\"Account not found\"}"; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody(notFoundResponse)); - - assertThrows( - org.stellar.sdk.federation.exception.NotFoundException.class, - () -> - new Federation(client) - .resolveAccountId( - "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY", domain)); - - RecordedRequest stellarTomlRequest = mockWebServer.takeRequest(); - assertEquals("GET", stellarTomlRequest.getMethod()); - assertEquals("/.well-known/stellar.toml", stellarTomlRequest.getPath()); - - RecordedRequest federationRequest = mockWebServer.takeRequest(); - assertEquals("GET", federationRequest.getMethod()); - String expectedPath = - "/federation?type=id&q=GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY"; - assertEquals(expectedPath, federationRequest.getPath()); - - mockWebServer.shutdown(); - mockWebServer.close(); - } - - @Test - public void testStellarTomlNotFoundInvalidExceptionThrows() throws IOException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("")); - assertThrows( - org.stellar.sdk.federation.exception.StellarTomlNotFoundInvalidException.class, - () -> new Federation(client).resolveAddress("bob*" + domain)); - mockWebServer.close(); - } - - @Test - public void testNoFederationServerExceptionThrows() throws IOException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = ""; - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - assertThrows( - org.stellar.sdk.federation.exception.NoFederationServerException.class, - () -> new Federation(client).resolveAddress("bob*" + domain)); - mockWebServer.close(); - } - - @Test - public void testFederationServerInvalidExceptionThrows() throws IOException { - SSLSocketFactory sslSocketFactory = SslCertificateUtils.createSslSocketFactory(); - X509TrustManager trustAllCerts = SslCertificateUtils.createTrustAllCertsManager(); - MockWebServer mockWebServer = new MockWebServer(); - mockWebServer.useHttps(sslSocketFactory, false); - mockWebServer.start(); - HttpUrl baseUrl = mockWebServer.url(""); - String domain = String.format("%s:%d", baseUrl.host(), baseUrl.port()); - OkHttpClient client = - new OkHttpClient.Builder() - .sslSocketFactory(sslSocketFactory, trustAllCerts) - .hostnameVerifier((hostname, session) -> true) - .build(); - - String stellarToml = "FEDERATION_SERVER = \"http://" + domain + "/federation\""; - - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(stellarToml)); - - assertThrows( - org.stellar.sdk.federation.exception.FederationServerInvalidException.class, - () -> - new Federation(client) - .resolveAccountId( - "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY", domain)); - mockWebServer.shutdown(); - mockWebServer.close(); - } - - @Test - public void testMalformedAddressExceptionThrows() { - assertThrows( - IllegalArgumentException.class, - () -> new Federation().resolveAddress("bob*stellar.org*test")); - assertThrows(IllegalArgumentException.class, () -> new Federation().resolveAddress("bob")); - } -} diff --git a/src/test/kotlin/org/stellar/sdk/federation/FederationTest.kt b/src/test/kotlin/org/stellar/sdk/federation/FederationTest.kt new file mode 100644 index 000000000..4f296c666 --- /dev/null +++ b/src/test/kotlin/org/stellar/sdk/federation/FederationTest.kt @@ -0,0 +1,166 @@ +package org.stellar.sdk.federation + +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import java.net.URLEncoder +import okhttp3.OkHttpClient +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.stellar.sdk.SslCertificateUtils +import org.stellar.sdk.federation.exception.FederationServerInvalidException +import org.stellar.sdk.federation.exception.NoFederationServerException +import org.stellar.sdk.federation.exception.NotFoundException +import org.stellar.sdk.federation.exception.StellarTomlNotFoundInvalidException + +class FederationTest : + FunSpec({ + lateinit var mockWebServer: MockWebServer + lateinit var client: OkHttpClient + lateinit var domain: String + + // Runs before each test block + beforeTest { + val sslSocketFactory = SslCertificateUtils.createSslSocketFactory() + val trustAllCerts = SslCertificateUtils.createTrustAllCertsManager() + mockWebServer = MockWebServer() + mockWebServer.useHttps(sslSocketFactory, false) + mockWebServer.start() + val baseUrl = mockWebServer.url("") + domain = "${baseUrl.host}:${baseUrl.port}" + client = + OkHttpClient.Builder() + .sslSocketFactory(sslSocketFactory, trustAllCerts) + .hostnameVerifier { _, _ -> true } + .build() + } + + // Runs after each test block + afterTest { mockWebServer.shutdown() } + + // A helper function to create a Federation instance + fun federation() = Federation(client) + + test("resolveAddress success") { + val stellarToml = """FEDERATION_SERVER = "https://$domain/federation"""" + val successResponse = + """ + { + "stellar_address": "bob*$domain", + "account_id": "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY" + } + """ + .trimIndent() + + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(stellarToml)) + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(successResponse)) + + val response = federation().resolveAddress("bob*$domain") + + val stellarTomlRequest = mockWebServer.takeRequest() + stellarTomlRequest.method shouldBe "GET" + stellarTomlRequest.path shouldBe "/.well-known/stellar.toml" + + val federationRequest = mockWebServer.takeRequest() + federationRequest.method shouldBe "GET" + federationRequest.path shouldBe + "/federation?type=name&q=bob*${URLEncoder.encode(domain, "UTF-8")}" + + response.stellarAddress shouldBe "bob*$domain" + response.accountId shouldBe "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY" + response.memoType shouldBe null + response.memo shouldBe null + } + + test("resolveAddress success with memo") { + val stellarToml = """FEDERATION_SERVER = "https://$domain/federation"""" + val successResponse = + """ + { + "stellar_address": "bob*$domain", + "account_id": "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY", + "memo_type": "text", + "memo": "test federation" + } + """ + .trimIndent() + + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(stellarToml)) + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(successResponse)) + + val response = federation().resolveAddress("bob*$domain") + + response.stellarAddress shouldBe "bob*$domain" + response.accountId shouldBe "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY" + response.memoType shouldBe "text" + response.memo shouldBe "test federation" + } + + test("resolveAddress not found") { + val stellarToml = """FEDERATION_SERVER = "https://$domain/federation"""" + val notFoundResponse = """{"code":"not_found","message":"Account not found"}""" + + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(stellarToml)) + mockWebServer.enqueue(MockResponse().setResponseCode(404).setBody(notFoundResponse)) + + shouldThrow { federation().resolveAddress("bob*$domain") } + + // Verify requests were made correctly + mockWebServer.requestCount shouldBe 2 + } + + test("resolveAccountId success") { + val accountId = "GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY" + val stellarToml = """FEDERATION_SERVER = "https://$domain/federation"""" + val successResponse = + """ + { + "stellar_address": "bob*$domain", + "account_id": "$accountId" + } + """ + .trimIndent() + + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(stellarToml)) + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(successResponse)) + + val response = federation().resolveAccountId(accountId, domain) + + val stellarTomlRequest = mockWebServer.takeRequest() + stellarTomlRequest.method shouldBe "GET" + stellarTomlRequest.path shouldBe "/.well-known/stellar.toml" + + val federationRequest = mockWebServer.takeRequest() + federationRequest.method shouldBe "GET" + federationRequest.path shouldBe "/federation?type=id&q=$accountId" + + response.accountId shouldBe accountId + response.memo shouldBe null + } + + test("malformed address throws exception") { + shouldThrow { Federation().resolveAddress("bob*stellar.org*test") } + shouldThrow { Federation().resolveAddress("bob") } + } + + test("stellar.toml not found throws exception") { + mockWebServer.enqueue(MockResponse().setResponseCode(404).setBody("")) + + shouldThrow { + federation().resolveAddress("bob*$domain") + } + } + + test("no federation server in stellar.toml throws exception") { + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody("")) + + shouldThrow { federation().resolveAddress("bob*$domain") } + } + + test("federation server using http throws exception") { + val stellarToml = """FEDERATION_SERVER = "http://$domain/federation"""" + mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(stellarToml)) + + shouldThrow { federation().resolveAddress("bob*$domain") } + } + })