Add content-length header for empty bytestream (#866)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Also, fixed related tests
authored by Dima Boger · committed by GitHub · parent 21f5337 · Browse files · 5 files·+16 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+16 added−7 removed
- tests:+16 added−5 removed
- other:+0 added−2 removed
Provenance
- Commit
939f3ce- Tree
186af76- Author
Dima Boger <kotvberloge@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
21f5337
5 files changed+16 added−7 removed
modified:httpx/_content_streams.py-2 removed
69 unmodified lines70 def __init__(self, body: typing.Union[str, bytes]) -> None:71 self.body = body.encode("utf-8") if isinstance(body, str) else body72 73 def get_headers(self) -> typing.Dict[str, str]:74- if not self.body:75- return {}74 content_length = str(len(self.body))75 return {"Content-Length": content_length}76 77 def __iter__(self) -> typing.Iterator[bytes]:268 unmodified linesmodified:tests/client/test_headers.py+7 added
38 unmodified lines39 "headers": {40 "accept": "*/*",41 "accept-encoding": "gzip, deflate, br",42 "connection": "keep-alive",43+ "content-length": "0",44 "example-header": "example-value",45 "host": "example.org",46 "user-agent": f"python-httpx/{__version__}",47 }13 unmodified lines61 "headers": {62 "accept": "*/*",63 "accept-encoding": "gzip, deflate, br",64 "connection": "keep-alive",65+ "content-length": "0",66 "host": "example.org",67 "user-agent": "python-myclient/0.2.1",68 "x-auth-token": "FooBarBazToken",69 }13 unmodified lines83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",87+ "content-length": "0",88 "host": "example.org",89 "user-agent": f"python-httpx/{__version__}",90 "x-auth-token": "BazToken",91 }15 unmodified lines107 "headers": {108 "accept": "*/*",109 "accept-encoding": "gzip, deflate, br",110 "connection": "keep-alive",111+ "content-length": "0",112 "host": "example.org",113 "user-agent": f"python-httpx/{__version__}",114 }115 }4 unmodified lines120 "accept": "*/*",121 "accept-encoding": "gzip, deflate, br",122 "another-header": "AThing",123 "connection": "keep-alive",124+ "content-length": "0",125 "host": "example.org",126 "user-agent": "python-myclient/0.2.1",127 }128 }22 unmodified lines151 "headers": {152 "accept": "*/*",153 "accept-encoding": "gzip, deflate, br",154 "connection": "keep-alive",155+ "content-length": "0",156 "host": "example.org",157 "user-agent": f"python-httpx/{__version__}",158 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",159 }16 unmodified lines176 "headers": {177 "accept": "*/*",178 "accept-encoding": "gzip, deflate, br",179 "connection": "keep-alive",180+ "content-length": "0",181 "host": "example.org:123",182 "user-agent": f"python-httpx/{__version__}",183 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",184 }1 unmodified linemodified:tests/client/test_redirects.py+2 added-2 removed
277 unmodified lines278 data = b"Example request body"279 response = await client.post(url, data=data)280 assert response.url == URL("https://example.org/redirect_body_target")281 assert response.json()["body"] == "Example request body"282- assert "content-length" in response.json()["headers"]282+ assert response.json()["headers"]["content-length"] == "20"283 284 285 @pytest.mark.usefixtures("async_environment")286 async def test_no_body_redirect():5 unmodified lines292 data = b"Example request body"293 response = await client.post(url, data=data)294 assert response.url == URL("https://example.org/redirect_body_target")295 assert response.json()["body"] == ""296- assert "content-length" not in response.json()["headers"]296+ assert response.json()["headers"]["content-length"] == "0"297 298 299 @pytest.mark.usefixtures("async_environment")300 async def test_can_stream_if_no_redirect():95 unmodified linesmodified:tests/models/test_requests.py+1 added-1 removed
8 unmodified lines9 10 11 def test_no_content():12 request = httpx.Request("GET", "http://example.org")13- assert "Content-Length" not in request.headers13+ assert request.headers["Content-Length"] == "0"14 15 16 def test_content_length_header():17 request = httpx.Request("POST", "http://example.org", data=b"test 123")103 unmodified linesmodified:tests/test_content_streams.py+6 added-2 removed
23 unmodified lines24 sync_content = b"".join([part for part in stream])25 async_content = b"".join([part async for part in stream])26 27 assert stream.can_replay()28- assert stream.get_headers() == {}28+ assert stream.get_headers() == {29+ "Content-Length": "0",30+ }31 assert sync_content == b""32 assert async_content == b""33 34 162 unmodified lines197 sync_content = b"".join([part for part in stream])198 async_content = b"".join([part async for part in stream])199 200 assert stream.can_replay()199- assert stream.get_headers() == {}201+ assert stream.get_headers() == {202+ "Content-Length": "0",203+ }204 assert sync_content == b""205 assert async_content == b""206 207 3 unmodified lines