fix: interface body view bug and add number filter in the parmas value

This commit is contained in:
suxiaoxin 2017-11-30 14:40:09 +08:00
parent 65fccfeb87
commit 426df1b8ff
7 changed files with 58 additions and 34 deletions

View File

@ -257,28 +257,34 @@ function handleValueWithFilter(context){
}
}
function handleFilter(str, match, context){
match = match.trim();
try{
let a= filter(match, handleValueWithFilter(context))
return a;
}catch(err){
return str;
}
}
function handleParamsValue (val, context={}){
const variableRegexp = /\{\{\s*((?:\$|\@)?.+?)\}\}/g;
const variableRegexp = /\{\{\s*([^}]+?)\}\}/g;
if (!val || typeof val !== 'string') {
return val;
}
val = val.trim();
if (!/^\{\{[\s\S]+\}\}$/.test(val)) {
val = val.trim()
let match = val.match(/^\{\{([^\}]+)\}\}$/);
if (!match){
if(val[0] ==='@' || val[0] === '$'){
val = '{{' + val + '}}';
}else{
return val;
return handleFilter(val, val, context);
}
}else{
return handleFilter(val, match[1], context);
}
return val.replace(variableRegexp, function(str, match){
match = match.trim();
try{
return filter(match, handleValueWithFilter(context))
}catch(err){
return str;
}
})
return val.replace(variableRegexp, handleFilter)
}
exports.handleJson = handleJson;

View File

@ -19,7 +19,8 @@ const METHODS_LIST = [
{ name: 'unbase64', type: false, params: [], desc: 'base64解密' },
{ name: 'concat', type: true, component: "input", params: [], desc: '连接字符串' },
{ name: 'lconcat', type: true, component: "input", params: [], desc: '左连接' },
{ name: 'upper', type: false, desc: '所有字母变成大写' }
{ name: 'upper', type: false, desc: '所有字母变成大写' },
{ name: 'number', type: false, desc: '字符串转换为数字类型' }
]

View File

@ -455,18 +455,21 @@ export default class Run extends Component {
</Spin>
</Tabs.TabPane>
<Tabs.TabPane style={{display: this.props.type === 'case' ? '' : 'none'}} className="response-test" tab={<Tooltip title="测试脚本,可断言返回结果,使用方法请查看文档">Test</Tooltip>} key="test">
<h3 style={{margin: '5px'}}>
&nbsp;是否开启:&nbsp;
<Switch checked={this.state.enable_script} onChange={e => this.setState({ enable_script: e })} />
</h3>
<p style={{margin: '10px'}}>Test 脚本只有做自动化测试才执行</p>
<AceEditor
onChange={this.onOpenTest}
className="case-script"
data={this.state.test_script}
/>
</Tabs.TabPane>
{this.props.type === 'case' ?
<Tabs.TabPane className="response-test" tab={<Tooltip title="测试脚本,可断言返回结果,使用方法请查看文档">Test</Tooltip>} key="test">
<h3 style={{margin: '5px'}}>
&nbsp;是否开启:&nbsp;
<Switch checked={this.state.enable_script} onChange={e => this.setState({ enable_script: e })} />
</h3>
<p style={{margin: '10px'}}>Test 脚本只有做自动化测试才执行</p>
<AceEditor
onChange={this.onOpenTest}
className="case-script"
data={this.state.test_script}
/>
</Tabs.TabPane>
: null }
</Tabs>

View File

@ -256,6 +256,10 @@ class View extends Component {
let requestShow = (dataSource&& dataSource.length) || (req_dataSource && req_dataSource.length) || (this.props.curData.req_query && this.props.curData.req_query.length) || (this.props.curData.req_body_other) || (this.props.curData.req_body_form && this.props.curData.req_body_form.length);
let methodColor = variable.METHOD_COLOR[this.props.curData.method ? this.props.curData.method.toLowerCase() : "get"];
let bodyShow = (this.props.curData.req_body_other) || (this.props.curData.req_body_form && this.props.curData.req_body_form.length);
// statusColor = statusColor[this.props.curData.status?this.props.curData.status.toLowerCase():"undone"];
let h = this.countEnter(this.props.curData.req_body_other);
const aceEditor = <div style={{ display: this.props.curData.req_body_other && (this.props.curData.req_body_type !== "form" ) ? "block" : "none" }} className="colBody">
@ -315,7 +319,7 @@ class View extends Component {
</div> : ""}
<div style={{display: this.props.curData.method && HTTP_METHOD[this.props.curData.method.toUpperCase()].request_body ? '' : 'none'}}>
<h3 className="col-title">Body:</h3>
<h3 style={{display: bodyShow? '' : 'none'}} className="col-title">Body:</h3>
{ aceEditor }
{
this.req_body_form(this.props.curData.req_body_type, this.props.curData.req_body_form)

View File

@ -80,6 +80,10 @@ const stringHandles = {
length: function(str){
return str.length;
},
number: function(str){
return !isNaN(str) ? +str : str;
}
}

View File

@ -19,5 +19,9 @@ test('handleParamsValue', t => {
t.is(handleParamsValue(json), json)
t.is(handleParamsValue(' {{ dkkdjf }}'), 'dkkdjf')
t.is(handleParamsValue(' {{ dkkdjf | upper | kkk }}'), '{{ dkkdjf | upper | kkk }}')
t.is(handleParamsValue('aaa {{ aaaa | upper }} bbbb'), 'aaa AAAA bbbb')
t.is(handleParamsValue('aaa {{ aaaa | upper }} bbbb,aaa {{ aaaa | upper }} bbbb'), 'aaa AAAA bbbb,aaa AAAA bbbb')
t.is(handleParamsValue("{{aaaa | length}}"), 4);
t.is(handleParamsValue("{{4444 | number}}"), 4444);
});

View File

@ -2,11 +2,13 @@ import test from 'ava';
const rewire = require("rewire");
const lib = rewire('../common/lib.js');
const initPlugins = lib.initPlugins;
const plugin = rewire('../common/plugin.js')
const initPlugins = plugin.initPlugins;
test('initPlugins', t=>{
lib.__set__("getPluginConfig", function(){
plugin.__set__("getPluginConfig", function(){
return {
server: true,
client: true
@ -27,7 +29,7 @@ test('initPlugins', t=>{
})
test('initPlugins2', t=>{
lib.__set__("getPluginConfig", function(){
plugin.__set__("getPluginConfig", function(){
return {
server: true,
client: false
@ -48,7 +50,7 @@ test('initPlugins2', t=>{
})
test('initPlugins3', t=>{
lib.__set__("getPluginConfig", function(){
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: true
@ -64,7 +66,7 @@ test('initPlugins3', t=>{
})
test('initPlugins3', t=>{
lib.__set__("getPluginConfig", function(){
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: true
@ -94,7 +96,7 @@ test('initPlugins3', t=>{
})
test('initPlugins3', t=>{
lib.__set__("getPluginConfig", function(){
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: false