-
-
Notifications
You must be signed in to change notification settings - Fork 8
Closed
fastify/light-my-request
#286Labels
help wantedExtra attention is neededExtra attention is needed
Description
Hello there.
I would like to add testing to my endpoint which uses fastify/multipart to accept a file upload and then writes it to the filesystem.
In the testing directory of this repository I saw that popular form-data library is used, however, I think this can also be achieved without it?
What I have come up with, but for some reason does not work:
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const filePath = join(__dirname, '../example.jpg')
describe('POST /asset', () => {
it('should return 201 when the payload is valid', async () => {
const rawData = readFileSync(filePath, 'binary')
const body = new FormData()
const blob = new Blob([rawData])
body.set('file', blob, 'example.jpg')
const res = await app.inject({
method: 'POST',
url: '/asset',
payload: {
file: body.get('file')
}
})
assert.deepStrictEqual(res.statusCode, 201)
})
})However, this gives me the following error:
{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body/file should be a file"}
This is my route definition (I do not think this is relevant, but maybe it could be):
fastify.post(
'/',
{
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['file'],
properties: {
file: { isFile: true }
}
}
}
},
createAsset
)Is there any way to make this testing work?
Thank you very much in advance.
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededExtra attention is needed