Skip to content

test(node-http-handler): add test cases for NodeHttpHandler #1652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/poor-chicken-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
75 changes: 75 additions & 0 deletions packages/node-http-handler/src/node-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import https from "https";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { NodeHttpHandler } from "./node-http-handler";
import * as setConnectionTimeoutModule from "./set-connection-timeout";
import * as setSocketTimeoutModule from "./set-socket-timeout";
import { timing } from "./timing";

vi.mock("http", async () => {
Expand Down Expand Up @@ -54,6 +56,8 @@ describe("NodeHttpHandler", () => {
describe("constructor and #handle", () => {
const randomMaxSocket = Math.round(Math.random() * 50) + 1;
const randomSocketAcquisitionWarningTimeout = Math.round(Math.random() * 10000) + 1;
const randomConnectionTimeout = Math.round(Math.random() * 10000) + 1;
const randomRequestTimeout = Math.round(Math.random() * 10000) + 1;

beforeEach(() => {});

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

it.each([
["an options hash", { connectionTimeout: randomConnectionTimeout }],
[
"a provider",
async () => ({
connectionTimeout: randomConnectionTimeout,
}),
],
])("sets connectionTimeout correctly when input is %s", async (_, option) => {
vi.spyOn(setConnectionTimeoutModule, "setConnectionTimeout");
const nodeHttpHandler = new NodeHttpHandler(option);
await nodeHttpHandler.handle({} as any);
expect(vi.mocked(setConnectionTimeoutModule.setConnectionTimeout).mock.calls[0][2]).toBe(
randomConnectionTimeout
);
});

it.each([
["an options hash", { requestTimeout: randomRequestTimeout }],
[
"a provider",
async () => ({
requestTimeout: randomRequestTimeout,
}),
],
])("sets requestTimeout correctly when input is %s", async (_, option) => {
vi.spyOn(setSocketTimeoutModule, "setSocketTimeout");
const nodeHttpHandler = new NodeHttpHandler(option);
await nodeHttpHandler.handle({} as any);
expect(vi.mocked(setSocketTimeoutModule.setSocketTimeout).mock.calls[0][2]).toBe(randomRequestTimeout);
});

it.each([
["an options hash", { httpAgent: new http.Agent({ keepAlive: false, maxSockets: randomMaxSocket }) }],
[
Expand Down Expand Up @@ -211,6 +247,45 @@ describe("NodeHttpHandler", () => {
});
});

describe("create", () => {
const randomRequestTimeout = Math.round(Math.random() * 10000) + 1;

it.each([
["existing handler instance", new NodeHttpHandler()],
[
"custom HttpHandler object",
{
handle: vi.fn(),
} as any,
],
])("returns the input handler when passed %s", (_, handler) => {
const result = NodeHttpHandler.create(handler);
expect(result).toBe(handler);
});

it.each([
["undefined", undefined],
["an empty options hash", {}],
["empty provider", async () => undefined],
])("creates new handler instance when input is %s", async (_, input) => {
const result = NodeHttpHandler.create(input);
expect(result).toBeInstanceOf(NodeHttpHandler);
});

it.each([
["an options hash", { requestTimeout: randomRequestTimeout }],
["a provider", async () => ({ requestTimeout: randomRequestTimeout })],
])("creates new handler instance with config when input is %s", async (_, input) => {
const result = NodeHttpHandler.create(input);
expect(result).toBeInstanceOf(NodeHttpHandler);

// Verify configuration by calling handle
await result.handle({} as any);

expect(result.httpHandlerConfigs().requestTimeout).toBe(randomRequestTimeout);
});
});

describe("#destroy", () => {
it("should be callable and return nothing", () => {
const nodeHttpHandler = new NodeHttpHandler();
Expand Down
Loading