From 0fc1944ad4eabacdedec19d22c9516072a5c699d Mon Sep 17 00:00:00 2001 From: yumiguan <41277086+yumiguan@users.noreply.github.com> Date: Thu, 25 May 2023 21:33:42 +0800 Subject: [PATCH] feature/core-keep-origin-request-data (#757) * Core support keep origin request data; Inspector add keep_origin_data switch * update version * Update code * remove unused func * recover config * add case --- e2e_tests/test_core.py | 7 + e2e_tests/test_flow_editor.py | 9 + frontend/src/api/inspector.js | 14 -- frontend/src/store/inspector.js | 8 + frontend/src/store/settings.js | 20 ++ frontend/src/views/inspector/ButtonBar.vue | 114 ++++++----- lyrebird/mock/handlers/handler_context.py | 7 + .../handlers/http_data_helper/__init__.py | 25 +++ .../content_encoding/__init__.py | 14 +- .../content_encoding/default.py | 4 + .../http_data_helper/content_encoding/gzip.py | 5 + .../http_data_helper/content_type/__init__.py | 13 +- .../http_data_helper/content_type/default.py | 5 + .../http_data_helper/content_type/form.py | 6 +- .../content_type/javascript.py | 4 + .../http_data_helper/content_type/json.py | 4 + .../http_data_helper/content_type/text.py | 4 + lyrebird/version.py | 2 +- tests/test_mock_server.py | 181 +++++++++++++++++- 19 files changed, 383 insertions(+), 63 deletions(-) diff --git a/e2e_tests/test_core.py b/e2e_tests/test_core.py index cf8805360..5783afac6 100644 --- a/e2e_tests/test_core.py +++ b/e2e_tests/test_core.py @@ -32,6 +32,13 @@ def test_json(lyrebird, mock_server): assert r.text == hashlib.md5(mock_server.api_post.encode() + data.encode()).hexdigest() +def test_javascript(lyrebird, mock_server): + data = "(function(){\n var name = 'Lyrebird';\n })();" + headers = {"Content-Type": "application/javascript"} + r = requests.post(url=lyrebird.uri_mock + mock_server.api_post, data=data, headers=headers) + assert r.text == hashlib.md5(mock_server.api_post.encode() + data.encode()).hexdigest() + + def test_text(lyrebird, mock_server): data = "asdasdasd" headers = {"Content-Type": "text/plain"} diff --git a/e2e_tests/test_flow_editor.py b/e2e_tests/test_flow_editor.py index af7875735..7e4159ece 100644 --- a/e2e_tests/test_flow_editor.py +++ b/e2e_tests/test_flow_editor.py @@ -38,6 +38,15 @@ def test_flow_editor_json(lyrebird_with_args, mock_server): assert r.text == hashlib.md5(mock_server.api_post.encode() + data.encode()).hexdigest() +def test_flow_editor_js(lyrebird_with_args, mock_server): + lyrebird_with_args.start(checker_path=flow_editor_path) + + data = "console.log('hello world')" + headers = {"Content-Type": "application/javascript"} + r = requests.post(url=lyrebird_with_args.uri_mock + mock_server.api_post, data=data, headers=headers) + assert r.text == hashlib.md5(mock_server.api_post.encode() + data.encode()).hexdigest() + + def test_flow_editor_text(lyrebird_with_args, mock_server): lyrebird_with_args.start(checker_path=flow_editor_path) data = "asdasdasd" diff --git a/frontend/src/api/inspector.js b/frontend/src/api/inspector.js index 8045a7aaa..52f8644c3 100644 --- a/frontend/src/api/inspector.js +++ b/frontend/src/api/inspector.js @@ -1,19 +1,5 @@ import axios from 'axios' -export const getDiffModeStatus = () => { - return axios({ - url: '/api/diffmode' - }) -} - -export const setDiffModeStatus = (status) => { - return axios({ - url: '/api/diffmode', - data: { status }, - method: 'PUT' - }) -} - export const getFlowFilters = () => { return axios({ url: '/api/flowfilter' diff --git a/frontend/src/store/inspector.js b/frontend/src/store/inspector.js index 75f8c8e74..b514dd630 100644 --- a/frontend/src/store/inspector.js +++ b/frontend/src/store/inspector.js @@ -11,6 +11,8 @@ export default { focusedFlowDetail: null, originFlowList: [], recordMode: '', + diffMode: 'normal', + isRequestKeepOriginData: false, flowFilters: [], selectedFlowFilter: {} }, @@ -54,6 +56,12 @@ export default { setRecordMode (state, recordMode) { state.recordMode = recordMode }, + setDiffMode (state, diffMode) { + state.diffMode = diffMode + }, + setIsRequestKeepOriginData (state, isRequestKeepOriginData) { + state.isRequestKeepOriginData = isRequestKeepOriginData + }, setFlowFilters (state, flowFilters) { state.flowFilters = flowFilters }, diff --git a/frontend/src/store/settings.js b/frontend/src/store/settings.js index 2d6ed6276..e2b42f3e4 100644 --- a/frontend/src/store/settings.js +++ b/frontend/src/store/settings.js @@ -2,8 +2,10 @@ import * as api from '@/api' import { bus } from '@/eventbus' var configCommitMap = [ + {'name': 'mock.mode', 'commit': 'setDiffMode'}, {'name': 'mock.data.showLabel', 'commit': 'setIsLabelDisplay'}, {'name': 'mock.data.tree.closeReload', 'commit': 'setIsCloseReloadWhenEnter'}, + {'name': 'mock.request.keep_origin_data', 'commit': 'setIsRequestKeepOriginData'}, {'name': 'mock.data.tree.undeletableId', 'commit': 'concatTreeUndeletableId'}, {'name': 'mock.data.detail.stickyTopKey', 'commit': 'concatStickyTopKey'}, {'name': 'mock.data.detail.undeletableKey', 'commit': 'concatUndeletableKey'}, @@ -73,6 +75,24 @@ export default { .catch(error => { bus.$emit('msg.error', `Update config failed ${error.data.message}`) }) + }, + commitAndupdateConfigByKey({ dispatch, commit }, { command, val }) { + let updateConfig = {} + for (const config of configCommitMap) { + if (config.commit == command) { + updateConfig[config.name] = val + commit(command, val) + break + } + } + api.updateConfigByKey(updateConfig) + .then(_ => { + dispatch('loadConfig') + bus.$emit('msg.success', `Update config success!`) + }) + .catch(error => { + bus.$emit('msg.error', `Update config failed ${error.data.message}`) + }) } } } diff --git a/frontend/src/views/inspector/ButtonBar.vue b/frontend/src/views/inspector/ButtonBar.vue index 813c044dc..63a60c656 100644 --- a/frontend/src/views/inspector/ButtonBar.vue +++ b/frontend/src/views/inspector/ButtonBar.vue @@ -29,30 +29,8 @@ Clear - - Diff Mode - - - - Get the proxy response while the request is mocked - - - - - Mock Group + Mock Group + + + + + + + + + + + + + + Diff Mode + Get the proxy response while the request is mocked + + + + + + + + + + Origin request body + Keep origin request body + + + + + @@ -158,7 +184,6 @@