Skip to content
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

Testing fastify/multipart file upload without form-data library #1017

Closed
shania-g opened this issue Mar 25, 2024 · 3 comments · Fixed by fastify/light-my-request#286
Closed
Labels
help wanted Extra attention is needed

Comments

@shania-g
Copy link

shania-g commented Mar 25, 2024

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.

@shania-g shania-g added the help wanted Extra attention is needed label Mar 25, 2024
@climba03003
Copy link
Member

It doesn't work because you didn't provide the proper header.

@shania-g
Copy link
Author

shania-g commented Apr 4, 2024

It doesn't work because you didn't provide the proper header.

You mean for the inject I should include multipart/form-data as a header?

@climba03003
Copy link
Member

climba03003 commented Apr 5, 2024

see fastify/light-my-request#286

If you don't want to use form-data, you need to wait until the above PR landed and published.
Otherwise form-data package should be the only reliable choice.

You may copy the form-data.js in that PR and use it to provide Content-Type header and stream payload.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants