test_content_streams.py
This file
Path
tests/test_content_streams.py
116 lines · 3.2 KiB · Python
Last changed by
Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
History of this fileTake it
Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 1–116why, one step back1import io23import pytest45from httpx.content_streams import encode678@pytest.mark.asyncio9async def test_empty_content():10 stream = encode()11 content = b"".join([part async for part in stream])1213 assert stream.can_replay()14 assert stream.get_headers() == {}15 assert content == b""161718@pytest.mark.asyncio19async def test_bytes_content():20 stream = encode(data=b"Hello, world!")21 content = b"".join([part async for part in stream])2223 assert stream.can_replay()24 assert stream.get_headers() == {"Content-Length": "13"}25 assert content == b"Hello, world!"262728@pytest.mark.asyncio29async def test_aiterator_content():30 async def hello_world():31 yield b"Hello, "32 yield b"world!"3334 stream = encode(data=hello_world())35 content = b"".join([part async for part in stream])3637 assert not stream.can_replay()38 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}39 assert content == b"Hello, world!"404142@pytest.mark.asyncio43async def test_json_content():44 stream = encode(json={"Hello": "world!"})45 content = b"".join([part async for part in stream])4647 assert stream.can_replay()48 assert stream.get_headers() == {49 "Content-Length": "19",50 "Content-Type": "application/json",51 }52 assert content == b'{"Hello": "world!"}'535455@pytest.mark.asyncio56async def test_urlencoded_content():57 stream = encode(data={"Hello": "world!"})58 content = b"".join([part async for part in stream])5960 assert stream.can_replay()61 assert stream.get_headers() == {62 "Content-Length": "14",63 "Content-Type": "application/x-www-form-urlencoded",64 }65 assert content == b"Hello=world%21"666768@pytest.mark.asyncio69async def test_multipart_files_content():70 files = {"file": io.BytesIO(b"<file content>")}71 stream = encode(files=files, boundary=b"+++")72 content = b"".join([part async for part in stream])7374 assert stream.can_replay()75 assert stream.get_headers() == {76 "Content-Length": "138",77 "Content-Type": "multipart/form-data; boundary=+++",78 }79 assert content == b"".join(80 [81 b"--+++\r\n",82 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',83 b"Content-Type: application/octet-stream\r\n",84 b"\r\n",85 b"<file content>\r\n",86 b"--+++--\r\n",87 ]88 )899091@pytest.mark.asyncio92async def test_multipart_data_and_files_content():93 data = {"message": "Hello, world!"}94 files = {"file": io.BytesIO(b"<file content>")}95 stream = encode(data=data, files=files, boundary=b"+++")96 content = b"".join([part async for part in stream])9798 assert stream.can_replay()99 assert stream.get_headers() == {100 "Content-Length": "210",101 "Content-Type": "multipart/form-data; boundary=+++",102 }103 assert content == b"".join(104 [105 b"--+++\r\n",106 b'Content-Disposition: form-data; name="message"\r\n',107 b"\r\n",108 b"Hello, world!\r\n",109 b"--+++\r\n",110 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',111 b"Content-Type: application/octet-stream\r\n",112 b"\r\n",113 b"<file content>\r\n",114 b"--+++--\r\n",115 ]116 )