Yokan / rebuild
Course 1 8624561

IsolatedPlatformHttpTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/http/IsolatedPlatformHttpTest.kt

test / fixture37 lines
1package eu.friedcutlet.yokan.services.http23import com.sun.net.httpserver.HttpServer4import eu.friedcutlet.yokan.domain.ProfileId5import kotlinx.coroutines.runBlocking6import java.net.InetSocketAddress7import kotlin.test.*89class IsolatedPlatformHttpTest {10    @Test fun isolatedEnginesDoNotReplayResponseCookiesAndRetainExplicitOwnerCredentials() = runBlocking {11        val received = mutableListOf<Pair<String?, String?>>()12        val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 0)13        server.createContext("/") { exchange ->14            synchronized(received) { received.add(exchange.requestHeaders.getFirst("Cookie") to exchange.requestHeaders.getFirst("Authorization")) }15            exchange.responseHeaders.add("Set-Cookie", "ambient=shared; Path=/")16            val bytes = "ok".encodeToByteArray()17            exchange.sendResponseHeaders(200, bytes.size.toLong())18            exchange.responseBody.use { it.write(bytes) }19        }20        server.start()21        val a = createPlatformHttpService(ProfileId("p_${"a".repeat(32)}"))22        val b = createPlatformHttpService(ProfileId("p_${"b".repeat(32)}"))23        val url = "http://127.0.0.1:${server.address.port}/"24        try {25            assertEquals(200, a.request(HttpRequest(url)).statusCode)26            assertEquals(200, a.request(HttpRequest(url)).statusCode)27            b.request(HttpRequest(url, headers = mapOf("Cookie" to "owner=B", "Authorization" to "Bearer profile-B")))28            a.request(HttpRequest(url, headers = mapOf("Cookie" to "owner=A", "Authorization" to "Bearer profile-A")))29            assertEquals(listOf(null to null, null to null, "owner=B" to "Bearer profile-B", "owner=A" to "Bearer profile-A"), received)30            a.closeAndJoin()31            assertEquals("ok", b.request(HttpRequest(url)).body)32            assertEquals(null to null, received.last())33        } finally {34            a.closeAndJoin(); b.closeAndJoin(); server.stop(0)35        }36    }37}38

Search chapters, reference and source filenames.