Skip to content

Commit

Permalink
remove pragma: no branch in templating.py and add a test for the …
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
lealre committed Dec 18, 2024
1 parent bf34abb commit 846eb14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(
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: # pragma: no branch
if "http.response.debug" in extensions:
await send(
{
"type": "http.response.debug",
Expand Down
34 changes: 34 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from starlette.responses import Response
from starlette.routing import Route
from starlette.templating import Jinja2Templates
from starlette.types import Message, Receive, Scope, Send
from tests.types import TestClientFactory


Expand Down Expand Up @@ -308,3 +309,36 @@ def page(request: Request) -> Response:
assert response.headers["x-key"] == "value"
assert response.headers["content-type"] == "text/plain; charset=utf-8"
spy.assert_called()


@pytest.mark.anyio
async def test_branch_coverage_http_response_debug_not_in_message(tmpdir: Path) -> None:
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
file.write("<html>Hello</html>")

templates = Jinja2Templates(directory=str(tmpdir))

async def app(scope: Scope, receive: Receive, send: Send) -> None:
assert scope["type"] == "http"
request = Request(scope, receive)
response = templates.TemplateResponse(request, "index.html")
await response(scope, receive, send)

async def receive() -> Message:
raise NotImplementedError("Should not be called!")

async def send(message: Message) -> None:
if message["type"] == "http.response.start":
assert message["status"] == 200
if message["type"] == "http.response.body":
assert message["body"] == b"<html>Hello</html>"
assert "http.response.debug" not in message["type"]

scope = {
"type": "http",
"method": "GET",
"path": "/",
}

await app(scope, receive, send)

0 comments on commit 846eb14

Please sign in to comment.