test_requests.py
This file
Path
tests/models/test_requests.py
141 lines · 4.4 KiB · Python
Last changed by
Header refinements (#1248)Tom Christiebefore adoption19b863a
History of this fileTake it
The walk stopped after 200 commits: lines older than 5380417 point at it.
Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
lines 1–12why, one step back1import pytest23import httpx456def test_request_repr():7 request = httpx.Request("GET", "http://example.org")8 assert repr(request) == "<Request('GET', 'http://example.org')>"91011def test_no_content():12 request = httpx.Request("GET", "http://example.org")Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 13why, one step back13 assert "Content-Length" not in request.headersDrop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
lines 14–36why, one step back141516def test_content_length_header():17 request = httpx.Request("POST", "http://example.org", data=b"test 123")18 assert request.headers["Content-Length"] == "8"192021def test_url_encoded_data():22 request = httpx.Request("POST", "http://example.org", data={"test": "123"})23 request.read()2425 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"26 assert request.content == b"test=123"272829def test_json_encoded_data():30 request = httpx.Request("POST", "http://example.org", json={"test": 123})31 request.read()3233 assert request.headers["Content-Type"] == "application/json"34 assert request.content == b'{"test": 123}'3536Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 37–48why, one step back37def test_headers():38 request = httpx.Request("POST", "http://example.org", json={"test": 123})3940 assert request.headers == httpx.Headers(41 {42 "Host": "example.org",43 "Content-Type": "application/json",44 "Content-Length": "13",45 }46 )4748Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
lines 49–87why, one step back49def test_read_and_stream_data():50 # Ensure a request may still be streamed if it has been read.51 # Needed for cases such as authentication classes that read the request body.52 request = httpx.Request("POST", "http://example.org", json={"test": 123})53 request.read()54 content = b"".join([part for part in request.stream])55 assert content == request.content565758@pytest.mark.asyncio59async def test_aread_and_stream_data():60 # Ensure a request may still be streamed if it has been read.61 # Needed for cases such as authentication classes that read the request body.62 request = httpx.Request("POST", "http://example.org", json={"test": 123})63 await request.aread()64 content = b"".join([part async for part in request.stream])65 assert content == request.content666768@pytest.mark.asyncio69async def test_cannot_access_content_without_read():70 # Ensure a request may still be streamed if it has been read.71 # Needed for cases such as authentication classes that read the request body.72 request = httpx.Request("POST", "http://example.org", json={"test": 123})73 with pytest.raises(httpx.RequestNotRead):74 request.content757677def test_transfer_encoding_header():78 async def streaming_body(data):79 yield data # pragma: nocover8081 data = streaming_body(b"test 123")8283 request = httpx.Request("POST", "http://example.org", data=data)84 assert "Content-Length" not in request.headers85 assert request.headers["Transfer-Encoding"] == "chunked"8687Ignore transfer-encoding if content-length presents (#1170)Joebefore adoption5b6d33e
lines 88–104why, one step back88def test_ignore_transfer_encoding_header_if_content_length_exists():89 """90 `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.91 See https://github.com/encode/httpx/issues/116892 """9394 def streaming_body(data):95 yield data # pragma: nocover9697 data = streaming_body(b"abcd")9899 headers = {"Content-Length": "4"}100 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)101 assert "Transfer-Encoding" not in request.headers102 assert request.headers["Content-Length"] == "4"103104Drop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
lines 105–133why, one step back105def test_override_host_header():106 headers = {"host": "1.2.3.4:80"}107108 request = httpx.Request("GET", "http://example.org", headers=headers)109 assert request.headers["Host"] == "1.2.3.4:80"110111112def test_override_accept_encoding_header():113 headers = {"Accept-Encoding": "identity"}114115 request = httpx.Request("GET", "http://example.org", headers=headers)116 assert request.headers["Accept-Encoding"] == "identity"117118119def test_override_content_length_header():120 async def streaming_body(data):121 yield data # pragma: nocover122123 data = streaming_body(b"test 123")124 headers = {"Content-Length": "8"}125126 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)127 assert request.headers["Content-Length"] == "8"128129130def test_url():131 url = "http://example.org"132 request = httpx.Request("GET", url)133 assert request.url.scheme == "http"URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 134why, one step back134 assert request.url.port is NoneDrop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
lines 135–139why, one step back135 assert request.url.full_path == "/"136137 url = "https://example.org/abc?foo=bar"138 request = httpx.Request("GET", url)139 assert request.url.scheme == "https"URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 140why, one step back140 assert request.url.port is NoneDrop backend parameter on AsyncClient (#791)Florimond Mancabefore adoption5380417
line 141why, one step back141 assert request.url.full_path == "/abc?foo=bar"