Skip to content
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

Additional headers for WS accept message. #1361

Merged
merged 6 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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 docs/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ For example: `websocket.path_params['username']`

### Accepting the connection

* `await websocket.accept(subprotocol=None)`
* `await websocket.accept(subprotocol=None, headers=None)`

### Sending data

Expand Down
2 changes: 2 additions & 0 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def __init__(
self.app = app
self.scope = scope
self.accepted_subprotocol = None
self.extra_headers = None
self.portal_factory = portal_factory
self._receive_queue: "queue.Queue[typing.Any]" = queue.Queue()
self._send_queue: "queue.Queue[typing.Any]" = queue.Queue()
Expand All @@ -315,6 +316,7 @@ def __enter__(self) -> "WebSocketTestSession":
self.exit_stack.close()
raise
self.accepted_subprotocol = message.get("subprotocol", None)
self.extra_headers = message.get("headers", None)
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This should probably be message.get("headers", []) with #1422

Copy link
Member

Choose a reason for hiding this comment

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

I think this part is ok. When passing None you'll need to check assert websocket.extra_headers is None but with that change:

def test_additional_headers(test_client_factory):
     def app(scope):
         async def asgi(receive, send):
             websocket = WebSocket(scope, receive=receive, send=send)
             await websocket.accept(headers=None)
             await websocket.close()

         return asgi

     client = test_client_factory(app)
     with client.websocket_connect("/") as websocket:
         assert websocket.extra_headers == []

Am I missing something?

Copy link
Sponsor Member

@Kludex Kludex Jan 21, 2022

Choose a reason for hiding this comment

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

You're not. I don't have strong feelings on this. 👍

return self

def __exit__(self, *args: typing.Any) -> None:
Expand Down
10 changes: 8 additions & 2 deletions starlette/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ async def send(self, message: Message) -> None:
else:
raise RuntimeError('Cannot call "send" once a close message has been sent.')

async def accept(self, subprotocol: str = None) -> None:
async def accept(
self,
subprotocol: str = None,
headers: typing.Iterable[typing.Tuple[bytes, bytes]] = None,
) -> None:
if self.client_state == WebSocketState.CONNECTING:
# If we haven't yet seen the 'connect' message, then wait for it first.
await self.receive()
await self.send({"type": "websocket.accept", "subprotocol": subprotocol})
await self.send(
{"type": "websocket.accept", "subprotocol": subprotocol, "headers": headers}
)

def _raise_on_disconnect(self, message: Message) -> None:
if message["type"] == "websocket.disconnect":
Expand Down
14 changes: 14 additions & 0 deletions tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ async def asgi(receive, send):
assert websocket.accepted_subprotocol == "wamp"


def test_additional_headers(test_client_factory):
def app(scope):
async def asgi(receive, send):
websocket = WebSocket(scope, receive=receive, send=send)
await websocket.accept(headers=[(b"additional", b"header")])
await websocket.close()

return asgi

client = test_client_factory(app)
with client.websocket_connect("/") as websocket:
assert websocket.additional_headers == [(b"additional", b"header")]
Kludex marked this conversation as resolved.
Show resolved Hide resolved


def test_websocket_exception(test_client_factory):
def app(scope):
async def asgi(receive, send):
Expand Down