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
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"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}'353637def 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.content444546@pytest.mark.asyncio47async def test_aread_and_stream_data():48 # 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.content636465def 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")