Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

183 lines · 5.7 KiB · Python

Last changed by

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

History of this file

Take it

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 1–2why, one step back
1import typing
2

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 3–18why, one step back
3import pytest
45import httpx678def test_request_repr():9 request = httpx.Request("GET", "http://example.org")10 assert repr(request) == "<Request('GET', 'http://example.org')>"111213def test_no_content():14 request = httpx.Request("GET", "http://example.org")15 assert "Content-Length" not in request.headers161718def test_content_length_header():

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 19why, one step back
19 request = httpx.Request("POST", "http://example.org", content=b"test 123")

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 20–22why, one step back
20 assert request.headers["Content-Length"] == "8"
2122

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 23–56why, one step back
23def test_iterable_content():
24 class Content:25 def __iter__(self):26 yield b"test 123" # pragma: nocover2728 request = httpx.Request("POST", "http://example.org", content=Content())29 assert request.headers == httpx.Headers(30 {"Host": "example.org", "Transfer-Encoding": "chunked"}31 )323334def test_generator_with_transfer_encoding_header():35 def content():36 yield b"test 123" # pragma: nocover3738 request = httpx.Request("POST", "http://example.org", content=content())39 assert request.headers == httpx.Headers(40 {"Host": "example.org", "Transfer-Encoding": "chunked"}41 )424344def test_generator_with_content_length_header():45 def content():46 yield b"test 123" # pragma: nocover4748 headers = {"Content-Length": "8"}49 request = httpx.Request(50 "POST", "http://example.org", content=content(), headers=headers51 )52 assert request.headers == httpx.Headers(53 {"Host": "example.org", "Content-Length": "8"}54 )5556

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 57–72why, one step back
57def test_url_encoded_data():
58 request = httpx.Request("POST", "http://example.org", data={"test": "123"})59 request.read()6061 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"62 assert request.content == b"test=123"636465def test_json_encoded_data():66 request = httpx.Request("POST", "http://example.org", json={"test": 123})67 request.read()6869 assert request.headers["Content-Type"] == "application/json"70 assert request.content == b'{"test": 123}'7172

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 73–84why, one step back
73def test_headers():
74 request = httpx.Request("POST", "http://example.org", json={"test": 123})7576 assert request.headers == httpx.Headers(77 {78 "Host": "example.org",79 "Content-Type": "application/json",80 "Content-Length": "13",81 }82 )8384

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 85–89why, one step back
85def test_read_and_stream_data():
86 # Ensure a request may still be streamed if it has been read.87 # Needed for cases such as authentication classes that read the request body.88 request = httpx.Request("POST", "http://example.org", json={"test": 123})89 request.read()

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 90–91why, one step back
90 assert request.stream is not None
91 assert isinstance(request.stream, typing.Iterable)

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 92–101why, one step back
92 content = b"".join([part for part in request.stream])
93 assert content == request.content949596@pytest.mark.asyncio97async def test_aread_and_stream_data():98 # Ensure a request may still be streamed if it has been read.99 # Needed for cases such as authentication classes that read the request body.100 request = httpx.Request("POST", "http://example.org", json={"test": 123})101 await request.aread()

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 102–103why, one step back
102 assert request.stream is not None
103 assert isinstance(request.stream, typing.AsyncIterable)

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 104–110why, one step back
104 content = b"".join([part async for part in request.stream])
105 assert content == request.content106107108@pytest.mark.asyncio109async def test_cannot_access_content_without_read():110 # Ensure a request may still be streamed if it has been read.

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

line 111why, one step back
111 # Needed for cases such as authentication classes that read the request body.

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 112–127why, one step back
112 request = httpx.Request("POST", "http://example.org", json={"test": 123})
113 with pytest.raises(httpx.RequestNotRead):114 request.content115116117def test_transfer_encoding_header():118 async def streaming_body(data):119 yield data # pragma: nocover120121 data = streaming_body(b"test 123")122123 request = httpx.Request("POST", "http://example.org", data=data)124 assert "Content-Length" not in request.headers125 assert request.headers["Transfer-Encoding"] == "chunked"126127

Ignore transfer-encoding if content-length presents (#1170)Joebefore adoption5b6d33e

lines 128–144why, one step back
128def test_ignore_transfer_encoding_header_if_content_length_exists():
129 """130 `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.131 See https://github.com/encode/httpx/issues/1168132 """133134 def streaming_body(data):135 yield data # pragma: nocover136137 data = streaming_body(b"abcd")138139 headers = {"Content-Length": "4"}140 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)141 assert "Transfer-Encoding" not in request.headers142 assert request.headers["Content-Length"] == "4"143144

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 145–173why, one step back
145def test_override_host_header():
146 headers = {"host": "1.2.3.4:80"}147148 request = httpx.Request("GET", "http://example.org", headers=headers)149 assert request.headers["Host"] == "1.2.3.4:80"150151152def test_override_accept_encoding_header():153 headers = {"Accept-Encoding": "identity"}154155 request = httpx.Request("GET", "http://example.org", headers=headers)156 assert request.headers["Accept-Encoding"] == "identity"157158159def test_override_content_length_header():160 async def streaming_body(data):161 yield data # pragma: nocover162163 data = streaming_body(b"test 123")164 headers = {"Content-Length": "8"}165166 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)167 assert request.headers["Content-Length"] == "8"168169170def test_url():171 url = "http://example.org"172 request = httpx.Request("GET", url)173 assert request.url.scheme == "http"

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 174why, one step back
174 assert request.url.port is None

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 175–176why, one step back
175 assert request.url.path == "/"
176 assert request.url.raw_path == b"/"

Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261

lines 177–180why, one step back
177
178 url = "https://example.org/abc?foo=bar"179 request = httpx.Request("GET", url)180 assert request.url.scheme == "https"

URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480

line 181why, one step back
181 assert request.url.port is None

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

lines 182–183why, one step back
182 assert request.url.path == "/abc"
183 assert request.url.raw_path == b"/abc?foo=bar"