2024-04-07 07:30:49 +02:00
|
|
|
import { describe, expect, it, test } from "bun:test";
|
2024-03-08 06:34:50 +01:00
|
|
|
import { RequestParser } from "..";
|
|
|
|
|
|
|
|
|
|
describe("RequestParser", () => {
|
2024-04-07 07:30:49 +02:00
|
|
|
describe("Should parse query parameters correctly", () => {
|
|
|
|
|
test("With text parameters", async () => {
|
|
|
|
|
const request = new Request(
|
|
|
|
|
"http://localhost?param1=value1¶m2=value2",
|
|
|
|
|
);
|
2024-04-25 20:50:30 +02:00
|
|
|
const result = await new RequestParser(request).parseQuery<{
|
2024-04-07 07:30:49 +02:00
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("With Array", async () => {
|
|
|
|
|
const request = new Request(
|
|
|
|
|
"http://localhost?test[]=value1&test[]=value2",
|
|
|
|
|
);
|
2024-04-25 20:50:30 +02:00
|
|
|
const result = await new RequestParser(request).parseQuery<{
|
2024-04-07 07:30:49 +02:00
|
|
|
test: string[];
|
|
|
|
|
}>();
|
2024-04-25 20:50:30 +02:00
|
|
|
expect(result?.test).toEqual(["value1", "value2"]);
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-18 01:47:03 +02:00
|
|
|
test("With Array of objects", async () => {
|
|
|
|
|
const request = new Request(
|
|
|
|
|
"http://localhost?test[][key]=value1&test[][value]=value2",
|
|
|
|
|
);
|
2024-04-25 20:50:30 +02:00
|
|
|
const result = await new RequestParser(request).parseQuery<{
|
2024-04-18 01:47:03 +02:00
|
|
|
test: { key: string; value: string }[];
|
|
|
|
|
}>();
|
2024-04-25 20:50:30 +02:00
|
|
|
expect(result?.test).toEqual([{ key: "value1", value: "value2" }]);
|
2024-04-18 01:47:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("With Array of multiple objects", async () => {
|
|
|
|
|
const request = new Request(
|
|
|
|
|
"http://localhost?test[][key]=value1&test[][value]=value2&test[][key]=value3&test[][value]=value4",
|
|
|
|
|
);
|
2024-04-25 20:50:30 +02:00
|
|
|
const result = await new RequestParser(request).parseQuery<{
|
2024-04-18 01:47:03 +02:00
|
|
|
test: { key: string[]; value: string[] }[];
|
|
|
|
|
}>();
|
2024-04-25 20:50:30 +02:00
|
|
|
expect(result?.test).toEqual([
|
2024-04-18 01:47:03 +02:00
|
|
|
{ key: ["value1", "value3"], value: ["value2", "value4"] },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("With both at once", async () => {
|
|
|
|
|
const request = new Request(
|
|
|
|
|
"http://localhost?param1=value1¶m2=value2&test[]=value1&test[]=value2",
|
|
|
|
|
);
|
2024-04-25 20:50:30 +02:00
|
|
|
const result = await new RequestParser(request).parseQuery<{
|
2024-04-07 07:30:49 +02:00
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
test: string[];
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
param1: "value1",
|
|
|
|
|
param2: "value2",
|
|
|
|
|
test: ["value1", "value2"],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
it("should parse JSON body correctly", async () => {
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ param1: "value1", param2: "value2" }),
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
it("should handle invalid JSON body", async () => {
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: "invalid json",
|
|
|
|
|
});
|
2024-04-14 12:36:25 +02:00
|
|
|
const result = new RequestParser(request).toObject<{
|
2024-04-07 07:30:49 +02:00
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
}>();
|
2024-04-14 12:36:25 +02:00
|
|
|
expect(result).rejects.toThrow();
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
describe("should parse form data correctly", () => {
|
|
|
|
|
test("With basic text parameters", async () => {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("param1", "value1");
|
|
|
|
|
formData.append("param2", "value2");
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("With File object", async () => {
|
|
|
|
|
const file = new File(["content"], "filename.txt", {
|
|
|
|
|
type: "text/plain",
|
|
|
|
|
});
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("file", file);
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
file: File;
|
|
|
|
|
}>();
|
2024-04-25 20:50:30 +02:00
|
|
|
expect(result?.file).toBeInstanceOf(File);
|
|
|
|
|
expect(await result?.file?.text()).toEqual("content");
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("With Array", async () => {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("test[]", "value1");
|
|
|
|
|
formData.append("test[]", "value2");
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
test: string[];
|
|
|
|
|
}>();
|
2024-04-25 20:50:30 +02:00
|
|
|
expect(result?.test).toEqual(["value1", "value2"]);
|
2024-04-07 07:30:49 +02:00
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("With all three at once", async () => {
|
|
|
|
|
const file = new File(["content"], "filename.txt", {
|
|
|
|
|
type: "text/plain",
|
|
|
|
|
});
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("param1", "value1");
|
|
|
|
|
formData.append("param2", "value2");
|
|
|
|
|
formData.append("file", file);
|
|
|
|
|
formData.append("test[]", "value1");
|
|
|
|
|
formData.append("test[]", "value2");
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
file: File;
|
|
|
|
|
test: string[];
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
|
param1: "value1",
|
|
|
|
|
param2: "value2",
|
|
|
|
|
file: file,
|
|
|
|
|
test: ["value1", "value2"],
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
|
2024-04-07 07:30:49 +02:00
|
|
|
test("URL Encoded", async () => {
|
|
|
|
|
const request = new Request("http://localhost", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
|
|
|
|
body: "param1=value1¶m2=value2",
|
|
|
|
|
});
|
|
|
|
|
const result = await new RequestParser(request).toObject<{
|
|
|
|
|
param1: string;
|
|
|
|
|
param2: string;
|
|
|
|
|
}>();
|
|
|
|
|
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-03-08 06:34:50 +01:00
|
|
|
});
|