Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nock library - how to match any url

Tags:

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.

  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);   }); 
like image 204
henry.oswald Avatar asked Feb 03 '13 23:02

henry.oswald


People also ask

How does nock work?

Nock works by overriding the http. request and http. ClientRequest functions, intercepting all requests made to a specified URL and returning specified responses that mimic the data that the real URL would return.

How do you throw a nock error?

you can set up nock like this to simulate a 500 error: nock('https://google.com') . get('/') . reply(500, 'FAILED!

How do you pass a header on a nock?

Specifying Request HeadersThe function will be passed the header value. If reqheaders is not specified or if host is not part of it, Nock will automatically add host value to request header. If no request headers are specified for mocking then Nock will automatically skip matching of request headers.


2 Answers

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); }); 
like image 175
thtsigma Avatar answered Sep 21 '22 17:09

thtsigma


We can use regexp too

nock("http://www.google.com")    .get(/.*/) 
like image 38
Kevin Law Avatar answered Sep 19 '22 17:09

Kevin Law