From 2bfa699e60bd4980270b5732d7ba161b2bfdfa59 Mon Sep 17 00:00:00 2001 From: Herrtian <70463940+Herrtian@users.noreply.github.com> Date: Tue, 16 Jun 2026 14:11:44 +0200 Subject: [PATCH] test: simplify included route method check for coverage --- fastapi/routing.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 131bb202d..604d39790 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -1192,18 +1192,18 @@ class APIRoute(routing.Route): effective_context = _get_scope_effective_route_context(scope) if effective_context is not None and effective_context.original_route is self: methods = effective_context.methods - if methods and ( - scope["method"] not in methods - and not self._is_head_for_get(scope, methods) - ): - headers = {"Allow": ", ".join(self._allow_methods(methods))} - if "app" in scope: - raise HTTPException(status_code=405, headers=headers) - response = PlainTextResponse( - "Method Not Allowed", status_code=405, headers=headers - ) - await response(scope, receive, send) - return + if methods: + method = scope["method"] + method_is_allowed = method in methods or self._is_head_for_get(scope, methods) + if not method_is_allowed: + headers = {"Allow": ", ".join(self._allow_methods(methods))} + if "app" in scope: + raise HTTPException(status_code=405, headers=headers) + response = PlainTextResponse( + "Method Not Allowed", status_code=405, headers=headers + ) + await response(scope, receive, send) + return token = _effective_route_context_var.set(effective_context) try: app = request_response(self.get_route_handler())