]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
tests: branch coverage in `_utils.py`, `middleware/gzip.py`, `routing.py` and `reques...
authorRenan Heckert Leal <123672970+lealre@users.noreply.github.com>
Sat, 28 Dec 2024 04:55:20 +0000 (04:55 +0000)
committerGitHub <noreply@github.com>
Sat, 28 Dec 2024 04:55:20 +0000 (05:55 +0100)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/_utils.py
starlette/middleware/gzip.py
starlette/requests.py
starlette/routing.py
tests/test_routing.py

index 74c9f24f77c413557cb32957af55b6a7126ef5cc..a22a274592b019a61832bffc3a6eed91e8ca12ea 100644 (file)
@@ -75,9 +75,9 @@ def collapse_excgroups() -> typing.Generator[None, None, None]:
     try:
         yield
     except BaseException as exc:
-        if has_exceptiongroups:
+        if has_exceptiongroups:  # pragma: no cover
             while isinstance(exc, BaseExceptionGroup) and len(exc.exceptions) == 1:
-                exc = exc.exceptions[0]  # pragma: no cover
+                exc = exc.exceptions[0]
 
         raise exc
 
index 127b91e7a77b88b8ad1fc12b5ebad8ae46bc1aea..b677063da3702bc0d966b324edb9dc23dcff046e 100644 (file)
@@ -13,7 +13,7 @@ class GZipMiddleware:
         self.compresslevel = compresslevel
 
     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
-        if scope["type"] == "http":
+        if scope["type"] == "http":  # pragma: no branch
             headers = Headers(scope=scope)
             if "gzip" in headers.get("Accept-Encoding", ""):
                 responder = GZipResponder(self.app, self.minimum_size, compresslevel=self.compresslevel)
@@ -88,7 +88,7 @@ class GZipResponder:
                 await self.send(self.initial_message)
                 await self.send(message)
 
-        elif message_type == "http.response.body":
+        elif message_type == "http.response.body":  # pragma: no branch
             # Remaining body in streaming GZip response.
             body = message.get("body", b"")
             more_body = message.get("more_body", False)
index a29164d0d3b1c24092406c7e0888067907591cee..f85a3b914939abfa4b70c73659178d836787bf84 100644 (file)
@@ -230,7 +230,7 @@ class Request(HTTPConnection):
                     self._stream_consumed = True
                 if body:
                     yield body
-            elif message["type"] == "http.disconnect":
+            elif message["type"] == "http.disconnect":  # pragma: no branch
                 self._is_disconnected = True
                 raise ClientDisconnect()
         yield b""
@@ -250,7 +250,7 @@ class Request(HTTPConnection):
         return self._json
 
     async def _get_form(self, *, max_files: int | float = 1000, max_fields: int | float = 1000) -> FormData:
-        if self._form is None:
+        if self._form is None:  # pragma: no branch
             assert (
                 parse_options_header is not None
             ), "The `python-multipart` library must be installed to use form parsing."
index 3b3c52968c88b3f24b0bcb4f31b2102fbeae5feb..74daeadb0beec0ca243bf8a52ee97b7b3aad8716 100644 (file)
@@ -196,7 +196,7 @@ class BaseRoute:
             if scope["type"] == "http":
                 response = PlainTextResponse("Not Found", status_code=404)
                 await response(scope, receive, send)
-            elif scope["type"] == "websocket":
+            elif scope["type"] == "websocket":  # pragma: no branch
                 websocket_close = WebSocketClose()
                 await websocket_close(scope, receive, send)
             return
@@ -398,7 +398,7 @@ class Mount(BaseRoute):
 
     def matches(self, scope: Scope) -> tuple[Match, Scope]:
         path_params: dict[str, typing.Any]
-        if scope["type"] in ("http", "websocket"):
+        if scope["type"] in ("http", "websocket"):  # pragma: no branch
             root_path = scope.get("root_path", "")
             route_path = get_route_path(scope)
             match = self.path_regex.match(route_path)
@@ -481,7 +481,7 @@ class Host(BaseRoute):
         return getattr(self.app, "routes", [])
 
     def matches(self, scope: Scope) -> tuple[Match, Scope]:
-        if scope["type"] in ("http", "websocket"):
+        if scope["type"] in ("http", "websocket"):  # pragma:no branch
             headers = Headers(scope=scope)
             host = headers.get("host", "").split(":")[0]
             match = self.host_regex.match(host)
index 6d7e5d1545261c8335fa7da7d61431d3945c36ad..933fe7c31ef327d870b2577d3929064457165e23 100644 (file)
@@ -565,6 +565,16 @@ def test_url_for_with_double_mount() -> None:
     assert url == "/mount/static/123"
 
 
+def test_url_for_with_root_path_ending_with_slash(test_client_factory: TestClientFactory) -> None:
+    def homepage(request: Request) -> JSONResponse:
+        return JSONResponse({"index": str(request.url_for("homepage"))})
+
+    app = Starlette(routes=[Route("/", homepage, name="homepage")])
+    client = test_client_factory(app, base_url="https://www.example.org/", root_path="/sub_path/")
+    response = client.get("/sub_path/")
+    assert response.json() == {"index": "https://www.example.org/sub_path/"}
+
+
 def test_standalone_route_matches(
     test_client_factory: TestClientFactory,
 ) -> None: