Skip to content
Myr

Revert "Add content-length header for empty bytestream (#866)" (#898)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit 939f3ce7ce43d11f3a9829dabaa292b3a906b56d.

authored by Tom Christie · committed by GitHub · parent 4ef5de4 · Browse files · 5 files+7 added−16 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+7 added−16 removed

  • tests:+5 added−16 removed
  • other:+2 added−0 removed

5 files+7 added−16 removed

modified:httpx/_content_streams.py+2 added
71 unmodified lines
72 def __init__(self, body: typing.Union[str, bytes]) -> None:73 self.body = body.encode("utf-8") if isinstance(body, str) else body7475 def get_headers(self) -> typing.Dict[str, str]:76 if not self.body:77 return {}78 content_length = str(len(self.body))79 return {"Content-Length": content_length}8081 def __iter__(self) -> typing.Iterator[bytes]:
270 unmodified lines
modified:tests/client/test_headers.py-7 removed
38 unmodified lines
39 "headers": {40 "accept": "*/*",41 "accept-encoding": "gzip, deflate, br",42 "connection": "keep-alive",43 "content-length": "0",43 "example-header": "example-value",44 "host": "example.org",45 "user-agent": f"python-httpx/{__version__}",46 }
13 unmodified lines
60 "headers": {61 "accept": "*/*",62 "accept-encoding": "gzip, deflate, br",63 "connection": "keep-alive",65 "content-length": "0",64 "host": "example.org",65 "user-agent": "python-myclient/0.2.1",66 "x-auth-token": "FooBarBazToken",67 }
13 unmodified lines
81 "headers": {82 "accept": "*/*",83 "accept-encoding": "gzip, deflate, br",84 "connection": "keep-alive",87 "content-length": "0",85 "host": "example.org",86 "user-agent": f"python-httpx/{__version__}",87 "x-auth-token": "BazToken",88 }
15 unmodified lines
104 "headers": {105 "accept": "*/*",106 "accept-encoding": "gzip, deflate, br",107 "connection": "keep-alive",111 "content-length": "0",108 "host": "example.org",109 "user-agent": f"python-httpx/{__version__}",110 }111 }
4 unmodified lines
116 "accept": "*/*",117 "accept-encoding": "gzip, deflate, br",118 "another-header": "AThing",119 "connection": "keep-alive",124 "content-length": "0",120 "host": "example.org",121 "user-agent": "python-myclient/0.2.1",122 }123 }
22 unmodified lines
146 "headers": {147 "accept": "*/*",148 "accept-encoding": "gzip, deflate, br",149 "connection": "keep-alive",155 "content-length": "0",150 "host": "example.org",151 "user-agent": f"python-httpx/{__version__}",152 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",153 }
16 unmodified lines
170 "headers": {171 "accept": "*/*",172 "accept-encoding": "gzip, deflate, br",173 "connection": "keep-alive",180 "content-length": "0",174 "host": "example.org:123",175 "user-agent": f"python-httpx/{__version__}",176 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",177 }
1 unmodified line
modified:tests/client/test_redirects.py+2 added-2 removed
277 unmodified lines
278 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 response.json()["headers"]["content-length"] == "20"282 assert "content-length" in response.json()["headers"]283284285@pytest.mark.usefixtures("async_environment")286async def test_no_body_redirect():
5 unmodified lines
292 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 response.json()["headers"]["content-length"] == "0"296 assert "content-length" not in response.json()["headers"]297298299@pytest.mark.usefixtures("async_environment")300async def test_can_stream_if_no_redirect():
95 unmodified lines
modified:tests/models/test_requests.py+1 added-1 removed
8 unmodified lines
91011def test_no_content():12 request = httpx.Request("GET", "http://example.org")13 assert request.headers["Content-Length"] == "0"13 assert "Content-Length" not in request.headers141516def test_content_length_header():17 request = httpx.Request("POST", "http://example.org", data=b"test 123")
103 unmodified lines
modified:tests/test_content_streams.py+2 added-6 removed
23 unmodified lines
24 sync_content = b"".join([part for part in stream])25 async_content = b"".join([part async for part in stream])2627 assert stream.can_replay()28 assert stream.get_headers() == {29 "Content-Length": "0",30 }28 assert stream.get_headers() == {}29 assert sync_content == b""30 assert async_content == b""3132
162 unmodified lines
195 sync_content = b"".join([part for part in stream])196 async_content = b"".join([part async for part in stream])197198 assert stream.can_replay()201 assert stream.get_headers() == {202 "Content-Length": "0",203 }199 assert stream.get_headers() == {}200 assert sync_content == b""201 assert async_content == b""202203
3 unmodified lines