我正在尝试模拟包含问号但不属于查询字符串的路径,例如: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 });