Skip to content

Commit

Permalink
Fix more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Sep 1, 2024
1 parent f55aa57 commit d59c797
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 38 deletions.
2 changes: 1 addition & 1 deletion starlette/formparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def on_headers_finished(self) -> None:
try:
self._current_part.field_name = _user_safe_decode(options[b"name"], self._charset)
except KeyError:
raise MultiPartException('The Content-Disposition header field "name" must be ' "provided.")
raise MultiPartException('The Content-Disposition header field "name" must be provided.')
if b"filename" in options:
self._current_files += 1
if self._current_files > self.max_files:
Expand Down
2 changes: 1 addition & 1 deletion starlette/middleware/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
</p>
<div id="{frame_filename}-{frame_lineno}" class="source-code {collapsed}">{code_context}</div>
</div>
"""
""" # noqa: E501

LINE = """
<p><span class="frame-line">
Expand Down
17 changes: 6 additions & 11 deletions starlette/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ async def receive(self) -> Message:
message = await self._receive()
message_type = message["type"]
if message_type != "websocket.connect":
raise RuntimeError('Expected ASGI message "websocket.connect", ' f"but got {message_type!r}")
raise RuntimeError(f'Expected ASGI message "websocket.connect", but got {message_type!r}')
self.client_state = WebSocketState.CONNECTED
return message
elif self.client_state == WebSocketState.CONNECTED:
message = await self._receive()
message_type = message["type"]
if message_type not in {"websocket.receive", "websocket.disconnect"}:
raise RuntimeError(
'Expected ASGI message "websocket.receive" or ' f'"websocket.disconnect", but got {message_type!r}'
f'Expected ASGI message "websocket.receive" or "websocket.disconnect", but got {message_type!r}'
)
if message_type == "websocket.disconnect":
self.client_state = WebSocketState.DISCONNECTED
Expand All @@ -61,14 +61,9 @@ async def send(self, message: Message) -> None:
"""
if self.application_state == WebSocketState.CONNECTING:
message_type = message["type"]
if message_type not in {
"websocket.accept",
"websocket.close",
"websocket.http.response.start",
}:
if message_type not in {"websocket.accept", "websocket.close", "websocket.http.response.start"}:
raise RuntimeError(
'Expected ASGI message "websocket.accept",'
'"websocket.close" or "websocket.http.response.start",'
'Expected ASGI message "websocket.accept", "websocket.close" or "websocket.http.response.start", '
f"but got {message_type!r}"
)
if message_type == "websocket.close":
Expand All @@ -82,7 +77,7 @@ async def send(self, message: Message) -> None:
message_type = message["type"]
if message_type not in {"websocket.send", "websocket.close"}:
raise RuntimeError(
'Expected ASGI message "websocket.send" or "websocket.close", ' f"but got {message_type!r}"
f'Expected ASGI message "websocket.send" or "websocket.close", but got {message_type!r}'
)
if message_type == "websocket.close":
self.application_state = WebSocketState.DISCONNECTED
Expand All @@ -94,7 +89,7 @@ async def send(self, message: Message) -> None:
elif self.application_state == WebSocketState.RESPONSE:
message_type = message["type"]
if message_type != "websocket.http.response.body":
raise RuntimeError('Expected ASGI message "websocket.http.response.body", ' f"but got {message_type!r}")
raise RuntimeError(f'Expected ASGI message "websocket.http.response.body", but got {message_type!r}')
if not message.get("more_body", False):
self.application_state = WebSocketState.DISCONNECTED
await self._send(message)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,7 @@ def run_shutdown() -> None: # pragma: no cover

with pytest.deprecated_call(match="The on_startup and on_shutdown parameters are deprecated"):
with pytest.warns(
UserWarning,
match=(
"The `lifespan` parameter cannot be used with `on_startup` or `on_shutdown`."
),
UserWarning, match="The `lifespan` parameter cannot be used with `on_startup` or `on_shutdown`."
):
app = Router(on_startup=[run_startup], on_shutdown=[run_shutdown], lifespan=lifespan)

Expand Down
26 changes: 5 additions & 21 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ async def receive() -> Message:
async def send(message: Message) -> None:
if message["type"] == "websocket.accept":
return
# Simulate the exception the server would send to the application when the
# client disconnects.
# Simulate the exception the server would send to the application when the client disconnects.
raise OSError

with pytest.raises(WebSocketDisconnect) as ctx:
Expand Down Expand Up @@ -334,19 +333,8 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
"headers": [(b"content-type", b"text/plain"), (b"foo", b"bar")],
}
)
await websocket.send(
{
"type": "websocket.http.response.body",
"body": b"hard",
"more_body": True,
}
)
await websocket.send(
{
"type": "websocket.http.response.body",
"body": b"body",
}
)
await websocket.send({"type": "websocket.http.response.body", "body": b"hard", "more_body": True})
await websocket.send({"type": "websocket.http.response.body", "body": b"body"})

client = test_client_factory(app)
with pytest.raises(WebSocketDenialResponse) as exc:
Expand Down Expand Up @@ -402,7 +390,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
client = test_client_factory(app)
with pytest.raises(
RuntimeError,
match=('Expected ASGI message "websocket.http.response.body", but got ' "'websocket.http.response.start'"),
match=("Expected ASGI message \"websocket.http.response.body\", but got 'websocket.http.response.start'"),
):
with client.websocket_connect("/"):
pass # pragma: no cover
Expand Down Expand Up @@ -490,11 +478,7 @@ async def mock_receive() -> Message: # type: ignore

async def mock_send(message: Message) -> None: ... # pragma: no cover

websocket = WebSocket(
{"type": "websocket", "path": "/abc/", "headers": []},
receive=mock_receive,
send=mock_send,
)
websocket = WebSocket({"type": "websocket", "path": "/abc/", "headers": []}, receive=mock_receive, send=mock_send)
assert websocket["type"] == "websocket"
assert dict(websocket) == {"type": "websocket", "path": "/abc/", "headers": []}
assert len(websocket) == 3
Expand Down

0 comments on commit d59c797

Please sign in to comment.