Skip to content

Commit

Permalink
tests: branches coverage in responses.py, staticfiles.py, `templa…
Browse files Browse the repository at this point in the history
…ting.py`, `middleware/wsgi.py` and `endpoints.py` (#2804)
  • Loading branch information
lealre authored Dec 25, 2024
1 parent cba90a7 commit 70a04ee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion starlette/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def dispatch(self) -> None:
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:
Expand Down
2 changes: 1 addition & 1 deletion starlette/middleware/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def start_response(
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)
Expand Down
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:
if "http.response.debug" in extensions: # pragma: no branch
await send(
{
"type": "http.response.debug",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,18 @@ async def app(scope: Scope, receive: Receive, send: Send) -> 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",
[
Expand Down
15 changes: 15 additions & 0 deletions tests/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 70a04ee

Please sign in to comment.