Make sure to include Content-Length header in calls with body

Fixes #70
This commit is contained in:
Honza Král
2014-03-31 16:41:08 -07:00
parent 7ce5b67a21
commit 49b7a1aca9
2 changed files with 18 additions and 7 deletions
+7 -4
View File
@@ -17,17 +17,17 @@ class Urllib3HttpConnection(Connection):
"""
def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, maxsize=10, **kwargs):
super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs)
headers = {}
self.headers = {}
if http_auth is not None:
if isinstance(http_auth, (tuple, list)):
http_auth = ':'.join(http_auth)
headers = urllib3.make_headers(basic_auth=http_auth)
self.headers = urllib3.make_headers(basic_auth=http_auth)
pool_class = urllib3.HTTPConnectionPool
if use_ssl:
pool_class = urllib3.HTTPSConnectionPool
self.pool = pool_class(host, port=port, timeout=self.timeout, headers=headers, maxsize=maxsize)
self.pool = pool_class(host, port=port, timeout=self.timeout, maxsize=maxsize)
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
url = self.url_prefix + url
@@ -40,7 +40,10 @@ class Urllib3HttpConnection(Connection):
kw = {}
if timeout:
kw['timeout'] = timeout
response = self.pool.urlopen(method, url, body, retries=0, **kw)
headers = self.headers.copy()
if body:
headers['content-length'] = str(len(body))
response = self.pool.urlopen(method, url, body, retries=False, headers=headers, **kw)
duration = time.time() - start
raw_data = response.data.decode('utf-8')
except Exception as e:
+11 -3
View File
@@ -35,15 +35,15 @@ class TestUrllib3Connection(TestCase):
def test_http_auth(self):
con = Urllib3HttpConnection(http_auth='username:secret')
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.pool.headers)
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.headers)
def test_http_auth_tuple(self):
con = Urllib3HttpConnection(http_auth=('username', 'secret'))
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.pool.headers)
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.headers)
def test_http_auth_list(self):
con = Urllib3HttpConnection(http_auth=['username', 'secret'])
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.pool.headers)
self.assertEquals({'authorization': 'Basic dXNlcm5hbWU6c2VjcmV0'}, con.headers)
def test_uses_https_if_specified(self):
con = Urllib3HttpConnection(use_ssl=True)
@@ -53,6 +53,14 @@ class TestUrllib3Connection(TestCase):
con = Urllib3HttpConnection()
self.assertIsInstance(con.pool, urllib3.HTTPConnectionPool)
def test_content_length_gets_set(self):
con = Urllib3HttpConnection()
m = con.pool.urlopen = Mock()
m.return_value.status = 200
con.perform_request('PUT', '/', body='0123456789')
m.assert_called_once_with('PUT', '/', '0123456789', headers={'content-length': '10'}, retries=False)
class TestRequestsConnection(TestCase):
def _get_mock_connection(self, connection_params={}, status_code=200, response_body='{}'):
con = RequestsHttpConnection(**connection_params)