8000 handle older versions of urllib3 · Sivateja0689/python-sasctl@c1ede59 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1ede59

Browse files
committed
handle older versions of urllib3
1 parent fbfe272 commit c1ede59

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/sasctl/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,14 @@ def __init__(self, hostname,
240240
if 'REQUESTS_CA_BUNDLE' not in os.environ:
241241
if verify_ssl:
242242
# Skip hostname verification if IP address specified instead
243-
# of DNS name. Prevents error from urllib3
244-
from urllib3.util.ssl_ import is_ipaddress
243+
# of DNS name. Prevents error from urllib3.
244+
try:
245+
from urllib3.util.ssl_ import is_ipaddress
246+
except ImportError:
247+
# is_ipaddres not present in older versions of urllib3
248+
def is_ipaddress(hst):
249+
return re.match(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$", hst)
250+
245251
verify_hostname = not is_ipaddress(hostname)
246252
adapter = SSLContextAdapter(assert_hostname=verify_hostname)
247253

tests/unit/test_session.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def test_ssl_context():
279279
del os.environ['REQUESTS_CA_BUNDLE']
280280

281281

282-
def test_verify_ssl():
282+
def test_verify_ssl(missing_packages):
283283
with mock.patch('sasctl.core.Session.get_token', return_value='token'):
284284
# Should verify SSL by default
285285
s = Session('hostname', 'username', 'password')
@@ -310,6 +310,12 @@ def test_verify_ssl():
310310
s = Session('hostname', 'username', 'password', verify_ssl=True)
311311
assert s.verify == True
312312

313+
with missing_packages('urllib3.util.ssl_'):
314+
# IP Address validation should work even if urllib3 import fails
315+
s = Session('127.0.0.1', 'username', 'password', verify_ssl=True)
316+
assert s.verify == True
317+
318+
313319

314320
def test_kerberos():
315321
with mock.patch('sasctl.core.Session._get_token_with_kerberos',
@@ -323,6 +329,7 @@ def test_kerberos():
323329
s = Session('hostname', 'username@REALM')
324330
assert s.auth.token == 'token'
325331

332+
326333
def test_authentication_failure():
327334
from sasctl.exceptions import AuthenticationError
328335

0 commit comments

Comments
 (0)
0