我怎样才能让诺克用“未定义”的身体做出反应?

问题描述

我正在测试一个处理不同情况的请求处理程序,其中一个需要使用 HTTP 代码 204 且没有响应正文 (typeof response.body === 'undefined') 的响应。

我尝试将回复设置为 nullundefined 而根本没有设置。在所有这些情况下,响应正文都是空字符串 ('')。

有没有办法从 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...