Status 400: Invalid multipart payload format when testing

1.1k views Asked by At

I have this route

server.route({
  method: 'POST',
  path: '/upload',
  config: {
    payload: {
      output: 'stream',
      parse: true,
      allow: 'multipart/form-data',
    },

    handler: fileUploadHandler
  }
})

which works perfectly fine on the browser; however, when request the API through jest + supertest or postman, Hapi returns Status 400: Invalid multipart payload format. Another thing I notice is that the fileUploadHandler is not even being called.

Error Stack Trace:

Error: Invalid multipart payload format
    at onError (/Users/trungnguyen/Desktop/bus-file-uploader/node_modules/subtext/lib/index.js:310:26)
    at g (events.js:291:16)
    at emitOne (events.js:101:20)
    at emit (events.js:188:7)
    at internals.Dispenser._emit (/Users/trungnguyen/Desktop/bus-file-uploader/node_modules/pez/lib/index.js:196:15)
    at internals.Dispenser._abort (/Users/trungnguyen/Desktop/bus-file-uploader/node_modules/pez/lib/index.js:202:10)
    at internals.Dispenser._onLineEnd (/Users/trungnguyen/Desktop/bus-file-uploader/node_modules/pez/lib/index.js:325:25)
    at _lines.on (/Users/trungnguyen/Desktop/bus-file-uploader/node_modules/pez/lib/index.js:94:14)
    at emitNone (events.js:86:13)
    at emit (events.js:185:7)

And here is my Supertest (written with Jest) code:

  test('returns 500 if file is not CSV', async () => {
    await request(server.listener)
      .post('/upload')
      .attach('file', INVALID_EXTENSION_FILE)
      .expect(500);
  })
0

There are 0 answers