mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Replace eslint and prettier with Biome
This commit is contained in:
parent
4a5a2ea590
commit
af0d627f19
199 changed files with 16493 additions and 16361 deletions
|
|
@ -1,158 +1,158 @@
|
|||
import { describe, it, expect, test } from "bun:test";
|
||||
import { describe, expect, it, test } from "bun:test";
|
||||
import { RequestParser } from "..";
|
||||
|
||||
describe("RequestParser", () => {
|
||||
describe("Should parse query parameters correctly", () => {
|
||||
test("With text parameters", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?param1=value1¶m2=value2"
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
}>();
|
||||
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
||||
});
|
||||
describe("Should parse query parameters correctly", () => {
|
||||
test("With text parameters", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?param1=value1¶m2=value2",
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
}>();
|
||||
expect(result).toEqual({ param1: "value1", param2: "value2" });
|
||||
});
|
||||
|
||||
test("With Array", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?test[]=value1&test[]=value2"
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
test: string[];
|
||||
}>();
|
||||
expect(result.test).toEqual(["value1", "value2"]);
|
||||
});
|
||||
test("With Array", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?test[]=value1&test[]=value2",
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
test: string[];
|
||||
}>();
|
||||
expect(result.test).toEqual(["value1", "value2"]);
|
||||
});
|
||||
|
||||
test("With both at once", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?param1=value1¶m2=value2&test[]=value1&test[]=value2"
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
test: string[];
|
||||
}>();
|
||||
expect(result).toEqual({
|
||||
param1: "value1",
|
||||
param2: "value2",
|
||||
test: ["value1", "value2"],
|
||||
});
|
||||
});
|
||||
});
|
||||
test("With both at once", async () => {
|
||||
const request = new Request(
|
||||
"http://localhost?param1=value1¶m2=value2&test[]=value1&test[]=value2",
|
||||
);
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
test: string[];
|
||||
}>();
|
||||
expect(result).toEqual({
|
||||
param1: "value1",
|
||||
param2: "value2",
|
||||
test: ["value1", "value2"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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" });
|
||||
});
|
||||
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" });
|
||||
});
|
||||
|
||||
it("should handle invalid JSON body", async () => {
|
||||
const request = new Request("http://localhost", {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: "invalid json",
|
||||
});
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
}>();
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
it("should handle invalid JSON body", async () => {
|
||||
const request = new Request("http://localhost", {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: "invalid json",
|
||||
});
|
||||
const result = await new RequestParser(request).toObject<{
|
||||
param1: string;
|
||||
param2: string;
|
||||
}>();
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
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" });
|
||||
});
|
||||
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" });
|
||||
});
|
||||
|
||||
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;
|
||||
}>();
|
||||
expect(result.file).toBeInstanceOf(File);
|
||||
expect(await result.file?.text()).toEqual("content");
|
||||
});
|
||||
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;
|
||||
}>();
|
||||
expect(result.file).toBeInstanceOf(File);
|
||||
expect(await result.file?.text()).toEqual("content");
|
||||
});
|
||||
|
||||
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[];
|
||||
}>();
|
||||
expect(result.test).toEqual(["value1", "value2"]);
|
||||
});
|
||||
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[];
|
||||
}>();
|
||||
expect(result.test).toEqual(["value1", "value2"]);
|
||||
});
|
||||
|
||||
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"],
|
||||
});
|
||||
});
|
||||
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"],
|
||||
});
|
||||
});
|
||||
|
||||
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" });
|
||||
});
|
||||
});
|
||||
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" });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue