Skip to content
Myr

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 file

Take it

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 1–4why, one step back
1import io
23import pytest4

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 5–6why, one step back
5from httpx import StreamConsumed
6from httpx._content_streams import ContentStream, encode

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 7–9why, one step back
7
89@pytest.mark.asyncio

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 10–21why, one step back
10async 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.asyncio

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 22–23why, one step back
22async def test_empty_content():
23 stream = encode()

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 24–25why, one step back
24 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 back
26
27 assert stream.can_replay()

Revert "Add content-length header for empty bytestream (#866)" (#898)Tom Christiebefore adoption631ba97

line 28why, one step back
28 assert stream.get_headers() == {}

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 29–30why, one step back
29 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 back
31
3233@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 back
36 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 back
38
39 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 back
41 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 back
43
4445@pytest.mark.asyncio

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 46–47why, one step back
46async 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 back
48 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 back
52 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 back
53
54 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 back
57

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 58–59why, one step back
58 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 back
60

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 61–64why, one step back
61 with pytest.raises(StreamConsumed):
62 [part for part in stream]6364

Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955

line 65why, one step back
65@pytest.mark.asyncio

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 66why, one step back
66async def test_aiterator_content():

Adds check to enforce single consumption of AsyncIteratorStream. (#697)Gabriel Straussbefore adoptionde8b955

lines 67–71why, one step back
67 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 back
72 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 back
73

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 74–76why, one step back
74 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 back
77

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 78–79why, one step back
78 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 back
80

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 81–82why, one step back
81 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 back
83

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

line 84why, one step back
84

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 85–87why, one step back
85@pytest.mark.asyncio
86async def test_json_content():87 stream = encode(json={"Hello": "world!"})

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 88–89why, one step back
88 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 back
90
91 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 back
96 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 back
98
99100@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 back
103 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 back
105
106 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 back
111 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 back
113
114115@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 back
119 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 back
121
122 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 back
127 assert sync_content == b"".join(

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 128–136why, one step back
128 [
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 back
137 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 back
147
148149@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 back
154 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 back
156
157 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 back
162 assert sync_content == b"".join(

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

lines 163–175why, one step back
163 [
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 back
176 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 )190191

Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f

lines 192–198why, one step back
192@pytest.mark.asyncio
193async 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 back
199 assert stream.get_headers() == {}

Fix multipart edge cases with `data={}` and/or `files={}` (#861)Dima Bogerbefore adoption430285f

lines 200–203why, one step back
200 assert sync_content == b""
201 assert async_content == b""202203

Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604

lines 204–206why, one step back
204def test_invalid_argument():
205 with pytest.raises(TypeError):206 encode(123)