|
| 1 | +import { createApp, toWebHandler } from "vinxi/http"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { serializeToJSONString } from "./serialization"; |
| 4 | +import { |
| 5 | + ARGS_PART, |
| 6 | + createUploadSerializer, |
| 7 | + FILE_PART, |
| 8 | + readUploadArgs, |
| 9 | + UPLOAD_HEADER |
| 10 | +} from "./uploads"; |
| 11 | + |
| 12 | +const serverFn = vi.hoisted(() => ({ |
| 13 | + args: [] as unknown[] |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("solidstart:server-fn-manifest", () => ({ |
| 17 | + default: { |
| 18 | + fn: { |
| 19 | + functionName: "fn", |
| 20 | + importer: async () => ({ |
| 21 | + fn: (...args: unknown[]) => { |
| 22 | + serverFn.args = args; |
| 23 | + return "done"; |
| 24 | + } |
| 25 | + }) |
| 26 | + } |
| 27 | + } |
| 28 | +})); |
| 29 | +vi.mock("../server/handler", () => ({ getExpectedRedirectStatus: () => 302 })); |
| 30 | +vi.mock("../server/pageEvent", () => ({ createPageEvent: async () => {} })); |
| 31 | + |
| 32 | +const { default: serverHandler } = await import("./server-handler"); |
| 33 | +const { createServerReference } = await import("./server-runtime"); |
| 34 | +const handle = toWebHandler(createApp().use(serverHandler)); |
| 35 | + |
| 36 | +let sent: Request | undefined; |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + serverFn.args = []; |
| 40 | + sent = undefined; |
| 41 | + vi.stubEnv("SERVER_BASE_URL", "http://localhost:3000"); |
| 42 | + vi.stubGlobal("fetch", async (url: string, init: RequestInit) => { |
| 43 | + sent = new Request(url, init); |
| 44 | + return handle(sent.clone()); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +/** Calls the server function through the client, and returns what it received. */ |
| 49 | +async function roundTrip(...args: unknown[]) { |
| 50 | + const fn = createServerReference(() => {}, "fn", "fn") as unknown as ( |
| 51 | + ...args: unknown[] |
| 52 | + ) => Promise<unknown>; |
| 53 | + expect(await fn(...args)).toBe("done"); |
| 54 | + return serverFn.args; |
| 55 | +} |
| 56 | + |
| 57 | +describe("server function uploads", () => { |
| 58 | + it("sends a file as the only argument", async () => { |
| 59 | + const [received] = await roundTrip(new File(["hello"], "hello.txt", { type: "text/plain" })); |
| 60 | + |
| 61 | + expect(sent!.headers.get(UPLOAD_HEADER)).toBe("true"); |
| 62 | + expect(received).toBeInstanceOf(File); |
| 63 | + expect((received as File).name).toBe("hello.txt"); |
| 64 | + expect((received as File).type).toBe("text/plain"); |
| 65 | + expect(await (received as File).text()).toBe("hello"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("sends files next to other arguments and inside them", async () => { |
| 69 | + const file = new File(["a"], "a.txt"); |
| 70 | + const form = new FormData(); |
| 71 | + form.set("avatar", new File(["png"], "avatar.png")); |
| 72 | + |
| 73 | + const [first, meta, received] = await roundTrip( |
| 74 | + file, |
| 75 | + { tags: ["x"], list: [new Blob(["b"])], map: new Map([["c", new File(["c"], "c.txt")]]) }, |
| 76 | + form |
| 77 | + ); |
| 78 | + |
| 79 | + expect(await (first as File).text()).toBe("a"); |
| 80 | + const value = meta as { tags: string[]; list: Blob[]; map: Map<string, File> }; |
| 81 | + expect(value.tags).toEqual(["x"]); |
| 82 | + expect(await value.list[0]!.text()).toBe("b"); |
| 83 | + expect(await value.map.get("c")!.text()).toBe("c"); |
| 84 | + expect(await ((received as FormData).get("avatar") as File).text()).toBe("png"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("sends a file used twice once, and keeps it the same value", async () => { |
| 88 | + const file = new File(["once"], "once.txt"); |
| 89 | + |
| 90 | + const [first, second] = await roundTrip(file, { again: file }); |
| 91 | + |
| 92 | + const form = await sent!.clone().formData(); |
| 93 | + expect(form.has(`${FILE_PART}0`)).toBe(true); |
| 94 | + expect(form.has(`${FILE_PART}1`)).toBe(false); |
| 95 | + expect((second as { again: File }).again).toBe(first); |
| 96 | + }); |
| 97 | + |
| 98 | + it("copies a blob from another frame", async () => { |
| 99 | + const source = new Blob(["other"], { type: "text/plain" }); |
| 100 | + // Not an instance of this realm's Blob, as one from another frame isn't. |
| 101 | + const foreign = { |
| 102 | + size: source.size, |
| 103 | + type: source.type, |
| 104 | + arrayBuffer: () => source.arrayBuffer(), |
| 105 | + slice: () => source.slice() |
| 106 | + }; |
| 107 | + |
| 108 | + const [received] = await roundTrip(foreign, 1); |
| 109 | + |
| 110 | + expect(await (received as Blob).text()).toBe("other"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("keeps calls without files as serialized text", async () => { |
| 114 | + const [value, count] = await roundTrip({ a: 1 }, 2); |
| 115 | + |
| 116 | + expect(sent!.headers.get("x-serialized")).toBe("true"); |
| 117 | + expect(sent!.headers.get(UPLOAD_HEADER)).toBeNull(); |
| 118 | + expect(value).toEqual({ a: 1 }); |
| 119 | + expect(count).toBe(2); |
| 120 | + }); |
| 121 | + |
| 122 | + it("rejects an upload whose part is missing", async () => { |
| 123 | + const form = new FormData(); |
| 124 | + form.set( |
| 125 | + ARGS_PART, |
| 126 | + await serializeToJSONString([new File(["x"], "x.txt")], [createUploadSerializer([])]) |
| 127 | + ); |
| 128 | + |
| 129 | + await expect(readUploadArgs(form)).rejects.toThrow(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("rejects an upload without its arguments", async () => { |
| 133 | + const form = new FormData(); |
| 134 | + form.set(`${FILE_PART}0`, new File(["x"], "x.txt")); |
| 135 | + |
| 136 | + await expect(readUploadArgs(form)).rejects.toThrow("Malformed server function upload."); |
| 137 | + }); |
| 138 | + |
| 139 | + it("does not read upload indexes from a body that is not multipart", async () => { |
| 140 | + const { deserializeFromJSONString } = await import("./serialization"); |
| 141 | + const body = await serializeToJSONString( |
| 142 | + [new File(["x"], "x.txt")], |
| 143 | + [createUploadSerializer([])] |
| 144 | + ); |
| 145 | + |
| 146 | + // A plain body is read without the upload plugin, so the index has no meaning there. |
| 147 | + await expect(deserializeFromJSONString(body)).rejects.toThrow(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments