From 2d2c831d071e181deceb80c8550ab6d231b7726e Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Fri, 6 May 2016 14:31:04 -0700 Subject: [PATCH 1/3] Add tests to verify that correct 'Content-Length' or 'Transfer-Encoding' headers are being set in PreparedRequest. --- tests/test_requests.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 2c298bab..9d6036bb 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1693,6 +1693,30 @@ class TestRequests: resp.close() assert resp.raw.closed + def test_empty_stream_with_auth_does_not_set_content_length_header(self, httpbin): + """Ensure that a byte stream with size 0 will not set both a Content-Length + and Transfer-Encoding header + """ + auth = ('user', 'pass') + url = httpbin('post') + file_obj = io.BytesIO(b'') + r = requests.Request('POST', url, auth=auth, data=file_obj) + prepared_request = r.prepare() + assert 'Transfer-Encoding' in prepared_request.headers + assert 'Content-Length' not in prepared_request.headers + + def test_stream_with_auth_does_not_set_transfer_encoding_header(self, httpbin): + """Ensure that a byte stream with size > 0 will not set both a Content-Length + and Transfer-Encoding header""" + auth = ('user', 'pass') + url = httpbin('post') + file_obj = io.BytesIO(b'test data') + r = requests.Request('POST', url, auth=auth, data=file_obj) + prepared_request = r.prepare() + assert 'Transfer-Encoding' not in prepared_request.headers + assert 'Content-Length' in prepared_request.headers + + class TestCaseInsensitiveDict: @pytest.mark.parametrize( From 14d71fd41a31d0ce275c33e59c86d767c9c3768b Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Tue, 10 May 2016 19:36:32 -0700 Subject: [PATCH 2/3] Test chunked upload doesn't set content length --- AUTHORS.rst | 1 + tests/test_requests.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 3aa2a1e5..1dbd0409 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -173,3 +173,4 @@ Patches and Suggestions - Om Prakash Kumar (`@iamprakashom `_) - Philipp Konrad (`@gardiac2002 `_) - Hussain Tamboli (`@hussaintamboli `_) +- Casey Davidson (`@davidsoncasey `_) diff --git a/tests/test_requests.py b/tests/test_requests.py index 9d6036bb..a4e4eb61 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1716,6 +1716,14 @@ class TestRequests: assert 'Transfer-Encoding' not in prepared_request.headers assert 'Content-Length' in prepared_request.headers + def test_chunked_upload_does_not_set_content_length_header(self, httpbin): + data = (i for i in [b'a', b'b', b'c']) + url = httpbin('post') + r = requests.Request('POST', url, data=data) + prepared_request = r.prepare() + assert 'Transfer-Encoding' in prepared_request.headers + assert 'Content-Length' not in prepared_request.headers + class TestCaseInsensitiveDict: From 13716728e757a22ccdb700c5c7db52c8be33d034 Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Sun, 4 Dec 2016 12:11:09 -0700 Subject: [PATCH 3/3] minor doc string cleanup --- tests/test_requests.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index a4e4eb61..49417717 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -1695,7 +1695,7 @@ class TestRequests: def test_empty_stream_with_auth_does_not_set_content_length_header(self, httpbin): """Ensure that a byte stream with size 0 will not set both a Content-Length - and Transfer-Encoding header + and Transfer-Encoding header. """ auth = ('user', 'pass') url = httpbin('post') @@ -1707,7 +1707,8 @@ class TestRequests: def test_stream_with_auth_does_not_set_transfer_encoding_header(self, httpbin): """Ensure that a byte stream with size > 0 will not set both a Content-Length - and Transfer-Encoding header""" + and Transfer-Encoding header. + """ auth = ('user', 'pass') url = httpbin('post') file_obj = io.BytesIO(b'test data') @@ -1717,6 +1718,9 @@ class TestRequests: assert 'Content-Length' in prepared_request.headers def test_chunked_upload_does_not_set_content_length_header(self, httpbin): + """Ensure that requests with a generator body stream using + Transfer-Encoding: chunked, not a Content-Length header. + """ data = (i for i in [b'a', b'b', b'c']) url = httpbin('post') r = requests.Request('POST', url, data=data)