Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/features/workspaces/files/workspace-file-processor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

import { requestWorkspaceFileProcessor } from "#/features/workspaces/files/workspace-file-processor";

const getRandom = vi.hoisted(() => vi.fn());

vi.mock("@cloudflare/containers", () => ({
Container: class {},
getRandom,
}));

describe("requestWorkspaceFileProcessor", () => {
beforeEach(() => {
getRandom.mockReset();
});

it("declares a known content length so Workers can stream the R2 body", async () => {
const fetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
getRandom.mockResolvedValue({
fetch,
startAndWaitForPorts: vi.fn().mockResolvedValue(undefined),
});

await requestWorkspaceFileProcessor({} as Cloudflare.Env, {
body: streamOf(new Uint8Array([1, 2, 3])),
contentType: "application/pdf",
path: "/preview/pdf",
sizeBytes: 3,
});

expect(fetch).toHaveBeenCalledOnce();
const request = fetch.mock.calls[0]![0] as Request;
expect(request.headers.get("content-length")).toBe("3");
expect(request.headers.get("x-file-size")).toBe("3");
expect(request.headers.get("content-type")).toBe("application/pdf");
});

it("encodes the optional file name header", async () => {
const fetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
getRandom.mockResolvedValue({
fetch,
startAndWaitForPorts: vi.fn().mockResolvedValue(undefined),
});

await requestWorkspaceFileProcessor({} as Cloudflare.Env, {
body: streamOf(new Uint8Array([1])),
contentType: "image/png",
fileName: "my file.png",
path: "/preview/image",
sizeBytes: 1,
});

const request = fetch.mock.calls[0]![0] as Request;
expect(request.headers.get("x-file-name")).toBe("my%20file.png");
});
});

function streamOf(bytes: Uint8Array) {
const body = new Response(bytes.slice().buffer).body;

if (!body) {
throw new Error("Test stream was not created.");
}

return body;
}
3 changes: 3 additions & 0 deletions src/features/workspaces/files/workspace-file-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export async function requestWorkspaceFileProcessor(
});

const headers = new Headers({
// Cloudflare Workers refuses to send a streamed request body without a known
// length, so declare it up front from the source object's verified size.
"content-length": String(input.sizeBytes),
"content-type": input.contentType,
"x-file-size": String(input.sizeBytes),
});
Expand Down
Loading