Here are the examples of the python api aiohttp.WSMessage taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
7 Examples
3
Example 1
Project: aiohttp Source File: test_websocket_parser.py
def test_close_frame_info(out, parser):
def parse_frame(buf):
yield
return (1, WSMsgType.CLOSE, b'0112345')
with mock.patch('aiohttp._ws_impl.parse_frame') as m_parse_frame:
m_parse_frame.side_effect = parse_frame
next(parser)
parser.send(b'')
res = out._buffer[0]
assert res == (WSMessage(WSMsgType.CLOSE, 12337, '12345'), 0)
3
Example 2
Project: aiohttp Source File: test_websocket_parser.py
def test_continuation(out, parser):
cur = 0
def parse_frame(buf, cont=False):
nonlocal cur
yield
if cur == 0:
cur = 1
return (0, WSMsgType.TEXT, b'line1')
else:
return (1, WSMsgType.CONTINUATION, b'line2')
with mock.patch('aiohttp._ws_impl.parse_frame') as m_parse_frame:
m_parse_frame.side_effect = parse_frame
next(parser)
parser.send(b'')
parser.send(b'')
res = out._buffer[0]
assert res == (WSMessage(WSMsgType.TEXT, 'line1line2', ''), 10)
3
Example 3
Project: aiohttp Source File: test_web_websocket.py
@asyncio.coroutine
def test_receive_str_nonstring(make_request):
req = make_request('GET', '/')
ws = WebSocketResponse()
yield from ws.prepare(req)
@asyncio.coroutine
def receive():
return WSMessage(WSMsgType.BINARY, b'data', b'')
ws.receive = receive
with pytest.raises(TypeError):
yield from ws.receive_str()
3
Example 4
Project: aiohttp Source File: test_web_websocket.py
@asyncio.coroutine
def test_receive_bytes_nonsbytes(make_request):
req = make_request('GET', '/')
ws = WebSocketResponse()
yield from ws.prepare(req)
@asyncio.coroutine
def receive():
return WSMessage(WSMsgType.TEXT, 'data', b'')
ws.receive = receive
with pytest.raises(TypeError):
yield from ws.receive_bytes()
0
Example 5
Project: aiohttp Source File: test_websocket_parser.py
def test_continuation_with_ping(out, parser):
frames = [
(0, WSMsgType.TEXT, b'line1'),
(0, WSMsgType.PING, b''),
(1, WSMsgType.CONTINUATION, b'line2'),
]
def parse_frame(buf, cont=False):
yield
return frames.pop(0)
with mock.patch('aiohttp._ws_impl.parse_frame') as m_parse_frame:
m_parse_frame.side_effect = parse_frame
next(parser)
parser.send(b'')
parser.send(b'')
parser.send(b'')
res = out._buffer[0]
assert res == (WSMessage(WSMsgType.PING, b'', ''), 0)
res = out._buffer[1]
assert res == (WSMessage(WSMsgType.TEXT, 'line1line2', ''), 10)
0
Example 6
Project: aiohttp Source File: test_websocket_parser.py
def test_continuation_with_close(out, parser):
frames = [
(0, WSMsgType.TEXT, b'line1'),
(0, WSMsgType.CLOSE,
build_close_frame(1002, b'test', noheader=True)),
(1, WSMsgType.CONTINUATION, b'line2'),
]
def parse_frame(buf, cont=False):
yield
return frames.pop(0)
with mock.patch('aiohttp._ws_impl.parse_frame') as m_parse_frame:
m_parse_frame.side_effect = parse_frame
next(parser)
parser.send(b'')
parser.send(b'')
parser.send(b'')
res = out._buffer[0]
assert res, (WSMessage(WSMsgType.CLOSE, 1002, 'test'), 0)
res = out._buffer[1]
assert res == (WSMessage(WSMsgType.TEXT, 'line1line2', ''), 10)
0
Example 7
Project: aiohttp Source File: test_websocket_parser.py
def test_continuation_with_close_empty(out, parser):
frames = [
(0, WSMsgType.TEXT, b'line1'),
(0, WSMsgType.CLOSE, b''),
(1, WSMsgType.CONTINUATION, b'line2'),
]
def parse_frame(buf, cont=False):
yield
return frames.pop(0)
with mock.patch('aiohttp._ws_impl.parse_frame') as m_parse_frame:
m_parse_frame.side_effect = parse_frame
next(parser)
parser.send(b'')
parser.send(b'')
parser.send(b'')
res = out._buffer[0]
assert res, (WSMessage(WSMsgType.CLOSE, 0, ''), 0)
res = out._buffer[1]
assert res == (WSMessage(WSMsgType.TEXT, 'line1line2', ''), 10)