package org.meshbay.client import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Test import org.meshbay.client.bridge.Refused import org.meshbay.client.hub.HubClient class HubClientTest { private fun hub(base: String = "") = HubClient(FakePrefs().apply { map["hubBase"] = base }) @Test fun `an empty address is refused, not stored as no hub`() { val e = assertThrows(Refused::class.java) { hub().setBase(" ") } assertEquals("Enter the address of a hub.", e.message) } @Test fun `plain http is refused except to loopback`() { for (url in listOf("http://example.org", "http://10.0.2.2:8770", "ftp://x", "http://192.168.1.2")) { val e = assertThrows(Refused::class.java) { hub().setBase(url) } assertEquals(url, "The hub address must be https", e.message) } } @Test fun `the page reaches the signed-in hub and nowhere else`() { val h = hub("https://hub.example") for (url in listOf("https://other.example/v1/x", "http://hub.example/v1/x", "https://hub.example:8443/v1/x", "https://hub.example.evil/v1/x", "not a url")) { assertThrows(url, Refused::class.java) { h.fetch(url, JSONObject()) } } } @Test fun `nothing is fetched before a hub is set`() { assertThrows(Refused::class.java) { hub("").fetch("https://hub.example/v1/x", null) } } @Test fun `unreachable hubs are described in words somebody can act on`() { assertTrue(HubClient.describeUnreachable("https://h", java.net.ConnectException()).startsWith("Nothing is listening at https://h")) assertTrue(HubClient.describeUnreachable("https://h", java.net.UnknownHostException()).contains("could not be found")) assertTrue(HubClient.describeUnreachable("https://h", javax.net.ssl.SSLHandshakeException("x")).contains("does not speak https")) } }