Skip to content

Commit

Permalink
feature/core-mock-ssr (#772)
Browse files Browse the repository at this point in the history
* Core support SSR, put mock data in request body

* Update version
  • Loading branch information
yumiguan authored Jul 28, 2023
1 parent bb8c762 commit 67fbf5d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions frontend/src/store/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
recordMode: '',
diffMode: 'normal',
isRequestKeepOriginData: false,
isSsrMockInBody: false,
flowFilters: [],
selectedFlowFilter: ''
},
Expand Down Expand Up @@ -62,6 +63,9 @@ export default {
setIsRequestKeepOriginData (state, isRequestKeepOriginData) {
state.isRequestKeepOriginData = isRequestKeepOriginData
},
setIsSsrMockInBody (state, isSsrMockInBody) {
state.isSsrMockInBody = isSsrMockInBody
},
setFlowFilters (state, flowFilters) {
state.flowFilters = flowFilters
},
Expand Down
1 change: 1 addition & 0 deletions frontend/src/store/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var configCommitMap = [
{'name': 'mock.data.tree.asynchronous', 'commit': 'setIsTreeLoadAsync'},
{'name': 'mock.data.shownConfig', 'commit': 'setIsDisplayConfiguration'},
{'name': 'mock.request.keep_origin_data', 'commit': 'setIsRequestKeepOriginData'},
{'name': 'mock.request.ssr.mock_in_body', 'commit': 'setIsSsrMockInBody'},
{'name': 'mock.data.tree.undeletableId', 'commit': 'concatTreeUndeletableId'},
{'name': 'mock.data.detail.stickyTopKey', 'commit': 'concatStickyTopKey'},
{'name': 'mock.data.detail.undeletableKey', 'commit': 'concatUndeletableKey'},
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/views/inspector/ButtonBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@
<v-list-item-subtitle>Keep origin request body</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>

<v-list-item>
<v-list-item-action>
<v-switch v-model="isSsrMockInBody"/>
</v-list-item-action>

<v-list-item-content>
<v-list-item-title>Open SSR</v-list-item-title>
<v-list-item-subtitle>Put mock data in request body instead of response data</v-list-item-subtitle>
</v-list-item-content>
</v-list-item>
</v-list>

</v-menu>
Expand Down Expand Up @@ -220,6 +231,18 @@ export default {
})
}
},
isSsrMockInBody: {
get () {
return this.$store.state.inspector.isSsrMockInBody
},
set (val) {
this.$store.dispatch('commitAndupdateConfigByKey', {
'command': 'setIsSsrMockInBody',
'isShowMessage': true,
val
})
}
},
isEmptySelectedFlow () {
return this.$store.state.inspector.selectedFlows.length === 0
},
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/views/inspector/FlowList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
<span>Get the server response while the request is mocked</span>
</v-tooltip>

<v-tooltip bottom v-if="item.status === 'ssr'">
<template v-slot:activator="{ on, attrs }">
<span v-bind="attrs" v-on="on" class="flow-list-item-source">
<v-chip label small
class="flow-list-item-tag"
color="#FFF7E2"
text-color="#D69600"
>SSR</v-chip>
</span>
</template>
<span>Put mock data in request body instead of response data</span>
</v-tooltip>

<v-tooltip bottom v-if="getRequestEditors(item).length">
<template v-slot:activator="{ on, attrs }">
<span v-bind="attrs" v-on="on" class="flow-list-item-source">
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from lyrebird.config.diff_mode import SettingDiffMode
from lyrebird.config.checker_switch import SettingCheckerSwitch

from .keywords import CONFIG_TREE_SHOW_CONFIG, CONFIG_TREE_LOAD_CHILDREN_ASYNC
from .keywords import *

logger = nlog.get_logger()

Expand Down
4 changes: 4 additions & 0 deletions lyrebird/config/keywords.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
CONFIG_TREE_SHOW_CONFIG = 'mock.data.shownConfig'
CONFIG_TREE_LOAD_CHILDREN_ASYNC= 'mock.data.tree.asynchronous'
CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY = 'mock.request.ssr.mock_in_body'
CONFIG_MOCK_REQUEST_SSR_MOCK_BODY_KEY = 'mock.request.ssr.mock_body_key'
CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY_PARAM = 'mock.request.ssr.mock_in_body.param'

4 changes: 4 additions & 0 deletions lyrebird/mock/blueprints/apis/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def get_flow_list_by_filter(filter_obj):
# Change status
if item['request']['headers'].get(headers.MITMPROXY_COMMAND):
info['status'] = item['request']['headers'][headers.MITMPROXY_COMMAND]
# SSR
if item['request']['headers'].get('lyrebird') == 'mock':
info['status'] = 'ssr'

req_list.append(info)
return req_list

Expand Down
24 changes: 24 additions & 0 deletions lyrebird/mock/handlers/mock_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from lyrebird.mock import context
from lyrebird.log import get_logger
from .mock_data_helper import MockDataHelper
from lyrebird.application import config
from lyrebird.config import CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY, CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY_PARAM, CONFIG_MOCK_REQUEST_SSR_MOCK_BODY_KEY


logger = get_logger()
Expand All @@ -26,6 +28,28 @@ def handle(self, handler_context):
logger.info(
f'<Mock> Hit Group:{activated_group.get("name")} - Data:{hit_data["name"]} \nURL: {handler_context.flow["request"]["url"]}\nGroupID:{activated_group["id"]} DataID:{hit_data["id"]}')

if config.get(CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY):
key = config.get(CONFIG_MOCK_REQUEST_SSR_MOCK_IN_BODY_PARAM, 'is_ssr')
is_open_ssr = handler_context.flow['request']['query'].get(key)
if is_open_ssr:
if 'data' not in handler_context.flow['request']:
handler_context.flow['request']['data'] = {}

resp_key = config.get(CONFIG_MOCK_REQUEST_SSR_MOCK_BODY_KEY, 'lyrebird_mock_response')

handler_context.flow['request']['data'][resp_key] = hit_data['response'].get('data', '')

handler_context.set_request_edited()
handler_context.flow['request']['headers']['lyrebird'] = 'mock'

handler_context.add_flow_action({
'id': 'mock_ssr',
'name': Path(__file__).name,
'group_id': activated_group['id'],
'mock_id': hit_data['id']
})
return

handler_context.flow['response']['code'] = hit_data['response']['code']
handler_context.flow['response']['headers'] = {k:v for k,v in hit_data['response']['headers'].items()}
handler_context.flow['response']['data'] = hit_data['response'].get('data', '')
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
IVERSION = (2, 15, 2)
IVERSION = (2, 16, 0)
VERSION = ".".join(str(i) for i in IVERSION)
LYREBIRD = "Lyrebird " + VERSION

0 comments on commit 67fbf5d

Please sign in to comment.