-from starlette.datastructures import Headers, QueryParams, URL
+from starlette.datastructures import Headers, MutableHeaders, QueryParams, URL
def test_url():
assert h["a"] == "123"
assert h.get("a") == "123"
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.values() == ["123", "456", "789"]
assert h.items() == [("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 != [(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():
assert q["a"] == "123"
assert q.get("a") == "123"
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.values() == ["123", "456", "789"]
assert q.items() == [("a", "123"), ("a", "456"), ("b", "789")]
-from starlette import Response, StreamingResponse, TestClient
+from starlette import FileResponse, Response, StreamingResponse, TestClient
import asyncio
assert response.headers["x-header-2"] == "789"
-def test_streaming_response_headers():
- def app(scope):
- async def asgi(receive, send):
- async def stream(msg):
- yield "hello, world"
+def test_file_response(tmpdir):
+ with open("xyz", "wb") as file:
+ file.write(b"<file content>")
- 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
+ def app(scope):
+ return FileResponse(path="xyz", filename="example.png")
client = TestClient(app)
response = client.get("/")
- assert response.headers["x-header-1"] == "123"
- assert response.headers["x-header-2"] == "789"
+ assert response.status_code == 200
+ assert response.content == b"<file content>"
+ assert response.headers["content-type"] == "image/png"
+ assert (
+ response.headers["content-disposition"] == 'attachment; filename="example.png"'
+ )