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

Question: Can I test websocket things? #61

Closed
item4 opened this issue Nov 15, 2017 · 15 comments
Closed

Question: Can I test websocket things? #61

item4 opened this issue Nov 15, 2017 · 15 comments

Comments

@item4
Copy link

item4 commented Nov 15, 2017

Can I test websocket things such as aiohttp websocket client?

@mindflayer
Copy link
Owner

Any protocol based on socket can be tested with it.

@item4
Copy link
Author

item4 commented Nov 15, 2017

I can not find about this. Where I can see docs?

@mindflayer
Copy link
Owner

I have never experimented with WebSockets, just saying it's something you can achieve with Mocket.

@item4
Copy link
Author

item4 commented Nov 15, 2017

OK. I'll read code of Mocket and try test websocket things. Regardless how it ends up, I will write after story at this issue.

@mindflayer
Copy link
Owner

Of course feel free to ask for help, and I will do my best to support you.

@item4
Copy link
Author

item4 commented Nov 16, 2017

from aiohttp.http import WS_KEY

from mocket.mocket import Mocket, Mocketizer
from mocket.mockhttp import Entry, Request, Response

class WebsocketResponse(Response):

    def __init__(
        self,
        body: bytes=b'',
        status: int=101,
        headers: dict=None,
        match_querystring: bool=True
    ) -> None:
        import pdb
        pdb.set_trace()
        self.body = body
        self.headers = headers or {}
        self.status = status
        self.match_querystring = match_querystring
        self.set_base_headers()

    @property
    def data(self):
        req: Request = Mocket.last_request()
        req_headers = {k: v for k, v in req.headers.items()}
        sec_accept = base64.b64encode(hashlib.sha1(
            req_headers['Sec-Websocket-Key'].encode() + WS_KEY
        ).digest()).decode()
        headers = {
            'upgrade': 'websocket',
            'connection': 'upgrade',
            'sec-websocket-accept': sec_accept,
        }
        self.set_extra_headers(headers)
        return self.get_protocol_data()


class WebsecketEntry(Entry):

    response_cls = WebsocketResponse

@pytest.mark.asyncio
async def test_receive():
    token = 'asdf1234'
    sec_key = base64.b64encode(os.urandom(16))
    with Mocketizer(None):
        WebsecketEntry.single_register(
            Entry.GET,
            'http://slack.websocket.io',
            status=101,
        )

        await websocket_things()

I was defeated. Mocket.last_request() is empty. I need header of last request for return Sec-Websocket-Accept but I can not get header.

@mindflayer
Copy link
Owner

I am not sure I got the purpose of your implementation of WebsocketResponse.data.

@item4
Copy link
Author

item4 commented Nov 16, 2017

websocket require handshaking with client. without mocking os.urandom, I can not get Sec-Websocket-Key from request.

@mindflayer
Copy link
Owner

I will have a look at it, since I'll be on holiday for the next two weeks.

@mindflayer
Copy link
Owner

mindflayer commented Nov 16, 2017

If you'd like to try something before I'll have a look, I suggest you to start recording a client/server session with Mocket and its vcrpy-ish feature.
Hint for doing it: [@item4] have a look at https://github.com/mindflayer/python-mocket/blob/master/tests/main/test_http.py#L39

@item4
Copy link
Author

item4 commented Nov 18, 2017

I need to make dynamic result.

  1. Client send sec_key via Sec-Websocket-Key header to server (sec_key is random value)
  2. Server response sec_accept(sign by sec_key) via Sec-Websocket-Accept to client (this response contain some headers such as Upgrade: websocket, Connection: upgrade)
  3. Client verify sec_key and sec_accept.
  4. Client use http connect to websocket

@item4
Copy link
Author

item4 commented Nov 18, 2017

truesocket_recording_dir do record response, do not request. I need request header.

@mindflayer
Copy link
Owner

mindflayer commented Dec 11, 2017

Sorry but it's been a tough period at home and I had no chance to look into it. It's true that Mocket has not been designed to process the request but only to provide the correct responses. I'll see what I can do.

@piranna
Copy link

piranna commented Aug 13, 2024

What's the status of this? I'm interested on a WebSocket animal too. How can we move this forward?

@mindflayer
Copy link
Owner

mindflayer commented Aug 13, 2024

What's the status of this? I'm interested on a WebSocket animal too. How can we move this forward?

Hi @piranna, I believe you are more interested in the feature than the example shared above.
Of course, testing websocket transactions is something that was mentioned several times in the past, but I admit I've never spent enough time on it to understand how difficult would be. Let's discuss under the new issue you opened.

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

No branches or pull requests

3 participants