kopia lustrzana https://github.com/simonw/datasette
request.args.getlist() returns [] if missing, refs #774
Also added some unit tests for request.argspull/783/head
rodzic
7ccd55a163
commit
84616a2364
|
@ -761,9 +761,9 @@ class RequestParameters(dict):
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def getlist(self, name, default=None):
|
def getlist(self, name):
|
||||||
"Return full list"
|
"Return full list"
|
||||||
return super().get(name, default)
|
return super().get(name) or []
|
||||||
|
|
||||||
|
|
||||||
class ConnectionProblem(Exception):
|
class ConnectionProblem(Exception):
|
||||||
|
|
|
@ -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.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 ``[]``.
|
||||||
|
|
|
@ -26,7 +26,7 @@ async def render_test_all_parameters(
|
||||||
datasette, columns, rows, sql, query_name, database, table, request, view_name, data
|
datasette, columns, rows, sql, query_name, database, table, request, view_name, data
|
||||||
):
|
):
|
||||||
headers = {}
|
headers = {}
|
||||||
for custom_header in request.args.getlist("header") or []:
|
for custom_header in request.args.getlist("header"):
|
||||||
key, value = custom_header.split(":")
|
key, value = custom_header.split(":")
|
||||||
headers[key] = value
|
headers[key] = value
|
||||||
result = await datasette.databases["fixtures"].execute("select 1 + 1")
|
result = await datasette.databases["fixtures"].execute("select 1 + 1")
|
||||||
|
|
|
@ -448,6 +448,16 @@ async def test_request_post_vars():
|
||||||
assert {"foo": "bar", "baz": "1"} == await 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 test_call_with_supported_arguments():
|
||||||
def foo(a, b):
|
def foo(a, b):
|
||||||
return "{}+{}".format(a, b)
|
return "{}+{}".format(a, b)
|
||||||
|
|
Ładowanie…
Reference in New Issue