Skip to content
Myr

test_requests.py

This file

Path

tests/models/test_requests.py

175 lines · 5.5 KiB · Python

Last changed by

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

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–28why, 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())

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 30–36why, one step back
30
3132def test_generator_with_transfer_encoding_header():33 def content():34 yield b"test 123" # pragma: nocover3536 request = httpx.Request("POST", "http://example.org", content=content())

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 38–47why, one step back
38
3940def test_generator_with_content_length_header():41 def content():42 yield b"test 123" # pragma: nocover4344 headers = {"Content-Length": "8"}45 request = httpx.Request(46 "POST", "http://example.org", content=content(), headers=headers47 )

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

lines 49–50why, one step back
49
50

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

lines 51–66why, one step back
51def test_url_encoded_data():
52 request = httpx.Request("POST", "http://example.org", data={"test": "123"})53 request.read()5455 assert request.headers["Content-Type"] == "application/x-www-form-urlencoded"56 assert request.content == b"test=123"575859def test_json_encoded_data():60 request = httpx.Request("POST", "http://example.org", json={"test": 123})61 request.read()6263 assert request.headers["Content-Type"] == "application/json"64 assert request.content == b'{"test": 123}'6566

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 67–69why, one step back
67def test_headers():
68 request = httpx.Request("POST", "http://example.org", json={"test": 123})69

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

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

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 75–76why, one step back
75
76

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

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

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

Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb

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

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

lines 104–119why, one step back
104 request = httpx.Request("POST", "http://example.org", json={"test": 123})
105 with pytest.raises(httpx.RequestNotRead):106 request.content107108109def test_transfer_encoding_header():110 async def streaming_body(data):111 yield data # pragma: nocover112113 data = streaming_body(b"test 123")114115 request = httpx.Request("POST", "http://example.org", data=data)116 assert "Content-Length" not in request.headers117 assert request.headers["Transfer-Encoding"] == "chunked"118119

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

lines 120–136why, one step back
120def test_ignore_transfer_encoding_header_if_content_length_exists():
121 """122 `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.123 See https://github.com/encode/httpx/issues/1168124 """125126 def streaming_body(data):127 yield data # pragma: nocover128129 data = streaming_body(b"abcd")130131 headers = {"Content-Length": "4"}132 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)133 assert "Transfer-Encoding" not in request.headers134 assert request.headers["Content-Length"] == "4"135136

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

lines 137–165why, one step back
137def test_override_host_header():
138 headers = {"host": "1.2.3.4:80"}139140 request = httpx.Request("GET", "http://example.org", headers=headers)141 assert request.headers["Host"] == "1.2.3.4:80"142143144def test_override_accept_encoding_header():145 headers = {"Accept-Encoding": "identity"}146147 request = httpx.Request("GET", "http://example.org", headers=headers)148 assert request.headers["Accept-Encoding"] == "identity"149150151def test_override_content_length_header():152 async def streaming_body(data):153 yield data # pragma: nocover154155 data = streaming_body(b"test 123")156 headers = {"Content-Length": "8"}157158 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)159 assert request.headers["Content-Length"] == "8"160161162def test_url():163 url = "http://example.org"164 request = httpx.Request("GET", url)165 assert request.url.scheme == "http"

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

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

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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

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

lines 169–172why, one step back
169
170 url = "https://example.org/abc?foo=bar"171 request = httpx.Request("GET", url)172 assert request.url.scheme == "https"

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

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

Finesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1

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