I am trying to simulate the response to this API URL
我正在尝试模拟对此API网址的响应
http://api.myapihost.com/images?foo=bar&spam=egg
The URL parameter combinations can vary. I am trying to intercept this request and respond with an empty object.
URL参数组合可以变化。我试图拦截此请求并使用空对象进行响应。
nock('http://api.myapihost.com')
.persist()
.get('/images', '*')
.reply(200, {});
I get this error message when my test case runs:
我的测试用例运行时收到此错误消息:
Uncaught Error: Nock: No match for HTTP request GET /images?height=2500
How can I configure nock to match any combination of URL parameters?
如何配置nock以匹配任何URL参数组合?
2 个解决方案
#1
3
You should use path filtering in order to match the URL parameters.
您应该使用路径过滤以匹配URL参数。
var scope = nock('http://api.myapihost.com')
.filteringPath(function(path) {
return '/images';
})
.get('/images')
.reply(200, {});
You could check the docs here
你可以在这里查看文档
#2
5
With nock you can specify regular expressions.
使用nock,您可以指定正则表达式。
Here is an example (tested with v9.2.3):
这是一个例子(用v9.2.3测试):
nock('http://api.myapihost.com')
.get(/images.*$/)
.reply(200, {});
There is also a simpler syntax using .query(true)
, if you want to mock the entire url regardless of the passed query string:
如果你想模拟整个url而不管传递的查询字符串是什么,那么还有一个更简单的语法使用.query(true):
nock('http://api.myapihost.com')
.get('/images')
.query(true)
.reply(200, {});
#1
3
You should use path filtering in order to match the URL parameters.
您应该使用路径过滤以匹配URL参数。
var scope = nock('http://api.myapihost.com')
.filteringPath(function(path) {
return '/images';
})
.get('/images')
.reply(200, {});
You could check the docs here
你可以在这里查看文档
#2
5
With nock you can specify regular expressions.
使用nock,您可以指定正则表达式。
Here is an example (tested with v9.2.3):
这是一个例子(用v9.2.3测试):
nock('http://api.myapihost.com')
.get(/images.*$/)
.reply(200, {});
There is also a simpler syntax using .query(true)
, if you want to mock the entire url regardless of the passed query string:
如果你想模拟整个url而不管传递的查询字符串是什么,那么还有一个更简单的语法使用.query(true):
nock('http://api.myapihost.com')
.get('/images')
.query(true)
.reply(200, {});