Skip to content

Commit

Permalink
Re-write request Content-Type when decoding (#24)
Browse files Browse the repository at this point in the history
* Re-write request Content-Type when decoding

* Update README
  • Loading branch information
florimondmanca committed Oct 26, 2021
1 parent 0adae0f commit 9172176
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ app = MessagePackMiddleware(..., packb=packb)

An ASGI application wrapped around `MessagePackMiddleware` will perform automatic content negotiation based on the client's capabilities. More precisely:

- If the client sends MessagePack-encoded data with the `application/x-msgpack` content type, `msgpack-asgi` will automatically re-encode it to JSON for your application to consume.
- If the client sends MessagePack-encoded data with the `application/x-msgpack` content type, `msgpack-asgi` will automatically re-encode the body to JSON and re-write the request `Content-Type` to `application/json` for your application to consume. (Note: this means applications will not be able to distinguish between MessagePack and JSON client requests.)
- If the client sent the `Accept: application/x-msgpack` header, `msgpack-asgi` will automatically re-encode any JSON response data to MessagePack for the client to consume.

(In other cases, `msgpack-asgi` won't intervene at all.)
Expand Down
10 changes: 9 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,14 @@ 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:
# We're going to present JSON content to the application,
# so rewrite `Content-Type` for consistency and compliance
# with possible downstream security checks in 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 9172176

Please sign in to comment.