fix: 当接口返回 body 不是 json,高级 mock 功能报错

This commit is contained in:
suxiaoxin 2017-11-23 16:32:15 +08:00
parent 449e707a2e
commit d7e6e7d761
3 changed files with 13 additions and 14 deletions

View File

@ -25,7 +25,7 @@ const roleAction = {
exports.isJson = function(json){
if(!json) return false;
try{
json = JSON.parse(json);
json = json5.parse(json);
return json;
}catch(e){
return false;
@ -260,7 +260,12 @@ exports.handleJson = handleJson;
exports.handleParamsValue = handleParamsValue;
exports.getMockText = (mockTpl) => {
try{
return JSON.stringify(Mock.mock(MockExtra(json5.parse(mockTpl), {})), null, " ")
}catch(err){
return ''
}
}
/**

View File

@ -285,17 +285,15 @@ export default class Run extends Component {
let reqBody;
if (bodyType === 'form') {
reqBody = this.arrToObj(bodyForm)
} else {
} else if(bodyType === 'json'){
reqBody = isJson(bodyOther);
if (reqBody === false) {
if (bodyType === 'json' && HTTP_METHOD[method].request_body) {
return message.error('请求 Body 的 json 格式有误')
}
reqBody = bodyOther;
} else {
reqBody = handleJson(reqBody, this.handleValue)
}
}else{
reqBody = bodyOther;
}
this.setState({ loading: true })
@ -545,11 +543,6 @@ export default class Run extends Component {
}, 0);
}
// @autobind
// fileChange(e, index) {
// console.log(e)
// console.log(index)
// }
@autobind
onTestSwitched(checked) {

View File

@ -168,6 +168,7 @@ class InterfaceEditForm extends Component {
values.method = this.state.method;
values.req_params = values.req_params || [];
values.req_headers = values.req_headers || [];
let isfile = false, isHavaContentType = false;
if (values.req_body_type === 'form') {
values.req_body_form.forEach((item) => {