Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment thread
Kludex marked this conversation as resolved.
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
Comment thread
Kludex marked this conversation as resolved.
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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be tested without the TestClient.

We have some tests that don't use the TestClient.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, I didn't think about it.

Added a test to the case based on other similar tests

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this back. I think this is enough for the "http.response.debug".

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
14 changes: 14 additions & 0 deletions tests/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ 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
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