【问题标题】:node-nock mocking a uri with a question mark in the pathnode-nock 在路径中模拟带有问号的 uri
【发布时间】:2018-03-01 16:50:26
【问题描述】:

我正在尝试模拟包含问号但不属于查询字符串的路径,例如:

https://example.com/index.php?/api/v2/get-item/1

Nock 在问号处分割路径,并希望我提供查询字符串键值对:

const scope = nock('https://example.com/index.php?/api/v2/get-item/')
  .get('/1')
  .reply(200, { item });

console.log(nock.activeMocks());

> [ 'GET https://example.com/index.php/1' ]

我已经尝试过对路径进行 URI 编码,但我仍然遇到同样的问题。这里最好的方法是什么?

【问题讨论】:

  • 自己去掉问号。使用 String.replace。根据协议,此类 URL 无效。
  • URL 是第三方依赖。看看这里的文档:docs.gurock.com/testrail-api2/reference-suites我不知道这是一个 php 还是 testrail 的东西?
  • 现有答案有帮助还是您仍需要帮助?

标签: javascript node.js nock


【解决方案1】:

您应该只在nock 调用中指定主机名。截至目前,您将其中的一些路径包括在内,其中一些在get 调用中。

改为这样做:

const scope = nock('https://example.com')
  .get('/index.php?/api/v2/get-item/1')
  .reply(200, { item });

【讨论】:

猜你喜欢
  • 2013-11-05
  • 2017-10-17
  • 1970-01-01
  • 2011-05-31
  • 2018-05-27
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
相关资源
最近更新 更多