Skip to content

Commit 0645f49

Browse files
authored
[Test Proxy] Make add_sanitizer a module-level method (Azure#20701)
1 parent ddf49b1 commit 0645f49

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

tools/azure-sdk-tools/devtools_testutils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .mgmt_testcase import AzureMgmtTestCase, AzureMgmtPreparer
2-
from .azure_recorded_testcase import AzureRecordedTestCase
2+
from .azure_recorded_testcase import add_sanitizer, AzureRecordedTestCase
33
from .azure_testcase import AzureTestCase, is_live, get_region_override
44
from .resource_testcase import (
55
FakeResource,
@@ -21,6 +21,7 @@
2121
from .fake_credential import FakeTokenCredential
2222

2323
__all__ = [
24+
"add_sanitizer",
2425
"AzureMgmtTestCase",
2526
"AzureMgmtPreparer",
2627
"AzureRecordedTestCase",

tools/azure-sdk-tools/devtools_testutils/azure_recorded_testcase.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,42 @@
3131
pass
3232

3333
if TYPE_CHECKING:
34-
from typing import Optional
34+
from typing import Any
3535

3636

3737
load_dotenv(find_dotenv())
3838

3939

40+
def add_sanitizer(sanitizer, **kwargs):
41+
# type: (ProxyRecordingSanitizer, **Any) -> None
42+
"""Registers a sanitizer, matcher, or transform with the test proxy.
43+
44+
:param sanitizer: The name of the sanitizer, matcher, or transform you want to add.
45+
:type sanitizer: ProxyRecordingSanitizer or str
46+
47+
:keyword str value: The substitution value.
48+
:keyword str regex: A regex for a sanitizer. Can be defined as a simple regex, or if a ``group_for_replace`` is
49+
provided, a substitution operation.
50+
:keyword str group_for_replace: The capture group that needs to be operated upon. Do not provide if you're invoking
51+
a simple replacement operation.
52+
"""
53+
request_args = {}
54+
request_args["value"] = kwargs.get("value") or "fakevalue"
55+
request_args["regex"] = kwargs.get("regex") or "[a-z]+(?=(?:-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)"
56+
request_args["group_for_replace"] = kwargs.get("group_for_replace")
57+
58+
if sanitizer == ProxyRecordingSanitizer.URI:
59+
requests.post(
60+
"{}/Admin/AddSanitizer".format(PROXY_URL),
61+
headers={"x-abstraction-identifier": ProxyRecordingSanitizer.URI.value},
62+
json={
63+
"regex": request_args["regex"],
64+
"value": request_args["value"],
65+
"groupForReplace": request_args["group_for_replace"]
66+
},
67+
)
68+
69+
4070
def is_live():
4171
"""A module version of is_live, that could be used in pytest marker."""
4272
if not hasattr(is_live, "_cache"):
@@ -81,18 +111,6 @@ def in_recording(self):
81111
def recording_processors(self):
82112
return []
83113

84-
def add_sanitizer(self, sanitizer, regex=None, value=None):
85-
# type: (ProxyRecordingSanitizer, Optional[str], Optional[str]) -> None
86-
if sanitizer == ProxyRecordingSanitizer.URI:
87-
requests.post(
88-
"{}/Admin/AddSanitizer".format(PROXY_URL),
89-
headers={"x-abstraction-identifier": ProxyRecordingSanitizer.URI.value},
90-
json={
91-
"regex": regex or "[a-z]+(?=(?:-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)",
92-
"value": value or "fakevalue"
93-
},
94-
)
95-
96114
def is_playback(self):
97115
return not self.is_live
98116

0 commit comments

Comments
 (0)