诺克库 – 如何匹配任何url
您好我正在尝试nock库,但我正在努力与查询string匹配的随机模式。 我认为像下面的代码应该工作,但我不能得到任何工作。
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); });
我以前没有用过这个,但是从阅读文档来看,这可能会有所帮助。
怎么样这样的事情:
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); });
只是为了完成thtsigma的回答:
如果要匹配任何范围(协议,域和端口),也可以添加“ 范围过滤”
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); });
有了这个,任何url都将被匹配。