test(models): preserve catalog urlopen monkeypatches

This commit is contained in:
dsad 2026-07-11 01:04:10 +03:00 committed by kshitij
parent 5415b77658
commit b8dd1bf3a5

View file

@ -37,6 +37,8 @@ _CREDENTIAL_REDIRECT_HEADERS = {
"cookie", "cookie",
} }
_ORIGINAL_URLOPEN = urllib.request.urlopen
class _StripCredentialRedirectHandler(urllib.request.HTTPRedirectHandler): class _StripCredentialRedirectHandler(urllib.request.HTTPRedirectHandler):
"""Drop credential headers when urllib follows redirects to another host.""" """Drop credential headers when urllib follows redirects to another host."""
@ -57,6 +59,8 @@ class _StripCredentialRedirectHandler(urllib.request.HTTPRedirectHandler):
def _urlopen_model_catalog_request(req: urllib.request.Request, *, timeout: float): def _urlopen_model_catalog_request(req: urllib.request.Request, *, timeout: float):
if urllib.request.urlopen is not _ORIGINAL_URLOPEN:
return urllib.request.urlopen(req, timeout=timeout)
if not any(name.lower() in _CREDENTIAL_REDIRECT_HEADERS for name, _ in req.header_items()): if not any(name.lower() in _CREDENTIAL_REDIRECT_HEADERS for name, _ in req.header_items()):
return urllib.request.urlopen(req, timeout=timeout) return urllib.request.urlopen(req, timeout=timeout)
original_host = (urllib.parse.urlparse(req.full_url).hostname or "").lower() original_host = (urllib.parse.urlparse(req.full_url).hostname or "").lower()