diff --git a/datasette/app.py b/datasette/app.py index 28b5e857..978fb7e7 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -12,7 +12,7 @@ from pathlib import Path import click from markupsafe import Markup from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PrefixLoader -from sanic.exceptions import InvalidUsage, NotFound +from sanic.exceptions import NotFound from .views.base import DatasetteError, ureg, AsgiRouter from .views.database import DatabaseDownload, DatabaseView @@ -665,10 +665,6 @@ class Datasette: status = 404 info = {} message = exception.args[0] - elif isinstance(exception, InvalidUsage): - status = 405 - info = {} - message = exception.args[0] elif isinstance(exception, DatasetteError): status = exception.status info = exception.error_dict diff --git a/datasette/views/base.py b/datasette/views/base.py index 4bf251fb..8d514688 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -54,6 +54,11 @@ class DatasetteError(Exception): class BaseView(AsgiView): ds = None + async def head(self, *args, **kwargs): + response = await self.get(*args, **kwargs) + response.body = b"" + return response + def _asset_urls(self, key, template, context): # Flatten list-of-lists from plugins: seen_urls = set() diff --git a/tests/fixtures.py b/tests/fixtures.py index c5f2bfd4..00140f50 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -37,10 +37,10 @@ class TestClient: self.asgi_app = asgi_app @async_to_sync - async def get(self, path, allow_redirects=True, redirect_count=0): - return await self._get(path, allow_redirects, redirect_count) + async def get(self, path, allow_redirects=True, redirect_count=0, method="GET"): + return await self._get(path, allow_redirects, redirect_count, method) - async def _get(self, path, allow_redirects=True, redirect_count=0): + async def _get(self, path, allow_redirects=True, redirect_count=0, method="GET"): query_string = b"" if "?" in path: path, _, query_string = path.partition("?") @@ -50,7 +50,7 @@ class TestClient: { "type": "http", "http_version": "1.0", - "method": "GET", + "method": method, "path": unquote(path), "raw_path": path.encode("ascii"), "query_string": query_string, diff --git a/tests/test_html.py b/tests/test_html.py index 7a5c32c4..32fa2fe3 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -46,6 +46,11 @@ 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 + + def test_static(app_client): response = app_client.get("/-/static/app2.css") assert response.status == 404