feat: body support mock and ver params

This commit is contained in:
suxiaoxin 2017-10-18 17:46:37 +08:00
parent 110c9491e4
commit 9f2c47cad5
4 changed files with 109 additions and 47 deletions

View File

@ -19,6 +19,16 @@ const roleAction = {
'viewGroup': 'guest'
}
exports.isJson = function(json){
if(!json) return false;
try{
json = JSON.parse(json);
return json;
}catch(e){
return false;
}
}
exports.checkAuth = (action, role)=>{
return Roles[roleAction[action]] <= Roles[role];
}
@ -73,39 +83,6 @@ exports.debounce = (func, wait) => {
};
exports.simpleJsonPathParse = function (key, json){
if(!key || typeof key !== 'string' || key.indexOf('$.') !== 0 || key.length <= 2){
return null;
}
let keys = key.substr(2).split(".");
keys = keys.filter(item=>{
return item;
})
for(let i=0, l = keys.length; i< l; i++){
try{
let m = keys[i].match(/(.*?)\[([0-9]+)\]/)
if(m){
json = json[m[1]][m[2]];
}else{
json = json[keys[i]];
}
}catch(e){
json = null;
break;
}
}
return json;
}
exports.handleMockWord =(word) =>{
if(!word || typeof word !== 'string' || word[0] !== '@') return word;
return Mock.mock(word);
}
// 从 Javascript 对象中选取随机属性
exports.pickRandomProperty = (obj) => {
let result;
@ -175,3 +152,39 @@ exports.entries = (obj) => {
}
return res;
}
function simpleJsonPathParse(key, json){
if(!key || typeof key !== 'string' || key.indexOf('$.') !== 0 || key.length <= 2){
return null;
}
let keys = key.substr(2).split(".");
keys = keys.filter(item=>{
return item;
})
for(let i=0, l = keys.length; i< l; i++){
try{
let m = keys[i].match(/(.*?)\[([0-9]+)\]/)
if(m){
json = json[m[1]][m[2]];
}else{
json = json[keys[i]];
}
}catch(e){
json = null;
break;
}
}
return json;
}
function handleMockWord(word) {
if(!word || typeof word !== 'string' || word[0] !== '@') return word;
return Mock.mock(word);
}
exports.simpleJsonPathParse = simpleJsonPathParse;
exports.handleMockWord = handleMockWord;

View File

@ -10,7 +10,7 @@ import URL from 'url';
const MockExtra = require('common/mock-extra.js')
import './Postman.scss';
import json5 from 'json5'
import { handleMockWord } from '../../common.js'
import { handleMockWord, isJson } from '../../common.js'
function json_parse(data) {
try {
@ -214,6 +214,18 @@ export default class Run extends Component {
pathname: urlObj.pathname ? URL.resolve(urlObj.pathname, '.' + path) : path,
query: this.getQueryObj(query)
});
let reqBody;
if(bodyType === 'form'){
reqBody = this.arrToObj(bodyForm)
}else{
let resBody = isJson(bodyOther);
if(resBody === false){
resBody = bodyOther;
}else{
reqBody = this.handleJson(resBody)
}
}
this.setState({ loading: true })
let that = this;
@ -221,7 +233,7 @@ export default class Run extends Component {
url: href,
method,
headers: this.getHeadersObj(headers),
data: bodyType === 'form' ? this.arrToObj(bodyForm) : bodyOther,
data: reqBody,
files: bodyType === 'form' ? this.getFiles(bodyForm) : {},
file: bodyType === 'file' ? 'single-file' : null,
success: (res, header, third) => {
@ -275,7 +287,6 @@ export default class Run extends Component {
}
},
error: (err, header, third) => {
console.log('err', third);
this.setState({
resStatusCode: third.res.status,
resStatusText: third.res.statusText
@ -490,6 +501,22 @@ export default class Run extends Component {
return headersObj;
}
handleJson = (data)=>{
if(!data){
return data;
}
if(typeof data === 'string'){
return handleMockWord(data);
}else if(typeof data === 'object'){
for(let i in data){
data[i] = this.handleJson(data[i]);
}
}else{
return data;
}
return data;
}
bindAceEditor = () => {
mockEditor({
container: 'res-body-pretty',

View File

@ -7,8 +7,7 @@ import { Tooltip, Icon, Button, Spin, Modal, message ,Select} from 'antd'
import { fetchInterfaceColList, fetchCaseList, setColData } from '../../../../reducer/modules/interfaceCol'
import HTML5Backend from 'react-dnd-html5-backend';
import { DragDropContext } from 'react-dnd';
import { handleMockWord, simpleJsonPathParse } from '../../../../common.js'
// import { formatTime } from '../../../../common.js'
import { isJson, handleMockWord, simpleJsonPathParse } from '../../../../common.js'
import * as Table from 'reactabular-table';
import * as dnd from 'reactabular-dnd';
import * as resolve from 'table-resolver';
@ -202,13 +201,23 @@ class InterfaceColContent extends Component {
result.url = href;
result.method = interfaceData.method;
result.headers = that.getHeadersObj(interfaceData.req_headers);
result.body = interfaceData.req_body_type === 'form' ? that.arrToObj(interfaceData.req_body_form) : interfaceData.req_body_other;
if(interfaceData.req_body_type === 'form'){
result.body = that.arrToObj(interfaceData.req_body_form)
}else{
let reqBody = isJson(interfaceData.req_body_other);
if(reqBody === false){
result.body = this.handleValue(interfaceData.req_body_other)
}else{
result.body = JSON.stringify(this.handleJson(reqBody))
}
}
window.crossRequest({
url: href,
method: interfaceData.method,
headers: that.getHeadersObj(interfaceData.req_headers),
data: interfaceData.req_body_type === 'form' ? that.arrToObj(interfaceData.req_body_form) : interfaceData.req_body_other,
data: result.body,
success: (res, header) => {
res = json_parse(res);
result.res_header = header;
@ -253,18 +262,29 @@ class InterfaceColContent extends Component {
})
}
handleVarWord(val) {
return simpleJsonPathParse(val, this.records)
handleJson = (data)=>{
if(!data){
return data;
}
if(typeof data === 'string'){
return this.handleValue(data);
}else if(typeof data === 'object'){
for(let i in data){
data[i] = this.handleJson(data[i]);
}
}else{
return data;
}
return data;
}
handleValue(val) {
handleValue = (val) => {
if (!val || typeof val !== 'string') {
return val;
} else if (val[0] === '@') {
return handleMockWord(val);
} else if (val.indexOf('$.') === 0) {
return this.handleVarWord(val);
return simpleJsonPathParse(val, this.records);
}
return val;
}
@ -280,6 +300,8 @@ class InterfaceColContent extends Component {
return obj;
}
getQueryObj = (query) => {
query = query || [];
const queryObj = {};

View File

@ -34,7 +34,7 @@ class interfaceCase extends baseModel {
test_status: {type: String, enum: ['ok', 'invalid', 'error', '']},
test_report: [],
test_res_header: Schema.Types.Mixed,
mock_verify: {type: Boolean, default: true}
mock_verify: {type: Boolean, default: false}
};
}