vendredi 5 janvier 2018

nockBack filteringPath not working

I'm using the lastest nock v9.1.5. I have some API calls that add a timestamp at the end of it. e.g. http://ift.tt/2F3j1v5.

I'm try filteringPath to replace the real timestamp with a fixed one, so it can match the fixtures. However, it doesn't work.

I wonder:

  1. Why filteringPath is not working and I have to call directly transformPathFunction?
  2. How can I transform the request's path to match the fixture?

This is my what I have tried so far:

const { nockDone } = await nockBack('balanceFixtures.json', { before: transform, after: transform, afterRecord }); 
const balance = await getBalance(sources);
nockDone();

I'm saving the data with a fixed timestamp using the afterRecord:

function afterRecord(nockedObjects) {
  nockedObjects.forEach(nocked => {
    nocked.path = nocked.path.replace(/(timestamp)=([0-9]+)/g, 'timestamp=777');;
  });

  return nockedObjects;
}

That is working fine. However, when I tried to transform the path it doesn't work:

var transform = function (scope) {
  scope.transformPathFunction = function (path) {
    const newPath = path.replace(/(timestamp)=([0-9]+)/g, 'timestamp=777');
    console.log(`transformPathFunction the path... ${path} -> ${newPath}`);
    return newPath;
  }
  scope.filteringPath = function (path) {
    console.log(`filtering the path... ${path}`);
    return path.replace(/(timestamp)=([0-9]+)/g, 'timestamp=777');
  };
}

I can see in the terminal:

transformPathFunction the path... /path?timestamp=1515194215378&recvWindow=60000 -> /path?timestamp=777&recvWindow=60000

and it still fails:

/Users/admejiar/.nvm/versions/node/v9.3.0/lib/node_modules/mocha/lib/runner.js:707
  err.uncaught = true;
               ^

TypeError: Cannot create property 'uncaught' on string 'signedRequest error: Error: Nock: No match for request {
  "method": "GET",
  "url": "http://ift.tt/2EcKIk6",
  "headers": {
    "user-agent": "Mozilla/4.0 (compatible; Node Example API)",
    "content-type": "application/x-www-form-urlencoded",
    "x-mbx-apikey": "x",
    "host": "api.example.com"
  }
} Got instead {
  "method": "GET",
  "url": "http://ift.tt/2EcKIk6",
  "headers": {
    "user-agent": "Mozilla/4.0 (compatible; Node Example API)",
    "content-type": "application/x-www-form-urlencoded",
    "x-mbx-apikey": "x",
    "host": "api.example.com"
  }
}'
    at Runner.uncaught (/Users/admejiar/.nvm/versions/node/v9.3.0/lib/node_modules/mocha/lib/runner.js:707:16)
    at process.uncaught (/Users/admejiar/.nvm/versions/node/v9.3.0/lib/node_modules/mocha/lib/runner.js:821:10)
    at process.emit (events.js:159:13)
    at process._fatalException (bootstrap_node.js:387:26)

Aucun commentaire:

Enregistrer un commentaire