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

[Feature Request] Inheriting cookies from TestClient to WebSocketConnection. #25

Closed
otsuka opened this issue Jan 16, 2020 · 4 comments · Fixed by #27
Closed

[Feature Request] Inheriting cookies from TestClient to WebSocketConnection. #25

otsuka opened this issue Jan 16, 2020 · 4 comments · Fixed by #27

Comments

@otsuka
Copy link
Contributor

otsuka commented Jan 16, 2020

First of all, thanks for developing such a useful library.

I use cookies for authentication, and accept WebSocket connection from the authenticated user.
I can set cookies to a TestClient instance as bellow, although, the cookies are not included in WebSocket connection request.

TestClient.websocket_connect() method has extra_headers argument, but adding cookies directly to HTTP Header is not easy.

So It would be helpful if WebSocketSession could take over cookies from TestClient.

async with TestClient(app) as client
    ck = SimpleCookie()
    ck["foo"] = "bar"
    client.cookie_jar = ck

    async with client.websocket_connect(endpoint) as ws:
        ...
@masipcat
Copy link
Contributor

Hi @otsuka,
Feel free to open a PR with these features if you want :) but if you prefer, I can do it myself during the weekend.

@otsuka
Copy link
Contributor Author

otsuka commented Jan 17, 2020

@masipcat
I will appreciate it if you would do it.

@otsuka
Copy link
Contributor Author

otsuka commented Jan 17, 2020

In my use case, I have to write tests dealing with multiple users like chat app.

So it might be convenient to be able to set cookies in each WebSocketSession instance in addition to inheriting cookies from TestClient.

It would be useful if we could set cookies as easily as the code below (like aiohttp.ClientSession)

async with TestClient(app):  # invoking 'startup' only once

    # I don't use `async with WebSocketSession` to avoid deep nesting when handling multiple ws connections
    ws1 = WebSocketSession(app, ws_endpoint, cookies={'sessionid': 'xxxxxxxxxxxxxx'})
    ws2 = WebSocketSession(app, ws_endpoint, cookies={'sessionid': 'yyyyyyyyyyyyyy'})
    await ws1.connect()
    await ws2.connect()

    await ws1.send_str('Hello')  # BTW, send_text() is better for consistency, isn't it?
    text = await ws2.receive_text()
    assert text == 'Hello'

    await ws2.close()
    await ws1.close()

@masipcat
Copy link
Contributor

Version 1.4.0 released! I added the argument 'cookies' to websocket_connect() (and WebSocketSession as well) and I renamed ws.send_str() to ws.send_text().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants