File size: 2,803 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { headers } from "next/headers";

import { POST } from "./route";
import { auth } from "@/lib/auth";
import { uploadTooLargeMessage } from "@/lib/video-upload";

vi.mock("next/headers", () => ({
  headers: vi.fn(),
}));

vi.mock("@/lib/auth", () => ({
  auth: {
    api: {
      getSession: vi.fn(),
    },
  },
}));

describe("/api/upload", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    vi.unstubAllEnvs();
    vi.mocked(headers).mockResolvedValue(new Headers());
    vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "owner" } } as never);
    vi.stubEnv("BACKEND_AUTH_SECRET", "secret");
  });

  afterEach(() => vi.unstubAllGlobals());

  it("rejects a declared body over the cap before reading it", async () => {
    const fetchMock = vi.fn();
    vi.stubGlobal("fetch", fetchMock);
    const response = await POST(new Request("http://app/api/upload", {
      method: "POST",
      headers: { "content-length": String(4_000_000_000 + 1024 * 1024 + 1) },
      body: "not-read",
    }));
    expect(response.status).toBe(413);
    await expect(response.json()).resolves.toEqual({ detail: uploadTooLargeMessage(4_000_000_000) });
    expect(fetchMock).not.toHaveBeenCalled();
  });

  it("rejects a chunked upload that does not declare its length", async () => {
    const fetchMock = vi.fn();
    vi.stubGlobal("fetch", fetchMock);
    const response = await POST(new Request("http://app/api/upload", { method: "POST" }));
    expect(response.status).toBe(411);
    expect(fetchMock).not.toHaveBeenCalled();
  });

  it("streams the raw body to the backend without parsing it", async () => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
      video_path: "upload://stored.mp4",
    }), {
      status: 200,
      headers: { "content-type": "application/json" },
    }));
    vi.stubGlobal("fetch", fetchMock);
    const request = new Request("http://app/api/upload", {
      method: "POST",
      headers: {
        "content-length": "10",
        "content-type": "multipart/form-data; boundary=abc",
        "idempotency-key": "retry-key-1",
      },
      body: "file-bytes",
    });
    const formData = vi.spyOn(request, "formData");

    const response = await POST(request);

    expect(response.status).toBe(200);
    expect(formData).not.toHaveBeenCalled();
    expect(fetchMock).toHaveBeenCalledTimes(1);
    const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit & { duplex?: string }];
    expect(url).toMatch(/\/upload$/);
    expect(init.duplex).toBe("half");
    expect(init.body).toBe(request.body);
    expect(init.headers).toEqual(expect.objectContaining({
      "content-type": "multipart/form-data; boundary=abc",
      "content-length": "10",
      "idempotency-key": "retry-key-1",
    }));
  });
});