diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index deafc9f..b38d43e 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -332,6 +332,8 @@ def __init__(self, url, **kwargs): :param dav_endpoint_version: 1 (default) to force using a specific endpoint version instead of relying on capabilities :param debug: set to True to print debugging messages to stdout, defaults to False + :param timeout: The number of seconds that the client will wait for to respond, + or None to not set any timeout. """ if not url.endswith('/'): url += '/' @@ -341,6 +343,7 @@ def __init__(self, url, **kwargs): self._debug = kwargs.get('debug', False) self._verify_certs = kwargs.get('verify_certs', True) self._dav_endpoint_version = kwargs.get('dav_endpoint_version', 1) + self._timeout = kwargs.get('timeout', None) self._capabilities = None self._version = None @@ -1784,7 +1787,8 @@ def _make_ocs_request(self, method, service, action, **kwargs): print('OCS request: %s %s %s' % (method, self.url + path, attributes)) - res = self._session.request(method, self.url + path, **attributes) + res = self._session.request( + method, self.url + path, timeout=self._timeout, **attributes) return res def _make_dav_request(self, method, path, **kwargs): @@ -1806,6 +1810,7 @@ def _make_dav_request(self, method, path, **kwargs): res = self._session.request( method, self._webdav_url + parse.quote(self._encode_string(path)), + timeout=self._timeout, **kwargs ) if self._debug: diff --git a/owncloud/test/test.py b/owncloud/test/test.py index 222f11e..6c7075d 100644 --- a/owncloud/test/test.py +++ b/owncloud/test/test.py @@ -7,6 +7,8 @@ import shutil import owncloud import datetime +import mock +import requests import time import tempfile import random @@ -1267,6 +1269,35 @@ def tearDown(self): self.client.logout() +class TestTimeout(unittest.TestCase): + def setUp(self): + self.client = owncloud.Client(Config['owncloud_url'], timeout=15) + + @mock.patch.object(requests.Session, 'request', autospec=True, + return_value=requests.Response()) + def test_timeout_is_set_on_ocs_requests(self, mock_req): + mock_req.return_value.status_code = 404 + try: + self.client.login(Config['owncloud_login'], Config['owncloud_password']) + except owncloud.owncloud.HTTPResponseError: + pass + mock_req.assert_called_with( + mock.ANY, + mock.ANY, + timeout=15, + ) + @mock.patch.object(requests.Session, 'request', autospec=True, + return_value=requests.Response()) + def test_timeout_is_set_on_dav_requests(self, mock_req): + mock_req.status_code = 404 + self.client.list('/') + mock_req.assert_called_with( + mock.ANY, + mock.ANY, + timeout=15, + ) + + class TestOCSRequest(unittest.TestCase): def setUp(self):