Skip to content

Commit

Permalink
Fixed Channel to handle the case where ssl module is not available, c…
Browse files Browse the repository at this point in the history
…loses #194
  • Loading branch information
vmagamedov committed Jun 30, 2024
1 parent 5916cba commit b98d2a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 4 additions & 4 deletions grpclib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,10 @@ def __init__(
if port is None:
port = 50051

if ssl is not None and _ssl is None:
raise RuntimeError('SSL is not supported.')

if ssl is True:
if _ssl is None:
if ssl is not None:
raise RuntimeError('SSL is not supported')
elif ssl is True:
ssl = self._get_default_ssl_context()
elif isinstance(ssl, _ssl.DefaultVerifyPaths):
ssl = self._get_default_ssl_context(verify_paths=ssl)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_client_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import certifi
from h2.connection import H2Connection

import grpclib.client
from grpclib.client import Channel, Handler
from grpclib.config import Configuration
from grpclib.protocol import H2Protocol
Expand Down Expand Up @@ -95,3 +96,11 @@ def test_default_verify_paths():
po.assert_called_once_with(tf, td, None)
assert default_verify_paths.openssl_cafile_env == "SSL_CERT_FILE"
assert default_verify_paths.openssl_capath_env == "SSL_CERT_DIR"


def test_no_ssl_support():
with patch.object(grpclib.client, "_ssl", None):
Channel()
with pytest.raises(RuntimeError) as err:
Channel(ssl=True)
err.match("SSL is not supported")

0 comments on commit b98d2a0

Please sign in to comment.