libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [28/33] libcloud git commit: Fix response class tests, fix route53 missing file. Added requests_mock as a testing dependency.
Date Tue, 05 Apr 2016 11:17:19 GMT
Fix response class tests, fix route53 missing file. Added requests_mock as a testing dependency.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/6b9f90c5
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/6b9f90c5
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/6b9f90c5

Branch: refs/heads/requests
Commit: 6b9f90c59faae02cfcd92d41c412291377a9331e
Parents: 3a492b8
Author: anthony-shaw <anthony.p.shaw@gmail.com>
Authored: Tue Apr 5 17:45:01 2016 +1000
Committer: anthony-shaw <anthony.p.shaw@gmail.com>
Committed: Tue Apr 5 17:45:01 2016 +1000

----------------------------------------------------------------------
 libcloud/common/base.py                         |   7 +-
 .../fixtures/route53/record_does_not_exist.xml  |  17 +++
 libcloud/test/test_response_classes.py          | 144 ++++++-------------
 requirements-tests.txt                          |   3 +-
 setup.py                                        |   3 +-
 5 files changed, 70 insertions(+), 104 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/6b9f90c5/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index 67d66b1..60cfb04 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -148,11 +148,11 @@ class Response(object):
 
         # http.client In Python 3 doesn't automatically lowercase the header
         # names
-        self.headers = lowercase_keys(dict(response.getheaders()))
+        self.headers = lowercase_keys(dict(response.headers))
         self.error = response.reason
         self.status = response.status_code
 
-        self.body = response.text if response.text is not None else ''
+        self.body = response.text.strip() if response.text is not None else ''
 
         if not self.success():
             raise exception_from_message(code=self.status,
@@ -226,8 +226,7 @@ class JsonResponse(Response):
     """
 
     def parse_body(self):
-        if self.body is not None and \
-        len(self.body) == 0 and not self.parse_zero_length_body:
+        if len(self.body) == 0 and not self.parse_zero_length_body:
             return self.body
 
         try:

http://git-wip-us.apache.org/repos/asf/libcloud/blob/6b9f90c5/libcloud/test/dns/fixtures/route53/record_does_not_exist.xml
----------------------------------------------------------------------
diff --git a/libcloud/test/dns/fixtures/route53/record_does_not_exist.xml b/libcloud/test/dns/fixtures/route53/record_does_not_exist.xml
new file mode 100644
index 0000000..6a767d1
--- /dev/null
+++ b/libcloud/test/dns/fixtures/route53/record_does_not_exist.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ListResourceRecordSetsResponse xmlns="https://route53.amazonaws.com/doc/2012-02-29/">
+   <ResourceRecordSets>
+
+      <ResourceRecordSet>
+         <Name>definitely.not.what.you.askedfor.t.com</Name>
+         <Type>CNAME</Type>
+         <TTL>86400</TTL>
+         <ResourceRecords>
+            <ResourceRecord>
+               <Value>t.com</Value>
+            </ResourceRecord>
+         </ResourceRecords>
+      </ResourceRecordSet>
+
+  </ResourceRecordSets>
+</ListResourceRecordSetsResponse>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/6b9f90c5/libcloud/test/test_response_classes.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_response_classes.py b/libcloud/test/test_response_classes.py
index 0bdd06a..37d2386 100644
--- a/libcloud/test/test_response_classes.py
+++ b/libcloud/test/test_response_classes.py
@@ -15,137 +15,85 @@
 
 import sys
 import unittest
-import zlib
-import gzip
 
-from mock import Mock
+import requests
+import requests_mock
 
-from libcloud.utils.py3 import httplib, b, StringIO, PY3
-from libcloud.common.base import Response, XmlResponse, JsonResponse
+from libcloud.common.base import XmlResponse, JsonResponse
 from libcloud.common.types import MalformedResponseError
+from libcloud.httplib_ssl import LibcloudConnection
 
 
 class ResponseClassesTests(unittest.TestCase):
     def setUp(self):
-        self._mock_response = Mock()
-        self._mock_response.getheaders.return_value = []
-        self._mock_response.status = httplib.OK
-        self._mock_response._original_data = None
-        self._mock_connection = Mock()
+        self.mock_connection = LibcloudConnection(host='mock.com', port=80)
+        self.mock_connection.driver = None
 
     def test_XmlResponse_class(self):
-        self._mock_response.text = '<foo>bar</foo>'
-        response = XmlResponse(response=self._mock_response,
-                               connection=self._mock_connection)
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/2', text='<foo>bar</foo>')
+            response_obj = requests.get('mock://test.com/2')
+            response = XmlResponse(response=response_obj,
+                                   connection=self.mock_connection)
 
         parsed = response.parse_body()
         self.assertEqual(parsed.tag, 'foo')
         self.assertEqual(parsed.text, 'bar')
 
     def test_XmlResponse_class_malformed_response(self):
-        self._mock_response.text = '<foo>'
-
-        try:
-            XmlResponse(response=self._mock_response,
-                        connection=self._mock_connection)
-        except MalformedResponseError:
-            pass
-        else:
-            self.fail('Exception was not thrown')
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/', text='<foo>')
+            response_obj = requests.get('mock://test.com/')
+            try:
+                XmlResponse(response=response_obj,
+                            connection=self.mock_connection)
+            except MalformedResponseError:
+                pass
+            else:
+                self.fail('Exception was not thrown')
 
     def test_XmlResponse_class_zero_length_body_strip(self):
-        self._mock_response.text = ' '
-
-        response = XmlResponse(response=self._mock_response,
-                               connection=self._mock_connection)
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/', text=' ')
+            response_obj = requests.get('mock://test.com/')
+            response = XmlResponse(response=response_obj,
+                                   connection=self.mock_connection)
 
         parsed = response.parse_body()
         self.assertEqual(parsed, '')
 
     def test_JsonResponse_class_success(self):
-        self._mock_response.read.return_value = '{"foo": "bar"}'
-        response = JsonResponse(response=self._mock_response,
-                                connection=self._mock_connection)
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/', text='{"foo": "bar"}')
+            response_obj = requests.get('mock://test.com/')
+            response = JsonResponse(response=response_obj,
+                                    connection=self.mock_connection)
 
         parsed = response.parse_body()
         self.assertEqual(parsed, {'foo': 'bar'})
 
     def test_JsonResponse_class_malformed_response(self):
-        self._mock_response.text = '{"foo": "bar'
-
-        try:
-            JsonResponse(response=self._mock_response,
-                         connection=self._mock_connection)
-        except MalformedResponseError:
-            pass
-        else:
-            self.fail('Exception was not thrown')
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/', text='{"foo": "bar"')
+            response_obj = requests.get('mock://test.com/')
+            try:
+                JsonResponse(response=response_obj,
+                             connection=self.mock_connection)
+            except MalformedResponseError:
+                pass
+            else:
+                self.fail('Exception was not thrown')
 
     def test_JsonResponse_class_zero_length_body_strip(self):
-        self._mock_response.text = ' '
-
-        response = JsonResponse(response=self._mock_response,
-                                connection=self._mock_connection)
+        with requests_mock.mock() as m:
+            m.register_uri('GET', 'mock://test.com/', text=' ')
+            response_obj = requests.get('mock://test.com/')
+            response = JsonResponse(response=response_obj,
+                                    connection=self.mock_connection)
 
         parsed = response.parse_body()
         self.assertEqual(parsed, '')
 
-    def test_deflate_encoding(self):
-        original_data = 'foo bar ponies, wooo zlib'
-        compressed_data = zlib.compress(b(original_data))
-
-        self._mock_response.text = compressed_data
-        self._mock_response.getheaders.return_value = \
-            {'Content-Encoding': 'deflate'}
-
-        response = Response(response=self._mock_response,
-                            connection=self._mock_connection)
-
-        body = response.parse_body()
-        self.assertEqual(body, original_data)
-
-        self._mock_response.getheaders.return_value = \
-            {'Content-Encoding': 'zlib'}
-
-        response = Response(response=self._mock_response,
-                            connection=self._mock_connection)
-
-        body = response.parse_body()
-        self.assertEqual(body, original_data)
-
-    def test_gzip_encoding(self):
-        original_data = 'foo bar ponies, wooo gzip'
-
-        if PY3:
-            from io import BytesIO
-            string_io = BytesIO()
-        else:
-            string_io = StringIO()
-
-        stream = gzip.GzipFile(fileobj=string_io, mode='w')
-        stream.write(b(original_data))
-        stream.close()
-        compressed_data = string_io.getvalue()
-
-        self._mock_response.text = compressed_data
-        self._mock_response.getheaders.return_value = \
-            {'Content-Encoding': 'gzip'}
-
-        response = Response(response=self._mock_response,
-                            connection=self._mock_connection)
-
-        body = response.parse_body()
-        self.assertEqual(body, original_data)
-
-        self._mock_response.getheaders.return_value = \
-            {'Content-Encoding': 'x-gzip'}
-
-        response = Response(response=self._mock_response,
-                            connection=self._mock_connection)
-
-        body = response.parse_body()
-        self.assertEqual(body, original_data)
-
 
 if __name__ == '__main__':
     sys.exit(unittest.main())

http://git-wip-us.apache.org/repos/asf/libcloud/blob/6b9f90c5/requirements-tests.txt
----------------------------------------------------------------------
diff --git a/requirements-tests.txt b/requirements-tests.txt
index 3a40ef0..22317b2 100644
--- a/requirements-tests.txt
+++ b/requirements-tests.txt
@@ -1,4 +1,5 @@
 pep8>=1.7.0,<1.8
 flake8>=2.5.1,<2.6
 mock>=1.0.1,<1.1
-requests
\ No newline at end of file
+requests
+requests_mock
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/libcloud/blob/6b9f90c5/setup.py
----------------------------------------------------------------------
diff --git a/setup.py b/setup.py
index dc1589f..351fcb9 100644
--- a/setup.py
+++ b/setup.py
@@ -60,7 +60,8 @@ SUPPORTED_VERSIONS = ['2.5', '2.6', '2.7', 'PyPy', '3.x']
 
 TEST_REQUIREMENTS = [
     'mock',
-    'requests'
+    'requests',
+    'requests-mock'
 ]
 
 if PY2_pre_279 or PY3_pre_32:


Mime
View raw message