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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/websockets.md
@@ -1,4 +1,3 @@

matiuszka marked this conversation as resolved.
Show resolved Hide resolved
Starlette includes a `WebSocket` class that fulfils a similar role
to the HTTP request, but that allows sending and receiving data on a websocket.

Expand Down Expand Up @@ -51,7 +50,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
Expand Up @@ -296,6 +296,7 @@ def __init__(
self.app = app
self.scope = scope
self.accepted_subprotocol = None
self.additional_headers = None
matiuszka marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -313,6 +314,7 @@ def __enter__(self) -> "WebSocketTestSession":
self.exit_stack.close()
raise
self.accepted_subprotocol = message.get("subprotocol", None)
self.additional_headers = message.get("headers", None)
return self

def __exit__(self, *args: typing.Any) -> None:
Expand Down
10 changes: 8 additions & 2 deletions starlette/websockets.py
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
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:
websocket.additional_headers = [(b"additional", b"header")]
matiuszka 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