Status 400: Invalid Multiple Payload Test Format

I have this route

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

    handler: fileUploadHandler
  }
})

which works fine in the browser; however, when requesting the API via jest + supertest or postman , Hapi returns a Status 400: Invalid multipart payload format. Another thing that I notice is that fileUploadHandler is not even 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 code (written using Jest):

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

Source: https://habr.com/ru/post/1665557/


All Articles