node-nock 模拟路径中带有问号的 uri

node-nock mocking a uri with a question mark in the path

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

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编码,但我仍然遇到同样的问题。这里最好的方法是什么?

您应该仅在 nock 调用中指定主机名。截至目前,您在 get 调用中包含了一些路径。

改为这样做:

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