tavern icon indicating copy to clipboard operation
tavern copied to clipboard

How to parametrize the proxy used by the underlying request call?

Open gc-ss opened this issue 4 years ago • 4 comments

Assume I am testing an endpoint that returns the current timestamp and I have code that operates on that timestamp. This code is under test by tavern.

I would like to parametrize the same test to run twice - once against a cached/known timestamp and then live/unknown timestamp.

If the test that runs against the known timestamp passes but fails against the unknown timestamp, I can be sure there wasn't a regression in my code while I was refactoring my code.

The underlying requests call is prepared here:

https://github.com/taverntesting/tavern/blob/c1f9da82590e85afd5a4b232963d9539ce08b954/tavern/_plugins/rest/request.py#L457

requests has a proxy option that can be passed in in the function call.

I would like to pass that proxy option as a parameter from tavern test so that I can run the same test through multiple proxy settings.

Here's a more detailed description of a usecase of service virtualization that requires this kind of parametrization: https://github.com/wrike/pytest-hoverfly/issues/3#issuecomment-821752084

I see a related issue that exists but that was closed by the OP https://github.com/taverntesting/tavern/issues/530

I also left a comment on another related issue where this usecase of service virtualization wasn't clear: https://github.com/taverntesting/tavern/issues/103

gc-ss avatar Apr 17 '21 02:04 gc-ss

This isn't supported at the moment (Tavern tries to focus more on the application layer than anything below that) but it might not be too difficult to add.

michaelboulton avatar Apr 18 '21 12:04 michaelboulton

This isn't supported at the moment (Tavern tries to focus more on the application layer than anything below that) but it might not be too difficult to add.

Thank you Michael, I appreciate it.

From my POV, I am thinking it's a matter of:

  1. adding proxies (https://docs.python-requests.org/en/latest/api/#requests.request) to _request_args: https://github.com/taverntesting/tavern/blob/c1f9da82590e85afd5a4b232963d9539ce08b954/tavern/_plugins/rest/request.py#L391
  2. that makes proxies available in self._request_args: https://github.com/taverntesting/tavern/blob/c1f9da82590e85afd5a4b232963d9539ce08b954/tavern/_plugins/rest/request.py#L435
  3. … hence when session.request(**self._request_args) is run, proxies is made to available the request : https://github.com/taverntesting/tavern/blob/c1f9da82590e85afd5a4b232963d9539ce08b954/tavern/_plugins/rest/request.py#L457

let me know what you think - is this the right approach?

If so, if I put up a PR with these changes, what additional steps would be needed for you to merge it in?

gc-ss avatar Apr 18 '21 23:04 gc-ss

If you want to make a PR then go ahead, it does look fairly simple to fix.

michaelboulton avatar May 01 '21 10:05 michaelboulton

If you want to make a PR then go ahead, it does look fairly simple to fix.

Will do. I have not made a PR to taverntesting before. Aside from ensuring the code I submit is PEP8 compliant, what are some other things to keep in mind?

Also, do you have some suggestions/references for me to look into or follow when writing tests for this update/change?

gc-ss avatar May 03 '21 05:05 gc-ss