MCPcopy
hub / github.com/directus/directus / createMockMultipartRequest

Function createMockMultipartRequest

api/src/ai/files/controllers/upload.test.ts:12–55  ·  view source on GitHub ↗
(
	fields: Record<string, string>,
	file?: { name: string; data: Buffer; type: string },
)

Source from the content-addressed store, hash-verified

10}));
11
12function createMockMultipartRequest(
13 fields: Record<string, string>,
14 file?: { name: string; data: Buffer; type: string },
15) {
16 const boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
17 const parts: string[] = [];
18
19 for (const [key, value] of Object.entries(fields)) {
20 parts.push(`--${boundary}\r\nContent-Disposition: form-data; name="${key}"\r\n\r\n${value}`);
21 }
22
23 if (file) {
24 parts.push(
25 `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${file.name}"\r\nContent-Type: ${file.type}\r\n\r\n`,
26 );
27 }
28
29 const bodyStart = parts.join('\r\n') + '\r\n';
30 const bodyEnd = `\r\n--${boundary}--\r\n`;
31
32 const chunks: Buffer[] = [Buffer.from(bodyStart)];
33
34 if (file) {
35 chunks.push(file.data);
36 }
37
38 chunks.push(Buffer.from(bodyEnd));
39 const body = Buffer.concat(chunks);
40
41 const stream = new Readable({
42 read() {
43 this.push(body);
44 this.push(null);
45 },
46 });
47
48 return {
49 stream,
50 headers: {
51 'content-type': `multipart/form-data; boundary=${boundary}`,
52 'content-length': String(body.length),
53 },
54 };
55}
56
57describe('aiFileUploadHandler', () => {
58 let mockReq: Partial<Request>;

Callers 1

upload.test.tsFile · 0.85

Calls 2

joinMethod · 0.80
fromMethod · 0.80

Tested by

no test coverage detected