Skip to content

Commit 1357b1f

Browse files
committed
fix(api): http-request-node test
1 parent bbfc7c1 commit 1357b1f

1 file changed

Lines changed: 125 additions & 1 deletion

File tree

apps/api/src/nodes/net/http-request-node.test.ts

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
import { Node } from "@dafthunk/types";
2-
import { describe, expect, it } from "vitest";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
33

44
import { NodeContext } from "../types";
55
import { HttpRequestNode } from "./http-request-node";
66

7+
// Mock fetch
8+
global.fetch = vi.fn();
9+
710
describe("HttpRequestNode", () => {
11+
beforeEach(() => {
12+
vi.clearAllMocks();
13+
});
14+
815
it("should make a GET request successfully", async () => {
16+
const mockResponse = {
17+
status: 200,
18+
statusText: "OK",
19+
headers: new Map([
20+
["content-type", "application/json"],
21+
["server", "httpbin.org"],
22+
]),
23+
text: vi
24+
.fn()
25+
.mockResolvedValue(JSON.stringify({ url: "https://httpbin.org/get" })),
26+
};
27+
28+
(global.fetch as any).mockResolvedValue(mockResponse);
29+
930
const nodeId = "http-request";
1031
const node = new HttpRequestNode({
1132
nodeId,
@@ -31,6 +52,20 @@ describe("HttpRequestNode", () => {
3152
});
3253

3354
it("should make a POST request with body", async () => {
55+
const mockResponse = {
56+
status: 200,
57+
statusText: "OK",
58+
headers: new Map([
59+
["content-type", "application/json"],
60+
["server", "httpbin.org"],
61+
]),
62+
text: vi
63+
.fn()
64+
.mockResolvedValue(JSON.stringify({ json: { test: "data" } })),
65+
};
66+
67+
(global.fetch as any).mockResolvedValue(mockResponse);
68+
3469
const nodeId = "http-request";
3570
const node = new HttpRequestNode({
3671
nodeId,
@@ -60,6 +95,22 @@ describe("HttpRequestNode", () => {
6095
});
6196

6297
it("should handle query parameters", async () => {
98+
const mockResponse = {
99+
status: 200,
100+
statusText: "OK",
101+
headers: new Map([
102+
["content-type", "application/json"],
103+
["server", "httpbin.org"],
104+
]),
105+
text: vi.fn().mockResolvedValue(
106+
JSON.stringify({
107+
args: { param1: "value1", param2: "value2" },
108+
})
109+
),
110+
};
111+
112+
(global.fetch as any).mockResolvedValue(mockResponse);
113+
63114
const nodeId = "http-request";
64115
const node = new HttpRequestNode({
65116
nodeId,
@@ -86,6 +137,25 @@ describe("HttpRequestNode", () => {
86137
});
87138

88139
it("should handle custom headers", async () => {
140+
const mockResponse = {
141+
status: 200,
142+
statusText: "OK",
143+
headers: new Map([
144+
["content-type", "application/json"],
145+
["server", "httpbin.org"],
146+
]),
147+
text: vi.fn().mockResolvedValue(
148+
JSON.stringify({
149+
headers: {
150+
"X-Custom-Header": "test-value",
151+
"User-Agent": "test-agent",
152+
},
153+
})
154+
),
155+
};
156+
157+
(global.fetch as any).mockResolvedValue(mockResponse);
158+
89159
const nodeId = "http-request";
90160
const node = new HttpRequestNode({
91161
nodeId,
@@ -118,6 +188,10 @@ describe("HttpRequestNode", () => {
118188
});
119189

120190
it("should handle timeout", async () => {
191+
(global.fetch as any).mockRejectedValue(
192+
new Error("The user aborted a request.")
193+
);
194+
121195
const nodeId = "http-request";
122196
const node = new HttpRequestNode({
123197
nodeId,
@@ -180,6 +254,20 @@ describe("HttpRequestNode", () => {
180254
});
181255

182256
it("should handle PUT request", async () => {
257+
const mockResponse = {
258+
status: 200,
259+
statusText: "OK",
260+
headers: new Map([
261+
["content-type", "application/json"],
262+
["server", "httpbin.org"],
263+
]),
264+
text: vi
265+
.fn()
266+
.mockResolvedValue(JSON.stringify({ json: { update: "data" } })),
267+
};
268+
269+
(global.fetch as any).mockResolvedValue(mockResponse);
270+
183271
const nodeId = "http-request";
184272
const node = new HttpRequestNode({
185273
nodeId,
@@ -206,6 +294,18 @@ describe("HttpRequestNode", () => {
206294
});
207295

208296
it("should handle DELETE request", async () => {
297+
const mockResponse = {
298+
status: 200,
299+
statusText: "OK",
300+
headers: new Map([
301+
["content-type", "application/json"],
302+
["server", "httpbin.org"],
303+
]),
304+
text: vi.fn().mockResolvedValue(JSON.stringify({})),
305+
};
306+
307+
(global.fetch as any).mockResolvedValue(mockResponse);
308+
209309
const nodeId = "http-request";
210310
const node = new HttpRequestNode({
211311
nodeId,
@@ -227,6 +327,18 @@ describe("HttpRequestNode", () => {
227327
});
228328

229329
it("should handle 404 error", async () => {
330+
const mockResponse = {
331+
status: 404,
332+
statusText: "NOT FOUND",
333+
headers: new Map([
334+
["content-type", "text/html"],
335+
["server", "httpbin.org"],
336+
]),
337+
text: vi.fn().mockResolvedValue("Not Found"),
338+
};
339+
340+
(global.fetch as any).mockResolvedValue(mockResponse);
341+
230342
const nodeId = "http-request";
231343
const node = new HttpRequestNode({
232344
nodeId,
@@ -248,6 +360,18 @@ describe("HttpRequestNode", () => {
248360
});
249361

250362
it("should handle 500 error", async () => {
363+
const mockResponse = {
364+
status: 500,
365+
statusText: "INTERNAL SERVER ERROR",
366+
headers: new Map([
367+
["content-type", "text/html"],
368+
["server", "httpbin.org"],
369+
]),
370+
text: vi.fn().mockResolvedValue("Internal Server Error"),
371+
};
372+
373+
(global.fetch as any).mockResolvedValue(mockResponse);
374+
251375
const nodeId = "http-request";
252376
const node = new HttpRequestNode({
253377
nodeId,

0 commit comments

Comments
 (0)