nock library - how to match any url

前端 未结 3 1637
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 10:18

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 anyth

相关标签:
3条回答
  • 2021-02-12 10:39

    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);
    });
    
    0 讨论(0)
  • 2021-02-12 10:51

    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.

    0 讨论(0)
  • 2021-02-12 11:00

    We can use regexp too

    nock("http://www.google.com")
       .get(/.*/)
    
    0 讨论(0)
提交回复
热议问题