forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-uploads-test.ts
135 lines (119 loc) · 4.5 KB
/
file-uploads-test.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import * as fs from "fs/promises";
import * as path from "path";
import { test, expect } from "@playwright/test";
import { createFixture, createAppFixture, js } from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
import { PlaywrightFixture } from "./helpers/playwright-fixture";
test.describe("file-uploads", () => {
let fixture: Fixture;
let appFixture: AppFixture;
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/fileUploadHandler.js": js`
import * as path from "path";
import {
unstable_composeUploadHandlers as composeUploadHandlers,
unstable_createFileUploadHandler as createFileUploadHandler,
unstable_createMemoryUploadHandler as createMemoryUploadHandler,
} from "@remix-run/node";
export let uploadHandler = composeUploadHandlers(
createFileUploadHandler({
directory: path.resolve(__dirname, "..", "uploads"),
maxPartSize: 10_000, // 10kb
// you probably want to avoid conflicts in production
// do not set to false or passthrough filename in real
// applications.
avoidFileConflicts: false,
file: ({ filename }) => filename
}),
createMemoryUploadHandler(),
);
`,
"app/routes/file-upload.jsx": js`
import {
unstable_parseMultipartFormData as parseMultipartFormData,
} from "@remix-run/node";
import { Form, useActionData } from "@remix-run/react";
import { uploadHandler } from "~/fileUploadHandler";
export let action = async ({ request }) => {
try {
let formData = await parseMultipartFormData(request, uploadHandler);
if (formData.get("test") !== "hidden") {
return { errorMessage: "hidden field not in form data" };
}
let file = formData.get("file");
if (typeof file === "string" || !file) {
return { errorMessage: "invalid file type" };
}
return { name: file.name, size: file.size };
} catch (error) {
return { errorMessage: error.message };
}
};
export default function Upload() {
return (
<>
<Form method="post" encType="multipart/form-data">
<label htmlFor="file">Choose a file:</label>
<input type="file" id="file" name="file" />
<input type="hidden" name="test" value="hidden" />
<button type="submit">Submit</button>
</Form>
<pre>{JSON.stringify(useActionData(), null, 2)}</pre>
</>
);
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(async () => {
await appFixture.close();
});
test("handles files under upload size limit", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
let uploadFile = path.join(
fixture.projectDir,
"toUpload",
"underLimit.txt"
);
let uploadData = Array(1_000).fill("a").join(""); // 1kb
await fs
.mkdir(path.dirname(uploadFile), { recursive: true })
.catch(() => {});
await fs.writeFile(uploadFile, uploadData, "utf8");
await app.goto("/file-upload");
await app.uploadFile("#file", uploadFile);
await app.clickSubmitButton("/file-upload");
expect(await app.getHtml("pre")).toBe(`<pre>
{
"name": "underLimit.txt",
"size": 1000
}</pre
>`);
let written = await fs.readFile(
path.join(fixture.projectDir, "uploads/underLimit.txt"),
"utf8"
);
expect(written).toBe(uploadData);
});
test("rejects files over upload size limit", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
let uploadFile = path.join(fixture.projectDir, "toUpload", "overLimit.txt");
let uploadData = Array(10_001).fill("a").join(""); // 10.000001KB
await fs
.mkdir(path.dirname(uploadFile), { recursive: true })
.catch(() => {});
await fs.writeFile(uploadFile, uploadData, "utf8");
await app.goto("/file-upload");
await app.uploadFile("#file", uploadFile);
await app.clickSubmitButton("/file-upload");
expect(await app.getHtml("pre")).toBe(`<pre>
{
"errorMessage": "Field \\"file\\" exceeded upload size of 10000 bytes."
}</pre
>`);
});
});