assaf / node-replay

When API testing slows you down: record and replay HTTP responses like a boss
http://documentup.com/assaf/node-replay
MIT License
522 stars 107 forks source link

User defined naming of files #158

Closed andrewluetgers closed 3 years ago

andrewluetgers commented 6 years ago

allows user to provide a uidFn option to define how to name mock files.

function mockName(method, url) {
  return (method + "|" + url).replace(/\//g, "_").replace("?", "__").replace(/&/g, "--") + ".txt";
}

let Replay = require('replay');
Replay.mode = 'record';
Replay.fixtures = myFixturesPath;
Replay.uidFn = req => mockName(req.method, req.url.host+req.url.path);
djanowski commented 5 years ago

Hi @andrewluetgers, thanks for your PR!

I don't think we're going to make this change, for now. However I would like to give you feedback anyway:

andrewluetgers commented 5 years ago

Sorry for the low quality PR.

The PR includes a bunch of changes which are unrelated to your proposed change

Its clear there are two things in here that I should have submitted separately.

1: Custom file names motivation - now I can easily find a file based on the request if I want to edit it, also helps in understanding what mocks I have in place. Of course there are any number of naming schemes one could use which includes adding a random value to eliminate the risk duplicates if one chooses.

2: prettified JSON motivation - for easy diffing and manual editing of JSON responses that are normally sent in a single line.

what happens when we have two files with the same name?

The file gets overwritten but you would have to commit a change and could view the diff and your tests may fail so the issue will rear its head quickly if it is an issue. You would then change the naming scheme and/or your tests.

So our front-end Puppeteer testing process looks like this:

ericclemmons commented 5 years ago

I'd like this as well (none of the libraries I've found seem to support it!):

A good example of this problem is with OAUTH APIs:

andrewluetgers commented 5 years ago

@ericclemmons the way I implemented this you provide a function that returns a file name given the request object so this could be easily handled. However I believe nested folder paths would need more coding to support. In my case I just wanted a basic request path as the file name.

function mockName(method, url) {
    return (method + "|" + url).replace(/\//g, "_").replace("?", "__").replace(/&/g, "--");
}

Replay.uidFn = req => mockName(req.method, req.url.host+ req.url.path) + ".txt";
capaj commented 3 years ago

this should be closed IMHO in favor of https://github.com/assaf/node-replay/pull/162