Merge pull request #3545 from nateprewitt/new_prepare_content_length

removing redundant logic from prepare_content_length
This commit is contained in:
Ian Cordasco
2016-09-22 07:49:57 -05:00
committed by GitHub
2 changed files with 20 additions and 12 deletions
+10 -12
View File
@@ -424,7 +424,6 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
# Nottin' on you.
body = None
content_type = None
length = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
@@ -475,17 +474,16 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
self.body = body
def prepare_content_length(self, body):
if hasattr(body, 'seek') and hasattr(body, 'tell'):
curr_pos = body.tell()
body.seek(0, 2)
end_pos = body.tell()
self.headers['Content-Length'] = builtin_str(max(0, end_pos - curr_pos))
body.seek(curr_pos, 0)
elif body is not None:
l = super_len(body)
if l:
self.headers['Content-Length'] = builtin_str(l)
elif (self.method not in ('GET', 'HEAD')) and (self.headers.get('Content-Length') is None):
"""Prepare Content-Length header based on request method and body"""
if body is not None:
length = super_len(body)
if length:
# If length exists, set it. Otherwise, we fallback
# to Transfer-Encoding: chunked.
self.headers['Content-Length'] = builtin_str(length)
elif self.method not in ('GET', 'HEAD') and self.headers.get('Content-Length') is None:
# Set Content-Length to 0 for methods that can have a body
# but don't provide one. (i.e. not GET or HEAD)
self.headers['Content-Length'] = '0'
def prepare_auth(self, auth, url=''):
+10
View File
@@ -90,6 +90,16 @@ class TestRequests:
req = requests.Request(method, httpbin(method.lower())).prepare()
assert 'Content-Length' not in req.headers
@pytest.mark.parametrize('method', ('POST', 'PUT', 'PATCH', 'OPTIONS'))
def test_no_body_content_length(self, httpbin, method):
req = requests.Request(method, httpbin(method.lower())).prepare()
assert req.headers['Content-Length'] == '0'
@pytest.mark.parametrize('method', ('POST', 'PUT', 'PATCH', 'OPTIONS'))
def test_empty_content_length(self, httpbin, method):
req = requests.Request(method, httpbin(method.lower()), data='').prepare()
assert req.headers['Content-Length'] == '0'
def test_override_content_length(self, httpbin):
headers = {
'Content-Length': 'not zero'