def test_send(self): client = WebsocketClient(self.session, "wss://localhost:0") with patch.object(client, "ws") as mock_ws: client.send("foo") client.send(b"foo", ABNF.OPCODE_BINARY) client.send_json({"foo": "bar", "baz": "qux"}) self.assertEqual(mock_ws.send.call_args_list, [ call("foo", ABNF.OPCODE_TEXT), call(b"foo", ABNF.OPCODE_BINARY), call("{\"foo\":\"bar\",\"baz\":\"qux\"}", ABNF.OPCODE_TEXT), ])
def subject(self): with patch.object(Streamlink, "load_builtin_plugins"), \ patch.object(Streamlink, "resolve_url") as mock_resolve_url, \ patch.object(Streamlink, "resolve_url_no_redirect") as mock_resolve_url_no_redirect: session = Streamlink() session.load_plugins( os.path.join(os.path.dirname(__file__), "plugin")) with patch("streamlink_cli.main.os.geteuid", create=True, new=Mock(return_value=1000)), \ patch("streamlink_cli.main.streamlink", session), \ patch("streamlink_cli.main.CONFIG_FILES", []), \ patch("streamlink_cli.main.setup_streamlink"), \ patch("streamlink_cli.main.setup_plugins"), \ patch("streamlink_cli.main.setup_http_session"), \ patch("streamlink_cli.main.setup_signals"), \ patch("streamlink_cli.main.setup_options") as mock_setup_options: with self.assertRaises(SystemExit) as cm: streamlink_cli.main.main() self.assertEqual(cm.exception.code, 0) mock_resolve_url.assert_not_called() mock_resolve_url_no_redirect.assert_not_called() mock_setup_options.assert_not_called()
def test_close(self): class WebsocketClientSubclass(WebsocketClient): running = Event() status = False def run(self): self.status = self.running.wait(4) client = WebsocketClientSubclass(self.session, "wss://localhost:0") with patch.object(client.ws, "close") as mock_ws_close: mock_ws_close.side_effect = lambda *_, **__: client.running.set() client.start() client.close(reason="foo") self.assertFalse(client.is_alive()) self.assertTrue(client.status) self.assertEqual(mock_ws_close.call_args_list, [ call( status=STATUS_NORMAL, reason=b"foo", timeout=3 ) ])
def test_filtered_closed(self): thread, reader, writer, segments = self.subject( [Playlist( 0, [SegmentFiltered(0), SegmentFiltered(1)])]) # mock the reader thread's filter_event.wait method, so that the main thread can wait on its call filter_event_wait_called = Event() orig_wait = reader.filter_event.wait def mocked_wait(*args, **kwargs): filter_event_wait_called.set() return orig_wait(*args, **kwargs) mock = patch.object(reader.filter_event, "wait", side_effect=mocked_wait) mock.start() # write first filtered segment and trigger the filter_event's lock self.assertTrue(reader.filter_event.is_set(), "Doesn't let the reader wait if not filtering") self.await_write() self.assertFalse(reader.filter_event.is_set(), "Lets the reader wait if filtering") # make reader read (no data available yet) thread.read_wait.set() # before calling reader.close(), wait until reader thread's filter_event.wait was called filter_event_wait_called.wait() # close stream while reader is waiting for filtering to end thread.reader.close() thread.read_done.wait() thread.read_done.clear() self.assertEqual(thread.data, [b""], "Stops reading on stream close") self.assertFalse(thread.error, "Is not a read timeout on stream close") mock.stop()
def test_args_and_proxy(self): self.session.set_option("http-proxy", "https://*****:*****@hostname:1234") client = WebsocketClient( self.session, "wss://localhost:0", subprotocols=["sub1", "sub2"], cookie="cookie", sockopt=("sockopt1", "sockopt2"), sslopt={"ssloptkey": "ssloptval"}, host="customhost", origin="customorigin", suppress_origin=True, ping_interval=30, ping_timeout=4, ) self.assertEqual(client.ws.url, "wss://localhost:0") self.assertEqual(client.ws.subprotocols, ["sub1", "sub2"]) self.assertEqual(client.ws.cookie, "cookie") with patch.object(client.ws, "run_forever") as mock_ws_run_forever: client.start() client.join(1) self.assertFalse(client.is_alive()) self.assertEqual(mock_ws_run_forever.call_args_list, [ call( sockopt=("sockopt1", "sockopt2"), sslopt={"ssloptkey": "ssloptval"}, host="customhost", origin="customorigin", suppress_origin=True, ping_interval=30, ping_timeout=4, proxy_type="https", http_proxy_host="hostname", http_proxy_port=1234, http_proxy_auth=("username", "password") ) ])
if __name__ == '__main__': from tests.mock import patch import sys import eventlet from eventlet import hubs with patch.object(hubs, 'notify_opened') as mock_func: eventlet.monkey_patch(builtins=True) with open(__file__, 'r') as f: mock_func.assert_called_with(f.fileno()) if sys.version_info.major == 2: with file(__file__, 'r') as f: mock_func.assert_called_with(f.fileno()) print('pass')