使用 supertest 测试二进制响应 [英] Testing binary response with supertest
问题描述
我正在使用 express 开发 API,并使用 supertest 对其进行测试.我的 API 端点正在返回 tar.gz 文件.我想测试一下,文件是否正确发送并且内容是否正确.我在弄清楚如何检索数据时遇到了麻烦.我天真的方法是将 res.text
的内容(其中 const res = request(app).get('/project/export')
保存到文件中,提取它并检查它的内容.但是 res.text
的简单保存似乎不起作用,并且提取功能无法将其识别为正确压缩的文件.
任何帮助表示赞赏.随意建议其他模块/方法如何测试快速应用程序.谢谢!
在 Jest 中运行测试时,在请求上设置 .responseType('blob')
将导致 req.body
成为 Buffer
.
https://visionmedia.github.io/superagent/#binary>
例如:
const response = await request(app).get('/项目/导出').responseType('blob')等待 fs.promises.writeFile('export.tar.gz', response.body)
I'm developing an API with express and testing it with supertest. My API endpoint is returning tar.gz file. I would like to test, if file is properly sent and it's content is correct. I'm having troubles figuring out how to retrieve data. My naive approach was to save content of res.text
(where const res = request(app).get('/project/export')
to a file, extract it and check it's content. But simple saving of res.text
does not seem to work and extracting function does not recognise it as properly compressed file.
Any help appreciated. Feel free to suggest other modules/approaches how to test an express app. Thanks!
When running tests in Jest, setting .responseType('blob')
on the request will cause req.body
to be a Buffer
.
https://visionmedia.github.io/superagent/#binary
For example:
const response = await request(app)
.get('/project/export')
.responseType('blob')
await fs.promises.writeFile('export.tar.gz', response.body)
这篇关于使用 supertest 测试二进制响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!