1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(node/http): call callback after request is sent (#19871)

Fixes #19762
This commit is contained in:
Leo Kettmeir 2023-07-19 01:30:19 +02:00 committed by GitHub
parent cfb9478a43
commit bf4e99cbd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View file

@ -713,11 +713,17 @@ Deno.test(
"[node/http] client end with callback", "[node/http] client end with callback",
{ permissions: { net: true } }, { permissions: { net: true } },
async () => { 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(); const promise = deferred();
let body = ""; let body = "";
const request = http.request( const request = http.request(
"http://localhost:4545/http_version", "http://localhost:5928/",
(resp) => { (resp) => {
resp.on("data", (chunk) => { resp.on("data", (chunk) => {
body += chunk; body += chunk;
@ -729,10 +735,14 @@ Deno.test(
}, },
); );
request.on("error", promise.reject); request.on("error", promise.reject);
request.end(); request.end(() => {
assert(received);
});
await promise; await promise;
ac.abort();
await server.finished;
assertEquals(body, "HTTP/1.1"); assertEquals(body, "hello");
}, },
); );

View file

@ -629,14 +629,13 @@ class ClientRequest extends OutgoingMessage {
core.tryClose(this._bodyWriteRid); core.tryClose(this._bodyWriteRid);
} }
})(),
]);
try { try {
cb?.(); cb?.();
} catch (_) { } catch (_) {
// //
} }
})(),
]);
if (this._timeout) { if (this._timeout) {
this._timeout.removeEventListener("abort", this._timeoutCb); this._timeout.removeEventListener("abort", this._timeoutCb);
webClearTimeout(this._timeout[timerId]); webClearTimeout(this._timeout[timerId]);