Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nockBack filteringPath not working with queries #1041

Closed
amejiarosario opened this issue Jan 5, 2018 · 4 comments
Closed

nockBack filteringPath not working with queries #1041

amejiarosario opened this issue Jan 5, 2018 · 4 comments

Comments

@amejiarosario
Copy link

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. example.com/path?timestamp=1515193247399&recvWindow=60000.

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

This is my setup
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": "https://api.example.com/path?timestamp=1515194215378&recvWindow=60000",
  "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": "https://api.example.com/path?timestamp=1515194215378&recvWindow=60000",
  "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)

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?
@gr2m
Copy link
Member

gr2m commented Jan 6, 2018

@amejiarosario the error message is currently rather confusing, see #1029 (comment)

If you could create a minimal test case like https://runkit.com/gr2m/5a2b2a9b890d1b00120eada4, that’d be helpful for folks to help you.

Even better, investigate the problem and send a PR. If you use nock at relevant projects at Cisco it would be great if you could get some time to help out maintain nock, it could really use an overhaul but I can’t do it myself :)

@stale
Copy link

stale bot commented Sep 14, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@stale stale bot added the stale label Sep 14, 2018
@stale stale bot closed this as completed Sep 21, 2018
@lock
Copy link

lock bot commented Oct 5, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

@lock lock bot locked as resolved and limited conversation to collaborators Oct 5, 2018
@nockbot
Copy link
Collaborator

nockbot commented Nov 3, 2018

🎉 This issue has been resolved in version 10.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants