Skip to content

Commit

Permalink
Add a timeout parameter
Browse files Browse the repository at this point in the history
The default behavior of the module requests is to set no time out on the
requests. When a server does not reply to a request, it may cause the
library to hang indefinitely.

Fixes owncloud#262
  • Loading branch information
qdii committed Nov 4, 2023
1 parent 353f3ca commit 89eb74d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion owncloud/owncloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '/'
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions owncloud/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import shutil
import owncloud
import datetime
import mock
import requests
import time
import tempfile
import random
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 89eb74d

Please sign in to comment.