Skip to content

Commit

Permalink
Support 'verify' option
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlockwood committed Oct 17, 2017
1 parent 969f30c commit 3b972b3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
20 changes: 13 additions & 7 deletions httsleep/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class HttSleeper(object):
function that takes the response as an argument returning True.
:param auth: a (username, password) tuple for HTTP authentication.
:param headers: a dict of HTTP headers.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param polling_interval: how many seconds to sleep between requests.
:param max_retries: the maximum number of retries to make, after which
a StopIteration exception is raised.
Expand All @@ -46,7 +49,7 @@ class HttSleeper(object):
"""
def __init__(self, url_or_request, until=None, alarms=None,
status_code=None, json=None, jsonpath=None, text=None, callback=None,
auth=None, headers=None,
auth=None, headers=None, verify=None,
polling_interval=DEFAULT_POLLING_INTERVAL,
max_retries=DEFAULT_MAX_RETRIES,
ignore_exceptions=None,
Expand Down Expand Up @@ -90,6 +93,7 @@ def __init__(self, url_or_request, until=None, alarms=None,
self.session = requests.Session()
self.log = logging.getLogger()
self.log.setLevel(loglevel)
self.verify = verify

def _set_conditions(self, attribute, conditions):
value = []
Expand Down Expand Up @@ -145,7 +149,7 @@ def run(self):
"""
while True:
try:
response = self.session.send(self.request.prepare())
response = self.session.send(self.request.prepare(), verify=self.verify)
for condition in self.alarms:
if self.meets_condition(response, condition):
raise Alarm(response, condition)
Expand Down Expand Up @@ -192,9 +196,9 @@ def meets_condition(response, condition):
return True


def httsleep(url_or_request, until=None, alarms=None,
status_code=None, json=None, jsonpath=None, text=None, callback=None,
auth=None, headers=None,
def httsleep(url_or_request, until=None, alarms=None, status_code=None,
json=None, jsonpath=None, text=None, callback=None,
auth=None, headers=None, verify=None,
polling_interval=DEFAULT_POLLING_INTERVAL,
max_retries=DEFAULT_MAX_RETRIES,
ignore_exceptions=None,
Expand All @@ -207,7 +211,9 @@ def httsleep(url_or_request, until=None, alarms=None,
return HttSleeper(
url_or_request, until=until, alarms=alarms, status_code=status_code,
json=json, jsonpath=jsonpath, text=text, callback=callback,
auth=auth, headers=headers, polling_interval=polling_interval,
max_retries=max_retries, ignore_exceptions=ignore_exceptions,
auth=auth, headers=headers, verify=verify,
polling_interval=polling_interval,
max_retries=max_retries,
ignore_exceptions=ignore_exceptions,
loglevel=loglevel
).run()
5 changes: 5 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def test_headers():
assert obj.request.headers == headers


def test_verify():
obj = HttSleeper(URL, CONDITION, verify=False)
assert obj.kwargs == {'verify': False}


def test_ignore_exceptions_default_value():
obj = HttSleeper(URL, CONDITION)
assert obj.ignore_exceptions == tuple()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def test_run_success():
assert not mock_sleep.called


@httpretty.activate
def test_run_success_with_verify():
"""Should return response when a success criteria has been reached"""
httpretty.register_uri(httpretty.GET, URL, body='<html></html>', status=200)
with mock.patch('httsleep.main.sleep') as mock_sleep:
httsleep = HttSleeper(URL, {'status_code': 200}, verify=False)
resp = httsleep.run()
assert resp.status_code == 200
assert not mock_sleep.called


@httpretty.activate
def test_run_alarm():
"""Should raise an Alarm when a failure criteria has been reached"""
Expand Down

0 comments on commit 3b972b3

Please sign in to comment.