Skip to content

Commit

Permalink
Add test for fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Nov 22, 2024
1 parent 90be296 commit a3625b7
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions library/helpers/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as t from "tap";
import { createServer, Server } from "http";
import { fetch } from "./fetch";

let server: Server;

// Start an HTTP server before running tests
t.beforeEach(async () => {
server = createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (chunk: Buffer) => chunks.push(chunk));
req.on("end", () => {
const body = Buffer.concat(chunks).toString();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ method: req.method, body }));
});
});
await new Promise((resolve) => server.listen(0, resolve)); // Start the server asynchronously
});

// Stop the server after running tests
t.afterEach(async () => {
await new Promise((resolve) => server.close(resolve)); // Stop the server asynchronously
});

t.test("should make a GET request", async (t) => {
const url = new URL(`http://localhost:${(server.address() as any).port}`);
const response = await fetch({ url });

t.equal(response.statusCode, 200);
t.same(JSON.parse(response.body), { method: "GET", body: "" });
});

t.test("should make a POST request with body", async (t) => {
const url = new URL(`http://localhost:${(server.address() as any).port}`);
const response = await fetch({
url,
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ key: "value" }),
});

t.equal(response.statusCode, 200);
t.same(JSON.parse(response.body), {
method: "POST",
body: '{"key":"value"}',
});
});

0 comments on commit a3625b7

Please sign in to comment.