LinkedInAttic / sepia

Sepia is a VCR-like module for node.js that records HTTP interactions, then plays them back exactly like the first time they were invoked
Apache License 2.0
279 stars 32 forks source link

Substitution #22

Closed aneilbaboo closed 6 years ago

aneilbaboo commented 7 years ago

Note: This replaces the previous PR #20 (which was from my master branch).

Adds substitution capability suggested in #13.

Now you can do:

var sepia = require('sepia');

sepia.substitute('<MYSECRET>', function () { return process.env.MY_ACCESS_TOKEN; });
sepia.substitute('<MYSECRET2>', function () { return process.env.MY_ACCESS_TOKEN2; });
// etc.

And the fixtures will contain <MYSECRET> and <MYSECRET2>, while the requests will contain the value of MY_ACCESS_TOKEN and MY_ACCESS_TOKEN2.

I added tests, but couldn't quite figure out how to test the fixture files themselves. I did check that they look right, though.

@avik-das - Could you take a look?

jimmyeisenhauer commented 7 years ago

I like this idea a lot. Trying to understand the code. Does it have limits to how many secrets you can set up? Or limits on how many secrets can be in a single response?

aneilbaboo commented 7 years ago

@jimmyeisenhauer No limits. Just call sepia.substitute(...) with a new key and function.

Updated the PR body to make that clearer.

jimmyeisenhauer commented 7 years ago

thanks for the info! I got your PR working. I think it will be really helpful for my needs and my guess others as well. Not just secrets but any kind of replacement you want to do. I am working on DateTime values to be relative instead of hard coded.

I did notice it is only 1 replacement per secret per response. For example if you had actual_value multiple times in the api response that you wanted to sub you would need to create two secret_values with the same actual_value.

I was looking at maybe looping the substituteWithRealValues but curious if you tried this and if performance takes too big of a hit?

aneilbaboo commented 7 years ago

Thanks for catching that, @jimmyeisenhauer. I pushed a fix which uses text.split(key).replace(value) to do the substitution in a single pass per substituteWith* call.

aneilbaboo commented 7 years ago

Is anyone still maintaining this repo? There are a couple of other PRs that would be nice to get merged.

Ping @vslosh @egoldblum @ethankao @deepankgupta @salvipriyanka @ashimaatul

egoldblum commented 7 years ago

@aneilbaboo I'm not at LinkedIn anymore, but I'm trying to track down someone there to get the ball rolling. Thanks for your patience.

aneilbaboo commented 7 years ago

Awesome. Thanks.

aneilbaboo commented 7 years ago

@egoldblum - any luck finding the maintainers?

asakusuma commented 7 years ago

@aneilbaboo I'll try to find you a maintainer

mseminatore commented 7 years ago

@egoldblum This seems like a nice fix. It would be really good to get this and other PR's (like my bug fix for Windows) merged. Collecting too many good fixes spread across forks for now.