Skip to content

Commit

Permalink
honor the scheme and verify arguments as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Emma May committed Nov 16, 2024
1 parent 5671705 commit 69a1caa
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
22 changes: 15 additions & 7 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ def __init__(
host: str | None = None,
port: int | None = None,
token: str | None = None,
scheme: str = "http",
scheme: str | None = None,
consistency: str = "default",
dc=None,
verify: bool = True,
verify: bool | None = None,
cert=None,
) -> None:
"""
Expand Down Expand Up @@ -118,11 +118,19 @@ def __init__(
if not port:
port = 8500

use_ssl = os.getenv("CONSUL_HTTP_SSL")
if use_ssl is not None:
scheme = "https" if use_ssl == "true" else "http"
if os.getenv("CONSUL_HTTP_SSL_VERIFY") is not None:
verify = os.getenv("CONSUL_HTTP_SSL_VERIFY") == "true"
if scheme is None:
use_ssl = os.getenv("CONSUL_HTTP_SSL")
if use_ssl:
scheme = "https" if use_ssl.lower() == "true" else "http"
else:
scheme = "http"

if verify is None:
ssl_verify = os.getenv("CONSUL_HTTP_SSL_VERIFY")
if ssl_verify:
verify = ssl_verify.lower() == "true"
else:
verify = True

self.http = self.http_connect(host, port, scheme, verify, cert)
self.token = os.getenv("CONSUL_HTTP_TOKEN", token)
Expand Down
80 changes: 78 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def __init__(
) -> None:
self.host = host
self.port = int(port)
self.scheme = scheme
self.verify = verify

def get(self, callback, path, params=None, headers=None): # pylint: disable=unused-argument
return Request("get", path, params, headers, None)
Expand All @@ -30,7 +32,7 @@ def delete(self, callback, path, params=None, headers=None): # pylint: disable=

class Consul(consul.base.Consul):
def http_connect(self, host: str, port: int, scheme, verify: bool = True, cert=None):
return HTTPClient(host, port, scheme, verify=verify, cert=None)
return HTTPClient(host=host, port=port, scheme=scheme, verify=verify, cert=None)


def _should_support(c: Consul) -> tuple[Callable[..., Any], ...]:
Expand Down Expand Up @@ -135,7 +137,7 @@ class TestBaseInit:
),
],
)
def test_base_init(self, monkeypatch, env, host, port, want) -> None:
def test_base_init_addr(self, monkeypatch, env, host, port, want) -> None:
if env:
monkeypatch.setenv("CONSUL_HTTP_ADDR", env)
else:
Expand All @@ -148,6 +150,80 @@ def test_base_init(self, monkeypatch, env, host, port, want) -> None:
assert c.http.host == want["host"]
assert c.http.port == want["port"]

@pytest.mark.parametrize(
("env", "scheme", "want"),
[
(
"true",
None,
"https"
),
(
"false",
None,
"http"
),
(
None,
"https",
"https"
),
(
None,
"http",
"http"
),
(
"http",
"https",
"https"
),
],
)
def test_base_init_scheme(self, monkeypatch, env, scheme, want) -> None:
if env:
monkeypatch.setenv("CONSUL_HTTP_SSL", env)
else:
try:
monkeypatch.delenv("CONSUL_HTTP_SSL")
except KeyError:
pass

c = Consul(scheme=scheme)
assert c.http.scheme == want

@pytest.mark.parametrize(
("env", "verify", "want"),
[
(
"true",
None,
True
),
(
None,
True,
True
),
(
"false",
True,
True
),
],
)
def test_base_init_verify(self, monkeypatch, env, verify, want) -> None:
if env:
monkeypatch.setenv("CONSUL_HTTP_SSL_VERIFY", env)
else:
try:
monkeypatch.delenv("CONSUL_HTTP_SSL_VERIFY")
except KeyError:
pass

c = Consul(verify=verify)
assert c.http.verify == want


class TestIndex:
"""
Expand Down

0 comments on commit 69a1caa

Please sign in to comment.