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 2013/09/04 12:28:10 UTC

[03/14] git commit: Only send Content-Length: 0 even if "data" is None.

Only send Content-Length: 0 even if "data" is None.

Fixes LIBCLOUD-390.


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

Branch: refs/heads/0.13.1
Commit: c10249d1d733ba5215820c5d935265de5ae6bc84
Parents: 9e94e39
Author: Tomaz Muraus <to...@apache.org>
Authored: Wed Sep 4 10:58:44 2013 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Wed Sep 4 11:58:41 2013 +0200

----------------------------------------------------------------------
 CHANGES                          |  2 +-
 libcloud/common/base.py          | 10 ++++++----
 libcloud/test/test_connection.py | 37 +++++++++++++----------------------
 3 files changed, 21 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/c10249d1/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 1206d98..3727eb9 100644
--- a/CHANGES
+++ b/CHANGES
@@ -5,7 +5,7 @@ Changes with Apache Libcloud in development
  *) General
 
     - Fix a regression introduced in 0.13.0 and make sure to include
-      Content-Length 0 with PUT and POST requests. (LIBCLOUD-362)
+      Content-Length 0 with PUT and POST requests. (LIBCLOUD-362, LIBCLOUD-390)
       [Tomaz Muraus]
 
 Changes with Apache Libcloud 0.13.0:

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c10249d1/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index 0634588..4ffdbd5 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -591,12 +591,14 @@ class Connection(object):
         else:
             headers.update({'Host': self.host})
 
-        # Encode data if necessary
-        if data is not None:
+        if data:
+            # Encode data if provided
             data = self.encode_data(data)
+            headers.update({'Content-Length': str(len(data))})
+        else:
             # Only send Content-Length 0 with POST and PUT request
-            if len(data) > 0 or (len(data) == 0 and method in ['POST', 'PUT']):
-                headers.update({'Content-Length': str(len(data))})
+            if method.upper() in ['POST', 'PUT']:
+                headers.update({'Content-Length': '0'})
 
         params, headers = self.pre_connect_hook(params, headers)
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c10249d1/libcloud/test/test_connection.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index 938e170..798f7ec 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -49,39 +49,30 @@ class ConnectionClassTestCase(unittest.TestCase):
         call_kwargs = con.connection.request.call_args[1]
         self.assertTrue('Content-Length' not in call_kwargs['headers'])
 
-        # 'a' as data, content length should be present (data is GET is not
+        # 'a' as data, content length should be present (data in GET is not
         # corect, but anyways)
         con.request('/test', method='GET', data='a')
         call_kwargs = con.connection.request.call_args[1]
         self.assertEqual(call_kwargs['headers']['Content-Length'], '1')
 
         ## POST, PUT method
-        # No data, no content length should be present
-        con.request('/test', method='POST', data=None)
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertTrue('Content-Length' not in call_kwargs['headers'])
-
-        con.request('/test', method='PUT', data=None)
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertTrue('Content-Length' not in call_kwargs['headers'])
+        # No data, content length should be present
+        for method in ['POST', 'PUT', 'post', 'put']:
+            con.request('/test', method=method, data=None)
+            call_kwargs = con.connection.request.call_args[1]
+            self.assertEqual(call_kwargs['headers']['Content-Length'], '0')
 
         # '' as data, content length should be present
-        con.request('/test', method='POST', data='')
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertEqual(call_kwargs['headers']['Content-Length'], '0')
-
-        con.request('/test', method='PUT', data='')
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertEqual(call_kwargs['headers']['Content-Length'], '0')
+        for method in ['POST', 'PUT', 'post', 'put']:
+            con.request('/test', method=method, data='')
+            call_kwargs = con.connection.request.call_args[1]
+            self.assertEqual(call_kwargs['headers']['Content-Length'], '0')
 
         # 'a' as data, content length should be present
-        con.request('/test', method='POST', data='a')
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertEqual(call_kwargs['headers']['Content-Length'], '1')
-
-        con.request('/test', method='PUT', data='a')
-        call_kwargs = con.connection.request.call_args[1]
-        self.assertEqual(call_kwargs['headers']['Content-Length'], '1')
+        for method in ['POST', 'PUT', 'post', 'put']:
+            con.request('/test', method=method, data='a')
+            call_kwargs = con.connection.request.call_args[1]
+            self.assertEqual(call_kwargs['headers']['Content-Length'], '1')
 
 
 if __name__ == '__main__':