def test_close(self, m_client, m_os, WebSocketWriter): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) writer = WebSocketWriter.return_value = mock.Mock() reader = resp.connection.reader.set_parser.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', loop=self.loop)) self.assertFalse(resp.closed) msg = websocket.Message(websocket.MSG_CLOSE, b'', b'') reader.read.return_value = asyncio.Future(loop=self.loop) reader.read.return_value.set_result(msg) res = self.loop.run_until_complete(resp.close()) writer.close.assert_called_with(1000, b'') self.assertTrue(resp.closed) self.assertTrue(res) self.assertIsNone(resp.exception()) # idempotent res = self.loop.run_until_complete(resp.close()) self.assertFalse(res) self.assertEqual(writer.close.call_count, 1)
def test_close_exc2(self, m_client, m_os, WebSocketWriter): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) writer = WebSocketWriter.return_value = mock.Mock() resp.connection.reader.set_parser.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', loop=self.loop)) self.assertFalse(resp.closed) exc = ValueError() writer.close.side_effect = exc self.loop.run_until_complete(resp.close()) self.assertTrue(resp.closed) self.assertIs(resp.exception(), exc) resp._closed = False writer.close.side_effect = asyncio.CancelledError() self.assertRaises(asyncio.CancelledError, self.loop.run_until_complete, resp.close())
def test_reader_read_exception(self, m_client, m_os, WebSocketWriter): hresp = mock.Mock() hresp.status = 101 hresp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(hresp) WebSocketWriter.return_value = mock.Mock() reader = hresp.connection.reader.set_parser.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', loop=self.loop)) exc = ValueError() reader.read.return_value = asyncio.Future(loop=self.loop) reader.read.return_value.set_exception(exc) msg = self.loop.run_until_complete(resp.receive()) self.assertEqual(msg.tp, aiohttp.MsgType.error) self.assertIs(resp.exception(), exc)
def test_close_exc2(self, m_client, m_os, WebSocketWriter): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) writer = WebSocketWriter.return_value = mock.Mock() resp.connection.reader.set_parser.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', loop=self.loop)) self.assertFalse(resp.closed) exc = ValueError() writer.close.side_effect = exc self.loop.run_until_complete(resp.close()) self.assertTrue(resp.closed) self.assertIs(resp.exception(), exc) resp._closed = False writer.close.side_effect = asyncio.CancelledError() self.assertRaises(asyncio.CancelledError, self.loop.run_until_complete, resp.close())
def test_ws_connect_err_upgrade(self, m_client, m_os): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: 'test', hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) with self.assertRaises(errors.WSServerHandshakeError) as ctx: self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', protocols=('t1', 't2', 'chat'), loop=self.loop)) self.assertEqual(ctx.exception.message, 'Invalid upgrade header')
def test_send_data_type_errors(self, m_client, m_os, WebSocketWriter): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) WebSocketWriter.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', loop=self.loop)) self.assertRaises(TypeError, resp.send_str, b's') self.assertRaises(TypeError, resp.send_bytes, 'b')
def test_ws_connect_global_loop(self, m_client, m_os): asyncio.set_event_loop(self.loop) resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) resp = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org')) self.assertIs(resp._loop, self.loop) asyncio.set_event_loop(None)
def test_send_data_type_errors(self, m_client, m_os, WebSocketWriter): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) WebSocketWriter.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', loop=self.loop)) self.assertRaises(TypeError, resp.send_str, b's') self.assertRaises(TypeError, resp.send_bytes, 'b')
def test_ws_connect(self, m_client, m_os): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, hdrs.SEC_WEBSOCKET_PROTOCOL: 'chat' } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) res = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', protocols=('t1', 't2', 'chat'), loop=self.loop)) self.assertIsInstance(res, websocket_client.ClientWebSocketResponse) self.assertEqual(res.protocol, 'chat')
def test_ws_connect_err_upgrade(self, m_client, m_os): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: 'test', hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) with self.assertRaises(errors.WSServerHandshakeError) as ctx: self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', protocols=('t1', 't2', 'chat'), loop=self.loop)) self.assertEqual( ctx.exception.message, 'Invalid upgrade header')
def test_ws_connect(self, m_client, m_os): resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, hdrs.SEC_WEBSOCKET_PROTOCOL: 'chat' } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(resp) res = self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', protocols=('t1', 't2', 'chat'), loop=self.loop)) self.assertIsInstance(res, websocket_client.ClientWebSocketResponse) self.assertEqual(res.protocol, 'chat')
def test_ws_connect_custom_response(self, m_req, m_os): class CustomResponse(websocket_client.ClientWebSocketResponse): def read(self, decode=False): return 'customized!' resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_req.return_value = asyncio.Future(loop=self.loop) m_req.return_value.set_result(resp) res = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', ws_response_class=CustomResponse, loop=self.loop)) self.assertEqual(res.read(), 'customized!')
def test_reader_read_exception(self, m_client, m_os, WebSocketWriter): hresp = mock.Mock() hresp.status = 101 hresp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_client.request.return_value = asyncio.Future(loop=self.loop) m_client.request.return_value.set_result(hresp) WebSocketWriter.return_value = mock.Mock() reader = hresp.connection.reader.set_parser.return_value = mock.Mock() resp = self.loop.run_until_complete( websocket_client.ws_connect('http://test.org', loop=self.loop)) exc = ValueError() reader.read.return_value = asyncio.Future(loop=self.loop) reader.read.return_value.set_exception(exc) msg = self.loop.run_until_complete(resp.receive()) self.assertEqual(msg.tp, aiohttp.MsgType.error) self.assertIs(resp.exception(), exc)
def test_ws_connect_custom_response(self, m_req, m_os): class CustomResponse(websocket_client.ClientWebSocketResponse): def read(self, decode=False): return 'customized!' resp = mock.Mock() resp.status = 101 resp.headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_ACCEPT: self.ws_key, } m_os.urandom.return_value = self.key_data m_req.return_value = asyncio.Future(loop=self.loop) m_req.return_value.set_result(resp) res = self.loop.run_until_complete( websocket_client.ws_connect( 'http://test.org', ws_response_class=CustomResponse, loop=self.loop)) self.assertEqual(res.read(), 'customized!')