]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
tests: branches coverage in `responses.py`, `staticfiles.py`, `templating.py`, `middl...
authorRenan Heckert Leal <123672970+lealre@users.noreply.github.com>
Wed, 25 Dec 2024 08:54:42 +0000 (08:54 +0000)
committerGitHub <noreply@github.com>
Wed, 25 Dec 2024 08:54:42 +0000 (08:54 +0000)
starlette/endpoints.py
starlette/middleware/wsgi.py
starlette/templating.py
tests/test_responses.py
tests/test_staticfiles.py

index eb1dace42df506f8f92cc5b648d3f9246e9b2bcb..107690266e16e85add5b7960e1915d153ceb8ccc 100644 (file)
@@ -74,7 +74,7 @@ class WebSocketEndpoint:
                 if message["type"] == "websocket.receive":
                     data = await self.decode(websocket, message)
                     await self.on_receive(websocket, data)
-                elif message["type"] == "websocket.disconnect":
+                elif message["type"] == "websocket.disconnect":  # pragma: no branch
                     close_code = int(message.get("code") or status.WS_1000_NORMAL_CLOSURE)
                     break
         except Exception as exc:
index 71f4ab5de93cf36c160f66623f75632a431cbfd0..6e0a3fae6c176485e506b498200ac22c499c6c50 100644 (file)
@@ -121,7 +121,7 @@ class WSGIResponder:
         exc_info: typing.Any = None,
     ) -> None:
         self.exc_info = exc_info
-        if not self.response_started:
+        if not self.response_started:  # pragma: no branch
             self.response_started = True
             status_code_string, _ = status.split(" ", 1)
             status_code = int(status_code_string)
index 78bfb8c2658d79de386aa9fd60b94b7cfd75977c..6b01aac9209fdcc893f4b24fd09d8a3f14149ec9 100644 (file)
@@ -43,7 +43,7 @@ class _TemplateResponse(HTMLResponse):
     async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
         request = self.context.get("request", {})
         extensions = request.get("extensions", {})
-        if "http.response.debug" in extensions:
+        if "http.response.debug" in extensions:  # pragma: no branch
             await send(
                 {
                     "type": "http.response.debug",
index 9f801419a2bd98b86e3ead0c46be5a48c73d398c..28f2feb2d647d111c1362d69636ac7a4566090f5 100644 (file)
@@ -393,6 +393,18 @@ def test_set_cookie_path_none(test_client_factory: TestClientFactory) -> None:
     assert response.headers["set-cookie"] == "mycookie=myvalue; SameSite=lax"
 
 
+def test_set_cookie_samesite_none(test_client_factory: TestClientFactory) -> None:
+    async def app(scope: Scope, receive: Receive, send: Send) -> None:
+        response = Response("Hello, world!", media_type="text/plain")
+        response.set_cookie("mycookie", "myvalue", samesite=None)
+        await response(scope, receive, send)
+
+    client = test_client_factory(app)
+    response = client.get("/")
+    assert response.text == "Hello, world!"
+    assert response.headers["set-cookie"] == "mycookie=myvalue; Path=/"
+
+
 @pytest.mark.parametrize(
     "expires",
     [
index 8f742359339e438dccfa75958f1b8d773b705372..b4f13171987eac9ef07e2e9f42122f16735b18a8 100644 (file)
@@ -216,6 +216,21 @@ def test_staticfiles_304_with_etag_match(tmpdir: Path, test_client_factory: Test
     assert second_resp.content == b""
 
 
+def test_staticfiles_200_with_etag_mismatch(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
+    path = os.path.join(tmpdir, "example.txt")
+    with open(path, "w") as file:
+        file.write("<file content>")
+
+    app = StaticFiles(directory=tmpdir)
+    client = test_client_factory(app)
+    first_resp = client.get("/example.txt")
+    assert first_resp.status_code == 200
+    assert first_resp.headers["etag"] != '"123"'
+    second_resp = client.get("/example.txt", headers={"if-none-match": '"123"'})
+    assert second_resp.status_code == 200
+    assert second_resp.content == b"<file content>"
+
+
 def test_staticfiles_304_with_last_modified_compare_last_req(
     tmpdir: Path, test_client_factory: TestClientFactory
 ) -> None: