问题描述
我正在测试一个处理不同情况的请求处理程序,其中一个需要使用 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 {
.
.
.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)