【发布时间】:2022-01-26 09:08:53
【问题描述】:
目的:
使用文件流响应测试控制器方法。
控制器:
@Get('/resources/pdf/:fileId')
@HttpCode(HttpStatus.OK)
public async downloadPdf(
@Param('fileId') fileId: string,
@Res() response: Response
): Promise<void> {
const fileReadableStream = await myService.downloadPdf(fileId);
response.setHeader('Content-Type', 'application/pdf');
fileReadableStream.pipe(response);
}
测试单元:
describe('FileDownloadController', () => {
it('should download file with specific header', async () => {
await request(app.getHttpServer())
.get('/resources/pdf/myFileId')
.expect(200)
.expect('Content-Type', 'application/pdf')
.expect('my file content');
});
});
【问题讨论】:
标签: unit-testing jestjs nestjs