async def test_pretty_json_response(obj: Any, values: Dict[str, Any]): app = FakeApplication() @app.router.get("/") async def home(): return pretty_json(obj) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 content_type = response.headers.get_single(b"content-type") assert content_type == b"application/json" raw = await response.text() data = await response.json() for name, value in values.items(): assert data.get(name) == value if isinstance(value, str): assert f' "{name}": "{value}"' in raw else: assert f' "{name}": ' in raw
async def test_file_response_from_generator_inline_with_name(): app = FakeApplication() @app.router.get("/") async def home(): return file( get_example_css, "text/css", file_name="home.css", content_disposition=ContentDispositionType.INLINE, ) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 assert response.headers.get_single(b"content-type") == b"text/css" assert ( response.headers.get_single(b"content-disposition") == b'inline; filename="home.css"' ) text = await response.text() assert text == await read_from_asynciterable(get_example_css)
async def test_file_response_from_bytes_io(): app = FakeApplication() bytes_io: Optional[BytesIO] = None @app.router.get("/") async def home(): nonlocal bytes_io bytes_io = BytesIO() bytes_io.write("Żywią i bronią".encode("utf-8")) return file(bytes_io, "text/plain; charset=utf-8", file_name="foo.txt") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 assert response.headers.get_single(b"content-type") == b"text/plain; charset=utf-8" assert ( response.headers.get_single(b"content-disposition") == b'attachment; filename="foo.txt"' ) assert bytes_io is not None assert bytes_io.closed # type: ignore
async def test_file_response_from_fs_with_filename(): app = FakeApplication() file_path = get_file_path("example.config", "files2") @app.router.get("/") async def home(): return file(file_path, "text/plain; charset=utf-8", file_name="foo.xml") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 assert response.headers.get_single(b"content-type") == b"text/plain; charset=utf-8" assert ( response.headers.get_single(b"content-disposition") == b'attachment; filename="foo.xml"' ) text = await response.text() with open(file_path, mode="rt", encoding="utf8") as f: contents = f.read() assert contents == text
async def test_created_response_with_value(): app = FakeApplication() @app.router.get("/") async def home(): return created( Foo(UUID("726807b3-5a82-4a59-8bed-65639d3529ba"), "example", False), location="https://foo.org/foo/726807b3-5a82-4a59-8bed-65639d3529ba", ) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 201 location = response.headers.get_single(b"location") assert location == b"https://foo.org/foo/726807b3-5a82-4a59-8bed-65639d3529ba" content = await app.response.json() assert content.get("id") == "726807b3-5a82-4a59-8bed-65639d3529ba" assert content.get("name") == "example" assert content.get("ufo") is False
async def test_status_code_response_with_empty_body(status: int): app = FakeApplication() @app.router.get("/") async def home(): return status_code(status) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) assert app.response.status == status content = await app.response.text() assert content == ""
async def test_status_method_response_empty_body(method, expected_status): app = FakeApplication() @app.router.get("/") async def home(): return method() app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) assert app.response.status == expected_status content = await app.response.text() assert content == ""
async def test_no_body_method(method, expected_status): app = FakeApplication() @app.router.get("/") async def home(): return method() app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == expected_status assert response.has_body() is False
async def test_created_response_with_empty_body(): app = FakeApplication() @app.router.get("/") async def home(): return created(location="https://foo.org/foo/001") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 201 location = response.headers.get_single(b"location") assert location == b"https://foo.org/foo/001"
async def test_status_code_response_with_object(status: int): app = FakeApplication() @app.router.get("/") async def home(): return status_code(status, Foo(uuid4(), "foo", True)) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) assert app.response.status == status content = await app.response.json() assert content.get("name") == "foo" assert content.get("ufo") is True
async def test_redirect_method_bytes_location(method, expected_status): app = FakeApplication() @app.router.get("/") async def home(): return method(b"https://foo.org/somewhere") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == expected_status location = response.headers.get_single(b"location") assert location == b"https://foo.org/somewhere"
async def test_file_response_from_generator(): app = FakeApplication() @app.router.get("/") async def home(): return file(get_example_css, "text/css") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 assert response.headers.get_single(b"content-type") == b"text/css" assert response.headers.get_single(b"content-disposition") == b"attachment" text = await response.text() assert text == await read_from_asynciterable(get_example_css)
async def test_html_response_with_status(content, status): app = FakeApplication() @app.router.get("/") async def home(): return html(content, status) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == status content_type = response.headers.get_single(b"content-type") assert content_type == b"text/html; charset=utf-8" body = await response.text() assert body == content
async def test_text_response_default_status(): app = FakeApplication() @app.router.get("/") async def home(): return text("Hello World") app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 content_type = response.headers.get_single(b"content-type") assert content_type == b"text/plain; charset=utf-8" body = await response.text() assert body == "Hello World"
async def test_file_response_from_byte_array(): app = FakeApplication() value = bytearray() value.extend(b"Hello!\n") value.extend(b"World!\n\n") value.extend(b"...") expected_result = bytes(value).decode("utf8") @app.router.get("/") async def home(): return file( value, "text/plain", file_name="example.txt", content_disposition=ContentDispositionType.INLINE, ) app.normalize_handlers() await app( get_example_scope("GET", "/", []), MockReceive(), MockSend(), ) response = app.response assert response.status == 200 assert response.headers.get_single(b"content-type") == b"text/plain" assert ( response.headers.get_single(b"content-disposition") == b'inline; filename="example.txt"' ) text = await response.text() assert text == expected_result