Skip to content

Commit

Permalink
Prototype of basic fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
mccoyp committed Jun 28, 2022
1 parent 56e5b3f commit b907890
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/azure-sdk-tools/devtools_testutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .envvariable_loader import EnvironmentVariableLoader
PowerShellPreparer = EnvironmentVariableLoader # Backward compat
from .proxy_startup import start_test_proxy, stop_test_proxy, test_proxy
from .proxy_testcase import recorded_by_proxy
from .proxy_testcase import recorded_by_proxy, recorded_test
from .sanitizers import (
add_body_key_sanitizer,
add_body_regex_sanitizer,
Expand Down Expand Up @@ -66,6 +66,7 @@
"PowerShellPreparer",
"EnvironmentVariableLoader",
"recorded_by_proxy",
"recorded_test",
"test_proxy",
"set_bodiless_matcher",
"set_custom_default_matcher",
Expand Down
41 changes: 41 additions & 0 deletions tools/azure-sdk-tools/devtools_testutils/proxy_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,44 @@ def combined_call(*args, **kwargs):
return test_output

return record_wrap


@pytest.fixture
def recorded_test(request):
if sys.version_info.major == 2 and not is_live():
pytest.skip("Playback testing is incompatible with the azure-sdk-tools test proxy on Python 2")

def transform_args(*args, **kwargs):
copied_positional_args = list(args)
request = copied_positional_args[1]

transform_request(request, recording_id)

return tuple(copied_positional_args), kwargs

if is_live_and_not_recording():
return

test_id = get_test_id()
recording_id, variables = start_record_or_playback(test_id)
original_transport_func = RequestsTransport.send

def combined_call(*args, **kwargs):
adjusted_args, adjusted_kwargs = transform_args(*args, **kwargs)
result = original_transport_func(*adjusted_args, **adjusted_kwargs)

# make the x-recording-upstream-base-uri the URL of the request
# this makes the request look like it was made to the original endpoint instead of to the proxy
# without this, things like LROPollers can get broken by polling the wrong endpoint
parsed_result = url_parse.urlparse(result.request.url)
upstream_uri = url_parse.urlparse(result.request.headers["x-recording-upstream-base-uri"])
upstream_uri_dict = {"scheme": upstream_uri.scheme, "netloc": upstream_uri.netloc}
original_target = parsed_result._replace(**upstream_uri_dict).geturl()

result.request.url = original_target
return result

RequestsTransport.send = combined_call
yield # test gets run here
RequestsTransport.send = original_transport_func # test finished running -- tear down
stop_record_or_playback(test_id, recording_id, None) # TODO: how do we provide variables to record?

0 comments on commit b907890

Please sign in to comment.