Skip to content

Commit a290d8d

Browse files
author
Signal Linden
authored
Merge pull request #5 from secondlife/signal/stream
Add basic streaming support
2 parents 19ab827 + 1b90db3 commit a290d8d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
extend-ignore = E203

llsd_asgi/middleware.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,9 @@ async def receive_with_llsd(self) -> Message:
107107
assert message["type"] == "http.request"
108108

109109
body = message["body"]
110-
more_body = message.get("more_body", False)
111-
if more_body:
112-
# Some implementations (e.g. HTTPX) may send one more empty-body message.
113-
# Make sure they don't send one that contains a body, or it means
114-
# that clients attempt to stream the request body.
110+
while message.get("more_body", False):
115111
message = await self.receive()
116-
if message["body"] != b"": # pragma: no cover
117-
raise NotImplementedError("Streaming the request body isn't supported yet")
112+
body += message["body"]
118113

119114
message["body"] = json.dumps(self.parse(body), cls=JSONEncoder).encode()
120115

tests/test_middleware.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
5151
)
5252

5353

54+
@pytest.mark.asyncio
55+
async def test_streaming_request() -> None:
56+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
57+
request = Request(scope, receive=receive)
58+
body = await request.json()
59+
text = f"message={body['message']!r}"
60+
61+
response = PlainTextResponse(text)
62+
await response(scope, receive, send)
63+
64+
app = LLSDMiddleware(app)
65+
66+
async def stream_bytes(content):
67+
b = llsd.format_xml(content)
68+
for chunk in range(0, len(b), 2):
69+
yield b[chunk : chunk + 2]
70+
71+
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
72+
r = await client.post(
73+
"/",
74+
content=stream_bytes({"message": "Hello, world!"}),
75+
headers={"content-type": "application/llsd+xml"},
76+
)
77+
assert r.status_code == 200
78+
assert r.text == "message='Hello, world!'"
79+
80+
5481
@pytest.mark.asyncio
5582
async def test_non_llsd_request() -> None:
5683
async def app(scope: Scope, receive: Receive, send: Send) -> None:

0 commit comments

Comments
 (0)