test_requests.py
This file
Path
tests/models/test_requests.py
113 lines · 3.5 KiB · Python
Last changed by
Public Auth API (#732)Tom Christiebefore adoption12dd157
History of this fileTake it
1import 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")13 assert "Content-Length" not in request.headers141516def test_content_length_header():17 request = httpx.Request("POST", "http://example.org", data=b"test 123")18 assert request.headers["Content-Length"] == "8"192021@pytest.mark.asyncio22async def test_url_encoded_data():23 request = httpx.Request("POST", "http://example.org", data={"test": "123"})24 await request.aread()2526 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"27 assert request.content == b"test=123"282930@pytest.mark.asyncio31async def test_json_encoded_data():32 request = httpx.Request("POST", "http://example.org", json={"test": 123})33 await request.aread()3435 assert request.headers["Content-Type"] == "application/json"36 assert request.content == b'{"test": 123}'373839@pytest.mark.asyncio40async def test_read_and_stream_data():41 # Ensure a request may still be streamed if it has been read.42 # Needed for cases such as authentication classes that read the request body.43 request = httpx.Request("POST", "http://example.org", json={"test": 123})44 await request.aread()45 content = b"".join([part async for part in request.stream])46 assert content == request.content474849@pytest.mark.asyncio50async def test_cannot_access_content_without_read():51 # Ensure a request may still be streamed if it has been read.52 # Needed for cases such as authentication classes that read the request body.53 request = httpx.Request("POST", "http://example.org", json={"test": 123})54 with pytest.raises(httpx.RequestNotRead):55 request.content565758def test_transfer_encoding_header():59 async def streaming_body(data):60 yield data # pragma: nocover6162 data = streaming_body(b"test 123")6364 request = httpx.Request("POST", "http://example.org", data=data)65 assert "Content-Length" not in request.headers66 assert request.headers["Transfer-Encoding"] == "chunked"676869def test_override_host_header():70 headers = {"host": "1.2.3.4:80"}7172 request = httpx.Request("GET", "http://example.org", headers=headers)73 assert request.headers["Host"] == "1.2.3.4:80"747576def test_override_accept_encoding_header():77 headers = {"Accept-Encoding": "identity"}7879 request = httpx.Request("GET", "http://example.org", headers=headers)80 assert request.headers["Accept-Encoding"] == "identity"818283def test_override_content_length_header():84 async def streaming_body(data):85 yield data # pragma: nocover8687 data = streaming_body(b"test 123")88 headers = {"Content-Length": "8"}8990 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)91 assert request.headers["Content-Length"] == "8"929394def test_url():95 url = "http://example.org"96 request = httpx.Request("GET", url)97 assert request.url.scheme == "http"98 assert request.url.port == 8099 assert request.url.full_path == "/"100101 url = "https://example.org/abc?foo=bar"102 request = httpx.Request("GET", url)103 assert request.url.scheme == "https"104 assert request.url.port == 443105 assert request.url.full_path == "/abc?foo=bar"106107108def test_invalid_urls():109 with pytest.raises(httpx.InvalidURL):110 httpx.Request("GET", "example.org")111112 with pytest.raises(httpx.InvalidURL):113 httpx.Request("GET", "http:///foo")