package eu.friedcutlet.yokan.services.together import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.HttpService import eu.friedcutlet.yokan.services.http.RecordingHttpService import kotlinx.coroutines.CancellationException import kotlinx.coroutines.test.runTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertTrue class RoomApiTest { @Test fun allRestMethodsRejectRevokedAccountsBeforeDispatch() = runTest { var calls = 0 val http = HttpService { calls++; error("Revoked REST dispatch") } val api = RoomApi(http) { false } assertFailsWith { api.create(Hub, Media) } assertFailsWith { api.info(Hub, "abcdef") } assertFailsWith { api.close(Hub, "abcdef") } assertEquals(0, calls) } @Test fun allRestMethodsRejectResponsesAfterRevocationBeforeParsingOrReportingErrors() = runTest { for (method in listOf("POST", "GET", "DELETE")) { var current = true val http = HttpService { request -> assertEquals(method, request.method); current = false HttpResponse(401, "invalid response") } val api = RoomApi(http) { current } assertFailsWith { when (method) { "POST" -> api.create(Hub, Media) "GET" -> api.info(Hub, "abcdef") else -> api.close(Hub, "abcdef") } } } } @Test fun standaloneRestCompatibilityRetainsPolicyHeadersCodesAndErrors() = runTest { val http = RecordingHttpService { request -> when (request.method) { "POST" -> HttpResponse(201, "{\"code\":\"ABCDEF\",\"join_url\":\"join\"}") "GET" -> HttpResponse(404, "{}") else -> HttpResponse(204) } } val api = RoomApi(http) assertEquals("ABCDEF", api.create(Hub, Media, policy = "open", playlistPolicy = "all", maxParticipants = 8).getOrThrow().code) assertEquals("No room with code ABCDEF.", api.info(Hub, "abcdef").exceptionOrNull()?.message) assertTrue(api.close(Hub, "abcdef").isSuccess) assertEquals(listOf("POST", "GET", "DELETE"), http.requests.map { it.method }) http.requests.forEach { assertEquals("Bearer token", it.headers["Authorization"]); assertEquals("device", it.headers["X-Device"]) } assertTrue(requireNotNull(http.requests.first().body).contains("\"max_participants\":8")) assertTrue(http.requests.last().url.endsWith("/rooms/ABCDEF")) } private companion object { val Hub = HubSession("https://hub.test", "token", "device") val Media = RoomMedia("manga", 1, "/title") } }