Skip to content

Commit ace4136

Browse files
test(node-http-handler): add test cases for NodeHttpHandler (#1652)
* test(node-http-handler): add test for connectionTimeout configuration * test(node-http-handler): add test for requestTimeout configuration * test(node-http-handler): add tests for NodeHttpHandler.create() static method * chore: add empty changeset * style: fix code format
1 parent 9f58ff3 commit ace4136

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.changeset/poor-chicken-wave.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/node-http-handler/src/node-http-handler.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import https from "https";
44
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
55

66
import { NodeHttpHandler } from "./node-http-handler";
7+
import * as setConnectionTimeoutModule from "./set-connection-timeout";
8+
import * as setSocketTimeoutModule from "./set-socket-timeout";
79
import { timing } from "./timing";
810

911
vi.mock("http", async () => {
@@ -54,6 +56,8 @@ describe("NodeHttpHandler", () => {
5456
describe("constructor and #handle", () => {
5557
const randomMaxSocket = Math.round(Math.random() * 50) + 1;
5658
const randomSocketAcquisitionWarningTimeout = Math.round(Math.random() * 10000) + 1;
59+
const randomConnectionTimeout = Math.round(Math.random() * 10000) + 1;
60+
const randomRequestTimeout = Math.round(Math.random() * 10000) + 1;
5761

5862
beforeEach(() => {});
5963

@@ -110,6 +114,38 @@ describe("NodeHttpHandler", () => {
110114
expect(vi.mocked(timing.setTimeout).mock.calls[0][1]).toBe(randomSocketAcquisitionWarningTimeout);
111115
});
112116

117+
it.each([
118+
["an options hash", { connectionTimeout: randomConnectionTimeout }],
119+
[
120+
"a provider",
121+
async () => ({
122+
connectionTimeout: randomConnectionTimeout,
123+
}),
124+
],
125+
])("sets connectionTimeout correctly when input is %s", async (_, option) => {
126+
vi.spyOn(setConnectionTimeoutModule, "setConnectionTimeout");
127+
const nodeHttpHandler = new NodeHttpHandler(option);
128+
await nodeHttpHandler.handle({} as any);
129+
expect(vi.mocked(setConnectionTimeoutModule.setConnectionTimeout).mock.calls[0][2]).toBe(
130+
randomConnectionTimeout
131+
);
132+
});
133+
134+
it.each([
135+
["an options hash", { requestTimeout: randomRequestTimeout }],
136+
[
137+
"a provider",
138+
async () => ({
139+
requestTimeout: randomRequestTimeout,
140+
}),
141+
],
142+
])("sets requestTimeout correctly when input is %s", async (_, option) => {
143+
vi.spyOn(setSocketTimeoutModule, "setSocketTimeout");
144+
const nodeHttpHandler = new NodeHttpHandler(option);
145+
await nodeHttpHandler.handle({} as any);
146+
expect(vi.mocked(setSocketTimeoutModule.setSocketTimeout).mock.calls[0][2]).toBe(randomRequestTimeout);
147+
});
148+
113149
it.each([
114150
["an options hash", { httpAgent: new http.Agent({ keepAlive: false, maxSockets: randomMaxSocket }) }],
115151
[
@@ -211,6 +247,45 @@ describe("NodeHttpHandler", () => {
211247
});
212248
});
213249

250+
describe("create", () => {
251+
const randomRequestTimeout = Math.round(Math.random() * 10000) + 1;
252+
253+
it.each([
254+
["existing handler instance", new NodeHttpHandler()],
255+
[
256+
"custom HttpHandler object",
257+
{
258+
handle: vi.fn(),
259+
} as any,
260+
],
261+
])("returns the input handler when passed %s", (_, handler) => {
262+
const result = NodeHttpHandler.create(handler);
263+
expect(result).toBe(handler);
264+
});
265+
266+
it.each([
267+
["undefined", undefined],
268+
["an empty options hash", {}],
269+
["empty provider", async () => undefined],
270+
])("creates new handler instance when input is %s", async (_, input) => {
271+
const result = NodeHttpHandler.create(input);
272+
expect(result).toBeInstanceOf(NodeHttpHandler);
273+
});
274+
275+
it.each([
276+
["an options hash", { requestTimeout: randomRequestTimeout }],
277+
["a provider", async () => ({ requestTimeout: randomRequestTimeout })],
278+
])("creates new handler instance with config when input is %s", async (_, input) => {
279+
const result = NodeHttpHandler.create(input);
280+
expect(result).toBeInstanceOf(NodeHttpHandler);
281+
282+
// Verify configuration by calling handle
283+
await result.handle({} as any);
284+
285+
expect(result.httpHandlerConfigs().requestTimeout).toBe(randomRequestTimeout);
286+
});
287+
});
288+
214289
describe("#destroy", () => {
215290
it("should be callable and return nothing", () => {
216291
const nodeHttpHandler = new NodeHttpHandler();

0 commit comments

Comments
 (0)