test_requests.py
This file
Path
tests/models/test_requests.py
120 lines · 3.8 KiB · Python
Last changed by
Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
History of this fileTake it
The walk stopped after 200 commits: lines older than 206c537 point at it.
Drop sync (#544)Tom Christiebefore adoption206c537
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 sync (#544)Tom Christiebefore adoption206c537
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 sync (#544)Tom Christiebefore adoption206c537
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()Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 24why, one step back24Drop sync (#544)Tom Christiebefore adoption206c537
line 25why, one step back25 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 sync (#544)Tom Christiebefore adoption206c537
lines 27–28why, one step back2728Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 29why, one step back29def test_json_encoded_data():Drop sync (#544)Tom Christiebefore adoption206c537
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()Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 32why, one step back32Drop sync (#544)Tom Christiebefore adoption206c537
line 33why, one step back33 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 sync (#544)Tom Christiebefore adoption206c537
lines 63–120why, 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"747576def test_override_host_header():77 headers = {"host": "1.2.3.4:80"}7879 request = httpx.Request("GET", "http://example.org", headers=headers)80 assert request.headers["Host"] == "1.2.3.4:80"818283def test_override_accept_encoding_header():84 headers = {"Accept-Encoding": "identity"}8586 request = httpx.Request("GET", "http://example.org", headers=headers)87 assert request.headers["Accept-Encoding"] == "identity"888990def test_override_content_length_header():91 async def streaming_body(data):92 yield data # pragma: nocover9394 data = streaming_body(b"test 123")95 headers = {"Content-Length": "8"}9697 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)98 assert request.headers["Content-Length"] == "8"99100101def test_url():102 url = "http://example.org"103 request = httpx.Request("GET", url)104 assert request.url.scheme == "http"105 assert request.url.port == 80106 assert request.url.full_path == "/"107108 url = "https://example.org/abc?foo=bar"109 request = httpx.Request("GET", url)110 assert request.url.scheme == "https"111 assert request.url.port == 443112 assert request.url.full_path == "/abc?foo=bar"113114115def test_invalid_urls():116 with pytest.raises(httpx.InvalidURL):117 httpx.Request("GET", "example.org")118119 with pytest.raises(httpx.InvalidURL):120 httpx.Request("GET", "http:///foo")