Skip to content

Commit

Permalink
resolve http handler conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut committed Aug 27, 2024
1 parent a4a9d71 commit 5fdece2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 10 additions & 2 deletions litestar/handlers/http_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
)
from litestar.types.builtin_types import NoneType
from litestar.utils import ensure_async_callable
from litestar.utils.predicates import is_async_callable
from litestar.utils.predicates import is_async_callable, is_class_and_subclass
from litestar.utils.scope.state import ScopeState
from litestar.utils.warnings import warn_implicit_sync_to_thread, warn_sync_to_thread_with_async_callable

Expand Down Expand Up @@ -615,7 +615,15 @@ def _validate_handler_function(self) -> None:
if self.http_methods == {HttpMethod.HEAD} and not self.parsed_fn_signature.return_type.is_subclass_of(
(NoneType, File, ASGIFileResponse)
):
raise ImproperlyConfiguredException("A response to a head request should not have a body")
field_definition = self.parsed_fn_signature.return_type
if not (
is_empty_response_annotation(field_definition)
or is_class_and_subclass(field_definition.annotation, File)
or is_class_and_subclass(field_definition.annotation, ASGIFileResponse)
):
raise ImproperlyConfiguredException(
f"{self}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body."
)

async def handle(self, connection: Request[Any, Any, Any]) -> None:
"""ASGI app that creates a :class:`~.connection.Request` from the passed in args, determines which handler function to call and then
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/test_handlers/test_http_handlers/test_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ def handler_subclass() -> MyResponse[None]:
Litestar(route_handlers=[handler, handler_subclass])


def test_head_decorator_raises_validation_error_if_method_is_passed() -> None:
with pytest.raises(ImproperlyConfiguredException):

@head("/", http_method=HttpMethod.HEAD)
def handler() -> None:
return

handler.on_registration(Litestar(), HTTPRoute(path="/", route_handlers=[handler]))


def test_head_decorator_does_not_raise_for_file_response() -> None:
@head("/")
def handler() -> "File":
Expand Down

0 comments on commit 5fdece2

Please sign in to comment.