IsolatedBrowserHttpEngineTest.kt
composeApp/src/wasmJsTest/kotlin/eu/friedcutlet/yokan/services/http/IsolatedBrowserHttpEngineTest.kt
1@file:OptIn(kotlin.js.ExperimentalWasmJsInterop::class)23package eu.friedcutlet.yokan.services.http45import kotlinx.coroutines.CompletableDeferred6import kotlinx.coroutines.async7import kotlinx.coroutines.awaitCancellation8import kotlinx.coroutines.cancelAndJoin9import kotlinx.coroutines.test.runTest10import kotlin.test.*1112class IsolatedBrowserHttpEngineTest {13 @Test fun largeResponseStreamsWithBackpressureAndOmitsAmbientCredentials() = runTest {14 installFetchFixture(2100)15 val service = KtorHttpService(IsolatedBrowserHttpEngine())16 try {17 var received = 0L18 var maximumAhead = 0L19 val response = service.stream(HttpRequest("https://browser-stream.test/large")) { chunk ->20 assertTrue(chunk.all { it == 7.toByte() })21 received += chunk.size22 maximumAhead = maxOf(maximumAhead, producedChunks() * 32768L - received)23 }24 assertEquals(200, response.statusCode)25 assertEquals(2100L * 32768, received)26 assertTrue(received > 64L * 1024 * 1024)27 assertTrue(maximumAhead < 8L * 32768, "Fetch must backpressure the consumer; ahead=$maximumAhead")28 assertEquals("omit", requestCredentials())29 assertEquals("no-store", requestCache())30 assertTrue(fixtureAborted())31 assertTrue(fixtureReleased())32 } finally { service.closeAndJoin(); restoreFetch() }33 }3435 @Test fun cancellingTheConsumerAbortsAndJoinsTheNativeReader() = runTest {36 installFetchFixture(2100)37 val service = KtorHttpService(IsolatedBrowserHttpEngine())38 val entered = CompletableDeferred<Unit>()39 try {40 val transfer = async {41 service.stream(HttpRequest("https://browser-stream.test/cancel")) {42 entered.complete(Unit)43 awaitCancellation()44 }45 }46 entered.await()47 transfer.cancelAndJoin()48 service.closeAndJoin()49 assertTrue(fixtureAborted())50 assertTrue(fixtureReleased())51 assertTrue(producedChunks() < 20)52 } finally { service.closeAndJoin(); restoreFetch() }53 }54}5556private fun installFetchFixture(chunks: Int): Unit = js(57 """{58 const original = globalThis.fetch;59 const fixture = { original, produced: 0, aborted: false, cancelled: false, credentials: '', cache: '' };60 globalThis.yokanFetchFixture = fixture;61 globalThis.fetch = async (url, options) => {62 fixture.credentials = options.credentials;63 fixture.cache = options.cache;64 let controller;65 options.signal.addEventListener('abort', () => {66 fixture.aborted = true;67 if (controller && !fixture.cancelled && fixture.produced < chunks) controller.error(new DOMException('Aborted', 'AbortError'));68 });69 const body = new ReadableStream({70 start(value) { controller = value; },71 pull(value) {72 if (fixture.produced === chunks) { value.close(); return; }73 fixture.produced++;74 value.enqueue(new Uint8Array(32768).fill(7));75 },76 cancel() { fixture.cancelled = true; }77 });78 fixture.body = body;79 return new Response(body, { status: 200, headers: { 'Content-Type': 'application/octet-stream' } });80 };81 }""",82)83private fun producedChunks(): Int = js("globalThis.yokanFetchFixture.produced")84private fun fixtureAborted(): Boolean = js("globalThis.yokanFetchFixture.aborted")85private fun fixtureReleased(): Boolean = js("!globalThis.yokanFetchFixture.body.locked")86private fun requestCredentials(): String = js("globalThis.yokanFetchFixture.credentials")87private fun requestCache(): String = js("globalThis.yokanFetchFixture.cache")88private fun restoreFetch(): Unit = js("{ globalThis.fetch = globalThis.yokanFetchFixture.original; delete globalThis.yokanFetchFixture; }")89