Skip to content

Commit

Permalink
Decode as LLSD when in quirks mode too
Browse files Browse the repository at this point in the history
In addition to assuming that outgoing content should be presented as LLSD,
quirks mode should also assume that incoming content is LLSD unless
told otherwise.
  • Loading branch information
bennettgoble committed Feb 19, 2024
1 parent 3775ee1 commit b8310d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion llsd_asgi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
headers = MutableHeaders(scope=scope)

try:
self.parse = _CONTENT_TYPE_TO_PARSE[headers.get("content-type")]
self.parse = _CONTENT_TYPE_TO_PARSE[
headers.get("content-type", "application/llsd+xml" if self.quirks else None)
]
self.should_decode_from_llsd_to_json = True
except KeyError:
self.should_decode_from_llsd_to_json = False
Expand Down
21 changes: 20 additions & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async def lifespan_only_app(scope: Scope, receive: Receive, send: Send) -> None:
"accept",
[(None), ("*/*"), ("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")],
)
async def test_quirks(accept: str) -> None:
async def test_quirks_encode(accept: str) -> None:
app = LLSDMiddleware(JSONResponse({"message": "Hello, world!"}), quirks=True)

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


@pytest.mark.asyncio
async def test_quirks_decode():
async def app(scope: Scope, receive: Receive, send: Send) -> None:
request = Request(scope, receive=receive)
data = await request.json()
message = data["message"]
text = f"message={message!r}"

response = PlainTextResponse(text)
await response(scope, receive, send)

app = LLSDMiddleware(app, quirks=True)

async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
r = await client.post("/", content=llsd.format_xml({"message": "Hello, world!"}))
assert r.status_code == 200
assert r.text == "message='Hello, world!'"


@pytest.mark.asyncio
@pytest.mark.parametrize(
"accept",
Expand Down

0 comments on commit b8310d4

Please sign in to comment.