Skip to content

Commit 4a01f3a

Browse files
authored
Merge pull request #3 from secondlife/signal/quirk-decode
Decode as LLSD when in quirks mode too
2 parents 3775ee1 + b8310d4 commit 4a01f3a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

llsd_asgi/middleware.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
4747
headers = MutableHeaders(scope=scope)
4848

4949
try:
50-
self.parse = _CONTENT_TYPE_TO_PARSE[headers.get("content-type")]
50+
self.parse = _CONTENT_TYPE_TO_PARSE[
51+
headers.get("content-type", "application/llsd+xml" if self.quirks else None)
52+
]
5153
self.should_decode_from_llsd_to_json = True
5254
except KeyError:
5355
self.should_decode_from_llsd_to_json = False

tests/test_middleware.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async def lifespan_only_app(scope: Scope, receive: Receive, send: Send) -> None:
150150
"accept",
151151
[(None), ("*/*"), ("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")],
152152
)
153-
async def test_quirks(accept: str) -> None:
153+
async def test_quirks_encode(accept: str) -> None:
154154
app = LLSDMiddleware(JSONResponse({"message": "Hello, world!"}), quirks=True)
155155

156156
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
@@ -166,6 +166,25 @@ async def test_quirks(accept: str) -> None:
166166
assert llsd.parse_xml(r.content) == {"message": "Hello, world!"}
167167

168168

169+
@pytest.mark.asyncio
170+
async def test_quirks_decode():
171+
async def app(scope: Scope, receive: Receive, send: Send) -> None:
172+
request = Request(scope, receive=receive)
173+
data = await request.json()
174+
message = data["message"]
175+
text = f"message={message!r}"
176+
177+
response = PlainTextResponse(text)
178+
await response(scope, receive, send)
179+
180+
app = LLSDMiddleware(app, quirks=True)
181+
182+
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
183+
r = await client.post("/", content=llsd.format_xml({"message": "Hello, world!"}))
184+
assert r.status_code == 200
185+
assert r.text == "message='Hello, world!'"
186+
187+
169188
@pytest.mark.asyncio
170189
@pytest.mark.parametrize(
171190
"accept",

0 commit comments

Comments
 (0)