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/bca57629 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/bca57629 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/bca57629 Branch: refs/heads/0.13.2 Commit: bca5762906e466dcc405ee057e440e021bfcdcf4 Parents: b571b43 Author: Ivan Kusalic <[email protected]> Authored: Thu Sep 12 15:22:47 2013 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Fri Sep 13 15:20:19 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/bca57629/libcloud/common/base.py ---------------------------------------------------------------------- diff --git a/libcloud/common/base.py b/libcloud/common/base.py index 4ffdbd5..f32f3ec 100644 --- a/libcloud/common/base.py +++ b/libcloud/common/base.py @@ -592,13 +592,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/bca57629/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')
