-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for saveRequestFiles with attachFieldsToBody set true #409
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9e26b7a
wip: failing test added
Ceres6 57278f9
fix: save request files from body added
Ceres6 b79f526
chore: test assertions extended
Ceres6 fb89829
feat: saveRequestFiles error on null buff added
Ceres6 d71293b
chore: fixed linting
Ceres6 fde5960
chore: fixed linting
Ceres6 6b38621
chore: async removed from filesFromFields generator
Ceres6 ec92938
Merge branch 'master' into master
Ceres6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const FormData = require('form-data') | ||
const Fastify = require('fastify') | ||
const multipart = require('..') | ||
const http = require('http') | ||
const path = require('path') | ||
const fs = require('fs') | ||
const { access } = require('fs').promises | ||
const EventEmitter = require('events') | ||
const { once } = EventEmitter | ||
|
||
const filePath = path.join(__dirname, '../README.md') | ||
|
||
test('should store file on disk, remove on response when attach fields to body is true', async function (t) { | ||
t.plan(22) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart, { | ||
attachFieldsToBody: true | ||
}) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
const files = await req.saveRequestFiles() | ||
|
||
t.ok(files[0].filepath) | ||
t.equal(files[0].fieldname, 'upload') | ||
t.equal(files[0].filename, 'README.md') | ||
t.equal(files[0].encoding, '7bit') | ||
t.equal(files[0].mimetype, 'text/markdown') | ||
t.ok(files[0].fields.upload) | ||
t.ok(files[1].filepath) | ||
t.equal(files[1].fieldname, 'upload') | ||
t.equal(files[1].filename, 'README.md') | ||
t.equal(files[1].encoding, '7bit') | ||
t.equal(files[1].mimetype, 'text/markdown') | ||
t.ok(files[1].fields.upload) | ||
t.ok(files[2].filepath) | ||
t.equal(files[2].fieldname, 'other') | ||
t.equal(files[2].filename, 'README.md') | ||
t.equal(files[2].encoding, '7bit') | ||
t.equal(files[2].mimetype, 'text/markdown') | ||
t.ok(files[2].fields.upload) | ||
|
||
await access(files[0].filepath, fs.constants.F_OK) | ||
await access(files[1].filepath, fs.constants.F_OK) | ||
await access(files[2].filepath, fs.constants.F_OK) | ||
|
||
reply.code(200).send() | ||
}) | ||
const ee = new EventEmitter() | ||
|
||
// ensure that file is removed after response | ||
fastify.addHook('onResponse', async (request, reply) => { | ||
try { | ||
await access(request.tmpUploads[0], fs.constants.F_OK) | ||
} catch (error) { | ||
t.equal(error.code, 'ENOENT') | ||
t.pass('Temp file was removed after response') | ||
ee.emit('response') | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const req = http.request(opts) | ||
form.append('upload', fs.createReadStream(filePath)) | ||
form.append('upload', fs.createReadStream(filePath)) | ||
form.append('other', fs.createReadStream(filePath)) | ||
|
||
form.pipe(req) | ||
|
||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 200) | ||
res.resume() | ||
await once(res, 'end') | ||
await once(ee, 'response') | ||
}) | ||
|
||
test('should throw on saving request files when attach fields to body is true but buffer is not stored', async function (t) { | ||
t.plan(3) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart, { | ||
attachFieldsToBody: true, | ||
onFile: async (part) => { | ||
for await (const chunk of part.file) { | ||
chunk.toString() | ||
} | ||
} | ||
}) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
await req.saveRequestFiles() | ||
reply.code(200).send() | ||
} catch (error) { | ||
t.ok(error instanceof fastify.multipartErrors.FileBufferNotFoundError) | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const req = http.request(opts) | ||
form.append('upload', fs.createReadStream(filePath)) | ||
|
||
form.pipe(req) | ||
|
||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 500) | ||
res.resume() | ||
await once(res, 'end') | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why async generator but not a generator?
It doesn't really need to be async.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already changed