test_content_streams.py
This file
Path
tests/test_content_streams.py
206 lines · 6.0 KiB · Python
Last changed by
Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
History of this fileTake it
Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 1–4why, one step back1import io23import pytest4Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
lines 5–6why, one step back5from httpx import StreamConsumed6from httpx._content_streams import ContentStream, encodeUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 7–9why, one step back789@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 10–21why, one step back10async def test_base_content():11 stream = ContentStream()12 sync_content = b"".join([part for part in stream])13 async_content = b"".join([part async for part in stream])1415 assert stream.can_replay()16 assert stream.get_headers() == {}17 assert sync_content == b""18 assert async_content == b""192021@pytest.mark.asyncioUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 22–23why, one step back22async def test_empty_content():23 stream = encode()Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 24–25why, one step back24 sync_content = b"".join([part for part in stream])25 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 26–27why, one step back2627 assert stream.can_replay()Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 28why, one step back28 assert stream.get_headers() == {}Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 29–30why, one step back29 assert sync_content == b""30 assert async_content == b""Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 31–35why, one step back313233@pytest.mark.asyncio34async def test_bytes_content():35 stream = encode(data=b"Hello, world!")Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 36–37why, one step back36 sync_content = b"".join([part for part in stream])37 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 38–40why, one step back3839 assert stream.can_replay()40 assert stream.get_headers() == {"Content-Length": "13"}Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 41–42why, one step back41 assert sync_content == b"Hello, world!"42 assert async_content == b"Hello, world!"Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 43–45why, one step back434445@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 46–47why, one step back46async def test_iterator_content():47 def hello_world():Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 48–51why, one step back48 yield b"Hello, "49 yield b"world!"5051 stream = encode(data=hello_world())Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 52why, one step back52 content = b"".join([part for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 53–56why, one step back5354 assert not stream.can_replay()55 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}56 assert content == b"Hello, world!"Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
line 57why, one step back57Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 58–59why, one step back58 with pytest.raises(RuntimeError):59 [part async for part in stream]Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
line 60why, one step back60Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 61–64why, one step back61 with pytest.raises(StreamConsumed):62 [part for part in stream]6364Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
line 65why, one step back65@pytest.mark.asyncioSync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 66why, one step back66async def test_aiterator_content():Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
lines 67–71why, one step back67 async def hello_world():68 yield b"Hello, "69 yield b"world!"7071 stream = encode(data=hello_world())Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 72why, one step back72 content = b"".join([part async for part in stream])Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
line 73why, one step back73Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 74–76why, one step back74 assert not stream.can_replay()75 assert stream.get_headers() == {"Transfer-Encoding": "chunked"}76 assert content == b"Hello, world!"Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955
line 77why, one step back77Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 78–79why, one step back78 with pytest.raises(RuntimeError):79 [part for part in stream]Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 80why, one step back80Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 81–82why, one step back81 with pytest.raises(StreamConsumed):82 [part async for part in stream]Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 83why, one step back83Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 84why, one step back84Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 85–87why, one step back85@pytest.mark.asyncio86async def test_json_content():87 stream = encode(json={"Hello": "world!"})Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 88–89why, one step back88 sync_content = b"".join([part for part in stream])89 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 90–95why, one step back9091 assert stream.can_replay()92 assert stream.get_headers() == {93 "Content-Length": "19",94 "Content-Type": "application/json",95 }Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 96–97why, one step back96 assert sync_content == b'{"Hello": "world!"}'97 assert async_content == b'{"Hello": "world!"}'Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 98–102why, one step back9899100@pytest.mark.asyncio101async def test_urlencoded_content():102 stream = encode(data={"Hello": "world!"})Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 103–104why, one step back103 sync_content = b"".join([part for part in stream])104 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 105–110why, one step back105106 assert stream.can_replay()107 assert stream.get_headers() == {108 "Content-Length": "14",109 "Content-Type": "application/x-www-form-urlencoded",110 }Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 111–112why, one step back111 assert sync_content == b"Hello=world%21"112 assert async_content == b"Hello=world%21"Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 113–118why, one step back113114115@pytest.mark.asyncio116async def test_multipart_files_content():117 files = {"file": io.BytesIO(b"<file content>")}118 stream = encode(files=files, boundary=b"+++")Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 119–120why, one step back119 sync_content = b"".join([part for part in stream])120 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 121–126why, one step back121122 assert stream.can_replay()123 assert stream.get_headers() == {124 "Content-Length": "138",125 "Content-Type": "multipart/form-data; boundary=+++",126 }Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 127why, one step back127 assert sync_content == b"".join(Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 128–136why, one step back128 [129 b"--+++\r\n",130 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',131 b"Content-Type: application/octet-stream\r\n",132 b"\r\n",133 b"<file content>\r\n",134 b"--+++--\r\n",135 ]136 )Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 137–146why, one step back137 assert async_content == b"".join(138 [139 b"--+++\r\n",140 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',141 b"Content-Type: application/octet-stream\r\n",142 b"\r\n",143 b"<file content>\r\n",144 b"--+++--\r\n",145 ]146 )Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 147–153why, one step back147148149@pytest.mark.asyncio150async def test_multipart_data_and_files_content():151 data = {"message": "Hello, world!"}152 files = {"file": io.BytesIO(b"<file content>")}153 stream = encode(data=data, files=files, boundary=b"+++")Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 154–155why, one step back154 sync_content = b"".join([part for part in stream])155 async_content = b"".join([part async for part in stream])Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 156–161why, one step back156157 assert stream.can_replay()158 assert stream.get_headers() == {159 "Content-Length": "210",160 "Content-Type": "multipart/form-data; boundary=+++",161 }Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 162why, one step back162 assert sync_content == b"".join(Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
lines 163–175why, one step back163 [164 b"--+++\r\n",165 b'Content-Disposition: form-data; name="message"\r\n',166 b"\r\n",167 b"Hello, world!\r\n",168 b"--+++\r\n",169 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',170 b"Content-Type: application/octet-stream\r\n",171 b"\r\n",172 b"<file content>\r\n",173 b"--+++--\r\n",174 ]175 )Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 176–191why, one step back176 assert async_content == b"".join(177 [178 b"--+++\r\n",179 b'Content-Disposition: form-data; name="message"\r\n',180 b"\r\n",181 b"Hello, world!\r\n",182 b"--+++\r\n",183 b'Content-Disposition: form-data; name="file"; filename="upload"\r\n',184 b"Content-Type: application/octet-stream\r\n",185 b"\r\n",186 b"<file content>\r\n",187 b"--+++--\r\n",188 ]189 )190191Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f
lines 192–198why, one step back192@pytest.mark.asyncio193async def test_empty_request():194 stream = encode(data={}, files={})195 sync_content = b"".join([part for part in stream])196 async_content = b"".join([part async for part in stream])197198 assert stream.can_replay()Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97
line 199why, one step back199 assert stream.get_headers() == {}Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f
lines 200–203why, one step back200 assert sync_content == b""201 assert async_content == b""202203Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 204–206why, one step back204def test_invalid_argument():205 with pytest.raises(TypeError):206 encode(123)