Hi I am trying out the nock library but am struggling with matching random patterns on query strings. I thought something like the code below should work but I can not get anything to work.
你好,我正在尝试nock库,但是我在查询字符串的随机模式匹配上有困难。我认为下面的代码应该可以工作,但是我不能让任何东西工作。
var nock, request;
request = require('request');
nock = require('nock');
nock("http://www.google.com").filteringPath(/.*/g).get("/").reply(200, "this should work?");
request("http://www.google.com?value=bob", function(err, res, body) {
return console.log(body);
});
2 个解决方案
#1
16
I haven't used this before, but from reading the docs maybe this will help.
我以前没有使用过这个,但是从阅读文档中可以看出这可能会有帮助。
How about something like this:
像这样的东西怎么样:
var nock = require('nock');
var request = require ('request');
nock("http://www.google.com")
.filteringPath(function(path){
return '/';
})
.get("/")
.reply(200, "this should work?");
request("http://www.google.com?value=bob", function(err, res, body) {
return console.log(body);
});
#2
4
Just to complete thtsigma's answer:
完成题外话:
You can also add Scope filtering if you want to match any scope (protocol, domain and port)
如果希望匹配任何范围(协议、域和端口),还可以添加范围筛选
var nock = require('nock');
var request = require ('request');
nock("http://www.whatever-here.com", {
filteringScope: function(scope) {
return true;
}
})
.filteringPath(function(path){
return "/";
})
.get("/")
.reply(200, "this should work?");
request("http://www.google.com?value=bob", function(err, res, body) {
return console.log(body);
});
With this, any url will be matched.
有了这个,任何url都将被匹配。
#1
16
I haven't used this before, but from reading the docs maybe this will help.
我以前没有使用过这个,但是从阅读文档中可以看出这可能会有帮助。
How about something like this:
像这样的东西怎么样:
var nock = require('nock');
var request = require ('request');
nock("http://www.google.com")
.filteringPath(function(path){
return '/';
})
.get("/")
.reply(200, "this should work?");
request("http://www.google.com?value=bob", function(err, res, body) {
return console.log(body);
});
#2
4
Just to complete thtsigma's answer:
完成题外话:
You can also add Scope filtering if you want to match any scope (protocol, domain and port)
如果希望匹配任何范围(协议、域和端口),还可以添加范围筛选
var nock = require('nock');
var request = require ('request');
nock("http://www.whatever-here.com", {
filteringScope: function(scope) {
return true;
}
})
.filteringPath(function(path){
return "/";
})
.get("/")
.reply(200, "this should work?");
request("http://www.google.com?value=bob", function(err, res, body) {
return console.log(body);
});
With this, any url will be matched.
有了这个,任何url都将被匹配。