Skip to content
Myr

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

1 file+164 added−131 removed

modified:tests/test_content.py+164 added-131 removed
2 unmodified lines
34import pytest56import httpx7from httpx._content import encode_request, encode_response78method = "POST"9url = "https://www.example.com"101112@pytest.mark.asyncio13async 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)1716 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])2019 assert headers == {}21 assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}22 assert sync_content == b""23 assert async_content == b""242526@pytest.mark.asyncio27async 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)3130 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])3433 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!"3839 # 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)4443 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])4746 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!"515253@pytest.mark.asyncio54async 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)5857 content = b"".join([part for part in stream])59 content = b"".join([part for part in request.stream])6059 assert headers == {"Content-Length": "13"}61 assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}62 assert content == b"Hello, world!"636465@pytest.mark.asyncio
10 unmodified lines
7677 async def __aiter__(self):78 yield self._content # pragma: no cover7978 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)8382 content = b"".join([part async for part in stream])84 content = b"".join([part async for part in request.stream])8584 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!"919293@pytest.mark.asyncio
1 unmodified line
95 def hello_world() -> typing.Iterator[bytes]:96 yield b"Hello, "97 yield b"world!"9894 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)10298 content = b"".join([part for part in stream])103 content = b"".join([part for part in request.stream])104100 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!"110111 with pytest.raises(httpx.StreamConsumed):104 [part for part in stream]112 [part for part in request.stream]113114 # 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)119112 content = b"".join([part for part in stream])120 content = b"".join([part for part in request.stream])121114 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!"127128129@pytest.mark.asyncio
1 unmodified line
131 async def hello_world() -> typing.AsyncIterator[bytes]:132 yield b"Hello, "133 yield b"world!"134124 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)138128 content = b"".join([part async for part in stream])139 content = b"".join([part async for part in request.stream])140130 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!"146147 with pytest.raises(httpx.StreamConsumed):134 [part async for part in stream]148 [part async for part in request.stream]149150 # 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)155142 content = b"".join([part async for part in stream])156 content = b"".join([part async for part in request.stream])157144 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!"163164165@pytest.mark.asyncio166async 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)170154 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])173157 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 line
181182183@pytest.mark.asyncio184async 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)188171 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])191174 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 line
199200201@pytest.mark.asyncio202async 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)206188 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])209191 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 line
217218219@pytest.mark.asyncio220async 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)224205 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])227208 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 line
235236237@pytest.mark.asyncio238async 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)242222 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])245225 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 lines
254255@pytest.mark.asyncio256async 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)267240 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])270243 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 lines
298@pytest.mark.asyncio299async 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)306277 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])309280 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 lines
343344345@pytest.mark.asyncio346async 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)350320 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])353323 assert headers == {}354 assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}355 assert sync_content == b""356 assert async_content == b""357358359def test_invalid_argument():360 with pytest.raises(TypeError):330 encode_request(123) # type: ignore361 httpx.Request(method, url, content=123) # type: ignore362363364@pytest.mark.asyncio365async 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)374343 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])377346 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 lines
413414415@pytest.mark.asyncio416async 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)420388 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])423391 assert headers == {}424 assert response.headers == {}425 assert sync_content == b""426 assert async_content == b""427428429@pytest.mark.asyncio430async 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)434402 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])437405 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!"441442
2 unmodified lines
445 def hello_world() -> typing.Iterator[bytes]:446 yield b"Hello, "447 yield b"world!"448416 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)452420 content = b"".join([part for part in stream])453 content = b"".join([part for part in response.stream])454422 assert headers == {"Transfer-Encoding": "chunked"}455 assert response.headers == {"Transfer-Encoding": "chunked"}456 assert content == b"Hello, world!"457458 with pytest.raises(httpx.StreamConsumed):426 [part for part in stream]459 [part for part in response.stream]460461462@pytest.mark.asyncio463async def test_response_aiterator_content():464 async def hello_world() -> typing.AsyncIterator[bytes]:465 yield b"Hello, "466 yield b"world!"467435 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)471439 content = b"".join([part async for part in stream])472 content = b"".join([part async for part in response.stream])473441 assert headers == {"Transfer-Encoding": "chunked"}474 assert response.headers == {"Transfer-Encoding": "chunked"}475 assert content == b"Hello, world!"476477 with pytest.raises(httpx.StreamConsumed):445 [part async for part in stream]478 [part async for part in response.stream]479480481def test_response_invalid_argument():482 with pytest.raises(TypeError):450 encode_response(123) # type: ignore483 httpx.Response(200, content=123) # type: ignore