diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index c8c2a52a2c..adeaf466d2 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -713,11 +713,17 @@ Deno.test( "[node/http] client end with callback", { permissions: { net: true } }, async () => { + let received = false; + const ac = new AbortController(); + const server = Deno.serve({ port: 5928, signal: ac.signal }, (_req) => { + received = true; + return new Response("hello"); + }); const promise = deferred(); let body = ""; const request = http.request( - "http://localhost:4545/http_version", + "http://localhost:5928/", (resp) => { resp.on("data", (chunk) => { body += chunk; @@ -729,10 +735,14 @@ Deno.test( }, ); request.on("error", promise.reject); - request.end(); + request.end(() => { + assert(received); + }); await promise; + ac.abort(); + await server.finished; - assertEquals(body, "HTTP/1.1"); + assertEquals(body, "hello"); }, ); diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 391906c40e..17ab8ce31f 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -629,14 +629,13 @@ class ClientRequest extends OutgoingMessage { core.tryClose(this._bodyWriteRid); } - - try { - cb?.(); - } catch (_) { - // - } })(), ]); + try { + cb?.(); + } catch (_) { + // + } if (this._timeout) { this._timeout.removeEventListener("abort", this._timeoutCb); webClearTimeout(this._timeout[timerId]);