Skip to content

Commit

Permalink
Re-write request Content-Type when decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Oct 25, 2021
1 parent 5f5c8c4 commit b67c653
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/msgpack_asgi/_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
self.started = False

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
headers = Headers(scope=scope)
headers = MutableHeaders(scope=scope)
self.should_decode_from_msgpack_to_json = (
"application/x-msgpack" in headers.get("content-type", "")
)
Expand All @@ -61,6 +61,13 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
)
self.receive = receive
self.send = send

if self.should_decode_from_msgpack_to_json:
# Rewrite Content-Type to match the re-encoded JSON content,
# to comply with CSRF constraints from some frameworks.
# See: https://github.com/florimondmanca/msgpack-asgi/issues/23
headers["content-type"] = "application/json"

await self.app(scope, self.receive_with_msgpack, self.send_with_msgpack)

async def receive_with_msgpack(self) -> Message:
Expand Down
13 changes: 8 additions & 5 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
"/", content=body, headers={"content-type": "application/x-msgpack"}
)
assert r.status_code == 200
assert r.text == "content_type='application/x-msgpack' message='Hello, world!'"
assert r.text == "content_type='application/json' message='Hello, world!'"


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

response = PlainTextResponse(text)
Expand All @@ -48,9 +47,13 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
app = MessagePackMiddleware(app)

async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
r = await client.post("/", json={"message": "Hello, world!"})
r = await client.post(
"/",
content="Hello, world!",
headers={"content-type": "text/plain"},
)
assert r.status_code == 200
assert r.text == "content_type='application/json' message='Hello, world!'"
assert r.text == "content_type='text/plain' message='Hello, world!'"


@pytest.mark.asyncio
Expand Down

0 comments on commit b67c653

Please sign in to comment.