nock.js - 如何匹配 URL 中的任何参数组合
nock.js - how to match any parameter combination in a URL
我正在尝试模拟对此的响应 API URL
http://api.myapihost.com/images?foo=bar&spam=egg
URL 参数组合可能会有所不同。我正在尝试拦截此请求并使用空对象进行响应。
nock('http://api.myapihost.com')
.persist()
.get('/images', '*')
.reply(200, {});
我在运行测试用例时收到此错误消息:
Uncaught Error: Nock: No match for HTTP request GET /images?height=2500
如何配置箭尾以匹配 URL 参数的任意组合?
您应该使用路径过滤来匹配 URL 参数。
var scope = nock('http://api.myapihost.com')
.filteringPath(function(path) {
return '/images';
})
.get('/images')
.reply(200, {});
您可以查看文档 here
与nock you can specify regular expressions.
这是一个示例(使用 v9.2.3 测试):
nock('http://api.myapihost.com')
.get(/images.*$/)
.reply(200, {});
还有一个更简单的语法使用 .query(true)
,如果你想模拟整个 url 而不管传递的查询字符串:
nock('http://api.myapihost.com')
.get('/images')
.query(true)
.reply(200, {});
我正在尝试模拟对此的响应 API URL
http://api.myapihost.com/images?foo=bar&spam=egg
URL 参数组合可能会有所不同。我正在尝试拦截此请求并使用空对象进行响应。
nock('http://api.myapihost.com')
.persist()
.get('/images', '*')
.reply(200, {});
我在运行测试用例时收到此错误消息:
Uncaught Error: Nock: No match for HTTP request GET /images?height=2500
如何配置箭尾以匹配 URL 参数的任意组合?
您应该使用路径过滤来匹配 URL 参数。
var scope = nock('http://api.myapihost.com')
.filteringPath(function(path) {
return '/images';
})
.get('/images')
.reply(200, {});
您可以查看文档 here
与nock you can specify regular expressions.
这是一个示例(使用 v9.2.3 测试):
nock('http://api.myapihost.com')
.get(/images.*$/)
.reply(200, {});
还有一个更简单的语法使用 .query(true)
,如果你想模拟整个 url 而不管传递的查询字符串:
nock('http://api.myapihost.com')
.get('/images')
.query(true)
.reply(200, {});