Search code examples
javascriptnode.jsnock

node-nock mocking a uri with a question mark in the path


I am trying to mock a path which contains a question mark but is not part of a query string, for example:

https://example.com/index.php?/api/v2/get-item/1

Nock is slicing the path at the question mark and expecting me to supply query string key-value pairs:

const scope = nock('https://example.com/index.php?/api/v2/get-item/')
  .get('/1')
  .reply(200, { item });

console.log(nock.activeMocks());

> [ 'GET https://example.com/index.php/1' ]

I have tried URI encoding the path but I still have the same problem. What is the best approach here?


Solution

  • You should specify the hostname only in the nock call. As of right now, you're including some of the path there, and some of it in the get call.

    Do this instead:

    const scope = nock('https://example.com')
      .get('/index.php?/api/v2/get-item/1')
      .reply(200, { item });