request.args.getlist() returns [] if missing, refs #774

Also added some unit tests for request.args
pull/783/head
Simon Willison 2020-05-29 15:51:30 -07:00
rodzic 7ccd55a163
commit 84616a2364
4 zmienionych plików z 14 dodań i 4 usunięć

Wyświetl plik

@ -761,9 +761,9 @@ class RequestParameters(dict):
except (KeyError, TypeError):
return default
def getlist(self, name, default=None):
def getlist(self, name):
"Return full list"
return super().get(name, default)
return super().get(name) or []
class ConnectionProblem(Exception):

Wyświetl plik

@ -276,4 +276,4 @@ Conider the querystring ``?foo=1&foo=2``. This will produce a ``request.args`` t
Calling ``request.args.get("foo")`` will return the first value, ``"1"``. If that key is not present it will return ``None`` - or the second argument if you passed one, which will be used as the default.
Calling ``request.args.getlist("foo")`` will return the full list, ``["1", "2"]``.
Calling ``request.args.getlist("foo")`` will return the full list, ``["1", "2"]``. If you call it on a missing key it will return ``[]``.

Wyświetl plik

@ -26,7 +26,7 @@ async def render_test_all_parameters(
datasette, columns, rows, sql, query_name, database, table, request, view_name, data
):
headers = {}
for custom_header in request.args.getlist("header") or []:
for custom_header in request.args.getlist("header"):
key, value = custom_header.split(":")
headers[key] = value
result = await datasette.databases["fixtures"].execute("select 1 + 1")

Wyświetl plik

@ -448,6 +448,16 @@ async def test_request_post_vars():
assert {"foo": "bar", "baz": "1"} == await request.post_vars()
def test_request_args():
request = Request.fake("/foo?multi=1&multi=2&single=3")
assert "1" == request.args.get("multi")
assert "3" == request.args.get("single")
assert ["1", "2"] == request.args.getlist("multi")
assert [] == request.args.getlist("missing")
with pytest.raises(KeyError):
request.args["missing"]
def test_call_with_supported_arguments():
def foo(a, b):
return "{}+{}".format(a, b)