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
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_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"919293def 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"122 assert request.url.port is None123 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"128 assert request.url.port is None129 assert request.url.full_path == "/abc?foo=bar"