|
51 | 51 | _get_socket_opts, |
52 | 52 | _remove_certs_for_non_https, |
53 | 53 | ) |
| 54 | +from tests.client.utils import temp_env |
54 | 55 | from tests.conftest import REQUEST_PATH, fake_response |
55 | 56 |
|
56 | | -CA_CERT_PATH = certifi.where() |
57 | | - |
58 | 57 | mocked_request = MagicMock(spec=urllib3.response.HTTPResponse) |
59 | 58 |
|
| 59 | + |
60 | 60 | def fake_request(response=None): |
61 | 61 | def request(*args, **kwargs): |
62 | 62 | if isinstance(response, list): |
@@ -373,14 +373,26 @@ def test_params(): |
373 | 373 |
|
374 | 374 |
|
375 | 375 | 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 |
384 | 396 |
|
385 | 397 |
|
386 | 398 | class ClientAddressRequestHandler(BaseHTTPRequestHandler): |
@@ -444,28 +456,6 @@ def test_client_keepalive(self): |
444 | 456 | self.assertEqual(result, another_result) |
445 | 457 |
|
446 | 458 |
|
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 | | - |
469 | 459 | class TimeoutRequestHandler(BaseHTTPRequestHandler): |
470 | 460 | """ |
471 | 461 | HTTP handler for use with TestingHTTPServer |
|
0 commit comments