package eu.friedcutlet.yokan.services.http import io.ktor.client.engine.HttpClientEngineBase import io.ktor.client.engine.HttpClientEngineCapability import io.ktor.client.engine.HttpClientEngineConfig import io.ktor.client.engine.callContext import io.ktor.client.plugins.HttpTimeoutCapability import io.ktor.client.request.HttpRequestData import io.ktor.client.request.HttpResponseData import io.ktor.http.Headers import io.ktor.http.HttpProtocolVersion import io.ktor.http.HttpStatusCode import io.ktor.util.date.GMTDate import io.ktor.utils.io.ByteChannel import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.InternalAPI import io.ktor.utils.io.writeFully import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeout import kotlin.test.Test import kotlin.test.assertContentEquals import kotlin.test.assertEquals class KtorHttpServiceStreamTest { @Test fun streamHandsOverChunksBeforeTheBodyEnds() = runBlocking { val body = ByteChannel() val service = KtorHttpService(ChannelEngine(body)) val firstChunk = CompletableDeferred() val headers = CompletableDeferred() val streaming = async { service.stream(HttpRequest(url = "https://example.test/episode.mp4", onStreamResponse = { headers.complete(it) })) { assertEquals(true, headers.isCompleted) firstChunk.complete(it) } } try { assertEquals(200, withTimeout(5_000) { headers.await() }.statusCode) body.writeFully("abc".encodeToByteArray()) body.flush() // A buffering client only returns once the body is closed, so this would time out. assertContentEquals("abc".encodeToByteArray(), withTimeout(5_000) { firstChunk.await() }) body.flushAndClose() assertEquals(200, withTimeout(5_000) { streaming.await() }.statusCode) } finally { streaming.cancel() service.close() } } @Test fun streamLeavesServerErrorRetriesToItsCallerAndBuffersTheErrorBody() = runBlocking { val engine = UnavailableEngine() val service = KtorHttpService(engine) try { var chunks = 0 val response = withTimeout(5_000) { service.stream(HttpRequest(url = "https://example.test/page.jpg")) { chunks++ } } assertEquals(503, response.statusCode) assertEquals(1, engine.calls) // The error page is handed back for inspection, never written through onChunk. assertEquals(0, chunks) assertContentEquals(UnavailableEngine.PAGE, response.body) } finally { service.close() } } @OptIn(InternalAPI::class) private class ChannelEngine(private val body: ByteReadChannel) : HttpClientEngineBase("channel-test") { override val config = HttpClientEngineConfig() override val supportedCapabilities: Set> = setOf(HttpTimeoutCapability) override suspend fun execute(data: HttpRequestData): HttpResponseData = HttpResponseData(HttpStatusCode.OK, GMTDate(), Headers.Empty, HttpProtocolVersion.HTTP_1_1, body, callContext()) } @OptIn(InternalAPI::class) private class UnavailableEngine : HttpClientEngineBase("unavailable-test") { var calls = 0 override val config = HttpClientEngineConfig() override val supportedCapabilities: Set> = setOf(HttpTimeoutCapability) override suspend fun execute(data: HttpRequestData): HttpResponseData { calls += 1 return HttpResponseData(HttpStatusCode.ServiceUnavailable, GMTDate(), Headers.Empty, HttpProtocolVersion.HTTP_1_1, ByteReadChannel(PAGE), callContext()) } companion object { val PAGE = "Just a moment...".encodeToByteArray() } } }