summaryrefslogtreecommitdiff
path: root/tests/unit_node/http_test.ts
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-07-03 19:00:39 +0530
committerGitHub <noreply@github.com>2024-07-03 19:00:39 +0530
commit496ea5903bbe8e86185c30fc6f478b0092d64d65 (patch)
tree12fa4b4057f51fa9cea3218f24df2305642302f5 /tests/unit_node/http_test.ts
parentdadc606419096325ef0b03b6216ee20bef442045 (diff)
fix(ext/node): don't wait for end() call to send http client request (#24390)
Closes https://github.com/denoland/deno/issues/24232 Closes https://github.com/denoland/deno/issues/24215
Diffstat (limited to 'tests/unit_node/http_test.ts')
-rw-r--r--tests/unit_node/http_test.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/unit_node/http_test.ts b/tests/unit_node/http_test.ts
index 4c3d39780..aeb92f129 100644
--- a/tests/unit_node/http_test.ts
+++ b/tests/unit_node/http_test.ts
@@ -5,8 +5,11 @@ import http, { type RequestOptions } from "node:http";
import url from "node:url";
import https from "node:https";
import net from "node:net";
+import fs from "node:fs";
+
import { assert, assertEquals, fail } from "@std/assert/mod.ts";
import { assertSpyCalls, spy } from "@std/testing/mock.ts";
+import { fromFileUrl, relative } from "@std/path/mod.ts";
import { gzip } from "node:zlib";
import { Buffer } from "node:buffer";
@@ -1179,3 +1182,72 @@ Deno.test("[node/http] client closing a streaming response doesn't terminate ser
assertEquals(server.listening, false);
clearInterval(interval!);
});
+
+Deno.test("[node/http] http.request() post streaming body works", async () => {
+ const server = http.createServer((req, res) => {
+ if (req.method === "POST") {
+ let receivedBytes = 0;
+ req.on("data", (chunk) => {
+ receivedBytes += chunk.length;
+ });
+ req.on("end", () => {
+ res.writeHead(200, { "Content-Type": "application/json" });
+ res.end(JSON.stringify({ bytes: receivedBytes }));
+ });
+ } else {
+ res.writeHead(405, { "Content-Type": "text/plain" });
+ res.end("Method Not Allowed");
+ }
+ });
+
+ const deferred = Promise.withResolvers<void>();
+ const timeout = setTimeout(() => {
+ deferred.reject(new Error("timeout"));
+ }, 5000);
+ server.listen(0, () => {
+ // deno-lint-ignore no-explicit-any
+ const port = (server.address() as any).port;
+ const filePath = relative(
+ Deno.cwd(),
+ fromFileUrl(new URL("./testdata/lorem_ipsum_512kb.txt", import.meta.url)),
+ );
+ const contentLength = 524289;
+
+ const options = {
+ hostname: "localhost",
+ port: port,
+ path: "/",
+ method: "POST",
+ headers: {
+ "Content-Type": "application/octet-stream",
+ "Content-Length": contentLength,
+ },
+ };
+
+ const req = http.request(options, (res) => {
+ let responseBody = "";
+ res.on("data", (chunk) => {
+ responseBody += chunk;
+ });
+
+ res.on("end", () => {
+ const response = JSON.parse(responseBody);
+ assertEquals(res.statusCode, 200);
+ assertEquals(response.bytes, contentLength);
+ deferred.resolve();
+ });
+ });
+
+ req.on("error", (e) => {
+ console.error(`Problem with request: ${e.message}`);
+ });
+
+ const readStream = fs.createReadStream(filePath);
+ readStream.pipe(req);
+ });
+ await deferred.promise;
+ assertEquals(server.listening, true);
+ server.close();
+ clearTimeout(timeout);
+ assertEquals(server.listening, false);
+});