RoomApiTest.kt
composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/together/RoomApiTest.kt
1package eu.friedcutlet.yokan.services.together23import eu.friedcutlet.yokan.services.http.HttpResponse4import eu.friedcutlet.yokan.services.http.HttpService5import eu.friedcutlet.yokan.services.http.RecordingHttpService6import kotlinx.coroutines.CancellationException7import kotlinx.coroutines.test.runTest8import kotlin.test.Test9import kotlin.test.assertEquals10import kotlin.test.assertFailsWith11import kotlin.test.assertTrue1213class RoomApiTest {14 @Test fun allRestMethodsRejectRevokedAccountsBeforeDispatch() = runTest {15 var calls = 016 val http = HttpService { calls++; error("Revoked REST dispatch") }17 val api = RoomApi(http) { false }18 assertFailsWith<CancellationException> { api.create(Hub, Media) }19 assertFailsWith<CancellationException> { api.info(Hub, "abcdef") }20 assertFailsWith<CancellationException> { api.close(Hub, "abcdef") }21 assertEquals(0, calls)22 }2324 @Test fun allRestMethodsRejectResponsesAfterRevocationBeforeParsingOrReportingErrors() = runTest {25 for (method in listOf("POST", "GET", "DELETE")) {26 var current = true27 val http = HttpService { request ->28 assertEquals(method, request.method); current = false29 HttpResponse(401, "invalid response")30 }31 val api = RoomApi(http) { current }32 assertFailsWith<CancellationException> {33 when (method) {34 "POST" -> api.create(Hub, Media)35 "GET" -> api.info(Hub, "abcdef")36 else -> api.close(Hub, "abcdef")37 }38 }39 }40 }4142 @Test fun standaloneRestCompatibilityRetainsPolicyHeadersCodesAndErrors() = runTest {43 val http = RecordingHttpService { request ->44 when (request.method) {45 "POST" -> HttpResponse(201, "{\"code\":\"ABCDEF\",\"join_url\":\"join\"}")46 "GET" -> HttpResponse(404, "{}")47 else -> HttpResponse(204)48 }49 }50 val api = RoomApi(http)51 assertEquals("ABCDEF", api.create(Hub, Media, policy = "open", playlistPolicy = "all", maxParticipants = 8).getOrThrow().code)52 assertEquals("No room with code ABCDEF.", api.info(Hub, "abcdef").exceptionOrNull()?.message)53 assertTrue(api.close(Hub, "abcdef").isSuccess)54 assertEquals(listOf("POST", "GET", "DELETE"), http.requests.map { it.method })55 http.requests.forEach { assertEquals("Bearer token", it.headers["Authorization"]); assertEquals("device", it.headers["X-Device"]) }56 assertTrue(requireNotNull(http.requests.first().body).contains("\"max_participants\":8"))57 assertTrue(http.requests.last().url.endsWith("/rooms/ABCDEF"))58 }5960 private companion object {61 val Hub = HubSession("https://hub.test", "token", "device")62 val Media = RoomMedia("manga", 1, "/title")63 }64}65