Websocket server for testing
Project description
Fake Websocket Server
Fake in the sense that it's actually a working server (created using
websockets
library) but one that that exists briefly to allow
integration testing.
Installation
pip install fakewsserver
Usage
One message sent, one received
from fakewsserver import assert_communication
async with assert_communication(
port=12345,
communication=[('hello', 'there')],
):
async with websockets.connect('ws://localhost:12345') as client:
await client.send('hello')
response = await client.recv()
assert response == 'there'
Utilities
When working with APIs it might be convenient to capture real messages to write tests against them. Here's a simple example (using the fake server; in real life we would connect to a real websocket server).
from fakewsserver import respond_with, write_communication
send = 'Hello there'
receive = ['General', 'Kenobi']
file = StringIO()
async with respond_with(port=12345, responses=receive):
async with websockets.connect('ws://localhost:12345') as client:
await write_communication(
to_send=send,
client=client,
file=file,
timeout=None,
num_responses=len(receive),
)
file.seek(0)
data_raw = file.read()
data = json.loads(data_raw)
assert data == [
dict(
type='send',
data='Hello there',
),
dict(
type='receive',
data='General',
),
dict(
type='receive',
data='Kenobi',
),
]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fakewsserver-0.1.1.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for fakewsserver-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ca29c247fc21efb8045b35a0d888d6e0ae527633865e0decdd9e83e6fea6daf |
|
MD5 | def66cd361ebcbe04dbb301a3bbadc1c |
|
BLAKE2b-256 | b4c10affe5cdfd82a00a981d7840f544f47a05e673563dd4d1bb749fce11f827 |