aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/node-http.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/bun.js/node-http.test.ts')
-rw-r--r--test/bun.js/node-http.test.ts145
1 files changed, 123 insertions, 22 deletions
diff --git a/test/bun.js/node-http.test.ts b/test/bun.js/node-http.test.ts
index 3ba383c2e..7818fba62 100644
--- a/test/bun.js/node-http.test.ts
+++ b/test/bun.js/node-http.test.ts
@@ -81,6 +81,7 @@ describe("node:http", () => {
describe("request", () => {
let server;
+ let serverPort;
let timer = null;
beforeAll(() => {
server = createServer((req, res) => {
@@ -89,7 +90,7 @@ describe("node:http", () => {
if (reqUrl.pathname === "/redirect") {
// Temporary redirect
res.writeHead(301, {
- Location: "http://localhost:8126/redirected",
+ Location: `http://localhost:${serverPort}/redirected`,
});
res.end("Got redirect!\n");
return;
@@ -137,7 +138,9 @@ describe("node:http", () => {
res.end("Hello World");
}
});
- server.listen(8126);
+ server.listen({ port: 0 }, (_, __, port) => {
+ serverPort = port;
+ });
});
afterAll(() => {
server.close();
@@ -145,7 +148,7 @@ describe("node:http", () => {
});
it("should make a standard GET request when passed string as first arg", done => {
- const req = request("http://localhost:8126", res => {
+ const req = request(`http://localhost:${serverPort}`, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -160,8 +163,24 @@ describe("node:http", () => {
req.end();
});
+ it("should make a https:// GET request when passed string as first arg", done => {
+ const req = request("https://example.com", res => {
+ let data = "";
+ res.setEncoding("utf8");
+ res.on("data", chunk => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ expect(data).toContain("This domain is for use in illustrative examples in documents");
+ done();
+ });
+ res.on("error", err => done(err));
+ });
+ req.end();
+ });
+
it("should make a POST request when provided POST method, even without a body", done => {
- const req = request({ host: "localhost", port: 8126, method: "POST" }, res => {
+ const req = request({ host: "localhost", port: serverPort, method: "POST" }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -177,7 +196,7 @@ describe("node:http", () => {
});
it("should correctly handle a POST request with a body", done => {
- const req = request({ host: "localhost", port: 8126, method: "POST" }, res => {
+ const req = request({ host: "localhost", port: serverPort, method: "POST" }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -194,7 +213,7 @@ describe("node:http", () => {
});
it("should noop request.setSocketKeepAlive without error", () => {
- const req = request("http://localhost:8126");
+ const req = request(`http://localhost:${serverPort}`);
req.setSocketKeepAlive(true, 1000);
req.end();
expect(true).toBe(true);
@@ -209,7 +228,7 @@ describe("node:http", () => {
const req1 = request(
{
host: "localhost",
- port: 8126,
+ port: serverPort,
path: "/timeout",
timeout: 500,
},
@@ -222,7 +241,7 @@ describe("node:http", () => {
const req2 = request(
{
host: "localhost",
- port: 8126,
+ port: serverPort,
path: "/timeout",
},
res => {
@@ -242,7 +261,7 @@ describe("node:http", () => {
const req1Done = createDone();
const req2Done = createDone();
- const req1 = request("http://localhost:8126/pathTest", res => {
+ const req1 = request(`http://localhost:${serverPort}/pathTest`, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -255,7 +274,7 @@ describe("node:http", () => {
res.on("error", err => req1Done(err));
});
- const req2 = request("http://localhost:8126", { path: "/pathTest" }, res => {
+ const req2 = request(`http://localhost:${serverPort}`, { path: "/pathTest" }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -276,7 +295,7 @@ describe("node:http", () => {
});
it("should emit response when response received", done => {
- const req = request("http://localhost:8126");
+ const req = request(`http://localhost:${serverPort}`);
req.on("response", res => {
expect(res.statusCode).toBe(200);
@@ -287,7 +306,7 @@ describe("node:http", () => {
// NOTE: Node http.request doesn't follow redirects by default
it("should handle redirects properly", done => {
- const req = request("http://localhost:8126/redirect", res => {
+ const req = request(`http://localhost:${serverPort}/redirect`, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -303,7 +322,7 @@ describe("node:http", () => {
});
it("should correctly attach headers to request", done => {
- const req = request({ host: "localhost", port: 8126, headers: { "X-Test": "test" } }, res => {
+ const req = request({ host: "localhost", port: serverPort, headers: { "X-Test": "test" } }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -320,7 +339,7 @@ describe("node:http", () => {
});
it("should correct casing of method param", done => {
- const req = request({ host: "localhost", port: 8126, method: "get" }, res => {
+ const req = request({ host: "localhost", port: serverPort, method: "get" }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -336,7 +355,7 @@ describe("node:http", () => {
});
it("should allow for port as a string", done => {
- const req = request({ host: "localhost", port: "8126", method: "GET" }, res => {
+ const req = request({ host: "localhost", port: `${serverPort}`, method: "GET" }, res => {
let data = "";
res.setEncoding("utf8");
res.on("data", chunk => {
@@ -350,10 +369,51 @@ describe("node:http", () => {
});
req.end();
});
+
+ it("should allow us to pass a URL object", done => {
+ const req = request(new URL(`http://localhost:${serverPort}`), { method: "POST" }, res => {
+ let data = "";
+ res.setEncoding("utf8");
+ res.on("data", chunk => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ expect(data).toBe("Hello WorldPOST\nHello World");
+ done();
+ });
+ res.on("error", err => done(err));
+ });
+ req.write("Hello World");
+ req.end();
+ });
+
+ it("should ignore body when method is GET/HEAD/OPTIONS", done => {
+ const createDone = createDoneDotAll(done);
+ const methods = ["GET", "HEAD", "OPTIONS"];
+ const dones = {};
+ for (const method of methods) {
+ dones[method] = createDone();
+ }
+ for (const method of methods) {
+ const req = request(`http://localhost:${serverPort}`, { method }, res => {
+ let data = "";
+ res.setEncoding("utf8");
+ res.on("data", chunk => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ expect(data).toBe(method === "GET" ? "Maybe GET maybe not\nHello World" : "");
+ dones[method]();
+ });
+ res.on("error", err => dones[method](err));
+ });
+ req.write("BODY");
+ req.end();
+ }
+ });
});
describe("signal", () => {
-
it("should abort and close the server", done => {
const server = createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
@@ -361,19 +421,19 @@ describe("node:http", () => {
});
//force timeout to not hang tests
- const interval = setTimeout(()=> {
+ const interval = setTimeout(() => {
expect(false).toBe(true);
server.close();
- done()
+ done();
}, 100);
-
+
const signal = AbortSignal.timeout(30);
- signal.addEventListener("abort", ()=> {
+ signal.addEventListener("abort", () => {
clearTimeout(interval);
expect(true).toBe(true);
- done()
+ done();
});
-
+
server.listen({ signal, port: 8130 });
});
});
@@ -457,4 +517,45 @@ describe("node:http", () => {
expect(globalAgent instanceof Agent).toBe(true);
});
});
+
+ describe("ClientRequest.signal", () => {
+ let server;
+ let server_port;
+ let server_host;
+ beforeAll(() => {
+ server = createServer((req, res) => {
+ Bun.sleep(10).then(() => {
+ res.writeHead(200, { "Content-Type": "text/plain" });
+ res.end("Hello World");
+ });
+ });
+ server.listen({ port: 0 }, (_err, host, port) => {
+ server_port = port;
+ server_host = host;
+ });
+ });
+ afterAll(() => {
+ server.close();
+ });
+ it("should attempt to make a standard GET request and abort", done => {
+ get(`http://127.0.0.1:${server_port}`, { signal: AbortSignal.timeout(5) }, res => {
+ let data = "";
+ res.setEncoding("utf8");
+ res.on("data", chunk => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ expect(true).toBeFalsy();
+ done();
+ });
+ res.on("error", _ => {
+ expect(true).toBeFalsy();
+ done();
+ });
+ }).on("error", err => {
+ expect(err?.name).toBe("AbortError");
+ done();
+ });
+ });
+ });
});