Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-write request Content-Type when decoding #24

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ That's all there is to it! You can now go reduce the size of your payloads.

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