javascript - 如何使 nock 以“未定义”的身体响应?
问题描述
我正在测试一个处理不同情况的请求处理程序,其中一个需要 HTTP 代码 204 的响应并且没有响应正文 ( typeof response.body === 'undefined'
)。
我试图将回复设置为null
,undefined
而根本没有设置它。在所有这些情况下,响应正文都是一个空字符串 ( ''
)。
有没有办法从 Nock 中删除响应正文?
这是我要运行的测试:
it('should return true if body is undefined (usually, 204 status responses - No content)', async () => {
nock('http://test.com')
.put('/')
.reply(204, undefined)
// .reply(204, null) also fails
// .reply(204) also fails
const result = await Request._request('http://test.com', { method: 'PUT' })
expect(result).to.be.true
})
这是我要测试的代码:
static async _request (url, options) {
return new Promise((resolve, reject) => {
request(url, options)
.then((response) => {
if (response.statusCode < 300) {
if (typeof response.body === 'undefined') {
resolve(true)
} else if (isJSON(response.body)) {
resolve(JSON.parse(response.body.replace(/\s/g, ' ')))
} else {
.
.
.
解决方案
推荐阅读
- r - 如何计算随机森林的类权重
- java - 安卓游戏,播放“Game Over”音效
- python - 参考云存储桶中的 Python 虚拟环境
- pip - Fish + Virtualenvs:错误的“which pip”位置,但正确的 pip 安装位置
- xcode - NSSoftLinking - 在(空)框架中找不到函数“SLSIsSuppressedByScreenTime”
- javascript - 将对象添加到嵌套数组
- amazon-web-services - DynamoDB:查询总是返回 null
- python - 我想从一个列表中删除所有零并将它们全部附加到另一个列表但并非全部删除
- reactjs - 如何将状态列表中的项目传递给子组件?
- sql - 从审计表中选择旧值/新值