Updated Branches:
  refs/heads/trunk 95db9a169 -> 8764ffe3f

LIBCLOUD-396: Do not set Content-Length if present in raw requests

Signed-off-by: Tomaz Muraus <[email protected]>


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

Branch: refs/heads/trunk
Commit: 7179aacecebd3a0be303e59b49c5fc946200bb88
Parents: 95db9a1
Author: Ivan Kusalic <[email protected]>
Authored: Thu Sep 12 15:22:47 2013 +0200
Committer: Tomaz Muraus <[email protected]>
Committed: Thu Sep 12 17:43:27 2013 +0200

----------------------------------------------------------------------
 libcloud/common/base.py          |  9 +++------
 libcloud/test/test_connection.py | 16 +++++++++++++++-
 2 files changed, 18 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/7179aace/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index 101e66c..43cd82f 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -572,13 +572,10 @@ class Connection(object):
             headers.update({'Host': self.host})
 
         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 method.upper() in ['POST', 'PUT']:
-                headers.update({'Content-Length': '0'})
+            headers['Content-Length'] = str(len(data))
+        elif method.upper() in ['POST', 'PUT'] and not raw:
+            headers['Content-Length'] = '0'
 
         params, headers = self.pre_connect_hook(params, headers)
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/7179aace/libcloud/test/test_connection.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index 798f7ec..646d253 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -17,7 +17,7 @@
 import sys
 import unittest
 
-from mock import Mock
+from mock import Mock, call
 
 from libcloud.common.base import Connection
 
@@ -68,6 +68,20 @@ class ConnectionClassTestCase(unittest.TestCase):
             call_kwargs = con.connection.request.call_args[1]
             self.assertEqual(call_kwargs['headers']['Content-Length'], '0')
 
+        # No data, raw request, do not touch Content-Length if present
+        for method in ['POST', 'PUT', 'post', 'put']:
+            con.request('/test', method=method, data=None,
+                        headers={'Content-Length': '42'}, raw=True)
+            putheader_call_list = con.connection.putheader.call_args_list
+            self.assertIn(call('Content-Length', '42'), putheader_call_list)
+
+        # '' as data, raw request, do not touch Content-Length if present
+        for method in ['POST', 'PUT', 'post', 'put']:
+            con.request('/test', method=method, data=None,
+                        headers={'Content-Length': '42'}, raw=True)
+            putheader_call_list = con.connection.putheader.call_args_list
+            self.assertIn(call('Content-Length', '42'), putheader_call_list)
+
         # 'a' as data, content length should be present
         for method in ['POST', 'PUT', 'post', 'put']:
             con.request('/test', method=method, data='a')

Reply via email to