extras.websocket — Simple Websocket Server (Deprecated)¶
An HTTP YellowService used to easily mock and test websocket connections.
Deprecated since version 0.6.6: Use webserver instead.
Note
Requires the websocket extra. For more information, see our installation guide.
service = WebsocketService()
# Let's write "Hello!" to the websocket upon connection to the "/hello"
# endpoint:
service.add("Hello!", "/hello")
# We'll also make a simple echo endpoint:
@service.route("/echo")
def echo(websocket):
data = None
while True:
# Yield sends out the data, and waits for incoming data.
data = yield data
service.start()
- class extras.websocket.WebsocketService[source]¶
A Simple WS server wrapped as
YellowService.Inherits from
YellowService.- property container_url: str[source]¶
Full URL to access the WS Server from inside a locally-running container (including port).
- add(side_effect, path=None, *, regex=None)[source]¶
Add a new route to the service.
- Parameters:
side_effect –
The response for when the route is accessed, can be one of:
A primitive value, one of :
str,bytes,bytearrayormemoryview, which will be sent to the client and the connection closed.The
Noneprimiitve, indicating that the websocket should close.An iterable of any combination of the above primitive types, which will be sent one by one to the client, waiting for messages between any two.
A
Callablewhich will be called with the Simple Websocket object as the argument. The function should return a primitive value, the value will be sent to the client and the connection closed.A
Callablewhich returns a generator. The callable will be called with the Simple Websocket object as the argument. The generator should yield primitive values, which will be sent to the client and between receiving data.
path (str | None) – The path to match the route against. Omit if using regex.
regex (
str|Pattern[str] |None) – The path pattern to match the route against. Omit if using path.
Note
exactly one of
pathorregexmust be specified.- Raises:
RuntimeError – If the path already exists.
- route(path=None, *, regex=None)[source]¶
A decorator to add a route to the service.
@service.route("/echo") def echo(websocket): data = None while True: data = yield data
path and regex parameters are the same as
add().
- set(side_effect, path=None, *, regex=None)[source]¶
Set a new route or replace an existing route to the service.
Parameters are the same as
add().
- patch(side_effect, path=None, regex=None) ContextManager[source]¶
returns a context manager that adds a route to the service on entry and removes it on exit.
Parameters are the same as
add().