except asyncio.TimeoutError:
raise ConnectTimeout()
- async def send(self, request: Request, stream: bool=False) -> Response:
+ async def send(self, request: Request, stream: bool = False) -> Response:
method = request.method.encode()
target = request.url.target
host_header = (b"host", request.url.netloc.encode("ascii"))
headers = event.headers
if stream:
- return Response(status_code=status_code, headers=headers, body=self.body_iter())
+ body_iter = self.body_iter()
+ return Response(status_code=status_code, headers=headers, body=body_iter)
# Get the response body.
body = b""
return Response(status_code=status_code, headers=headers, body=body)
- async def body_iter(self) -> typing.Iterable[bytes]:
+ async def body_iter(self) -> typing.AsyncIterator[bytes]:
event = await self._receive_event()
while isinstance(event, h11.Data):
yield event.data
import asyncio
-import json
import pytest
from uvicorn.config import Config
async def app(scope, receive, send):
- assert scope['type'] == 'http'
- await send({
- 'type': 'http.response.start',
- 'status': 200,
- 'headers': [
- [b'content-type', b'text/plain'],
- ]
- })
- await send({
- 'type': 'http.response.body',
- 'body': b'Hello, world!',
- })
+ assert scope["type"] == "http"
+ await send(
+ {
+ "type": "http.response.start",
+ "status": 200,
+ "headers": [[b"content-type", b"text/plain"]],
+ }
+ )
+ await send({"type": "http.response.body", "body": b"Hello, world!"})
@pytest.fixture
import pytest
+
import httpcore
@pytest.mark.asyncio
async def test_get(server):
async with httpcore.ConnectionPool() as http:
- response = await http.request('GET', "http://127.0.0.1:8000/")
+ response = await http.request("GET", "http://127.0.0.1:8000/")
assert response.status_code == 200
- assert response.body == b'Hello, world!'
+ assert response.body == b"Hello, world!"
@pytest.mark.asyncio
async def test_post(server):
async with httpcore.ConnectionPool() as http:
- response = await http.request('POST', "http://127.0.0.1:8000/", body=b"Hello, world!")
+ response = await http.request(
+ "POST", "http://127.0.0.1:8000/", body=b"Hello, world!"
+ )
assert response.status_code == 200
@pytest.mark.asyncio
async def test_stream_response(server):
async with httpcore.ConnectionPool() as http:
- response = await http.request('GET', "http://127.0.0.1:8000/", stream=True)
+ response = await http.request("GET", "http://127.0.0.1:8000/", stream=True)
assert response.status_code == 200
- assert not hasattr(response, 'body')
+ assert not hasattr(response, "body")
body = await response.read()
- assert body == b'Hello, world!'
+ assert body == b"Hello, world!"
@pytest.mark.asyncio
yield b"world!"
async with httpcore.ConnectionPool() as http:
- response = await http.request('POST', "http://127.0.0.1:8000/", body=hello_world())
+ response = await http.request(
+ "POST", "http://127.0.0.1:8000/", body=hello_world()
+ )
assert response.status_code == 200