test_requests.py
This file
Path
tests/models/test_requests.py
129 lines · 4.1 KiB · Python
Last changed by
Ignore transfer-encoding if content-length presents (#1170)Joebefore adoption5b6d33e
History of this fileTake it
The walk stopped after 200 commits: lines older than 2b92a78 point at it.
Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
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 `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 14–20why, 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"1920Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 21why, one step back21def test_url_encoded_data():Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
line 22why, one step back22 request = httpx.Request("POST", "http://example.org", data={"test": "123"})Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 23why, one step back23 request.read()Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 24–25why, one step back2425 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"Public Auth API (#732)Tom Christiebefore adoption12dd157
line 26why, one step back26 assert request.content == b"test=123"Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 27–28why, one step back2728Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 29why, one step back29def test_json_encoded_data():Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
line 30why, one step back30 request = httpx.Request("POST", "http://example.org", json={"test": 123})Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 31why, one step back31 request.read()Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 32–33why, one step back3233 assert request.headers["Content-Type"] == "application/json"Public Auth API (#732)Tom Christiebefore adoption12dd157
lines 34–36why, one step back34 assert request.content == b'{"test": 123}'3536Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 37–45why, one step back37def test_read_and_stream_data():38 # Ensure a request may still be streamed if it has been read.39 # Needed for cases such as authentication classes that read the request body.40 request = httpx.Request("POST", "http://example.org", json={"test": 123})41 request.read()42 content = b"".join([part for part in request.stream])43 assert content == request.content4445Public Auth API (#732)Tom Christiebefore adoption12dd157
line 46why, one step back46@pytest.mark.asyncioReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 47why, one step back47async def test_aread_and_stream_data():Public Auth API (#732)Tom Christiebefore adoption12dd157
lines 48–62why, one step back48 # Ensure a request may still be streamed if it has been read.49 # Needed for cases such as authentication classes that read the request body.50 request = httpx.Request("POST", "http://example.org", json={"test": 123})51 await request.aread()52 content = b"".join([part async for part in request.stream])53 assert content == request.content545556@pytest.mark.asyncio57async def test_cannot_access_content_without_read():58 # Ensure a request may still be streamed if it has been read.59 # Needed for cases such as authentication classes that read the request body.60 request = httpx.Request("POST", "http://example.org", json={"test": 123})61 with pytest.raises(httpx.RequestNotRead):62 request.contentDrop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 63–75why, one step back636465def test_transfer_encoding_header():66 async def streaming_body(data):67 yield data # pragma: nocover6869 data = streaming_body(b"test 123")7071 request = httpx.Request("POST", "http://example.org", data=data)72 assert "Content-Length" not in request.headers73 assert request.headers["Transfer-Encoding"] == "chunked"7475Ignore transfer-encoding if content-length presents (#1170)Joebefore adoption5b6d33e
lines 76–92why, one step back76def test_ignore_transfer_encoding_header_if_content_length_exists():77 """78 `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.79 See https://github.com/encode/httpx/issues/116880 """8182 def streaming_body(data):83 yield data # pragma: nocover8485 data = streaming_body(b"abcd")8687 headers = {"Content-Length": "4"}88 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)89 assert "Transfer-Encoding" not in request.headers90 assert request.headers["Content-Length"] == "4"9192Drop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 93–121why, one step back93def test_override_host_header():94 headers = {"host": "1.2.3.4:80"}9596 request = httpx.Request("GET", "http://example.org", headers=headers)97 assert request.headers["Host"] == "1.2.3.4:80"9899100def test_override_accept_encoding_header():101 headers = {"Accept-Encoding": "identity"}102103 request = httpx.Request("GET", "http://example.org", headers=headers)104 assert request.headers["Accept-Encoding"] == "identity"105106107def test_override_content_length_header():108 async def streaming_body(data):109 yield data # pragma: nocover110111 data = streaming_body(b"test 123")112 headers = {"Content-Length": "8"}113114 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)115 assert request.headers["Content-Length"] == "8"116117118def test_url():119 url = "http://example.org"120 request = httpx.Request("GET", url)121 assert request.url.scheme == "http"URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 122why, one step back122 assert request.url.port is NoneDrop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
lines 123–127why, one step back123 assert request.url.full_path == "/"124125 url = "https://example.org/abc?foo=bar"126 request = httpx.Request("GET", url)127 assert request.url.scheme == "https"URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 128why, one step back128 assert request.url.port is NoneDrop `Origin` from public API (#688)Andrés Álvarezbefore adoption2b92a78
line 129why, one step back129 assert request.url.full_path == "/abc?foo=bar"