self.headers["location"] = quote(str(url), safe=":/%#?=@[]!$&'()*+,;")
-Content = typing.Union[str, bytes]
+Content = typing.Union[str, bytes, memoryview]
SyncContentStream = typing.Iterable[Content]
AsyncContentStream = typing.AsyncIterable[Content]
ContentStream = typing.Union[AsyncContentStream, SyncContentStream]
}
)
async for chunk in self.body_iterator:
- if not isinstance(chunk, bytes):
+ if not isinstance(chunk, (bytes, memoryview)):
chunk = chunk.encode(self.charset)
await send({"type": "http.response.body", "body": chunk, "more_body": True})
assert response.headers["content-length"] == "10"
+def test_streaming_response_memoryview(test_client_factory: TestClientFactory) -> None:
+ app = StreamingResponse(content=iter([memoryview(b"hello"), memoryview(b"world")]))
+ client: TestClient = test_client_factory(app)
+ response = client.get("/")
+ assert response.text == "helloworld"
+
+
@pytest.mark.anyio
async def test_streaming_response_stops_if_receiving_http_disconnect() -> None:
streamed = 0