await request.post_body() method, closes #897

This commit is contained in:
Simon Willison 2020-07-17 13:12:35 -07:00
commit 1f6a134369
3 changed files with 37 additions and 3 deletions

View file

@ -1,4 +1,5 @@
from datasette.utils.asgi import Request
import json
import pytest
@ -26,6 +27,34 @@ async def test_request_post_vars():
assert {"foo": "bar", "baz": "1", "empty": ""} == await request.post_vars()
@pytest.mark.asyncio
async def test_request_post_body():
scope = {
"http_version": "1.1",
"method": "POST",
"path": "/",
"raw_path": b"/",
"query_string": b"",
"scheme": "http",
"type": "http",
"headers": [[b"content-type", b"application/json"]],
}
data = {"hello": "world"}
async def receive():
return {
"type": "http.request",
"body": json.dumps(data, indent=4).encode("utf-8"),
"more_body": False,
}
request = Request(scope, receive)
body = await request.post_body()
assert isinstance(body, bytes)
assert data == json.loads(body)
def test_request_args():
request = Request.fake("/foo?multi=1&multi=2&single=3")
assert "1" == request.args.get("multi")