diff --git a/datasette/utils/testing.py b/datasette/utils/testing.py index cabe2e5c..d4990784 100644 --- a/datasette/utils/testing.py +++ b/datasette/utils/testing.py @@ -16,6 +16,11 @@ class TestResponse: def status(self): return self.httpx_response.status_code + # Supports both for test-writing convenience + @property + def status_code(self): + return self.status + @property def headers(self): return self.httpx_response.headers @@ -24,6 +29,10 @@ class TestResponse: def body(self): return self.httpx_response.content + @property + def content(self): + return self.body + @property def cookies(self): return dict(self.httpx_response.cookies) diff --git a/tests/test_html.py b/tests/test_html.py index 83086cd4..3d4f2ed7 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -18,9 +18,9 @@ import urllib.parse def test_homepage(app_client_two_attached_databases): response = app_client_two_attached_databases.get("/") - assert response.status == 200 + assert response.status_code == 200 assert "text/html; charset=utf-8" == response.headers["content-type"] - soup = Soup(response.body, "html.parser") + soup = Soup(response.content, "html.parser") assert "Datasette Fixtures" == soup.find("h1").text assert ( "An example SQLite database demonstrating Datasette. Sign in as root user" @@ -48,30 +48,38 @@ def test_homepage(app_client_two_attached_databases): ] == table_links -def test_http_head(app_client): - response = app_client.get("/", method="HEAD") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_http_head(ds_client): + response = await ds_client.head("/") + assert response.status_code == 200 -def test_homepage_options(app_client): - response = app_client.get("/", method="OPTIONS") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_homepage_options(ds_client): + response = await ds_client.options("/") + assert response.status_code == 200 assert response.text == "ok" -def test_favicon(app_client): - response = app_client.get("/favicon.ico") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_favicon(ds_client): + response = await ds_client.get("/favicon.ico") + assert response.status_code == 200 assert response.headers["cache-control"] == "max-age=3600, immutable, public" assert int(response.headers["content-length"]) > 100 assert response.headers["content-type"] == "image/png" -def test_static(app_client): - response = app_client.get("/-/static/app2.css") - assert response.status == 404 - response = app_client.get("/-/static/app.css") - assert response.status == 200 +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_static(ds_client): + response = await ds_client.get("/-/static/app2.css") + assert response.status_code == 404 + response = await ds_client.get("/-/static/app.css") + assert response.status_code == 200 assert "text/css" == response.headers["content-type"] @@ -80,29 +88,31 @@ def test_static_mounts(): static_mounts=[("custom-static", str(pathlib.Path(__file__).parent))] ) as client: response = client.get("/custom-static/test_html.py") - assert response.status == 200 + assert response.status_code == 200 response = client.get("/custom-static/not_exists.py") - assert response.status == 404 + assert response.status_code == 404 response = client.get("/custom-static/../LICENSE") - assert response.status == 404 + assert response.status_code == 404 def test_memory_database_page(): with make_app_client(memory=True) as client: response = client.get("/_memory") - assert response.status == 200 + assert response.status_code == 200 def test_not_allowed_methods(): with make_app_client(memory=True) as client: for method in ("post", "put", "patch", "delete"): response = client.request(path="/_memory", method=method.upper()) - assert response.status == 405 + assert response.status_code == 405 -def test_database_page(app_client): - response = app_client.get("/fixtures") - soup = Soup(response.body, "html.parser") +@pytest.mark.ds_client +@pytest.mark.asyncio +async def test_database_page(ds_client): + response = await ds_client.get("/fixtures") + soup = Soup(response.text, "html.parser") # Should have a