Client cannot access local server inside android:...
# ktor
c
Client cannot access local server inside android:
Copy code
@RunWith(AndroidJUnit4::class)
class ServerInstrumentedTest {
    @Test
    fun should_test_server_client() = runTest {
        val app = ApplicationProvider.getApplicationContext<MainApplication>()
        val preference = app.preference
        val setting = Settings(preference)
        setting.localServerPort = 0
        val db = ZzDatabase.getDatabase(app.applicationContext)
        db.peer().clearData()
        val port = setting.localServerPort
        PeerServer.start(app, "127.0.0.1", port)
        val timeStamp = Time.now()
        val peer = Peer(
            "127.0.0.1",
            port,
            AddressType.IPV4,
            createdAt = timeStamp, updatedAt = timeStamp
        )

        runBlocking {
            val peerClient = PeerClient(app, peer)
            val response01 = peerClient.client.get("<http://www.baidu.com/>")
//            val response01 = peerClient.client.get(peer.baseUrl + "/")
            assertEquals(HttpStatusCode.OK, response01.status)
//            assertEquals("Hello From ZZ!\n", response01.bodyAsText())
        }
}
It can access out side servers, but not the local one. and this code can not be put inside android InstrumentedTest for my server needs reading configurations from android database
Copy code
@Test
    fun testRoot() = testApplication {
        application {
            module()
        }
        val response = client.get("/")
        assertEquals(HttpStatusCode.OK, response.status)
        assertEquals("Hello, world!", response.bodyAsText())
    }
a
That's because the Android emulator's network is isolated from the local host network. See this answer for more information.
c
Seems not the same problem. because my tests run inside android emulators or testing phones too. They should be in the same network.
a
Are you able to access the server with other HTTP clients, for example, HttpURLConnection?
c
Yes. It works.
HttpURLConnection returns HTTP 200