|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const FormData = require('form-data') |
| 5 | +const Fastify = require('fastify') |
| 6 | +const multipart = require('..') |
| 7 | +const http = require('http') |
| 8 | +const path = require('path') |
| 9 | +const fs = require('fs') |
| 10 | +const { access } = require('fs').promises |
| 11 | +const EventEmitter = require('events') |
| 12 | +const { once } = EventEmitter |
| 13 | + |
| 14 | +const filePath = path.join(__dirname, '../README.md') |
| 15 | + |
| 16 | +test('should store file on disk, remove on response when attach fields to body is true', async function (t) { |
| 17 | + t.plan(22) |
| 18 | + |
| 19 | + const fastify = Fastify() |
| 20 | + t.teardown(fastify.close.bind(fastify)) |
| 21 | + |
| 22 | + fastify.register(multipart, { |
| 23 | + attachFieldsToBody: true |
| 24 | + }) |
| 25 | + |
| 26 | + fastify.post('/', async function (req, reply) { |
| 27 | + t.ok(req.isMultipart()) |
| 28 | + |
| 29 | + const files = await req.saveRequestFiles() |
| 30 | + |
| 31 | + t.ok(files[0].filepath) |
| 32 | + t.equal(files[0].fieldname, 'upload') |
| 33 | + t.equal(files[0].filename, 'README.md') |
| 34 | + t.equal(files[0].encoding, '7bit') |
| 35 | + t.equal(files[0].mimetype, 'text/markdown') |
| 36 | + t.ok(files[0].fields.upload) |
| 37 | + t.ok(files[1].filepath) |
| 38 | + t.equal(files[1].fieldname, 'upload') |
| 39 | + t.equal(files[1].filename, 'README.md') |
| 40 | + t.equal(files[1].encoding, '7bit') |
| 41 | + t.equal(files[1].mimetype, 'text/markdown') |
| 42 | + t.ok(files[1].fields.upload) |
| 43 | + t.ok(files[2].filepath) |
| 44 | + t.equal(files[2].fieldname, 'other') |
| 45 | + t.equal(files[2].filename, 'README.md') |
| 46 | + t.equal(files[2].encoding, '7bit') |
| 47 | + t.equal(files[2].mimetype, 'text/markdown') |
| 48 | + t.ok(files[2].fields.upload) |
| 49 | + |
| 50 | + await access(files[0].filepath, fs.constants.F_OK) |
| 51 | + await access(files[1].filepath, fs.constants.F_OK) |
| 52 | + await access(files[2].filepath, fs.constants.F_OK) |
| 53 | + |
| 54 | + reply.code(200).send() |
| 55 | + }) |
| 56 | + const ee = new EventEmitter() |
| 57 | + |
| 58 | + // ensure that file is removed after response |
| 59 | + fastify.addHook('onResponse', async (request, reply) => { |
| 60 | + try { |
| 61 | + await access(request.tmpUploads[0], fs.constants.F_OK) |
| 62 | + } catch (error) { |
| 63 | + t.equal(error.code, 'ENOENT') |
| 64 | + t.pass('Temp file was removed after response') |
| 65 | + ee.emit('response') |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + await fastify.listen({ port: 0 }) |
| 70 | + // request |
| 71 | + const form = new FormData() |
| 72 | + const opts = { |
| 73 | + protocol: 'http:', |
| 74 | + hostname: 'localhost', |
| 75 | + port: fastify.server.address().port, |
| 76 | + path: '/', |
| 77 | + headers: form.getHeaders(), |
| 78 | + method: 'POST' |
| 79 | + } |
| 80 | + |
| 81 | + const req = http.request(opts) |
| 82 | + form.append('upload', fs.createReadStream(filePath)) |
| 83 | + form.append('upload', fs.createReadStream(filePath)) |
| 84 | + form.append('other', fs.createReadStream(filePath)) |
| 85 | + |
| 86 | + form.pipe(req) |
| 87 | + |
| 88 | + const [res] = await once(req, 'response') |
| 89 | + t.equal(res.statusCode, 200) |
| 90 | + res.resume() |
| 91 | + await once(res, 'end') |
| 92 | + await once(ee, 'response') |
| 93 | +}) |
| 94 | + |
| 95 | +test('should throw on saving request files when attach fields to body is true but buffer is not stored', async function (t) { |
| 96 | + t.plan(3) |
| 97 | + |
| 98 | + const fastify = Fastify() |
| 99 | + t.teardown(fastify.close.bind(fastify)) |
| 100 | + |
| 101 | + fastify.register(multipart, { |
| 102 | + attachFieldsToBody: true, |
| 103 | + onFile: async (part) => { |
| 104 | + for await (const chunk of part.file) { |
| 105 | + chunk.toString() |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + fastify.post('/', async function (req, reply) { |
| 111 | + t.ok(req.isMultipart()) |
| 112 | + |
| 113 | + try { |
| 114 | + await req.saveRequestFiles() |
| 115 | + reply.code(200).send() |
| 116 | + } catch (error) { |
| 117 | + t.ok(error instanceof fastify.multipartErrors.FileBufferNotFoundError) |
| 118 | + reply.code(500).send() |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + await fastify.listen({ port: 0 }) |
| 123 | + // request |
| 124 | + const form = new FormData() |
| 125 | + const opts = { |
| 126 | + protocol: 'http:', |
| 127 | + hostname: 'localhost', |
| 128 | + port: fastify.server.address().port, |
| 129 | + path: '/', |
| 130 | + headers: form.getHeaders(), |
| 131 | + method: 'POST' |
| 132 | + } |
| 133 | + |
| 134 | + const req = http.request(opts) |
| 135 | + form.append('upload', fs.createReadStream(filePath)) |
| 136 | + |
| 137 | + form.pipe(req) |
| 138 | + |
| 139 | + const [res] = await once(req, 'response') |
| 140 | + t.equal(res.statusCode, 500) |
| 141 | + res.resume() |
| 142 | + await once(res, 'end') |
| 143 | +}) |
0 commit comments