Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 0fa55bb point at it.

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 1–20why, one step back
1import pytest
23import 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"1920

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

lines 21–22why, one step back
21@pytest.mark.asyncio
22async def test_url_encoded_data():

Drop sync (#544)Tom Christiebefore adoption206c537

line 23why, one step back
23 request = httpx.Request("POST", "http://example.org", data={"test": "123"})

Public Auth API (#732)Tom Christiebefore adoption12dd157

line 24why, one step back
24 await request.aread()

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 25why, one step back
25

Drop sync (#544)Tom Christiebefore adoption206c537

line 26why, one step back
26 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"

Public Auth API (#732)Tom Christiebefore adoption12dd157

line 27why, one step back
27 assert request.content == b"test=123"

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 28–29why, one step back
28
29

Added RequestContent (#636)Tom Christiebefore adoption8d4f182

lines 30–31why, one step back
30@pytest.mark.asyncio
31async def test_json_encoded_data():

Drop sync (#544)Tom Christiebefore adoption206c537

line 32why, one step back
32 request = httpx.Request("POST", "http://example.org", json={"test": 123})

Public Auth API (#732)Tom Christiebefore adoption12dd157

line 33why, one step back
33 await request.aread()

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 34why, one step back
34

Drop sync (#544)Tom Christiebefore adoption206c537

line 35why, one step back
35 assert request.headers["Content-Type"] == "application/json"

Public Auth API (#732)Tom Christiebefore adoption12dd157

lines 36–55why, one step back
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.content

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 56–58why, one step back
56
5758def test_transfer_encoding_header():

Drop sync (#544)Tom Christiebefore adoption206c537

line 59why, one step back
59 async def streaming_body(data):

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 60–83why, one step back
60 yield data # pragma: nocover
6162 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():

Drop sync (#544)Tom Christiebefore adoption206c537

line 84why, one step back
84 async def streaming_body(data):

Add debug logging to SSLConfig (#378)Seth Michael Larsonbefore adoption0fa55bb

lines 85–113why, one step back
85 yield data # pragma: nocover
8687 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")