Skip to content

Commit 4a2fbf8

Browse files
committed
Migrate client ca tests
1 parent 8cc192d commit 4a2fbf8

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

tests/client/test_http.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
_get_socket_opts,
5252
_remove_certs_for_non_https,
5353
)
54+
from tests.client.utils import temp_env
5455
from tests.conftest import REQUEST_PATH, fake_response
5556

56-
CA_CERT_PATH = certifi.where()
57-
5857
mocked_request = MagicMock(spec=urllib3.response.HTTPResponse)
5958

59+
6060
def fake_request(response=None):
6161
def request(*args, **kwargs):
6262
if isinstance(response, list):
@@ -373,14 +373,26 @@ def test_params():
373373

374374

375375
def test_client_ca():
376-
os.environ["REQUESTS_CA_BUNDLE"] = CA_CERT_PATH
377-
try:
378-
Client("http://127.0.0.1:4200")
379-
except ProgrammingError:
380-
pytest.fail("HTTP not working with REQUESTS_CA_BUNDLE")
381-
finally:
382-
os.unsetenv("REQUESTS_CA_BUNDLE")
383-
os.environ["REQUESTS_CA_BUNDLE"] = ""
376+
"""
377+
Verify that if env variable `REQUESTS_CA_BUNDLE` is set, certs are loaded into the pool.
378+
"""
379+
with temp_env(REQUESTS_CA_BUNDLE=certifi.where()):
380+
client = Client("http://127.0.0.1:4200")
381+
assert 'ca_certs' in client._pool_kw
382+
383+
384+
def test_remove_certs_for_non_https():
385+
"""
386+
Verify that `_remove_certs_for_non_https` correctly removes ca_certs.
387+
"""
388+
d = _remove_certs_for_non_https("https", {"ca_certs": 1})
389+
assert "ca_certs" in d
390+
391+
kwargs = {"ca_certs": 1, "foobar": 2, "cert_file": 3}
392+
d = _remove_certs_for_non_https("http", kwargs)
393+
assert 'ca_certs' not in d
394+
assert 'cert_file' not in d
395+
assert 'foobar' in d
384396

385397

386398
class ClientAddressRequestHandler(BaseHTTPRequestHandler):
@@ -444,28 +456,6 @@ def test_client_keepalive(self):
444456
self.assertEqual(result, another_result)
445457

446458

447-
class RequestsCaBundleTest(TestCase):
448-
def test_open_client(self):
449-
os.environ["REQUESTS_CA_BUNDLE"] = CA_CERT_PATH
450-
try:
451-
Client("http://127.0.0.1:4200")
452-
except ProgrammingError:
453-
self.fail("HTTP not working with REQUESTS_CA_BUNDLE")
454-
finally:
455-
os.unsetenv("REQUESTS_CA_BUNDLE")
456-
os.environ["REQUESTS_CA_BUNDLE"] = ""
457-
458-
def test_remove_certs_for_non_https(self):
459-
d = _remove_certs_for_non_https("https", {"ca_certs": 1})
460-
self.assertIn("ca_certs", d)
461-
462-
kwargs = {"ca_certs": 1, "foobar": 2, "cert_file": 3}
463-
d = _remove_certs_for_non_https("http", kwargs)
464-
self.assertNotIn("ca_certs", d)
465-
self.assertNotIn("cert_file", d)
466-
self.assertIn("foobar", d)
467-
468-
469459
class TimeoutRequestHandler(BaseHTTPRequestHandler):
470460
"""
471461
HTTP handler for use with TestingHTTPServer

tests/client/test_serialization.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_naive_datetime_serialization():
7676

7777
def test_aware_datetime_serialization():
7878
"""
79-
Verify that a `datetime` that is tz aware type can be serialized.
79+
Verify that a `datetime` that is tz aware type can be serialized.
8080
"""
8181
data = dt.datetime.fromisoformat("2023-06-26T09:24:00.123+02:00")
8282
result = json_dumps(data)
@@ -106,13 +106,11 @@ def test_date_serialization():
106106
assert result == b'1461196800000'
107107

108108

109-
110109
def test_uuid_serialization():
111110
"""
112111
Verify that a `uuid.UUID` can be serialized. We do not care about specific uuid versions,
113-
just the object that is re-used across all versions by the uuid module.
112+
just the object that is re-used across all versions of the uuid module.
114113
"""
115114
data = uuid.UUID(bytes=(50583033507982468033520929066863110751).to_bytes(16), version=4)
116115
result = json_dumps(data)
117116
assert result == b'"260df019-a183-431f-ad46-115ccdf12a5f"'
118-

0 commit comments

Comments
 (0)