node.jsnock

How to build nock regex for dynamic urls


How do i build nock configuration for the following types of urls

http://example.com/harry/potter?param1=value1

and

http://example.com/harry/<value1>

I have two type of urls first is where i can have query params altough the base url is fixed.

Second one is where base url has dynamic values.

Currently i have

before(function(){
   nock('http://example.com')
       .get('/terminal/chrome_log')
       .reply(200, "OK");

  nock('http://example.com')
       .get(function(uri) {
           return uri.indexOf('harry') >= 0;
       })
        .reply(200, "OK");
    });

Would be of great help to know something that works with node nock.


Solution

  • You can specify path and query as reqex:

      nock('http://example.com')
           .get(/harry\/[^\/]+$/)
           .query({param1: 'value'})
           .reply(200, "OK");