From 8a416a5ba22bb8b9e6d9eb48e78a167fdea6d64c Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 23 Apr 2021 11:34:04 +0100 Subject: fix(runtime/js/http): cancel body on response failure (#10225) --- cli/tests/unit/http_test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'cli/tests') diff --git a/cli/tests/unit/http_test.ts b/cli/tests/unit/http_test.ts index d661acbd1..ab43323bd 100644 --- a/cli/tests/unit/http_test.ts +++ b/cli/tests/unit/http_test.ts @@ -228,3 +228,47 @@ unitTest( await promise; }, ); + +unitTest( + { perms: { net: true } }, + async function httpServerCancelBodyOnResponseFailure() { + const promise = (async () => { + const listener = Deno.listen({ port: 4501 }); + const conn = await listener.accept(); + const httpConn = Deno.serveHttp(conn); + const event = await httpConn.nextRequest(); + assert(event); + const { respondWith } = event; + let cancelReason = null; + const responseError = await assertThrowsAsync( + async () => { + let interval = 0; + await respondWith( + new Response( + new ReadableStream({ + start(controller) { + interval = setInterval(() => { + const message = `data: ${Date.now()}\n\n`; + controller.enqueue(new TextEncoder().encode(message)); + }, 200); + }, + cancel(reason) { + cancelReason = reason; + clearInterval(interval); + }, + }), + ), + ); + }, + Deno.errors.Http, + ); + assertEquals(cancelReason, responseError); + httpConn.close(); + listener.close(); + })(); + + const resp = await fetch("http://127.0.0.1:4501/"); + await resp.body!.cancel(); + await promise; + }, +); -- cgit v1.2.3