-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Replace WebSocket assertions with RuntimeError #1472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aminalaee
merged 6 commits into
Kludex:master
from
aminalaee:switch-websockets-asserts-with-runtimeerror
Feb 4, 2022
+169
−10
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f769acd
Switch WebSocket assertions with RuntimeError
aminalaee e698099
uplift coverage
aminalaee 7004dd7
Merge branch 'master' into switch-websockets-asserts-with-runtimeerror
aminalaee 430b9fd
update errors messages for send and receive
aminalaee b5f70d5
Merge branch 'switch-websockets-asserts-with-runtimeerror' of github.…
aminalaee 655854b
Tweak exception message - whitespace after comma.
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import pytest | ||
|
|
||
| from starlette import status | ||
| from starlette.websockets import WebSocket, WebSocketDisconnect | ||
| from starlette.websockets import WebSocket, WebSocketDisconnect, WebSocketState | ||
|
|
||
|
|
||
| def test_websocket_url(test_client_factory): | ||
|
|
@@ -422,3 +422,135 @@ async def asgi(receive, send): | |
| websocket.receive_text() | ||
| assert exc.value.code == status.WS_1001_GOING_AWAY | ||
| assert exc.value.reason == "Going Away" | ||
|
|
||
|
|
||
| def test_send_json_invalid_mode(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.accept() | ||
| await websocket.send_json({}, mode="invalid") | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_receive_json_invalid_mode(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.accept() | ||
| await websocket.receive_json(mode="invalid") | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_receive_text_before_accept(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.receive_text() | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_receive_bytes_before_accept(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.receive_bytes() | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_receive_json_before_accept(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.receive_json() | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_send_before_accept(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.send({"type": "websocket.send"}) | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_send_wrong_message_type(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.send({"type": "websocket.accept"}) | ||
| await websocket.send({"type": "websocket.accept"}) | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/"): | ||
| pass # pragma: nocover | ||
|
|
||
|
|
||
| def test_receive_before_accept(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.accept() | ||
| websocket.client_state = WebSocketState.CONNECTING | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't test this without modifying the client state. |
||
| await websocket.receive() | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/") as websocket: | ||
| websocket.send({"type": "websocket.send"}) | ||
|
|
||
|
|
||
| def test_receive_wrong_message_type(test_client_factory): | ||
| def app(scope): | ||
| async def asgi(receive, send): | ||
| websocket = WebSocket(scope, receive=receive, send=send) | ||
| await websocket.accept() | ||
| await websocket.receive() | ||
|
|
||
| return asgi | ||
|
|
||
| client = test_client_factory(app) | ||
| with pytest.raises(RuntimeError): | ||
| with client.websocket_connect("/") as websocket: | ||
| websocket.send({"type": "websocket.connect"}) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we think about this kind of style for the
message_typecase?...f'Expected ASGI message "websocket.connect", but got {message_type!r}'