javascript - Testing binary response with supertest - Stack Overflow

I'm developing an API with express and testing it with supertest. My API endpoint is returning tar

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 pressed file.

Any help appreciated. Feel free to suggest other modules/approaches how to test an express app. Thanks!

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 pressed file.

Any help appreciated. Feel free to suggest other modules/approaches how to test an express app. Thanks!

Share Improve this question asked Jan 3, 2020 at 10:07 David VassDavid Vass 1557 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

When running tests in Jest, setting .responseType('blob') on the request will cause response.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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745139824a4613377.html

相关推荐

  • javascript - Testing binary response with supertest - Stack Overflow

    I'm developing an API with express and testing it with supertest. My API endpoint is returning tar

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信