diff --git a/test-requirements.txt b/test-requirements.txt index ab4c471..11f684f 100755 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ nose==1.2.1 requests-mock==0.7.0 -pyhamcrest==1.8.1 \ No newline at end of file +pyhamcrest==1.8.1 +flexmock \ No newline at end of file diff --git a/tests/api_test.py b/tests/api_test.py index a54281c..7836f14 100644 --- a/tests/api_test.py +++ b/tests/api_test.py @@ -13,7 +13,9 @@ # limitations under the License. import base64 import unittest +import requests +from flexmock import flexmock, flexmock_teardown from hamcrest import assert_that, equal_to, raises, calling import requests_mock from requests_mock.exceptions import NoMockAddress @@ -28,6 +30,9 @@ def setUp(self): self.username = 'admin' self.password = 'test' + def tearDown(self): + flexmock_teardown() + @requests_mock.mock() def test_api_method_returns_without_arguments(self, request_mock): json_data = [ @@ -173,6 +178,28 @@ def test_api_http_post_method_raises_on_result_414(self, request_mock): assert_that(calling(ubersmith_api.support.ticket_submit.http_post), raises(UnknownError)) + def test_api_http_timeout(self): + payload = dict(status=True, data="plop") + response = flexmock(status_code=200, json=lambda: payload) + ubersmith_api = api.init(self.url, self.username, self.password, 666) + + flexmock(requests).should_receive("get").with_args( + url=self.url, auth=(self.username, self.password), timeout=666, params={'method': 'uber.method_list'} + ).and_return(response) + + ubersmith_api.uber.method_list() + + def test_api_http_default_timeout(self): + payload = dict(status=True, data="plop") + response = flexmock(status_code=200, json=lambda: payload) + ubersmith_api = api.init(self.url, self.username, self.password) + + flexmock(requests).should_receive("get").with_args( + url=self.url, auth=(self.username, self.password), timeout=60, params={'method': 'uber.method_list'} + ).and_return(response) + + ubersmith_api.uber.method_list() + @requests_mock.mock() def test_api_http_post_method_raises_on_result_500(self, request_mock): json_data = { diff --git a/ubersmith_client/api.py b/ubersmith_client/api.py index 5796838..2caf125 100644 --- a/ubersmith_client/api.py +++ b/ubersmith_client/api.py @@ -16,28 +16,30 @@ from ubersmith_client.exceptions import UbersmithException, get_exception_for -def init(url, user, password): - return UbersmithApi(url, user, password) +def init(url, user, password, timeout=60): + return UbersmithApi(url, user, password, timeout) class UbersmithApi(object): - def __init__(self, url, user, password): + def __init__(self, url, user, password, timeout): self.url = url self.user = user self.password = password + self.timeout = timeout def __getattr__(self, module): - return UbersmithRequest(self.url, self.user, self.password, module) + return UbersmithRequest(self.url, self.user, self.password, module, self.timeout) class UbersmithRequest(object): - def __init__(self, url, user, password, module): + def __init__(self, url, user, password, module, timeout): self.url = url self.user = user self.password = password self.module = module self.methods = [] self.http_methods = {'GET': 'get', 'POST': 'post'} + self.timeout = timeout def __getattr__(self, function): self.methods.append(function) @@ -48,7 +50,13 @@ def __call__(self, **kwargs): def process_request(self, http_method, **kwargs): callable_http_method = getattr(requests, http_method) - response = callable_http_method(self.url, auth=(self.user, self.password), **kwargs) + + response = callable_http_method( + self.url, + auth=(self.user, self.password), + timeout=self.timeout, + **kwargs + ) if response.status_code < 200 or response.status_code >= 400: raise get_exception_for(status_code=response.status_code)