|
9 | 9 | from .utils import override_environ
|
10 | 10 |
|
11 | 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 | + |
12 | 24 | def test_chunked_upload():
|
13 | 25 | """can safely send generators"""
|
14 | 26 | close_server = threading.Event()
|
@@ -48,29 +60,38 @@ def incomplete_chunked_response_handler(sock):
|
48 | 60 |
|
49 | 61 | def test_chunked_upload_uses_only_specified_host_header():
|
50 | 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_content |
58 |
| - assert request_content.count(b'Host: ') == 1 |
59 |
| - sock.send(text_200) |
| 63 | + close_server = threading.Event() |
| 64 | + server = Server(echo_response_handler, wait_to_close_event=close_server) |
60 | 65 |
|
61 |
| - return request_content |
| 66 | + 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 block |
62 | 73 |
|
| 74 | + expected_header = b'Host: %s\r\n' % custom_host.encode('utf-8') |
| 75 | + assert expected_header in r.content |
| 76 | + assert r.content.count(b'Host: ') == 1 |
| 77 | + |
| 78 | + |
| 79 | +def test_chunked_upload_doesnt_skip_host_header(): |
| 80 | + """Ensure we don't omit all Host headers with chunked requests.""" |
63 | 81 | close_server = threading.Event()
|
| 82 | + server = Server(echo_response_handler, wait_to_close_event=close_server) |
64 | 83 |
|
65 |
| - server = Server(single_host_resp_handler, wait_to_close_event=close_server) |
66 | 84 | data = iter([b'a', b'b', b'c'])
|
67 | 85 |
|
68 | 86 | with server as (host, port):
|
| 87 | + expected_host = '{}:{}'.format(host, port) |
69 | 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) |
71 | 90 | close_server.set() # release server block
|
72 | 91 |
|
73 |
| - assert r.status_code == 200 |
| 92 | + expected_header = b'Host: %s\r\n' % expected_host.encode('utf-8') |
| 93 | + assert expected_header in r.content |
| 94 | + assert r.content.count(b'Host: ') == 1 |
74 | 95 |
|
75 | 96 |
|
76 | 97 | def test_conflicting_content_lengths():
|
|
0 commit comments