Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

228 lines · 7.3 KiB · Python

Last changed by

Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104

History of this file

Take it

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

Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104

line 1why, one step back
1import pickle

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 4–19why, one step back
4import pytest
56import httpx789def test_request_repr():10 request = httpx.Request("GET", "http://example.org")11 assert repr(request) == "<Request('GET', 'http://example.org')>"121314def test_no_content():15 request = httpx.Request("GET", "http://example.org")16 assert "Content-Length" not in request.headers171819def test_content_length_header():

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

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

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 21–23why, one step back
21 assert request.headers["Content-Length"] == "8"
2223

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 24–29why, one step back
24def test_iterable_content():
25 class Content:26 def __iter__(self):27 yield b"test 123" # pragma: nocover2829 request = httpx.Request("POST", "http://example.org", content=Content())

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 30why, one step back
30 assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 31–37why, one step back
31
3233def test_generator_with_transfer_encoding_header():34 def content():35 yield b"test 123" # pragma: nocover3637 request = httpx.Request("POST", "http://example.org", content=content())

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 38why, one step back
38 assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 39–48why, one step back
39
4041def test_generator_with_content_length_header():42 def content():43 yield b"test 123" # pragma: nocover4445 headers = {"Content-Length": "8"}46 request = httpx.Request(47 "POST", "http://example.org", content=content(), headers=headers48 )

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

line 49why, one step back
49 assert request.headers == {"Host": "example.org", "Content-Length": "8"}

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 50–51why, one step back
50
51

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 52–70why, one step back
52def test_url_encoded_data():
53 request = httpx.Request("POST", "http://example.org", data={"test": "123"})54 request.read()5556 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"57 assert request.content == b"test=123"585960def test_json_encoded_data():61 request = httpx.Request("POST", "http://example.org", json={"test": 123})62 request.read()6364 assert request.headers["Content-Type"] == "application/json"65 assert request.content == b'{"test": 123}'666768def test_headers():69 request = httpx.Request("POST", "http://example.org", json={"test": 123})70

Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94

lines 71–75why, one step back
71 assert request.headers == {
72 "Host": "example.org",73 "Content-Type": "application/json",74 "Content-Length": "13",75 }

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 97–100why, one step back
97 content = b"".join([part async for part in request.stream])
98 assert content == request.content99100

For non-streaming cases, populate `request.content` automatically. (#1583)Tom Christiebefore adoption966550b

lines 101–106why, one step back
101def test_cannot_access_streaming_content_without_read():
102 # Ensure that streaming requests103 def streaming_body(): # pragma: nocover104 yield ""105106 request = httpx.Request("POST", "http://example.org", content=streaming_body())

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 107–116why, one step back
107 with pytest.raises(httpx.RequestNotRead):
108 request.content109110111def test_transfer_encoding_header():112 async def streaming_body(data):113 yield data # pragma: nocover114115 data = streaming_body(b"test 123")116

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

line 117why, one step back
117 request = httpx.Request("POST", "http://example.org", content=data)

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 118–133why, one step back
118 assert "Content-Length" not in request.headers
119 assert request.headers["Transfer-Encoding"] == "chunked"120121122def test_ignore_transfer_encoding_header_if_content_length_exists():123 """124 `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.125 See https://github.com/encode/httpx/issues/1168126 """127128 def streaming_body(data):129 yield data # pragma: nocover130131 data = streaming_body(b"abcd")132133 headers = {"Content-Length": "4"}

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

line 134why, one step back
134 request = httpx.Request("POST", "http://example.org", content=data, headers=headers)

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 135–159why, one step back
135 assert "Transfer-Encoding" not in request.headers
136 assert request.headers["Content-Length"] == "4"137138139def test_override_host_header():140 headers = {"host": "1.2.3.4:80"}141142 request = httpx.Request("GET", "http://example.org", headers=headers)143 assert request.headers["Host"] == "1.2.3.4:80"144145146def test_override_accept_encoding_header():147 headers = {"Accept-Encoding": "identity"}148149 request = httpx.Request("GET", "http://example.org", headers=headers)150 assert request.headers["Accept-Encoding"] == "identity"151152153def test_override_content_length_header():154 async def streaming_body(data):155 yield data # pragma: nocover156157 data = streaming_body(b"test 123")158 headers = {"Content-Length": "8"}159

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

line 160why, one step back
160 request = httpx.Request("POST", "http://example.org", content=data, headers=headers)

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 161–168why, one step back
161 assert request.headers["Content-Length"] == "8"
162163164def test_url():165 url = "http://example.org"166 request = httpx.Request("GET", url)167 assert request.url.scheme == "http"168 assert request.url.port is None

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

Drop `request.timer` attribute. (#1249)Tom Christiebefore adoptiona783fe5

lines 171–175why, one step back
171
172 url = "https://example.org/abc?foo=bar"173 request = httpx.Request("GET", url)174 assert request.url.scheme == "https"175 assert request.url.port is None

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

Make Request and Response picklable (#1579)Hannes Ljungbergbefore adoption2d57104

lines 178–228why, one step back
178
179180def test_request_picklable():181 request = httpx.Request("POST", "http://example.org", json={"test": 123})182 pickle_request = pickle.loads(pickle.dumps(request))183 assert pickle_request.method == "POST"184 assert pickle_request.url.path == "/"185 assert pickle_request.headers["Content-Type"] == "application/json"186 assert pickle_request.content == b'{"test": 123}'187 assert pickle_request.stream is not None188 assert request.headers == {189 "Host": "example.org",190 "Content-Type": "application/json",191 "content-length": "13",192 }193194195@pytest.mark.asyncio196async def test_request_async_streaming_content_picklable():197 async def streaming_body(data):198 yield data199200 data = streaming_body(b"test 123")201 request = httpx.Request("POST", "http://example.org", content=data)202 pickle_request = pickle.loads(pickle.dumps(request))203 with pytest.raises(httpx.RequestNotRead):204 pickle_request.content205 with pytest.raises(httpx.StreamClosed):206 await pickle_request.aread()207208 request = httpx.Request("POST", "http://example.org", content=data)209 await request.aread()210 pickle_request = pickle.loads(pickle.dumps(request))211 assert pickle_request.content == b"test 123"212213214def test_request_generator_content_picklable():215 def content():216 yield b"test 123" # pragma: nocover217218 request = httpx.Request("POST", "http://example.org", content=content())219 pickle_request = pickle.loads(pickle.dumps(request))220 with pytest.raises(httpx.RequestNotRead):221 pickle_request.content222 with pytest.raises(httpx.StreamClosed):223 pickle_request.read()224225 request = httpx.Request("POST", "http://example.org", content=content())226 request.read()227 pickle_request = pickle.loads(pickle.dumps(request))228 assert pickle_request.content == b"test 123"