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 fileTake 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 back1import typing2Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 3–18why, one step back3import pytest45import 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 back19 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 back20 assert request.headers["Content-Length"] == "8"2122Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 23–28why, one step back23def 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 back29 assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 30–36why, one step back303132def 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 back37 assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 38–47why, one step back383940def 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 back48 assert request.headers == {"Host": "example.org", "Content-Length": "8"}Drop ContentStream (#1295)Tom Christiebefore adoptionfbb21fb
lines 49–50why, one step back4950Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 51–66why, one step back51def 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}'6566Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 67–69why, one step back67def test_headers():68 request = httpx.Request("POST", "http://example.org", json={"test": 123})69Support header comparisons with dict or list. (#1326)Musale Martinbefore adoption815ef94
lines 70–74why, one step back70 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 back7576Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 77–81why, one step back77def 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 back82 assert request.stream is not None83 assert isinstance(request.stream, typing.Iterable)Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 84–93why, one step back84 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 back94 assert request.stream is not None95 assert isinstance(request.stream, typing.AsyncIterable)Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 96–102why, one step back96 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 back103 # 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 back104 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"118119Ignore transfer-encoding if content-length presents (#1170)Joebefore adoption5b6d33e
lines 120–136why, one step back120def 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"135136Fix VERSION_FILE in publish script. (#916)Tom Christiebefore adoption3010261
lines 137–165why, one step back137def 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 back166 assert request.url.port is NoneFinesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 167–168why, one step back167 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 back169170 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 back173 assert request.url.port is NoneFinesse URL properties (#1285)Tom Christiebefore adoption8e4a8a1
lines 174–175why, one step back174 assert request.url.path == "/abc"175 assert request.url.raw_path == b"/abc?foo=bar"