├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── ci └── azure-pipelines.yml ├── img └── msgpack-asgi.png ├── requirements.txt ├── scripts ├── build ├── check ├── install ├── lint ├── publish └── test ├── setup.cfg ├── setup.py ├── src └── msgpack_asgi │ ├── __init__.py │ ├── __version__.py │ ├── _middleware.py │ └── py.typed └── tests ├── __init__.py ├── test_middleware.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Tooling. 2 | .coverage 3 | .nox/ 4 | venv*/ 5 | .python-version 6 | 7 | # Caches. 8 | __pycache__/ 9 | *.pyc 10 | .mypy_cache/ 11 | .pytest_cache/ 12 | 13 | # Packaging. 14 | build/ 15 | dist/ 16 | *.egg-info/ 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). 6 | 7 | ## 1.1.0 - 2021-10-26 8 | 9 | ### Added 10 | 11 | - Support custom encoding/decoding implementation via the `packb=...` and `unpackb=...` optional parameters, allowing the use of alternative msgpack libraries. (Pull #20) 12 | 13 | ### Fixed 14 | 15 | - Properly re-write request `Content-Type` to `application/json`. (Pull #24) 16 | 17 | ## 1.0.0 - 2020-03-26 18 | 19 | _First production/stable release._ 20 | 21 | ### Changed 22 | 23 | - Switch to private module naming. Components should now be imported from the root package, e.g. `from msgpack_asgi import MessagePackMiddleware`. (Pull #5) 24 | 25 | ### Fixed 26 | 27 | - Add missing `MANIFEST.in`. (Pull #4) 28 | 29 | ## 0.1.0 - 2019-11-04 30 | 31 | Initial release. 32 | 33 | ### Added 34 | 35 | - Add the `MessagePackMiddleware` ASGI middleware. 36 | - Add the `MessagePackResponse` ASGI response class. 37 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guide 2 | 3 | Thank you for your interest in contributing to this project! 4 | 5 | Here are a few tips for getting started. 6 | 7 | ## Quickstart 8 | 9 | The project workflow is managed using shell scripts stored in `scripts/`. 10 | 11 | First, install dependencies: 12 | 13 | ``` 14 | scripts/install 15 | ``` 16 | 17 | To run the test suite, use: 18 | 19 | ``` 20 | scripts/test 21 | ``` 22 | 23 | To run code formatting: 24 | 25 | ``` 26 | scripts/lint 27 | ``` 28 | 29 | To run code checks alone: 30 | 31 | ``` 32 | scripts/check 33 | ``` 34 | 35 | ## Releasing 36 | 37 | _Notes to maintainers._ 38 | 39 | - Create a release PR with the following: 40 | - Bump the version in `__version__.py`. 41 | - Update `CHANGELOG.md` with PRs since the last release. PRs that do not alter behavior (such as docs updates, refactors, tooling updates, etc) should not be included. 42 | - Once the release PR is reviewed and merged, create a new release on the GitHub UI, including: 43 | - Tag version, like `2.1.0`. 44 | - Release title, `Version 2.1.0`. 45 | - Description copied from the changelog. 46 | - Once created, the release tag will trigger a 'deploy' job on CI, automatically pushing the new version to PyPI. 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Florimond Manca 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | graft src 2 | include README.md 3 | include CHANGELOG.md 4 | include LICENSE 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # msgpack-asgi 2 | 3 | [![Build Status](https://dev.azure.com/florimondmanca/public/_apis/build/status/florimondmanca.msgpack-asgi?branchName=master)](https://dev.azure.com/florimondmanca/public/_build/latest?definitionId=5&branchName=master) 4 | [![Coverage](https://codecov.io/gh/florimondmanca/msgpack-asgi/branch/master/graph/badge.svg)](https://codecov.io/gh/florimondmanca/msgpack-asgi) 5 | [![Package version](https://badge.fury.io/py/msgpack-asgi.svg)](https://pypi.org/project/msgpack-asgi) 6 | 7 | `msgpack-asgi` allows you to add automatic [MessagePack](https://msgpack.org/) content negotiation to ASGI applications (Starlette, FastAPI, Quart, etc.), with a single line of code: 8 | 9 | ```python 10 | app.add_middleware(MessagePackMiddleware) 11 | ``` 12 | 13 | _(You may want to adapt this snippet to your framework-specific middleware API.)_ 14 | 15 | This gives you the bandwitdth usage reduction benefits of MessagePack without having to change existing code. 16 | 17 | **Note**: this comes at a CPU usage cost, since `MessagePackMiddleware` will perform MsgPack decoding while your application continues to decode and encode JSON data (see also [How it works](#how-it-works)). If your use case is CPU-sensitive, rather than strictly focused on reducing network bandwidth, this package may not be for you. 18 | 19 | ## Installation 20 | 21 | Install with pip: 22 | 23 | ```bash 24 | pip install "msgpack-asgi==1.*" 25 | ``` 26 | 27 | ## Quickstart 28 | 29 | First, you'll need an ASGI application. Let's use this sample application, which exposes an endpoint that returns JSON data: 30 | 31 | ```python 32 | # For convenience, we use some ASGI components from Starlette. 33 | # Install with: `$ pip install starlette`. 34 | from starlette.requests import Request 35 | from starlette.responses import JSONResponse 36 | 37 | 38 | async def get_response(request): 39 | if request.method == "POST": 40 | data = await request.json() 41 | return JSONResponse({"data": data}, status_code=201) 42 | else: 43 | return JSONResponse({"message": "Hello, msgpack!"}) 44 | 45 | 46 | async def app(scope, receive, send): 47 | assert scope["type"] == "http" 48 | request = Request(scope=scope, receive=receive) 49 | response = await get_response(request) 50 | await response(scope, receive, send) 51 | ``` 52 | 53 | Then, wrap your application around `MessagePackMiddleware`: 54 | 55 | ```python 56 | from msgpack_asgi import MessagePackMiddleware 57 | 58 | app = MessagePackMiddleware(app) 59 | ``` 60 | 61 | Serve your application using an ASGI server, for example with [Uvicorn](https://www.uvicorn.org): 62 | 63 | ```bash 64 | uvicorn app:app 65 | ``` 66 | 67 | Now, let's make a request that accepts MessagePack data in response: 68 | 69 | ```bash 70 | curl -i http://localhost:8000 -H "Accept: application/x-msgpack" 71 | ``` 72 | 73 | You should get the following output: 74 | 75 | ```http 76 | HTTP/1.1 200 OK 77 | date: Fri, 01 Nov 2019 17:40:14 GMT 78 | server: uvicorn 79 | content-length: 25 80 | content-type: application/x-msgpack 81 | 82 | ��message�Hello, msgpack! 83 | ``` 84 | 85 | What happened? Since we told the application that we accepted MessagePack-encoded responses, `msgpack-asgi` automatically converted the JSON data returned by the Starlette application to MessagePack. 86 | 87 | We can make sure the response contains valid MessagePack data by making the request again in Python, and decoding the response content: 88 | 89 | ```python 90 | >>> import requests 91 | >>> import msgpack 92 | >>> url = "http://localhost:8000" 93 | >>> headers = {"accept": "application/x-msgpack"} 94 | >>> r = requests.get(url, headers=headers) 95 | >>> r.content 96 | b'\x81\xa7message\xafHello, msgpack!' 97 | >>> msgpack.unpackb(r.content, raw=False) 98 | {'message': 'Hello, msgpack!'} 99 | ``` 100 | 101 | `msgpack-asgi` also works in reverse: it will automatically decode MessagePack-encoded data sent by the client to JSON. We can try this out by making a `POST` request to our sample application with a MessagePack-encoded body: 102 | 103 | ```python 104 | >>> import requests 105 | >>> import msgpack 106 | >>> url = "http://localhost:8000" 107 | >>> data = msgpack.packb({"message": "Hi, there!"}) 108 | >>> headers = {"content-type": "application/x-msgpack"} 109 | >>> r = requests.post(url, data=data, headers=headers) 110 | >>> r.json() 111 | {'data': {'message': 'Hi, there!'}} 112 | ``` 113 | 114 | That's all there is to it! You can now go reduce the size of your payloads. 115 | 116 | ## Advanced usage 117 | 118 | ### Custom implementations 119 | 120 | `msgpack-asgi` supports customizing the default encoding/decoding implementation. This is useful for fine-tuning application performance via an alternative msgpack implementation for encoding, decoding, or both. 121 | 122 | To do so, use the following arguments: 123 | 124 | * `packb` - _(Optional, type: `(obj: Any) -> bytes`, default: `msgpack.packb`)_ - Used to encode outgoing data. 125 | * `unpackb` - _(Optional, type: `(data: bytes) -> Any`, default: `msgpack.unpackb`)_ - Used to decode incoming data. 126 | 127 | For example, to use the [`ormsgpack`](https://pypi.org/project/ormsgpack/) library for encoding: 128 | 129 | ```python 130 | import ormsgpack # Installed separately. 131 | from msgpack_asgi import MessagePackMiddleware 132 | 133 | def packb(obj): 134 | option = ... # See `ormsgpack` options. 135 | return ormsgpack.packb(obj, option=option) 136 | 137 | app = MessagePackMiddleware(..., packb=packb) 138 | ``` 139 | 140 | ## Limitations 141 | 142 | `msgpack-asgi` does not support request or response streaming. This is because the full request and response body content has to be loaded in memory before it can be re-encoded. 143 | 144 | ## How it works 145 | 146 | ![](https://github.com/florimondmanca/msgpack-asgi/blob/master/img/msgpack-asgi.png) 147 | 148 | An ASGI application wrapped around `MessagePackMiddleware` will perform automatic content negotiation based on the client's capabilities. More precisely: 149 | 150 | - 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.) 151 | - 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. 152 | 153 | (In other cases, `msgpack-asgi` won't intervene at all.) 154 | 155 | ## License 156 | 157 | MIT 158 | -------------------------------------------------------------------------------- /ci/azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | resources: 2 | repositories: 3 | - repository: templates 4 | type: github 5 | endpoint: github 6 | name: florimondmanca/azure-pipelines-templates 7 | ref: refs/tags/3.4 8 | 9 | trigger: 10 | - master 11 | - refs/tags/* 12 | 13 | pr: 14 | - master 15 | 16 | variables: 17 | - name: CI 18 | value: "true" 19 | - name: PIP_CACHE_DIR 20 | value: $(Pipeline.Workspace)/.cache/pip 21 | - group: pypi-credentials 22 | 23 | stages: 24 | - stage: test 25 | jobs: 26 | - template: job--python-check.yml@templates 27 | parameters: 28 | pythonVersion: "3.8" 29 | 30 | - template: job--python-test.yml@templates 31 | parameters: 32 | jobs: 33 | py36: null 34 | py37: null 35 | py38: 36 | coverage: true 37 | 38 | - stage: deploy 39 | condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/') 40 | jobs: 41 | - template: job--python-publish.yml@templates 42 | parameters: 43 | token: $(pypiToken) 44 | -------------------------------------------------------------------------------- /img/msgpack-asgi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florimondmanca/msgpack-asgi/6261b1e22b3689551f68038ee00adda5fbc04670/img/msgpack-asgi.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -e . 2 | 3 | # Packaging. 4 | twine 5 | wheel 6 | 7 | # Tooling. 8 | autoflake 9 | black==21.9b0 10 | flake8 11 | flake8-bugbear 12 | flake8-comprehensions 13 | isort==5.* 14 | httpx==0.19.* 15 | mypy 16 | pytest 17 | pytest-asyncio 18 | pytest-cov 19 | seed-isort-config 20 | -------------------------------------------------------------------------------- /scripts/build: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ -d "venv" ] ; then 5 | BIN="venv/bin/" 6 | fi 7 | 8 | set -x 9 | 10 | ${BIN}python setup.py sdist bdist_wheel 11 | ${BIN}twine check dist/* 12 | rm -r build 13 | -------------------------------------------------------------------------------- /scripts/check: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | export BIN="" 4 | if [ -d "venv" ] ; then 5 | export BIN="venv/bin/" 6 | fi 7 | export SOURCE_FILES="src/msgpack_asgi tests" 8 | 9 | set -x 10 | 11 | ${BIN}black --check --diff --target-version=py36 $SOURCE_FILES 12 | ${BIN}flake8 $SOURCE_FILES 13 | ${BIN}mypy $SOURCE_FILES 14 | ${BIN}isort --check --diff $SOURCE_FILES 15 | -------------------------------------------------------------------------------- /scripts/install: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | export BIN="venv/bin/" 4 | 5 | set -x 6 | 7 | python -m venv venv 8 | ${BIN}python -m pip install -U pip 9 | ${BIN}python -m pip install -r requirements.txt 10 | 11 | set +x 12 | 13 | echo 14 | echo "Success! You can now activate your virtual environment using:" 15 | echo "source ${BIN}activate" 16 | -------------------------------------------------------------------------------- /scripts/lint: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | export BIN="" 4 | if [ -d "venv" ] ; then 5 | export BIN="venv/bin/" 6 | fi 7 | export SOURCE_FILES="src/msgpack_asgi tests" 8 | 9 | set -x 10 | 11 | ${BIN}autoflake --in-place --recursive $SOURCE_FILES 12 | ${BIN}seed-isort-config --application-directories=msgpack_asgi 13 | ${BIN}isort $SOURCE_FILES 14 | ${BIN}black --target-version=py36 $SOURCE_FILES 15 | -------------------------------------------------------------------------------- /scripts/publish: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | BIN="" 4 | if [ -d "venv" ] ; then 5 | BIN="venv/bin/" 6 | fi 7 | 8 | set -x 9 | 10 | ${BIN}twine upload dist/* 11 | -------------------------------------------------------------------------------- /scripts/test: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | export BIN="" 4 | if [ -d 'venv' ] ; then 5 | export BIN="venv/bin/" 6 | fi 7 | 8 | if [ -z $CI ]; then 9 | scripts/check 10 | fi 11 | 12 | set -x 13 | ${BIN}pytest $@ 14 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = W503, E203, B305 3 | max-line-length = 88 4 | 5 | [mypy] 6 | disallow_untyped_defs = True 7 | ignore_missing_imports = True 8 | 9 | [tool:isort] 10 | profile = black 11 | known_first_party = msgpack_asgi,tests 12 | known_third_party = httpx,msgpack,pytest,setuptools,starlette 13 | 14 | [tool:pytest] 15 | addopts = 16 | -rxXs 17 | --cov=msgpack_asgi 18 | --cov=tests 19 | --cov-report=term-missing 20 | --cov-fail-under=100 21 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import re 2 | from pathlib import Path 3 | 4 | from setuptools import find_packages, setup 5 | 6 | 7 | def get_version(package: str) -> str: 8 | version = (Path("src") / package / "__version__.py").read_text() 9 | match = re.search("__version__ = ['\"]([^'\"]+)['\"]", version) 10 | assert match is not None 11 | return match.group(1) 12 | 13 | 14 | def get_long_description() -> str: 15 | with open("README.md", encoding="utf8") as readme: 16 | with open("CHANGELOG.md", encoding="utf8") as changelog: 17 | return readme.read() + "\n\n" + changelog.read() 18 | 19 | 20 | setup( 21 | name="msgpack-asgi", 22 | version=get_version("msgpack_asgi"), 23 | description="Drop-in MessagePack support for ASGI applications and frameworks", 24 | long_description=get_long_description(), 25 | long_description_content_type="text/markdown", 26 | url="http://github.com/florimondmanca/msgpack-asgi", 27 | author="Florimond Manca", 28 | author_email="florimond.manca@gmail.com", 29 | packages=find_packages("src"), 30 | package_dir={"": "src"}, 31 | include_package_data=True, 32 | zip_safe=False, 33 | install_requires=["msgpack", "starlette==0.*"], 34 | python_requires=">=3.6", 35 | license="MIT", 36 | classifiers=[ 37 | "Development Status :: 5 - Production/Stable", 38 | "Intended Audience :: Developers", 39 | "Operating System :: OS Independent", 40 | "Programming Language :: Python :: 3", 41 | "Programming Language :: Python :: 3.6", 42 | "Programming Language :: Python :: 3.7", 43 | "Programming Language :: Python :: 3.8", 44 | ], 45 | ) 46 | -------------------------------------------------------------------------------- /src/msgpack_asgi/__init__.py: -------------------------------------------------------------------------------- 1 | from .__version__ import __version__ 2 | from ._middleware import MessagePackMiddleware 3 | 4 | __all__ = ["__version__", "MessagePackMiddleware"] 5 | -------------------------------------------------------------------------------- /src/msgpack_asgi/__version__.py: -------------------------------------------------------------------------------- 1 | __version__ = "1.1.0" 2 | -------------------------------------------------------------------------------- /src/msgpack_asgi/_middleware.py: -------------------------------------------------------------------------------- 1 | import json 2 | from functools import partial 3 | from typing import Any, Callable 4 | 5 | import msgpack 6 | from starlette.datastructures import Headers, MutableHeaders 7 | from starlette.types import ASGIApp, Message, Receive, Scope, Send 8 | 9 | _msgpack_unpackb = partial(msgpack.unpackb, raw=False) 10 | 11 | 12 | class MessagePackMiddleware: 13 | def __init__( 14 | self, 15 | app: ASGIApp, 16 | *, 17 | packb: Callable[[Any], bytes] = msgpack.packb, 18 | unpackb: Callable[[bytes], Any] = _msgpack_unpackb, 19 | ) -> None: 20 | self.app = app 21 | self.packb = packb 22 | self.unpackb = unpackb 23 | 24 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: 25 | if scope["type"] == "http": 26 | responder = _MessagePackResponder( 27 | self.app, packb=self.packb, unpackb=self.unpackb 28 | ) 29 | await responder(scope, receive, send) 30 | return 31 | await self.app(scope, receive, send) 32 | 33 | 34 | class _MessagePackResponder: 35 | def __init__( 36 | self, 37 | app: ASGIApp, 38 | *, 39 | packb: Callable[[Any], bytes], 40 | unpackb: Callable[[bytes], Any], 41 | ) -> None: 42 | self.app = app 43 | self.packb = packb 44 | self.unpackb = unpackb 45 | self.should_decode_from_msgpack_to_json = False 46 | self.should_encode_from_json_to_msgpack = False 47 | self.receive: Receive = unattached_receive 48 | self.send: Send = unattached_send 49 | self.initial_message: Message = {} 50 | self.started = False 51 | 52 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: 53 | headers = MutableHeaders(scope=scope) 54 | self.should_decode_from_msgpack_to_json = ( 55 | "application/x-msgpack" in headers.get("content-type", "") 56 | ) 57 | # Take an initial guess, although we eventually may not 58 | # be able to do the conversion. 59 | self.should_encode_from_json_to_msgpack = ( 60 | "application/x-msgpack" in headers.getlist("accept") 61 | ) 62 | self.receive = receive 63 | self.send = send 64 | 65 | if self.should_decode_from_msgpack_to_json: 66 | # We're going to present JSON content to the application, 67 | # so rewrite `Content-Type` for consistency and compliance 68 | # with possible downstream security checks in some frameworks. 69 | # See: https://github.com/florimondmanca/msgpack-asgi/issues/23 70 | headers["content-type"] = "application/json" 71 | 72 | await self.app(scope, self.receive_with_msgpack, self.send_with_msgpack) 73 | 74 | async def receive_with_msgpack(self) -> Message: 75 | message = await self.receive() 76 | 77 | if not self.should_decode_from_msgpack_to_json: 78 | return message 79 | 80 | assert message["type"] == "http.request" 81 | 82 | body = message["body"] 83 | more_body = message.get("more_body", False) 84 | if more_body: 85 | # Some implementations (e.g. HTTPX) may send one more empty-body message. 86 | # Make sure they don't send one that contains a body, or it means 87 | # that clients attempt to stream the request body. 88 | message = await self.receive() 89 | if message["body"] != b"": # pragma: no cover 90 | raise NotImplementedError( 91 | "Streaming the request body isn't supported yet" 92 | ) 93 | 94 | obj = self.unpackb(body) 95 | message["body"] = json.dumps(obj).encode() 96 | 97 | return message 98 | 99 | async def send_with_msgpack(self, message: Message) -> None: 100 | if not self.should_encode_from_json_to_msgpack: 101 | await self.send(message) 102 | return 103 | 104 | if message["type"] == "http.response.start": 105 | headers = Headers(raw=message["headers"]) 106 | if headers["content-type"] != "application/json": 107 | # Client accepts msgpack, but the app did not send JSON data. 108 | # (Note that it may have sent msgpack-encoded data.) 109 | self.should_encode_from_json_to_msgpack = False 110 | await self.send(message) 111 | return 112 | 113 | # Don't send the initial message until we've determined how to 114 | # modify the ougoging headers correctly. 115 | self.initial_message = message 116 | 117 | elif message["type"] == "http.response.body": 118 | assert self.should_encode_from_json_to_msgpack 119 | 120 | body = message.get("body", b"") 121 | more_body = message.get("more_body", False) 122 | if more_body: # pragma: no cover 123 | raise NotImplementedError( 124 | "Streaming the response body isn't supported yet" 125 | ) 126 | 127 | body = self.packb(json.loads(body)) 128 | 129 | headers = MutableHeaders(raw=self.initial_message["headers"]) 130 | headers["Content-Type"] = "application/x-msgpack" 131 | headers["Content-Length"] = str(len(body)) 132 | message["body"] = body 133 | 134 | await self.send(self.initial_message) 135 | await self.send(message) 136 | 137 | 138 | async def unattached_receive() -> Message: 139 | raise RuntimeError("receive awaitable not set") # pragma: no cover 140 | 141 | 142 | async def unattached_send(message: Message) -> None: 143 | raise RuntimeError("send awaitable not set") # pragma: no cover 144 | -------------------------------------------------------------------------------- /src/msgpack_asgi/py.typed: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florimondmanca/msgpack-asgi/6261b1e22b3689551f68038ee00adda5fbc04670/src/msgpack_asgi/py.typed -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/florimondmanca/msgpack-asgi/6261b1e22b3689551f68038ee00adda5fbc04670/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_middleware.py: -------------------------------------------------------------------------------- 1 | import httpx 2 | import msgpack 3 | import pytest 4 | from starlette.requests import Request 5 | from starlette.responses import JSONResponse, PlainTextResponse, Response 6 | from starlette.types import Receive, Scope, Send 7 | 8 | from msgpack_asgi import MessagePackMiddleware 9 | from tests.utils import mock_receive, mock_send 10 | 11 | 12 | @pytest.mark.asyncio 13 | async def test_msgpack_request() -> None: 14 | async def app(scope: Scope, receive: Receive, send: Send) -> None: 15 | request = Request(scope, receive=receive) 16 | content_type = request.headers["content-type"] 17 | data = await request.json() 18 | message = data["message"] 19 | text = f"content_type={content_type!r} message={message!r}" 20 | 21 | response = PlainTextResponse(text) 22 | await response(scope, receive, send) 23 | 24 | app = MessagePackMiddleware(app) 25 | 26 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 27 | content = {"message": "Hello, world!"} 28 | body = msgpack.packb(content) 29 | r = await client.post( 30 | "/", content=body, headers={"content-type": "application/x-msgpack"} 31 | ) 32 | assert r.status_code == 200 33 | assert r.text == "content_type='application/json' message='Hello, world!'" 34 | 35 | 36 | @pytest.mark.asyncio 37 | async def test_non_msgpack_request() -> None: 38 | async def app(scope: Scope, receive: Receive, send: Send) -> None: 39 | request = Request(scope, receive=receive) 40 | content_type = request.headers["content-type"] 41 | message = (await request.body()).decode() 42 | text = f"content_type={content_type!r} message={message!r}" 43 | 44 | response = PlainTextResponse(text) 45 | await response(scope, receive, send) 46 | 47 | app = MessagePackMiddleware(app) 48 | 49 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 50 | r = await client.post( 51 | "/", 52 | content="Hello, world!", 53 | headers={"content-type": "text/plain"}, 54 | ) 55 | assert r.status_code == 200 56 | assert r.text == "content_type='text/plain' message='Hello, world!'" 57 | 58 | 59 | @pytest.mark.asyncio 60 | async def test_msgpack_accepted() -> None: 61 | app = MessagePackMiddleware(JSONResponse({"message": "Hello, world!"})) 62 | 63 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 64 | r = await client.get("/", headers={"accept": "application/x-msgpack"}) 65 | assert r.status_code == 200 66 | assert r.headers["content-type"] == "application/x-msgpack" 67 | expected_data = {"message": "Hello, world!"} 68 | assert int(r.headers["content-length"]) == len(msgpack.packb(expected_data)) 69 | assert msgpack.unpackb(r.content, raw=False) == expected_data 70 | 71 | 72 | @pytest.mark.asyncio 73 | async def test_msgpack_accepted_but_response_is_not_json() -> None: 74 | app = MessagePackMiddleware(PlainTextResponse("Hello, world!")) 75 | 76 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 77 | r = await client.get("/", headers={"accept": "application/x-msgpack"}) 78 | assert r.status_code == 200 79 | assert r.headers["content-type"] == "text/plain; charset=utf-8" 80 | assert r.text == "Hello, world!" 81 | 82 | 83 | @pytest.mark.asyncio 84 | async def test_msgpack_accepted_and_response_is_already_msgpack() -> None: 85 | data = msgpack.packb({"message": "Hello, world!"}) 86 | response = Response(data, media_type="application/x-msgpack") 87 | app = MessagePackMiddleware(response) 88 | 89 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 90 | r = await client.get("/", headers={"accept": "application/x-msgpack"}) 91 | assert r.status_code == 200 92 | assert r.headers["content-type"] == "application/x-msgpack" 93 | expected_data = {"message": "Hello, world!"} 94 | assert int(r.headers["content-length"]) == len(msgpack.packb(expected_data)) 95 | assert msgpack.unpackb(r.content, raw=False) == expected_data 96 | 97 | 98 | @pytest.mark.asyncio 99 | async def test_msgpack_not_accepted() -> None: 100 | app = MessagePackMiddleware(JSONResponse({"message": "Hello, world!"})) 101 | 102 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 103 | r = await client.get("/") 104 | assert r.status_code == 200 105 | assert r.headers["content-type"] == "application/json" 106 | assert r.json() == {"message": "Hello, world!"} 107 | with pytest.raises(ValueError): 108 | msgpack.unpackb(r.content) 109 | 110 | 111 | @pytest.mark.asyncio 112 | async def test_request_is_not_http() -> None: 113 | async def lifespan_only_app(scope: Scope, receive: Receive, send: Send) -> None: 114 | assert scope["type"] == "lifespan" 115 | 116 | app = MessagePackMiddleware(lifespan_only_app) 117 | scope = {"type": "lifespan"} 118 | await app(scope, mock_receive, mock_send) 119 | 120 | 121 | @pytest.mark.asyncio 122 | async def test_packb_unpackb() -> None: 123 | async def app(scope: Scope, receive: Receive, send: Send) -> None: 124 | request = Request(scope, receive) 125 | assert await request.json() == {"message": "unpacked"} 126 | 127 | response = JSONResponse({"message": "Hello, World!"}) 128 | await response(scope, receive, send) 129 | 130 | app = MessagePackMiddleware( 131 | app, packb=lambda obj: b"packed", unpackb=lambda byt: {"message": "unpacked"} 132 | ) 133 | 134 | async with httpx.AsyncClient(app=app, base_url="http://testserver") as client: 135 | r = await client.post( 136 | "/", 137 | content="Hello, World", 138 | headers={ 139 | "content-type": "application/x-msgpack", 140 | "accept": "application/x-msgpack", 141 | }, 142 | ) 143 | assert "packed" == r.text 144 | -------------------------------------------------------------------------------- /tests/utils.py: -------------------------------------------------------------------------------- 1 | from starlette.types import Message 2 | 3 | 4 | async def mock_receive() -> Message: 5 | raise NotImplementedError # pragma: no cover 6 | 7 | 8 | async def mock_send(message: Message) -> None: 9 | raise NotImplementedError # pragma: no cover 10 | --------------------------------------------------------------------------------