mardi 16 février 2021

File upload in JEST to an HAPI server

I have trying to get a test with Jest working for a HAPI backend using mulit-part. I want to test the upload of a file

The curl I used : curl -X POST --form file=@./tests/file.lic http://localhost:3001/license/validate is working fine

My hapi server has the following route :

  {
        method: 'POST',
        path: `${routePrefix}/validate`,
        options: {
          auth: false,
          payload: {
            parse: true,
            timeout: false,
            maxBytes: MAX_BYTES, 
            allow: 'multipart/form-data',
            multipart: {
              output: "stream"
            },
          },

        },
        handler: postValidateHandler,
      },

here is my test :

test('status endpoint returns 404 - invalid route', async () => {
    // Fill the form object

    var form = new FormData();

    const response = await server.inject({
      url: `${routePrefix}/validate`,
      method: 'POST',
      headers: form.getHeaders(),
      payload: fs.readFileSync(path.join(__dirname, 'file.lic')),

    });
    expect(response.statusCode).toEqual(200)
    console.log(response);

  })
})

but whatever I do I always get a

message: 'Invalid multipart payload format'

Any pointers what I am doing wrong ?

Aucun commentaire:

Enregistrer un commentaire