package eu.friedcutlet.yokan.services.media.player import java.net.Socket import java.net.URI import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertNotEquals import kotlinx.coroutines.runBlocking class JvmMediaStreamProxyTest { @Test fun reusesRegistrationsWithoutSharingDifferentAuthenticationHeaders() = runBlocking { val proxy = JvmMediaStreamProxy() try { val headers = mutableMapOf("Authorization" to "first") val first = proxy.proxyUrl("https://example.test/video.m3u8", headers) repeat(100) { assertEquals(first, proxy.proxyUrl("https://example.test/video.m3u8", headers)) } headers["Authorization"] = "second" assertNotEquals(first, proxy.proxyUrl("https://example.test/video.m3u8", headers)) assertEquals(first, proxy.proxyUrl("https://example.test/video.m3u8", mapOf("Authorization" to "first"))) } finally { proxy.closeAndJoin() } } @Test fun stoppingClosesIdleClientsAndRejectsRestart() = runBlocking { val proxy = JvmMediaStreamProxy() try { val url = URI(proxy.proxyUrl("https://example.test/video.mp4", mapOf("Referer" to "test"))) Socket(url.host, url.port).use { client -> client.soTimeout = 2_000 proxy.stop() // An accepted socket is closed; an unaccepted connection may be reset by the OS. val closed = try { client.getInputStream().read() == -1 } catch (_: java.net.SocketException) { true } kotlin.test.assertTrue(closed) } assertFailsWith { proxy.proxyUrl("https://example.test/video.mp4", mapOf("Referer" to "test")) } assertFailsWith { proxy.proxyUrl("https://example.test/video.mp4", emptyMap()) } } finally { proxy.closeAndJoin() } } }