问题描述
await request(app.getHttpServer())
.post('/uploads/avatar')
.set('Content-type','application/octet-stream')
.auth(userToken.accesstoken,{ type: 'bearer' })
.attach('file',buffer,'test.jpg')
.expect(({ status,body }) => {
console.log(body);
expect(status).toBe(400);
});
尝试不设置内容类型,在附加方法上传递选项,我总是遇到同样的错误。查看 superagent 上的文档,并按照它所说的进行尝试,但没有。
尝试通过 blob、缓冲区和文件路径...并且始终相同
这是所有请求的console.log
_events: [Object: null prototype] {
end: [Function: bound onceWrapper] {
listener: [Function: bound _clearTimeout]
}
},_eventsCount: 1,_maxListeners: undefined,_agent: false,_formData: FormData {
_overheadLength: 150,_valueLength: 37351,_valuesToMeasure: [],writable: false,readable: true,dataSize: 0,maxDataSize: 2097152,pauseStreams: true,_released: false,_streams: [
'----------------------------705062276704130245747665\r\n' +
'Content-disposition: form-data; name="file"; filename="test.jpg"\r\n' +
'Content-Type: image/jpeg\r\n' +
'\r\n',<Buffer 75 ab 5a 8a 66 a0 7b f8 e9 7a 06 da b1 ee b8 ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 60 00 60 00 00 ff db 00 43 00 06 04 05 06 05 04 06 06 05 06 ... 37301 more bytes>,[Function: bound ]
],_currentStream: null,_insideLoop: false,_pendingNext: false,_events: [Object: null prototype] { error: [Function] },_boundary: '--------------------------705062276704130245747665'
},method: 'POST',url: 'http://127.0.0.1:41895/uploads/avatar',_header: {
'user-agent': 'node-superagent/3.8.3',authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InVzZXJAY29sbGFtYXAub3JnIiwiZmlyc3RuYW1lIjoiVXNlciIsImxhc3RuYW1lIjoiVXNlciIsImF2YXRhciI6IiIsImlhdCI6MTYxNDEwMjk2OSwiZXhwIjoxNjE0MTAzMjY5fQ.YRud3pAACN51HDRleuc1lYd4-yuyrlANY9kmwSpA1O0'
},header: {
'User-Agent': 'node-superagent/3.8.3',Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InVzZXJAY29sbGFtYXAub3JnIiwiZmlyc3RuYW1lIjoiVXNlciIsImxhc3RuYW1lIjoiVXNlciIsImF2YXRhciI6IiIsImlhdCI6MTYxNDEwMjk2OSwiZXhwIjoxNjE0MTAzMjY5fQ.YRud3pAACN51HDRleuc1lYd4-yuyrlANY9kmwSpA1O0'
},writable: true,_redirects: 0,_maxRedirects: 0,cookies: '',qs: {},_query: [],qsRaw: [],_redirectList: [],_streamRequest: false,_buffer: true,app: Server {
_events: [Object: null prototype] {
request: [Function: bound lookup],connection: [Function: connectionListener],clientError: [Function: bound defaultClientErrorHandler]
},_eventsCount: 3,_connections: 0,_handle: TCP {
reading: false,onconnection: [Function: onconnection],[Symbol(owner)]: [Circular]
},_usingWorkers: false,_workers: [],_unref: false,allowHalfOpen: true,pauSEOnConnect: false,httpAllowHalfOpen: false,timeout: 0,keepAliveTimeout: 5000,maxHeadersCount: null,headersTimeout: 40000,_connectionKey: '6::::0',[Symbol(IncomingMessage)]: [Function: IncomingMessage],[Symbol(ServerResponse)]: [Function: ServerResponse],[Symbol(asyncId)]: 916
},_asserts: [],_server: Server {
_events: [Object: null prototype] {
request: [Function: bound lookup],[Symbol(asyncId)]: 916
}
}
我的控制器:
@Post('avatar')
async uploadFile(
@Req() req: FastifyRequest,// eslint-disable-next-line @typescript-eslint/no-explicit-any
@Res() reply: FastifyReply<any>,@GetUser() user: UserEntity,): Promise<string> {
try {
if (!req.isMultipart) {
reply.send(new BadRequestException());
return;
}
const data = await req.file();
const buffer = await data.toBuffer();
const { isSuccess,message } = await this.uploadsService.uploadAvatar(
buffer,user,);
if (isSuccess) {
reply
.code(200)
.header('Content-Type','application/json; charset=utf-8')
.send({ statusCode: 200,body: {} });
} else {
reply
.code(422)
.header('Content-Type','application/json; charset=utf-8')
.send({
statusCode: 422,body: {
status: 'fail',errors: message,},});
}
} catch (error) {
throw errorHandler.handle(error);
}
}
解决方法
如果您想使用 supertest 附加文件,则在从表单声明字段时必须使用另一种结构。
这是一个工作示例:
it('Check post with correct data',function(done) {
api.post(_url)
.set('Authorization','Bearer '+tokn)
.field('Content-Type','multipart/form-data')
.field('name','asdadas')
.attach('icon','test.png')
.expect(201)
.expect('Content-Type','application/json')
.end(function(err,res) {
expect(res.body).to.have.property("category_id");
expect(res.body.category_id).to.equal(2);
done();
});
});
我的代码很老了,但是配合async/await和箭头函数使用没有问题。这里只是一个想法。