-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Expand file tree
/
Copy pathgetUploadFormData.spec.ts
More file actions
196 lines (167 loc) · 5.3 KB
/
getUploadFormData.spec.ts
File metadata and controls
196 lines (167 loc) · 5.3 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { ReadableStream } from 'stream/web';
import { expect } from 'chai';
import { getUploadFormData } from './getUploadFormData';
const createMockRequest = (
fields: Record<string, string>,
file?: {
fieldname: string;
filename: string;
content: string | Buffer;
mimetype?: string;
},
options: { simulateError?: boolean } = {},
): Request => {
const boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
const parts: string[] = [];
if (file) {
parts.push(
`--${boundary}`,
`Content-Disposition: form-data; name="${file.fieldname}"; filename="${file.filename}"`,
`Content-Type: ${file.mimetype || 'application/octet-stream'}`,
'',
file.content.toString(),
);
}
for (const [name, value] of Object.entries(fields)) {
parts.push(`--${boundary}`, `Content-Disposition: form-data; name="${name}"`, '', value);
}
parts.push(`--${boundary}--`);
const buffer = Buffer.from(parts.join('\r\n'));
const mockRequest: any = {
headers: {
entries: () => [['content-type', `multipart/form-data; boundary=${boundary}`]],
},
body: new ReadableStream({
async pull(controller) {
if (options.simulateError) {
controller.error(new Error('aborted'));
return;
}
controller.enqueue(buffer);
controller.close();
},
}),
};
return mockRequest as Request & { headers: Record<string, string> };
};
describe('getUploadFormData', () => {
it('should successfully parse a single file upload and fields', async () => {
const mockRequest = createMockRequest(
{ fieldName: 'fieldValue' },
{
fieldname: 'fileField',
filename: 'test.txt',
content: 'Hello, this is a test file!',
mimetype: 'text/plain',
},
);
const result = await getUploadFormData({ request: mockRequest }, { field: 'fileField' });
expect(result).to.deep.include({
fieldname: 'fileField',
filename: 'test.txt',
mimetype: 'text/plain',
fields: { fieldName: 'fieldValue' },
});
expect(result.fileBuffer).to.not.be.undefined;
expect(result.fileBuffer.toString()).to.equal('Hello, this is a test file!');
});
it('should parse a file upload with multiple additional fields', async () => {
const mockRequest = createMockRequest(
{
fieldName: 'fieldValue',
extraField1: 'extraValue1',
extraField2: 'extraValue2',
},
{
fieldname: 'fileField',
filename: 'test_with_fields.txt',
content: 'This file has additional fields!',
mimetype: 'text/plain',
},
);
const result = await getUploadFormData({ request: mockRequest }, { field: 'fileField' });
expect(result).to.deep.include({
fieldname: 'fileField',
filename: 'test_with_fields.txt',
mimetype: 'text/plain',
fields: {
fieldName: 'fieldValue',
extraField1: 'extraValue1',
extraField2: 'extraValue2',
},
});
expect(result.fileBuffer).to.not.be.undefined;
expect(result.fileBuffer.toString()).to.equal('This file has additional fields!');
});
it('should handle a file upload when fileOptional is true', async () => {
const mockRequest = createMockRequest(
{ fieldName: 'fieldValue' },
{
fieldname: 'fileField',
filename: 'optional.txt',
content: 'This file is optional!',
mimetype: 'text/plain',
},
);
const result = await getUploadFormData({ request: mockRequest }, { fileOptional: true });
expect(result).to.deep.include({
fieldname: 'fileField',
filename: 'optional.txt',
mimetype: 'text/plain',
fields: { fieldName: 'fieldValue' },
});
expect(result.fileBuffer).to.not.be.undefined;
expect(result.fileBuffer?.toString()).to.equal('This file is optional!');
});
it('should throw an error when no file is uploaded and fileOptional is false', async () => {
const mockRequest = createMockRequest({ fieldName: 'fieldValue' });
try {
await getUploadFormData({ request: mockRequest }, { fileOptional: false });
throw new Error('Expected function to throw');
} catch (error) {
expect((error as Error).message).to.equal('[No file uploaded]');
}
});
it('should return fields without errors when no file is uploaded but fileOptional is true', async () => {
const mockRequest = createMockRequest({ fieldName: 'fieldValue' }); // No file
const result = await getUploadFormData({ request: mockRequest }, { fileOptional: true });
expect(result).to.deep.equal({
fields: { fieldName: 'fieldValue' },
file: undefined,
fileBuffer: undefined,
fieldname: undefined,
filename: undefined,
encoding: undefined,
mimetype: undefined,
});
});
it('should reject an oversized file', async () => {
const mockRequest = createMockRequest(
{},
{
fieldname: 'fileField',
filename: 'large.txt',
content: 'x'.repeat(1024 * 1024 * 2), // 2 MB file
mimetype: 'text/plain',
},
);
try {
await getUploadFormData(
{ request: mockRequest },
{ sizeLimit: 1024 * 1024 }, // 1 MB limit
);
throw new Error('Expected function to throw');
} catch (error) {
expect((error as Error).message).to.equal('[error-file-too-large]');
}
});
it('should handle an aborted request stream', async () => {
const mockRequest = createMockRequest({}, undefined, { simulateError: true });
try {
await getUploadFormData({ request: mockRequest }, { fileOptional: true });
throw new Error('Expected function to throw');
} catch (error) {
expect((error as Error).message).to.equal('aborted');
}
});
});