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 file upload without form-data library #516

Closed
2 tasks done
shania-g opened this issue Mar 25, 2024 · 1 comment · Fixed by fastify/light-my-request#286
Closed
2 tasks done

Testing file upload without form-data library #516

shania-g opened this issue Mar 25, 2024 · 1 comment · Fixed by fastify/light-my-request#286
Labels
good first issue Good for newcomers

Comments

@shania-g
Copy link

shania-g commented Mar 25, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

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.

@mcollina
Copy link
Member

Thanks for reporting. This is possible but I don't have time to write a full example, sorry.

(Others might, I'll tag this as a good first issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants