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

feat: basic websocket support #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kvalev
Copy link
Contributor

@kvalev kvalev commented Jan 26, 2020

This PR adds a very very basic (and highly likely buggy) support for mocking websockets.

@kvalev
Copy link
Contributor Author

kvalev commented Jan 26, 2020

@h2non the PR is missing a lot of tests, documentation and a bit of a polish, but let me know what you think so far.

@kvalev kvalev requested a review from h2non January 26, 2020 18:59
@kvalev kvalev force-pushed the websocket-interceptor branch 2 times, most recently from f8984a8 to 7d84960 Compare January 26, 2020 19:07
req.url = url

# TODO does this work multithreaded?!?
self._mock = self.engine.match(req)
Copy link
Owner

@h2non h2non Jan 28, 2020

Choose a reason for hiding this comment

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

It will not work. Mutex synchronization is required to make it safe. However, I feel that's another feature that has to be implemented in a separate PR.

Copy link
Owner

@h2non h2non left a comment

Choose a reason for hiding this comment

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

LGTM! This looks promising and ambitious!
Adding some extra tests would be great to cover more complex scenarios.

It would be great it can support as well this client, which works on top of asyncio:
https://github.com/aaugustin/websockets

At user API design level, I wonder about how the mock DSL can be improved to mock up exchanged frames with arbitrary responses provided by a routine.

I guess one obvious solution would be allowing the user to pass a function with a signature like:

def framer(frame, request) -> MockFrame:
   // Do magic things
   return mock

At a user definition API level, I'm inclined to overload the body() method to support this case:

def frame_mock(frame, request) -> MockFrame:
   // Do magic things
   return mock

(pook.get('ws://some-non-existing.org')
    .reply(204)
    .body(frame_mock)

Alternatively, implement a custom method for frames specific case:

(pook.get('ws://some-non-existing.org')
    .reply(204)
    .frames(frames_mock)

Opinions?

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 this pull request may close these issues.

None yet

2 participants