【问题标题】:Testing binary response with supertest用 supertest 测试二进制响应
【发布时间】:2020-04-21 21:39:15
【问题描述】:

我正在使用 express 开发一个 API,并使用 supertest 对其进行测试。我的 API 端点正在返回 tar.gz 文件。我想测试一下文件是否正确发送并且内容是否正确。我在弄清楚如何检索数据时遇到了麻烦。我天真的方法是将res.text 的内容(其中const res = request(app).get('/project/export') 保存到一个文件中,提取它并检查它的内容。但是res.text 的简单保存似乎不起作用,并且提取功能无法将其识别为正确压缩的文件.

任何帮助表示赞赏。随意建议其他模块/方法如何测试快速应用程序。谢谢!

【问题讨论】:

    标签: javascript express testing supertest superagent


    【解决方案1】:

    在 Jest 中运行测试时,在请求上设置 .responseType('blob') 将导致 req.body 成为 Buffer

    https://visionmedia.github.io/superagent/#binary

    例如:

    const response = await request(app)
      .get('/project/export')
      .responseType('blob')
    
    await fs.promises.writeFile('export.tar.gz', response.body)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 2014-10-10
      • 2010-12-11
      • 2019-07-31
      • 2021-03-07
      • 1970-01-01
      • 2012-06-16
      • 2014-09-02
      相关资源
      最近更新 更多