nock不能处理请求(请求 - 承诺)

时间:2017-09-29 14:11:53

标签: javascript node.js nock

我正在尝试运行以下代码:

const nock = require('nock');
const request = require('request-promise');

nock('https://some.api.here.com')
  .get('/objects')
  .matchHeader({})
  .reply(200, []);

request({
  method: 'GET',
  uri: `https://some.api.here.com/objects`,
  headers: {}
});

因此我看到一个错误:

check/node_modules/nock/lib/request_overrider.js:23
  var key = name.toLowerCase();
                 ^

TypeError: name.toLowerCase is not a function
    at getHeader (/check/node_modules/nock/lib/request_overrider.js:23:18)
    at Object.RequestOverrider.options.getHeader (/check/node_modules/nock/lib/request_overrider.js:125:12)
    at checkHeaders (/check/node_modules/nock/lib/interceptor.js:205:51)
    at Array.every (<anonymous>)
    at Interceptor.match (/check/node_modules/nock/lib/interceptor.js:209:39)
    at /check/node_modules/nock/lib/request_overrider.js:237:26
    at baseFindIndex (/check/node_modules/lodash/lodash.js:823:11)
    at findIndex (/check/node_modules/lodash/lodash.js:7275:14)
    at Function.find (/check/node_modules/lodash/lodash.js:5098:21)
    at end (/check/node_modules/nock/lib/request_overrider.js:236:21)
    at OverriddenClientRequest.RequestOverrider.req.end (/check/node_modules/nock/lib/request_overrider.js:153:7)
    at Request.end (/check/node_modules/request/request.js:1238:12)
    at /check/node_modules/request/request.js:413:12
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:611:11)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:578:3

有什么想法吗?

cat package.json
{
  "dependencies": {
    "nock": "^9.0.22",
    "request": "2.31.0",
    "request-promise": "^4.2.2"
  }
}

> node -v
v8.3.0

1 个答案:

答案 0 :(得分:2)

我认为您必须为key中的标题指定.matchHeader({}),这实际上是标题名称,看起来正确使用它的方式如下所示:

.matchHeader('accept', 'application/json')

因此,在您的情况下,您缺少标题名称。 Source

您还可以简要了解此模块的source(受影响的行)。