我有这条路线
server.route({
method: 'POST',
path: '/upload',
config: {
payload: {
output: 'stream',
parse: true,
allow: 'multipart/form-data',
},
handler: fileUploadHandler
}
})
在浏览器上完美运行;但是,当通过 jest + supertest 或邮递员请求API时,Hapi返回状态400:无效的多部分有效载荷格式。我注意到的另一件事是甚至没有调用 fileUploadHandler 。
错误堆栈跟踪:
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)
这是我的Supertest(用Jest编写)代码:
test('returns 500 if file is not CSV', async () => {
await request(server.listener)
.post('/upload')
.attach('file', INVALID_EXTENSION_FILE)
.expect(500);
})