From 69771bd0c77b8593584fa68aa78f2743891bb10c Mon Sep 17 00:00:00 2001 From: ye zhao Date: Thu, 3 Nov 2022 10:39:23 +0800 Subject: [PATCH] lyrebird v3.10.0 :rm mitmproxy from install require and support python3.10 (#688) * support py3.10; rm mitmproxy from install require * update tests requirements * Fix dm api code 400 * Fix UT: conf-api 400 * update e2e tests * Add gh-pages publish action * actions/checkout@2 * actions/checkout@v3 * Fix gh-pages actions path error * update gh-pages job name * Test PAT * use new gh-pages plugin * add new key github token * use JamesIves/github-pages-deploy-action@v3 * update actions id * add username for gh-pages publish action * update gh-pages action * update gh-pages action * Add permissions for gh-pages action * update github actions job name * Add github token permissions setting * update gh-pages action * Add mitm installer * Dockerfile: install mitmproxy into a special pythonpath; Fix e2e tests * Add py3.11 support * rm python 3.11 support * rm mitm installer from cmdline * rm unused import * Fix code review comments * Fix activate API error * Add UT for datamanager api * Fix no_mitm args error --- lyrebird/installer.py | 20 -------------------- lyrebird/manager.py | 12 +++++------- lyrebird/mitm/mitm_installer.py | 2 +- lyrebird/mitm/proxy_server.py | 1 - lyrebird/mock/blueprints/apis/mock.py | 10 ++++++++-- tests/test_dm_api.py | 27 +++++++++++++++++---------- 6 files changed, 31 insertions(+), 41 deletions(-) delete mode 100644 lyrebird/installer.py diff --git a/lyrebird/installer.py b/lyrebird/installer.py deleted file mode 100644 index d8386bad8..000000000 --- a/lyrebird/installer.py +++ /dev/null @@ -1,20 +0,0 @@ -from lyrebird.mitm.proxy_server import LyrebirdProxyServer -from click import secho - - -def install_mitm(): - LyrebirdProxyServer().download_mitmproxy() - - -installer = { - 'mitm': install_mitm -} - - -def install(extension_name): - global installer - install_func = installer.get(extension_name) - if install_func: - install_func() - else: - secho(f'Install failed. extension "{extension_name}" not found.') diff --git a/lyrebird/manager.py b/lyrebird/manager.py index 14b4e37b0..ce4b5128a 100644 --- a/lyrebird/manager.py +++ b/lyrebird/manager.py @@ -24,11 +24,10 @@ from lyrebird.notice_center import NoticeCenter from lyrebird.plugins import PluginManager from lyrebird.mitm.proxy_server import LyrebirdProxyServer -from lyrebird.mitm import init_mitm from lyrebird.task import BackgroundTaskServer from lyrebird.base_server import MultiProcessServerMessageDispatcher from lyrebird import utils -from lyrebird import installer + logger = log.get_logger() @@ -151,9 +150,6 @@ def thread_excepthook(args): if args.sub_command == 'gen': logger.debug('EXEC: Plugin project generator') gen(args) - elif args.sub_command == 'install': - logger.debug('EXEC: Installer') - installer.install(args.extension_name) else: logger.debug('EXEC: LYREBIRD START') run(args) @@ -183,9 +179,11 @@ def run(args: argparse.Namespace): application.server['task'] = BackgroundTaskServer() # Start mitmproxy server - conf_no_mitm = application._cm.config.get('no_mitm', None) + # if set --no-mitm in commandline , skip start proxy server + # if set proxy.no_mitm in config file, skip start proxy server + conf_no_mitm = application._cm.config.get('proxy.no_mitm', False) args_no_mitm = args.no_mitm - if conf_no_mitm is None: + if args_no_mitm: should_start_mitm = not args_no_mitm else: should_start_mitm = not conf_no_mitm diff --git a/lyrebird/mitm/mitm_installer.py b/lyrebird/mitm/mitm_installer.py index cfb970c28..412efb065 100644 --- a/lyrebird/mitm/mitm_installer.py +++ b/lyrebird/mitm/mitm_installer.py @@ -74,7 +74,7 @@ def download_mitmproxy(): resp = requests.get(download_url, stream=True) content_length = int(resp.headers.get('content-length')) - click.secho(f'\nmitmdupm not found\nStart downloading mitmproxy: {download_url}') + click.secho(f'\nmitmdump not found\nStart downloading mitmproxy: {download_url}') with click.progressbar(length=content_length) as bar, tempfile.NamedTemporaryFile('w+b') as tempf: for chunk in resp.iter_content(4*2048): tempf.write(chunk) diff --git a/lyrebird/mitm/proxy_server.py b/lyrebird/mitm/proxy_server.py index ff9c087b1..831b4af2c 100644 --- a/lyrebird/mitm/proxy_server.py +++ b/lyrebird/mitm/proxy_server.py @@ -90,7 +90,6 @@ def run(self, queue, config, *args, **kwargs): # Init logger log.init(config) logger = log.get_logger() - # TODO mitm_path = kwargs.get('mitm_path') self.start_mitmdump(config, logger, mitm_path) diff --git a/lyrebird/mock/blueprints/apis/mock.py b/lyrebird/mock/blueprints/apis/mock.py index 92a6417ab..7ad9518ff 100644 --- a/lyrebird/mock/blueprints/apis/mock.py +++ b/lyrebird/mock/blueprints/apis/mock.py @@ -127,8 +127,14 @@ def get(self): def put(self, group_id=None, action=None): if action == 'activate': # Only one group could be activated - if request.is_json and request.json.get('info'): - context.application.data_manager.activate(group_id, info=request.json.get('info')) + if request.is_json: + try: + info = request.json.get('info') + context.application.data_manager.activate(group_id, info=info) + except Exception: + # If a request have json content-type but dose not have body. + # Handle flask exception when parse json failed. + context.application.data_manager.activate(group_id) else: context.application.data_manager.activate(group_id) elif action == 'deactivate': diff --git a/tests/test_dm_api.py b/tests/test_dm_api.py index 89bf4420e..abfd22f50 100644 --- a/tests/test_dm_api.py +++ b/tests/test_dm_api.py @@ -155,18 +155,18 @@ def test_data_post(client): 'id': 'dataB-UUID', 'name': 'dataB', 'parent_id': 'root', - 'data': + 'data': { - 'id': 'dataB-UUID', - 'name': 'dataB', - 'rule': { - 'request.data.sort': None, - 'request.url': '(?=.*search)' - }, - 'request': { - 'url': 'http://unittest.com/api/detail' + 'id': 'dataB-UUID', + 'name': 'dataB', + 'rule': { + 'request.data.sort': None, + 'request.url': '(?=.*search)' + }, + 'request': { + 'url': 'http://unittest.com/api/detail' + } } - } }) assert resp.json['code'] == 1000 @@ -195,6 +195,13 @@ def test_data_activate_with_info(client): assert resp.json['code'] == 1000 +def test_data_activate_without_body(client): + data_id = 'dataA-UUID' + action = 'activate' + resp = client.put(f'/api/mock/{data_id}/{action}', headers={'Content-Type': 'application/json'}) + assert resp.json['code'] == 1000 + + def test_data_deactivate(client): data_id = 'dataA-UUID' action = 'deactivate'