From ac8add1a482cc1877ae90fc2b7cd91ef795b984d Mon Sep 17 00:00:00 2001 From: Ivan Kusalic Date: Thu, 12 Sep 2013 15:22:47 +0200 Subject: [PATCH] Do not set Content-Length if present in raw requests --- libcloud/common/base.py | 9 +++------ libcloud/test/test_connection.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) 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) 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') -- 1.8.2.3