You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2021/10/22 09:36:26 UTC

[libcloud] 03/03: Add additional test cases for asserting that timeout argument is correctly passed to the underlying requests send() method.

This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit 32653fe64a045f8c03d127544be86edc74dbfa74
Author: Tomaz Muraus <to...@tomaz.me>
AuthorDate: Fri Oct 22 11:28:22 2021 +0200

    Add additional test cases for asserting that timeout argument is
    correctly passed to the underlying requests send() method.
---
 libcloud/http.py           |  3 ++-
 libcloud/test/test_http.py | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/libcloud/http.py b/libcloud/http.py
index 8c957f4..33d6a25 100644
--- a/libcloud/http.py
+++ b/libcloud/http.py
@@ -218,7 +218,7 @@ class LibcloudConnection(LibcloudBaseConnection):
         return self.ca_cert if self.ca_cert is not None else self.verify
 
     def request(self, method, url, body=None, headers=None, raw=False,
-                stream=False):
+                stream=False, hooks=None):
         url = urlparse.urljoin(self.host, url)
         headers = self._normalize_headers(headers=headers)
 
@@ -231,6 +231,7 @@ class LibcloudConnection(LibcloudBaseConnection):
             stream=stream,
             verify=self.verification,
             timeout=self.session.timeout,
+            hooks=hooks,
         )
 
     def prepared_request(self, method, url, body=None,
diff --git a/libcloud/test/test_http.py b/libcloud/test/test_http.py
index d63f0b2..ce76252 100644
--- a/libcloud/test/test_http.py
+++ b/libcloud/test/test_http.py
@@ -20,6 +20,7 @@ import random
 import platform
 import warnings
 import threading
+import time
 
 from http.server import BaseHTTPRequestHandler
 from http.server import HTTPServer
@@ -138,10 +139,43 @@ class HttpLayerTestCase(unittest.TestCase):
         self.assertEqual(connection.response.status_code, httplib.OK)
         self.assertEqual(connection.response.content, b'/test/prepared-request-3')
 
+    def test_request_custom_timeout_no_timeout(self):
+        def response_hook(*args, **kwargs):
+            # Assert timeout has been passed correctly
+            self.assertEqual(kwargs['timeout'], 5)
+
+        hooks = {
+            'response': response_hook
+        }
+
+        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port, timeout=5)
+        connection.request(method='GET', url='/test', hooks=hooks)
+
+    def test_request_custom_timeout_timeout(self):
+        def response_hook(*args, **kwargs):
+            # Assert timeout has been passed correctly
+            self.assertEqual(kwargs['timeout'], 0.5)
+
+        hooks = {
+            'response': response_hook
+        }
+
+        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port, timeout=0.5)
+        self.assertRaisesRegex(requests.exceptions.ReadTimeout, 'Read timed out',
+                               connection.request,
+                               method='GET', url='/test-timeout', hooks=hooks)
+
 
 class MockHTTPServerRequestHandler(BaseHTTPRequestHandler):
     def do_GET(self):
-        if self.path in ['/test/prepared-request-1', '/test/prepared-request-2']:
+        if self.path in ['/test']:
+            self.send_response(requests.codes.ok)
+            self.end_headers()
+        if self.path in ['/test-timeout']:
+            time.sleep(1)
+            self.send_response(requests.codes.ok)
+            self.end_headers()
+        elif self.path in ['/test/prepared-request-1', '/test/prepared-request-2']:
             # Verify that chunked encoding is not used for prepared requests
             # with empty body
             # See https://github.com/apache/libcloud/issues/1487