Use httpx public API for 'test_content' tests (#2494)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent 7985f68 · Browse files · 1 file·+164 added−131 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+164 added−131 removed
- tests:+164 added−131 removed
Provenance
- Commit
71a1589- Tree
882fe7c- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
7985f68
1 file changed+164 added−131 removed
modified:tests/test_content.py+164 added-131 removed
2 unmodified lines3 4 import pytest5 6 import httpx7-from httpx._content import encode_request, encode_response7 8+method = "POST"9+url = "https://www.example.com"10 11+12 @pytest.mark.asyncio13 async def test_empty_content():12- headers, stream = encode_request()13- assert isinstance(stream, httpx.SyncByteStream)14- assert isinstance(stream, httpx.AsyncByteStream)14+ request = httpx.Request(method, url)15+ assert isinstance(request.stream, httpx.SyncByteStream)16+ assert isinstance(request.stream, httpx.AsyncByteStream)17 16- sync_content = b"".join([part for part in stream])17- async_content = b"".join([part async for part in stream])18+ sync_content = b"".join([part for part in request.stream])19+ async_content = b"".join([part async for part in request.stream])20 19- assert headers == {}21+ assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}22 assert sync_content == b""23 assert async_content == b""24 25 26 @pytest.mark.asyncio27 async def test_bytes_content():26- headers, stream = encode_request(content=b"Hello, world!")27- assert isinstance(stream, typing.Iterable)28- assert isinstance(stream, typing.AsyncIterable)28+ request = httpx.Request(method, url, content=b"Hello, world!")29+ assert isinstance(request.stream, typing.Iterable)30+ assert isinstance(request.stream, typing.AsyncIterable)31 30- sync_content = b"".join([part for part in stream])31- async_content = b"".join([part async for part in stream])32+ sync_content = b"".join([part for part in request.stream])33+ async_content = b"".join([part async for part in request.stream])34 33- assert headers == {"Content-Length": "13"}35+ assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}36 assert sync_content == b"Hello, world!"37 assert async_content == b"Hello, world!"38 39 # Support 'data' for compat with requests.40 with pytest.warns(DeprecationWarning):39- headers, stream = encode_request(data=b"Hello, world!") # type: ignore40- assert isinstance(stream, typing.Iterable)41- assert isinstance(stream, typing.AsyncIterable)41+ request = httpx.Request(method, url, data=b"Hello, world!") # type: ignore42+ assert isinstance(request.stream, typing.Iterable)43+ assert isinstance(request.stream, typing.AsyncIterable)44 43- sync_content = b"".join([part for part in stream])44- async_content = b"".join([part async for part in stream])45+ sync_content = b"".join([part for part in request.stream])46+ async_content = b"".join([part async for part in request.stream])47 46- assert headers == {"Content-Length": "13"}48+ assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}49 assert sync_content == b"Hello, world!"50 assert async_content == b"Hello, world!"51 52 53 @pytest.mark.asyncio54 async def test_bytesio_content():53- headers, stream = encode_request(content=io.BytesIO(b"Hello, world!"))54- assert isinstance(stream, typing.Iterable)55- assert not isinstance(stream, typing.AsyncIterable)55+ request = httpx.Request(method, url, content=io.BytesIO(b"Hello, world!"))56+ assert isinstance(request.stream, typing.Iterable)57+ assert not isinstance(request.stream, typing.AsyncIterable)58 57- content = b"".join([part for part in stream])59+ content = b"".join([part for part in request.stream])60 59- assert headers == {"Content-Length": "13"}61+ assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}62 assert content == b"Hello, world!"63 64 65 @pytest.mark.asyncio10 unmodified lines76 77 async def __aiter__(self):78 yield self._content # pragma: no cover79 78- headers, stream = encode_request(content=AsyncBytesIO(b"Hello, world!"))79- assert not isinstance(stream, typing.Iterable)80- assert isinstance(stream, typing.AsyncIterable)80+ request = httpx.Request(method, url, content=AsyncBytesIO(b"Hello, world!"))81+ assert not isinstance(request.stream, typing.Iterable)82+ assert isinstance(request.stream, typing.AsyncIterable)83 82- content = b"".join([part async for part in stream])84+ content = b"".join([part async for part in request.stream])85 84- assert headers == {"Transfer-Encoding": "chunked"}86+ assert request.headers == {87+ "Host": "www.example.com",88+ "Transfer-Encoding": "chunked",89+ }90 assert content == b"Hello, world!"91 92 93 @pytest.mark.asyncio1 unmodified line95 def hello_world() -> typing.Iterator[bytes]:96 yield b"Hello, "97 yield b"world!"98 94- headers, stream = encode_request(content=hello_world())95- assert isinstance(stream, typing.Iterable)96- assert not isinstance(stream, typing.AsyncIterable)99+ request = httpx.Request(method, url, content=hello_world())100+ assert isinstance(request.stream, typing.Iterable)101+ assert not isinstance(request.stream, typing.AsyncIterable)102 98- content = b"".join([part for part in stream])103+ content = b"".join([part for part in request.stream])104 100- assert headers == {"Transfer-Encoding": "chunked"}105+ assert request.headers == {106+ "Host": "www.example.com",107+ "Transfer-Encoding": "chunked",108+ }109 assert content == b"Hello, world!"110 111 with pytest.raises(httpx.StreamConsumed):104- [part for part in stream]112+ [part for part in request.stream]113 114 # Support 'data' for compat with requests.115 with pytest.warns(DeprecationWarning):108- headers, stream = encode_request(data=hello_world()) # type: ignore109- assert isinstance(stream, typing.Iterable)110- assert not isinstance(stream, typing.AsyncIterable)116+ request = httpx.Request(method, url, data=hello_world()) # type: ignore117+ assert isinstance(request.stream, typing.Iterable)118+ assert not isinstance(request.stream, typing.AsyncIterable)119 112- content = b"".join([part for part in stream])120+ content = b"".join([part for part in request.stream])121 114- assert headers == {"Transfer-Encoding": "chunked"}122+ assert request.headers == {123+ "Host": "www.example.com",124+ "Transfer-Encoding": "chunked",125+ }126 assert content == b"Hello, world!"127 128 129 @pytest.mark.asyncio1 unmodified line131 async def hello_world() -> typing.AsyncIterator[bytes]:132 yield b"Hello, "133 yield b"world!"134 124- headers, stream = encode_request(content=hello_world())125- assert not isinstance(stream, typing.Iterable)126- assert isinstance(stream, typing.AsyncIterable)135+ request = httpx.Request(method, url, content=hello_world())136+ assert not isinstance(request.stream, typing.Iterable)137+ assert isinstance(request.stream, typing.AsyncIterable)138 128- content = b"".join([part async for part in stream])139+ content = b"".join([part async for part in request.stream])140 130- assert headers == {"Transfer-Encoding": "chunked"}141+ assert request.headers == {142+ "Host": "www.example.com",143+ "Transfer-Encoding": "chunked",144+ }145 assert content == b"Hello, world!"146 147 with pytest.raises(httpx.StreamConsumed):134- [part async for part in stream]148+ [part async for part in request.stream]149 150 # Support 'data' for compat with requests.151 with pytest.warns(DeprecationWarning):138- headers, stream = encode_request(data=hello_world()) # type: ignore139- assert not isinstance(stream, typing.Iterable)140- assert isinstance(stream, typing.AsyncIterable)152+ request = httpx.Request(method, url, data=hello_world()) # type: ignore153+ assert not isinstance(request.stream, typing.Iterable)154+ assert isinstance(request.stream, typing.AsyncIterable)155 142- content = b"".join([part async for part in stream])156+ content = b"".join([part async for part in request.stream])157 144- assert headers == {"Transfer-Encoding": "chunked"}158+ assert request.headers == {159+ "Host": "www.example.com",160+ "Transfer-Encoding": "chunked",161+ }162 assert content == b"Hello, world!"163 164 165 @pytest.mark.asyncio166 async def test_json_content():150- headers, stream = encode_request(json={"Hello": "world!"})151- assert isinstance(stream, typing.Iterable)152- assert isinstance(stream, typing.AsyncIterable)167+ request = httpx.Request(method, url, json={"Hello": "world!"})168+ assert isinstance(request.stream, typing.Iterable)169+ assert isinstance(request.stream, typing.AsyncIterable)170 154- sync_content = b"".join([part for part in stream])155- async_content = b"".join([part async for part in stream])171+ sync_content = b"".join([part for part in request.stream])172+ async_content = b"".join([part async for part in request.stream])173 157- assert headers == {174+ assert request.headers == {175+ "Host": "www.example.com",176 "Content-Length": "19",177 "Content-Type": "application/json",178 }179 assert sync_content == b'{"Hello": "world!"}'1 unmodified line181 182 183 @pytest.mark.asyncio184 async def test_urlencoded_content():167- headers, stream = encode_request(data={"Hello": "world!"})168- assert isinstance(stream, typing.Iterable)169- assert isinstance(stream, typing.AsyncIterable)185+ request = httpx.Request(method, url, data={"Hello": "world!"})186+ assert isinstance(request.stream, typing.Iterable)187+ assert isinstance(request.stream, typing.AsyncIterable)188 171- sync_content = b"".join([part for part in stream])172- async_content = b"".join([part async for part in stream])189+ sync_content = b"".join([part for part in request.stream])190+ async_content = b"".join([part async for part in request.stream])191 174- assert headers == {192+ assert request.headers == {193+ "Host": "www.example.com",194 "Content-Length": "14",195 "Content-Type": "application/x-www-form-urlencoded",196 }197 assert sync_content == b"Hello=world%21"1 unmodified line199 200 201 @pytest.mark.asyncio202 async def test_urlencoded_boolean():184- headers, stream = encode_request(data={"example": True})185- assert isinstance(stream, typing.Iterable)186- assert isinstance(stream, typing.AsyncIterable)203+ request = httpx.Request(method, url, data={"example": True})204+ assert isinstance(request.stream, typing.Iterable)205+ assert isinstance(request.stream, typing.AsyncIterable)206 188- sync_content = b"".join([part for part in stream])189- async_content = b"".join([part async for part in stream])207+ sync_content = b"".join([part for part in request.stream])208+ async_content = b"".join([part async for part in request.stream])209 191- assert headers == {210+ assert request.headers == {211+ "Host": "www.example.com",212 "Content-Length": "12",213 "Content-Type": "application/x-www-form-urlencoded",214 }215 assert sync_content == b"example=true"1 unmodified line217 218 219 @pytest.mark.asyncio220 async def test_urlencoded_none():201- headers, stream = encode_request(data={"example": None})202- assert isinstance(stream, typing.Iterable)203- assert isinstance(stream, typing.AsyncIterable)221+ request = httpx.Request(method, url, data={"example": None})222+ assert isinstance(request.stream, typing.Iterable)223+ assert isinstance(request.stream, typing.AsyncIterable)224 205- sync_content = b"".join([part for part in stream])206- async_content = b"".join([part async for part in stream])225+ sync_content = b"".join([part for part in request.stream])226+ async_content = b"".join([part async for part in request.stream])227 208- assert headers == {228+ assert request.headers == {229+ "Host": "www.example.com",230 "Content-Length": "8",231 "Content-Type": "application/x-www-form-urlencoded",232 }233 assert sync_content == b"example="1 unmodified line235 236 237 @pytest.mark.asyncio238 async def test_urlencoded_list():218- headers, stream = encode_request(data={"example": ["a", 1, True]})219- assert isinstance(stream, typing.Iterable)220- assert isinstance(stream, typing.AsyncIterable)239+ request = httpx.Request(method, url, data={"example": ["a", 1, True]})240+ assert isinstance(request.stream, typing.Iterable)241+ assert isinstance(request.stream, typing.AsyncIterable)242 222- sync_content = b"".join([part for part in stream])223- async_content = b"".join([part async for part in stream])243+ sync_content = b"".join([part for part in request.stream])244+ async_content = b"".join([part async for part in request.stream])245 225- assert headers == {246+ assert request.headers == {247+ "Host": "www.example.com",248 "Content-Length": "32",249 "Content-Type": "application/x-www-form-urlencoded",250 }251 assert sync_content == b"example=a&example=1&example=true"2 unmodified lines254 255 @pytest.mark.asyncio256 async def test_multipart_files_content():257 files = {"file": io.BytesIO(b"<file content>")}258+ headers = {"Content-Type": "multipart/form-data; boundary=+++"}259+ request = httpx.Request(260+ method,261+ url,262+ files=files,263+ headers=headers,236- headers, stream = encode_request(files=files, boundary=b"+++")237- assert isinstance(stream, typing.Iterable)238- assert isinstance(stream, typing.AsyncIterable)264+ )265+ assert isinstance(request.stream, typing.Iterable)266+ assert isinstance(request.stream, typing.AsyncIterable)267 240- sync_content = b"".join([part for part in stream])241- async_content = b"".join([part async for part in stream])268+ sync_content = b"".join([part for part in request.stream])269+ async_content = b"".join([part async for part in request.stream])270 243- assert headers == {271+ assert request.headers == {272+ "Host": "www.example.com",273 "Content-Length": "138",274 "Content-Type": "multipart/form-data; boundary=+++",275 }276 assert sync_content == b"".join(21 unmodified lines298 @pytest.mark.asyncio299 async def test_multipart_data_and_files_content():300 data = {"message": "Hello, world!"}301 files = {"file": io.BytesIO(b"<file content>")}273- headers, stream = encode_request(data=data, files=files, boundary=b"+++")274- assert isinstance(stream, typing.Iterable)275- assert isinstance(stream, typing.AsyncIterable)302+ headers = {"Content-Type": "multipart/form-data; boundary=+++"}303+ request = httpx.Request(method, url, data=data, files=files, headers=headers)304+ assert isinstance(request.stream, typing.Iterable)305+ assert isinstance(request.stream, typing.AsyncIterable)306 277- sync_content = b"".join([part for part in stream])278- async_content = b"".join([part async for part in stream])307+ sync_content = b"".join([part for part in request.stream])308+ async_content = b"".join([part async for part in request.stream])309 280- assert headers == {310+ assert request.headers == {311+ "Host": "www.example.com",312 "Content-Length": "210",313 "Content-Type": "multipart/form-data; boundary=+++",314 }315 assert sync_content == b"".join(27 unmodified lines343 344 345 @pytest.mark.asyncio346 async def test_empty_request():316- headers, stream = encode_request(data={}, files={})317- assert isinstance(stream, typing.Iterable)318- assert isinstance(stream, typing.AsyncIterable)347+ request = httpx.Request(method, url, data={}, files={})348+ assert isinstance(request.stream, typing.Iterable)349+ assert isinstance(request.stream, typing.AsyncIterable)350 320- sync_content = b"".join([part for part in stream])321- async_content = b"".join([part async for part in stream])351+ sync_content = b"".join([part for part in request.stream])352+ async_content = b"".join([part async for part in request.stream])353 323- assert headers == {}354+ assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}355 assert sync_content == b""356 assert async_content == b""357 358 359 def test_invalid_argument():360 with pytest.raises(TypeError):330- encode_request(123) # type: ignore361+ httpx.Request(method, url, content=123) # type: ignore362 363 364 @pytest.mark.asyncio365 async def test_multipart_multiple_files_single_input_content():366 files = [367 ("file", io.BytesIO(b"<file content 1>")),368 ("file", io.BytesIO(b"<file content 2>")),369 ]339- headers, stream = encode_request(files=files, boundary=b"+++")340- assert isinstance(stream, typing.Iterable)341- assert isinstance(stream, typing.AsyncIterable)370+ headers = {"Content-Type": "multipart/form-data; boundary=+++"}371+ request = httpx.Request(method, url, files=files, headers=headers)372+ assert isinstance(request.stream, typing.Iterable)373+ assert isinstance(request.stream, typing.AsyncIterable)374 343- sync_content = b"".join([part for part in stream])344- async_content = b"".join([part async for part in stream])375+ sync_content = b"".join([part for part in request.stream])376+ async_content = b"".join([part async for part in request.stream])377 346- assert headers == {378+ assert request.headers == {379+ "Host": "www.example.com",380 "Content-Length": "271",381 "Content-Type": "multipart/form-data; boundary=+++",382 }383 assert sync_content == b"".join(29 unmodified lines413 414 415 @pytest.mark.asyncio416 async def test_response_empty_content():384- headers, stream = encode_response()385- assert isinstance(stream, typing.Iterable)386- assert isinstance(stream, typing.AsyncIterable)417+ response = httpx.Response(200)418+ assert isinstance(response.stream, typing.Iterable)419+ assert isinstance(response.stream, typing.AsyncIterable)420 388- sync_content = b"".join([part for part in stream])389- async_content = b"".join([part async for part in stream])421+ sync_content = b"".join([part for part in response.stream])422+ async_content = b"".join([part async for part in response.stream])423 391- assert headers == {}424+ assert response.headers == {}425 assert sync_content == b""426 assert async_content == b""427 428 429 @pytest.mark.asyncio430 async def test_response_bytes_content():398- headers, stream = encode_response(content=b"Hello, world!")399- assert isinstance(stream, typing.Iterable)400- assert isinstance(stream, typing.AsyncIterable)431+ response = httpx.Response(200, content=b"Hello, world!")432+ assert isinstance(response.stream, typing.Iterable)433+ assert isinstance(response.stream, typing.AsyncIterable)434 402- sync_content = b"".join([part for part in stream])403- async_content = b"".join([part async for part in stream])435+ sync_content = b"".join([part for part in response.stream])436+ async_content = b"".join([part async for part in response.stream])437 405- assert headers == {"Content-Length": "13"}438+ assert response.headers == {"Content-Length": "13"}439 assert sync_content == b"Hello, world!"440 assert async_content == b"Hello, world!"441 442 2 unmodified lines445 def hello_world() -> typing.Iterator[bytes]:446 yield b"Hello, "447 yield b"world!"448 416- headers, stream = encode_response(content=hello_world())417- assert isinstance(stream, typing.Iterable)418- assert not isinstance(stream, typing.AsyncIterable)449+ response = httpx.Response(200, content=hello_world())450+ assert isinstance(response.stream, typing.Iterable)451+ assert not isinstance(response.stream, typing.AsyncIterable)452 420- content = b"".join([part for part in stream])453+ content = b"".join([part for part in response.stream])454 422- assert headers == {"Transfer-Encoding": "chunked"}455+ assert response.headers == {"Transfer-Encoding": "chunked"}456 assert content == b"Hello, world!"457 458 with pytest.raises(httpx.StreamConsumed):426- [part for part in stream]459+ [part for part in response.stream]460 461 462 @pytest.mark.asyncio463 async def test_response_aiterator_content():464 async def hello_world() -> typing.AsyncIterator[bytes]:465 yield b"Hello, "466 yield b"world!"467 435- headers, stream = encode_response(content=hello_world())436- assert not isinstance(stream, typing.Iterable)437- assert isinstance(stream, typing.AsyncIterable)468+ response = httpx.Response(200, content=hello_world())469+ assert not isinstance(response.stream, typing.Iterable)470+ assert isinstance(response.stream, typing.AsyncIterable)471 439- content = b"".join([part async for part in stream])472+ content = b"".join([part async for part in response.stream])473 441- assert headers == {"Transfer-Encoding": "chunked"}474+ assert response.headers == {"Transfer-Encoding": "chunked"}475 assert content == b"Hello, world!"476 477 with pytest.raises(httpx.StreamConsumed):445- [part async for part in stream]478+ [part async for part in response.stream]479 480 481 def test_response_invalid_argument():482 with pytest.raises(TypeError):450- encode_response(123) # type: ignore483+ httpx.Response(200, content=123) # type: ignore