// ! API 参考 https://httpbin.org/#/ import { expect, describe, it } from "vitest"; import { Link, fetchRequester, Blob, File } from "../../src/index"; Link.registerRequester(fetchRequester); const root = __Root__; describe("基础 HTTP测试", function () { it("get 测试", async () => { const params = { folder: ["2", "3"], file: "This is a test File", }; const link = new Link({ url: root + "/get", params, }); return link.send().then(async (res) => { const data = await res.json(); expect(data.args).to.eql(params); }); }); it("post json 测试", async () => { const params = { folder: ["2", "3"], file: "This is a test File", }; const link = new Link({ url: root + "/post", method: "post", params, body: { type: "json", data: params, }, }); return link.send().then(async (res) => { const data: { data: string; json: string; files: any; form: any; args: any; } = await res.json(); expect(data.args).to.eql(params); expect(data.json).to.eql(params); }); }); it("post form 测试", async () => { const text = "This is A File"; // ! form 内传输文件均需要使用 File 包裹 const file = new File([new Blob([text])], "index.js"); const file2 = new File([Buffer.from(text)], "file2.mp3"); const params = { folder: ["2", "3"], files: "This is a test File", }; const link = new Link({ url: root + "/post", method: "post", params, body: { type: "form", data: { ...params, file, file2 }, }, }); return link.send().then(async (res) => { const data: { data: string; files: any; form: any; args: any; } = await res.json(); expect(data.args).to.eql(params); expect(data.form).to.eql({ ...params, folder: params.folder.join(), }); expect(data.files.file).to.eql(text); expect(data.files.file2).to.eql(text); }); }); });