Use getlist, instead of get_list

This commit is contained in:
Tom Christie 2018-07-11 16:30:40 +01:00
parent 5f194f73bf
commit 2e0bd33f59
3 changed files with 31 additions and 22 deletions

View File

@ -75,7 +75,7 @@ class QueryParams(typing.Mapping[str, str]):
self._dict = {k: v for k, v in reversed(items)} self._dict = {k: v for k, v in reversed(items)}
self._list = items self._list = items
def get_list(self, key: str) -> typing.List[str]: def getlist(self, key: str) -> typing.List[str]:
return [item_value for item_key, item_value in self._list if item_key == key] return [item_value for item_key, item_value in self._list if item_key == key]
def keys(self): def keys(self):

View File

@ -1,4 +1,4 @@
from starlette.datastructures import Headers, QueryParams, URL from starlette.datastructures import Headers, MutableHeaders, QueryParams, URL
def test_url(): def test_url():
@ -25,7 +25,7 @@ def test_headers():
assert h["a"] == "123" assert h["a"] == "123"
assert h.get("a") == "123" assert h.get("a") == "123"
assert h.get("nope", default=None) is None assert h.get("nope", default=None) is None
assert h.get_list("a") == ["123", "456"] assert h.getlist("a") == ["123", "456"]
assert h.keys() == ["a", "a", "b"] assert h.keys() == ["a", "a", "b"]
assert h.values() == ["123", "456", "789"] assert h.values() == ["123", "456", "789"]
assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")] assert h.items() == [("a", "123"), ("a", "456"), ("b", "789")]
@ -34,8 +34,21 @@ def test_headers():
assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])" assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
assert h == Headers([(b"a", b"123"), (b"b", b"789"), (b"a", b"456")]) assert h == Headers([(b"a", b"123"), (b"b", b"789"), (b"a", b"456")])
assert h != [(b"a", b"123"), (b"A", b"456"), (b"b", b"789")] assert h != [(b"a", b"123"), (b"A", b"456"), (b"b", b"789")]
h = Headers()
assert not h.items()
def test_mutable_headers():
h = MutableHeaders()
assert dict(h) == {}
h["a"] = "1"
assert dict(h) == {"a": "1"}
h["a"] = "2"
assert dict(h) == {"a": "2"}
h.setdefault("a", "3")
assert dict(h) == {"a": "2"}
h.setdefault("b", "4")
assert dict(h) == {"a": "2", "b": "4"}
del h["a"]
assert dict(h) == {"b": "4"}
def test_queryparams(): def test_queryparams():
@ -46,7 +59,7 @@ def test_queryparams():
assert q["a"] == "123" assert q["a"] == "123"
assert q.get("a") == "123" assert q.get("a") == "123"
assert q.get("nope", default=None) is None assert q.get("nope", default=None) is None
assert q.get_list("a") == ["123", "456"] assert q.getlist("a") == ["123", "456"]
assert q.keys() == ["a", "a", "b"] assert q.keys() == ["a", "a", "b"]
assert q.values() == ["123", "456", "789"] assert q.values() == ["123", "456", "789"]
assert q.items() == [("a", "123"), ("a", "456"), ("b", "789")] assert q.items() == [("a", "123"), ("a", "456"), ("b", "789")]

View File

@ -1,4 +1,4 @@
from starlette import Response, StreamingResponse, TestClient from starlette import FileResponse, Response, StreamingResponse, TestClient
import asyncio import asyncio
@ -67,22 +67,18 @@ def test_response_headers():
assert response.headers["x-header-2"] == "789" assert response.headers["x-header-2"] == "789"
def test_streaming_response_headers(): def test_file_response(tmpdir):
with open("xyz", "wb") as file:
file.write(b"<file content>")
def app(scope): def app(scope):
async def asgi(receive, send): return FileResponse(path="xyz", filename="example.png")
async def stream(msg):
yield "hello, world"
headers = {"x-header-1": "123", "x-header-2": "456"}
response = StreamingResponse(
stream("hello, world"), media_type="text/plain", headers=headers
)
response.headers["x-header-2"] = "789"
await response(receive, send)
return asgi
client = TestClient(app) client = TestClient(app)
response = client.get("/") response = client.get("/")
assert response.headers["x-header-1"] == "123" assert response.status_code == 200
assert response.headers["x-header-2"] == "789" assert response.content == b"<file content>"
assert response.headers["content-type"] == "image/png"
assert (
response.headers["content-disposition"] == 'attachment; filename="example.png"'
)