Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from __future__ import annotations

import errno
import os.path
import socket # noqa: F401
import typing
Expand Down Expand Up @@ -329,9 +330,10 @@ def cert_verify(
cert_loc = DEFAULT_CA_BUNDLE_PATH

if not cert_loc or not os.path.exists(cert_loc):
raise OSError(
f"Could not find a suitable TLS CA certificate bundle, "
f"invalid path: {cert_loc}"
raise FileNotFoundError(
errno.ENOENT,
"Could not find a suitable TLS CA certificate bundle, invalid path",
cert_loc
)

conn.cert_reqs = "CERT_REQUIRED"
Expand All @@ -353,13 +355,16 @@ def cert_verify(
conn.cert_file = cert
conn.key_file = None
if conn.cert_file and not os.path.exists(conn.cert_file):
raise OSError(
f"Could not find the TLS certificate file, "
f"invalid path: {conn.cert_file}"
raise FileNotFoundError(
errno.ENOENT,
"Could not find the TLS certificate file, invalid path",
conn.cert_file
)
if conn.key_file and not os.path.exists(conn.key_file):
raise OSError(
f"Could not find the TLS key file, invalid path: {conn.key_file}"
raise FileNotFoundError(
errno.ENOENT,
"Could not find the TLS key file, invalid path",
conn.key_file
)

def build_response(self, req: PreparedRequest, resp: Any) -> Response:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ def test_invalid_ca_certificate_path(self, httpbin_secure):
requests.get(httpbin_secure(), verify=INVALID_PATH)
assert (
str(e.value)
== f"Could not find a suitable TLS CA certificate bundle, invalid path: {INVALID_PATH}"
== f"[Errno 2] Could not find a suitable TLS CA certificate bundle, invalid path: '{INVALID_PATH}'"
)

def test_invalid_ssl_certificate_files(self, httpbin_secure):
Expand All @@ -981,13 +981,13 @@ def test_invalid_ssl_certificate_files(self, httpbin_secure):
requests.get(httpbin_secure(), cert=INVALID_PATH)
assert (
str(e.value)
== f"Could not find the TLS certificate file, invalid path: {INVALID_PATH}"
== f"[Errno 2] Could not find the TLS certificate file, invalid path: '{INVALID_PATH}'"
)

with pytest.raises(IOError) as e:
requests.get(httpbin_secure(), cert=(".", INVALID_PATH))
assert str(e.value) == (
f"Could not find the TLS key file, invalid path: {INVALID_PATH}"
f"[Errno 2] Could not find the TLS key file, invalid path: '{INVALID_PATH}'"
)

@pytest.mark.parametrize(
Expand Down