| 1 |
from datasette.utils.asgi import Request |
| 2 |
import json |
| 3 |
import pytest |
| 4 |
|
| 5 |
|
| 6 |
@pytest.mark.asyncio |
| 7 |
async def test_request_post_vars(): |
| 8 |
scope = { |
| 9 |
"http_version": "1.1", |
| 10 |
"method": "POST", |
| 11 |
"path": "/", |
| 12 |
"raw_path": b"/", |
| 13 |
"query_string": b"", |
| 14 |
"scheme": "http", |
| 15 |
"type": "http", |
| 16 |
"headers": [[b"content-type", b"application/x-www-form-urlencoded"]], |
| 17 |
} |
| 18 |
|
| 19 |
async def receive(): |
| 20 |
return { |
| 21 |
"type": "http.request", |
| 22 |
"body": b"foo=bar&baz=1&empty=", |
| 23 |
"more_body": False, |
| 24 |
} |
| 25 |
|
| 26 |
request = Request(scope, receive) |
| 27 |
assert {"foo": "bar", "baz": "1", "empty": ""} == await request.post_vars() |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.asyncio |
| 31 |
async def test_request_post_body(): |
| 32 |
scope = { |
| 33 |
"http_version": "1.1", |
| 34 |
"method": "POST", |
| 35 |
"path": "/", |
| 36 |
"raw_path": b"/", |
| 37 |
"query_string": b"", |
| 38 |
"scheme": "http", |
| 39 |
"type": "http", |
| 40 |
"headers": [[b"content-type", b"application/json"]], |
| 41 |
} |
| 42 |
|
| 43 |
data = {"hello": "world"} |
| 44 |
|
| 45 |
async def receive(): |
| 46 |
return { |
| 47 |
"type": "http.request", |
| 48 |
"body": json.dumps(data, indent=4).encode("utf-8"), |
| 49 |
"more_body": False, |
| 50 |
} |
| 51 |
|
| 52 |
request = Request(scope, receive) |
| 53 |
body = await request.post_body() |
| 54 |
assert isinstance(body, bytes) |
| 55 |
assert data == json.loads(body) |
| 56 |
|
| 57 |
|
| 58 |
def test_request_args(): |
| 59 |
request = Request.fake("/foo?multi=1&multi=2&single=3") |
| 60 |
assert "1" == request.args.get("multi") |
| 61 |
assert "3" == request.args.get("single") |
| 62 |
assert "1" == request.args["multi"] |
| 63 |
assert "3" == request.args["single"] |
| 64 |
assert ["1", "2"] == request.args.getlist("multi") |
| 65 |
assert [] == request.args.getlist("missing") |
| 66 |
assert "multi" in request.args |
| 67 |
assert "single" in request.args |
| 68 |
assert "missing" not in request.args |
| 69 |
expected = ["multi", "single"] |
| 70 |
assert expected == list(request.args.keys()) |
| 71 |
for i, key in enumerate(request.args): |
| 72 |
assert expected[i] == key |
| 73 |
assert 2 == len(request.args) |
| 74 |
with pytest.raises(KeyError): |
| 75 |
request.args["missing"] |
| 76 |
|
| 77 |
|
| 78 |
def test_request_fake_url_vars(): |
| 79 |
request = Request.fake("/") |
| 80 |
assert request.url_vars == {} |
| 81 |
request = Request.fake("/", url_vars={"database": "fixtures"}) |
| 82 |
assert request.url_vars == {"database": "fixtures"} |
| 83 |
|
| 84 |
|
| 85 |
def test_request_repr(): |
| 86 |
request = Request.fake("/foo?multi=1&multi=2&single=3") |
| 87 |
assert ( |
| 88 |
repr(request) |
| 89 |
== '<asgi.Request method="GET" url="http://localhost/foo?multi=1&multi=2&single=3">' |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
def test_request_url_vars(): |
| 94 |
scope = { |
| 95 |
"http_version": "1.1", |
| 96 |
"method": "POST", |
| 97 |
"path": "/", |
| 98 |
"raw_path": b"/", |
| 99 |
"query_string": b"", |
| 100 |
"scheme": "http", |
| 101 |
"type": "http", |
| 102 |
"headers": [[b"content-type", b"application/x-www-form-urlencoded"]], |
| 103 |
} |
| 104 |
assert {} == Request(scope, None).url_vars |
| 105 |
assert {"name": "cleo"} == Request( |
| 106 |
dict(scope, url_route={"kwargs": {"name": "cleo"}}), None |
| 107 |
).url_vars |
| 108 |
|
| 109 |
|
| 110 |
@pytest.mark.parametrize( |
| 111 |
"path,query_string,expected_full_path", |
| 112 |
[("/", "", "/"), ("/", "foo=bar", "/?foo=bar"), ("/foo", "bar", "/foo?bar")], |
| 113 |
) |
| 114 |
def test_request_properties(path, query_string, expected_full_path): |
| 115 |
path_with_query_string = path |
| 116 |
if query_string: |
| 117 |
path_with_query_string += "?" + query_string |
| 118 |
scope = { |
| 119 |
"http_version": "1.1", |
| 120 |
"method": "POST", |
| 121 |
"path": path, |
| 122 |
"raw_path": path_with_query_string.encode("latin-1"), |
| 123 |
"query_string": query_string.encode("latin-1"), |
| 124 |
"scheme": "http", |
| 125 |
"type": "http", |
| 126 |
} |
| 127 |
request = Request(scope, None) |
| 128 |
assert request.path == path |
| 129 |
assert request.query_string == query_string |
| 130 |
assert request.full_path == expected_full_path |
| 131 |
|
| 132 |
|
| 133 |
def test_request_blank_values(): |
| 134 |
request = Request.fake("/?a=b&foo=bar&foo=bar2&baz=") |
| 135 |
assert request.args._data == {"a": ["b"], "foo": ["bar", "bar2"], "baz": [""]} |
| 136 |
|
| 137 |
|
| 138 |
def test_json_in_query_string_name(): |
| 139 |
query_string = ( |
| 140 |
'?_through.["roadside_attraction_characteristics"%2C"characteristic_id"]=1' |
| 141 |
) |
| 142 |
request = Request.fake("/" + query_string) |
| 143 |
assert ( |
| 144 |
request.args[ |
| 145 |
'_through.["roadside_attraction_characteristics","characteristic_id"]' |
| 146 |
] |
| 147 |
== "1" |
| 148 |
) |