(
fields: Record<string, string>,
file?: { name: string; data: Buffer; type: string },
)
| 10 | })); |
| 11 | |
| 12 | function 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 | |
| 57 | describe('aiFileUploadHandler', () => { |
| 58 | let mockReq: Partial<Request>; |
no test coverage detected