Skip to content
Permalink
Browse files

added timeout option

  • Loading branch information...
WilliamMayor committed Jan 29, 2014
1 parent 15dcfc7 commit b2beaef316ddd7a846e3a800a87026abeb1245c6
Showing with 70 additions and 24 deletions.
  1. +8 −4 hapy/hapy.py
  2. +62 −20 tests/hapy_tests.py
@@ -26,7 +26,7 @@ def __init__(self, r):

class Hapy:

def __init__(self, base_url, username=None, password=None, insecure=True):
def __init__(self, base_url, username=None, password=None, insecure=True, timeout=None):
if base_url.endswith('/'):
base_url = base_url[:-1]
self.base_url = '%s/engine' % base_url
@@ -35,6 +35,7 @@ def __init__(self, base_url, username=None, password=None, insecure=True):
else:
self.auth = None
self.insecure = insecure
self.timeout = timeout

def _http_post(self, url, data, code=200):
r = requests.post(
@@ -43,7 +44,8 @@ def _http_post(self, url, data, code=200):
headers=HEADERS,
auth=self.auth,
verify=not self.insecure,
allow_redirects=False
allow_redirects=False,
timeout=self.timeout
)
self.lastresponse = r
if r.status_code != code:
@@ -55,7 +57,8 @@ def _http_get(self, url, code=200):
url=url,
headers=HEADERS,
auth=self.auth,
verify=not self.insecure
verify=not self.insecure,
timeout=self.timeout
)
self.lastresponse = r
if r.status_code != code:
@@ -68,7 +71,8 @@ def _http_put(self, url, data, code=200):
data=data,
headers=HEADERS,
auth=self.auth,
verify=not self.insecure
verify=not self.insecure,
timeout=self.timeout
)
self.lastresponse = r
if r.status_code != code:
@@ -109,7 +109,8 @@ def test_create_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -130,7 +131,8 @@ def test_add_job_directory(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -150,7 +152,30 @@ def test_build_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@patch('hapy.hapy.requests')
def test_supply_timeout(mock_requests):
h = hapy.Hapy(BASE_URL, timeout=0.005)
r = Mock()
r.status_code = 303
r.request = Mock()
mock_requests.post.return_value = r
name = 'test_build_job'
h.build_job(name)
mock_requests.post.assert_called_with(
url='https://localhost:8443/engine/job/%s' % name,
data=dict(
action='build'
),
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False,
timeout=0.005
)


@@ -170,7 +195,8 @@ def test_launch_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -189,7 +215,8 @@ def test_rescan_job_directory(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -209,7 +236,8 @@ def test_pause_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -229,7 +257,8 @@ def test_unpause_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -249,7 +278,8 @@ def test_terminate_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -269,7 +299,8 @@ def test_teardown_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -290,7 +321,8 @@ def test_copy_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -312,7 +344,8 @@ def test_copy_job_as_profile(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -332,7 +365,8 @@ def test_checkpoint_job(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)


@@ -359,7 +393,8 @@ def test_execute_script(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)
assert_is_none(raw)
assert_is_none(html)
@@ -388,7 +423,8 @@ def test_execute_script_raw(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)
assert_equals("raw", raw)
assert_is_none(html)
@@ -417,7 +453,8 @@ def test_execute_script_html(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)
assert_equals("html", html)
assert_is_none(raw)
@@ -446,7 +483,8 @@ def test_execute_script_both(mock_requests):
auth=None,
verify=False,
headers={'accept': 'application/xml'},
allow_redirects=False
allow_redirects=False,
timeout=None
)
assert_equals("raw", raw)
assert_equals("html", html)
@@ -474,7 +512,8 @@ def test_submit_configuration(mock_requests):
data='cxml',
auth=None,
verify=False,
headers={'accept': 'application/xml'}
headers={'accept': 'application/xml'},
timeout=None
)


@@ -513,7 +552,8 @@ def test_get_info(mock_requests):
url='https://localhost:8443/engine',
auth=None,
verify=False,
headers={'accept': 'application/xml'}
headers={'accept': 'application/xml'},
timeout=None
)
assert_equals('3.1.1', info['engine']['heritrixVersion'])

@@ -534,7 +574,8 @@ def test_get_job_info(mock_requests):
url='https://localhost:8443/engine/job/%s' % name,
auth=None,
verify=False,
headers={'accept': 'application/xml'}
headers={'accept': 'application/xml'},
timeout=None
)
assert_equals('test', info['job']['shortName'])

@@ -564,6 +605,7 @@ def side_effect(**kwargs):
url='https://localhost:8443/engine/job/test/jobdir/crawler-beans.cxml',
auth=None,
verify=False,
headers={'accept': 'application/xml'}
headers={'accept': 'application/xml'},
timeout=None
)
assert_equals(cxml, config)

0 comments on commit b2beaef

Please sign in to comment.
You can’t perform that action at this time.