mc-publish/tests/unit/utils/net/http-request-body.spec.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-01-11 14:57:34 -05:00
import { Readable } from "node:stream";
import { Blob } from "@/utils/net/blob";
2024-01-14 15:22:20 -05:00
import { FormData } from "@/utils/net/form-data";
2024-01-11 14:57:34 -05:00
import { isHttpRequestBody, isStreamableHttpRequestBody } from "@/utils/net/http-request-body";
describe("isHttpRequestBody", () => {
test("returns true when body is a string", () => {
const body = "string body";
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a Blob", () => {
const body = new Blob(["blob content"]);
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a Buffer", () => {
const body = Buffer.from("buffer content");
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a URLSearchParams", () => {
const body = new URLSearchParams({ param: "value" });
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a FormData", () => {
const body = new FormData();
body.append("key", "value");
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a ReadableStream", () => {
const body = Readable.from("stream content");
expect(isHttpRequestBody(body)).toBe(true);
});
test("returns false when body is not a valid HTTP request body type", () => {
expect(isHttpRequestBody({ key: "value" })).toBe(false);
expect(isHttpRequestBody(["foo"])).toBe(false);
expect(isHttpRequestBody(null)).toBe(false);
expect(isHttpRequestBody(undefined)).toBe(false);
});
});
describe("isStreamableHttpRequestBody", () => {
test("returns true when body is a Blob", () => {
const body = new Blob(["blob content"]);
expect(isStreamableHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a Buffer", () => {
const body = Buffer.from("buffer content");
expect(isStreamableHttpRequestBody(body)).toBe(true);
});
test("returns true when body is a ReadableStream", () => {
const body = Readable.from("stream content");
expect(isStreamableHttpRequestBody(body)).toBe(true);
});
test("returns false when body is not a valid streamable HTTP request body type", () => {
expect(isStreamableHttpRequestBody({ key: "value" })).toBe(false);
expect(isStreamableHttpRequestBody(["foo"])).toBe(false);
expect(isStreamableHttpRequestBody("string body")).toBe(false);
expect(isStreamableHttpRequestBody(new URLSearchParams({ param: "value" }))).toBe(false);
expect(isStreamableHttpRequestBody(null)).toBe(false);
expect(isStreamableHttpRequestBody(undefined)).toBe(false);
});
});