Skip to content

Commit

Permalink
request.args.getlist() returns [] if missing, refs #774
Browse files Browse the repository at this point in the history
Also added some unit tests for request.args
  • Loading branch information
simonw committed May 29, 2020
1 parent 7ccd55a commit 84616a2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,9 +761,9 @@ def get(self, name, default=None):
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):
Expand Down
2 changes: 1 addition & 1 deletion docs/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``[]``.
2 changes: 1 addition & 1 deletion tests/plugins/register_output_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ async def receive():
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)
Expand Down

0 comments on commit 84616a2

Please sign in to comment.