Add test for default chunked Host header
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by Nate Prewitt · parent 6fbfca9 · Browse files · 2 files·+36 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+36 added−14 removed
- tests:+34 added−13 removed
- other:+2 added−1 removed
Provenance
- Commit
4ab2550- Tree
8af269d- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Nate Prewitt <Nate.Prewitt@gmail.com>- Parent
6fbfca9
2 files changed+36 added−14 removed
modified:requests/adapters.py+2 added-1 removed
456 unmodified lines457 458 low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)459 460 try:461+ skip_host = 'Host' in request.headers462 low_conn.putrequest(request.method,463 url,464 skip_accept_encoding=True,464- skip_host='Host' in request.headers)465+ skip_host=skip_host)466 467 for header, value in request.headers.items():468 low_conn.putheader(header, value)469 69 unmodified linesmodified:tests/test_lowlevel.py+34 added-13 removed
7 unmodified lines8 9 from .utils import override_environ10 11 12+def echo_response_handler(sock):13+ """Simple handler that will take request and echo it back to requester."""14+ request_content = consume_socket_content(sock, timeout=0.5)15+16+ text_200 = (17+ b'HTTP/1.1 200 OK\r\n'18+ b'Content-Length: %d\r\n\r\n'19+ b'%s'20+ ) % (len(request_content), request_content)21+ sock.send(text_200)22+23+24 def test_chunked_upload():25 """can safely send generators"""26 close_server = threading.Event()27 server = Server.basic_response_server(wait_to_close_event=close_server)31 unmodified lines59 60 61 def test_chunked_upload_uses_only_specified_host_header():62 """Ensure we use only the specified Host header for chunked requests."""51- text_200 = (b'HTTP/1.1 200 OK\r\n'52- b'Content-Length: 0\r\n\r\n')53- wanted_host = 'sample-host'54- expected_header = 'Host: {}'.format(wanted_host).encode('utf-8')55- def single_host_resp_handler(sock):56- request_content = consume_socket_content(sock, timeout=0.5)57- assert expected_header in request_content58- assert request_content.count(b'Host: ') == 159- sock.send(text_200)63+ close_server = threading.Event()64+ server = Server(echo_response_handler, wait_to_close_event=close_server)65 61- return request_content66+ data = iter([b'a', b'b', b'c'])67+ custom_host = 'sample-host'68 69+ with server as (host, port):70+ url = 'http://{}:{}/'.format(host, port)71+ r = requests.post(url, data=data, headers={'Host': custom_host}, stream=True)72+ close_server.set() # release server block73+74+ expected_header = b'Host: %s\r\n' % custom_host.encode('utf-8')75+ assert expected_header in r.content76+ assert r.content.count(b'Host: ') == 177+78+79+def test_chunked_upload_doesnt_skip_host_header():80+ """Ensure we don't omit all Host headers with chunked requests."""81 close_server = threading.Event()82+ server = Server(echo_response_handler, wait_to_close_event=close_server)83 65- server = Server(single_host_resp_handler, wait_to_close_event=close_server)84 data = iter([b'a', b'b', b'c'])85 86 with server as (host, port):87+ expected_host = '{}:{}'.format(host, port)88 url = 'http://{}:{}/'.format(host, port)70- r = requests.post(url, data=data, headers={'Host': wanted_host}, stream=True)89+ r = requests.post(url, data=data, stream=True)90 close_server.set() # release server block91 73- assert r.status_code == 20092+ expected_header = b'Host: %s\r\n' % expected_host.encode('utf-8')93+ assert expected_header in r.content94+ assert r.content.count(b'Host: ') == 195 96 97 def test_conflicting_content_lengths():98 """Ensure we correctly throw an InvalidHeader error if multiple307 unmodified lines