libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject [libcloud] 02/03: Add basic mock http server based test cases for LibcloudConnection.prepared_request() method.
Date Sun, 27 Sep 2020 12:53:42 GMT
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 43d9fb88d3fe5fb20033df901c8009d0ba481b2d
Author: Tomaz Muraus <tomaz@tomaz.me>
AuthorDate: Sun Sep 27 14:24:43 2020 +0200

    Add basic mock http server based test cases for
    LibcloudConnection.prepared_request() method.
    
    Closes #1488.
---
 libcloud/test/test_connection.py |  7 ++++
 libcloud/test/test_http.py       | 79 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)

diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index a0f1c30..f00177b 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -40,6 +40,13 @@ class BaseConnectionClassTestCase(unittest.TestCase):
     def tearDown(self):
         if self.orig_proxy:
             os.environ['http_proxy'] = self.orig_proxy
+        elif 'http_proxy' in os.environ:
+            del os.environ['http_proxy']
+
+    @classmethod
+    def tearDownClass(cls):
+        if 'http_proxy' in os.environ:
+            del os.environ['http_proxy']
 
     def test_parse_proxy_url(self):
         conn = LibcloudBaseConnection()
diff --git a/libcloud/test/test_http.py b/libcloud/test/test_http.py
index 19d42de..e037b17 100644
--- a/libcloud/test/test_http.py
+++ b/libcloud/test/test_http.py
@@ -16,10 +16,18 @@
 import os
 import sys
 import os.path
+import random
 import warnings
+import threading
+
+from http.server import BaseHTTPRequestHandler
+from http.server import HTTPServer
+
+import requests
 
 import libcloud.security
 
+from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import reload
 from libcloud.utils.py3 import assertRaisesRegex
 from libcloud.http import LibcloudConnection
@@ -91,5 +99,76 @@ class TestHttpLibSSLTests(unittest.TestCase):
 
         self.assertTrue(self.httplib_object.ca_cert is not None)
 
+
+class HttpLayerTestCase(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        cls.listen_host = '127.0.0.1'
+        cls.listen_port = random.randint(10024, 65555)
+        cls.mock_server = HTTPServer((cls.listen_host, cls.listen_port), MockHTTPServerRequestHandler)
+
+        cls.mock_server_thread = threading.Thread(target=cls.mock_server.serve_forever)
+        cls.mock_server_thread.setDaemon(True)
+        cls.mock_server_thread.start()
+
+    @classmethod
+    def tearDownCls(cls):
+        cls.mock_server_thread.kill()
+
+    def test_prepared_request_empty_body_chunked_encoding_not_used(self):
+        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port)
+        connection.prepared_request(method='GET', url='/test/prepared-request-1', body='',
stream=True)
+
+        self.assertEqual(connection.response.status_code, httplib.OK)
+        self.assertEqual(connection.response.content, b'/test/prepared-request-1')
+
+        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port)
+        connection.prepared_request(method='GET', url='/test/prepared-request-2', body=None,
stream=True)
+
+        self.assertEqual(connection.response.status_code, httplib.OK)
+        self.assertEqual(connection.response.content, b'/test/prepared-request-2')
+
+    def test_prepared_request_with_body(self):
+        connection = LibcloudConnection(host=self.listen_host, port=self.listen_port)
+        connection.prepared_request(method='GET', url='/test/prepared-request-3', body='test
body', stream=True)
+
+        self.assertEqual(connection.response.status_code, httplib.OK)
+        self.assertEqual(connection.response.content, b'/test/prepared-request-3')
+
+
+class MockHTTPServerRequestHandler(BaseHTTPRequestHandler):
+    def do_GET(self):
+        if 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
+            headers = dict(self.headers)
+            assert 'Content-Length' not in headers
+
+            self.connection.setblocking(0)
+            # Body should not contain '0' which indicates chunked request
+            body = self.rfile.read(1)
+            assert body is None
+
+            self.send_response(requests.codes.ok)
+            self.end_headers()
+
+            self.wfile.write(self.path.encode('utf-8'))
+        elif self.path == '/test/prepared-request-3':
+            headers = dict(self.headers)
+            assert int(headers['Content-Length']) == 9
+
+            body = self.rfile.read(int(headers['Content-Length']))
+            assert body == b'test body'
+
+            self.send_response(requests.codes.ok)
+            self.end_headers()
+
+            self.wfile.write(self.path.encode('utf-8'))
+        else:
+            self.send_response(requests.codes.internal_server_error)
+            self.end_headers()
+
+
 if __name__ == '__main__':
     sys.exit(unittest.main())


Mime
View raw message