Skip to content

Commit 0268857

Browse files
authored
Move Stainless HTTP request header sanitizer into client library test code (#44231)
1 parent b943b28 commit 0268857

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

eng/tools/azure-sdk-tools/devtools_testutils/proxy_testcase.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,6 @@ def transform_httpx_request(request, recording_id: str) -> None:
189189
request.headers["x-recording-id"] = recording_id
190190
request.headers["x-recording-mode"] = "record" if is_live() else "playback"
191191

192-
# Remove all request headers that start with `x-stainless`, since they contain CPU info, OS info, etc.
193-
# Those change depending on which machine the tests are run on, so we cannot have a single test recording with those.
194-
headers_to_remove = [key for key in request.headers.keys() if key.lower().startswith("x-stainless")]
195-
for header in headers_to_remove:
196-
del request.headers[header]
197-
198192
# Rewrite URL to proxy
199193
updated_target = parsed_result._replace(**get_proxy_netloc()).geturl()
200194
request.url = httpx.URL(updated_target)

sdk/ai/azure-ai-projects/tests/conftest.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
import os
77
import pytest
88
from dotenv import load_dotenv, find_dotenv
9-
from devtools_testutils import remove_batch_sanitizers, add_general_regex_sanitizer, add_body_key_sanitizer
9+
from devtools_testutils import (
10+
remove_batch_sanitizers,
11+
add_general_regex_sanitizer,
12+
add_body_key_sanitizer,
13+
add_remove_header_sanitizer,
14+
)
1015

1116
if not load_dotenv(find_dotenv(), override=True):
1217
print("Did not find a .env file. Using default environment variable values for tests.")
@@ -101,6 +106,24 @@ def sanitize_url_paths():
101106
add_body_key_sanitizer(json_path="blobReference.credential.sasUri", value="sanitized-sas-uri")
102107
add_body_key_sanitizer(json_path="blobReferenceForConsumption.credential.sasUri", value="sanitized-sas-uri")
103108

109+
# Remove Stainless headers from OpenAI client requests, since they include platform and OS specific info, which we can't have in recorded requests.
110+
# Here is an example of all the `x-stainless` headers from a Responses call:
111+
# x-stainless-arch: other:amd64
112+
# x-stainless-async: false
113+
# x-stainless-lang: python
114+
# x-stainless-os: Windows
115+
# x-stainless-package-version: 2.8.1
116+
# x-stainless-read-timeout: 600
117+
# x-stainless-retry-count: 0
118+
# x-stainless-runtime: CPython
119+
# x-stainless-runtime-version: 3.14.0
120+
# Note that even though the doc string for `add_remove_header_sanitizer` says `condition` is supported, it is not implemented. So we can't do this:
121+
# add_remove_header_sanitizer(condition='{"uriRegex": "(?i)^x-stainless-.*$"}')
122+
# We have to explicitly list all the headers to remove:
123+
add_remove_header_sanitizer(
124+
headers="x-stainless-arch, x-stainless-async, x-stainless-lang, x-stainless-os, x-stainless-package-version, x-stainless-read-timeout, x-stainless-retry-count, x-stainless-runtime, x-stainless-runtime-version"
125+
)
126+
104127
# Remove the following sanitizers since certain fields are needed in tests and are non-sensitive:
105128
# - AZSDK3493: $..name
106129
# - AZSDK3430: $..id

0 commit comments

Comments
 (0)