Skip to content

Commit bcd8913

Browse files
committed
Handle missing session and unresolvable paths in test client
The test client assumed session middleware always ran and URL resolution always succeeded. This breaks for middleware-handled responses (e.g. healthcheck) that bypass session middleware and don't match URL routes.
1 parent f9be632 commit bcd8913

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

plain/plain/test/client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,23 @@ def request(self, **request: Any) -> ClientResponse:
569569
from plain.auth.requests import get_request_user
570570

571571
client_response.user = get_request_user(client_response.wsgi_request)
572-
except ImportError:
572+
except Exception:
573+
# ImportError if plain.auth not installed, or other exceptions
574+
# if session middleware didn't run (e.g. healthcheck)
573575
pass
574576

575577
# Attach the ResolverMatch instance to the response.
578+
# Returns None for paths handled by middleware (e.g. healthcheck)
579+
# that don't have a corresponding URL route.
576580
resolver = get_resolver()
577-
client_response.resolver_match = SimpleLazyObject(
578-
lambda: resolver.resolve(request["PATH_INFO"]),
579-
)
581+
582+
def _resolve_or_none():
583+
try:
584+
return resolver.resolve(request["PATH_INFO"])
585+
except Exception:
586+
return None
587+
588+
client_response.resolver_match = SimpleLazyObject(_resolve_or_none)
580589

581590
# Update persistent cookie data.
582591
if client_response.cookies:

0 commit comments

Comments
 (0)