feat: 增加返回结果验证功能

This commit is contained in:
zwjamnsss 2017-08-31 15:18:37 +08:00
parent 8a374d6028
commit 589ca5a9dd
5 changed files with 38 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Mock from 'mockjs'
import { Button, Input, Select, Card, Alert, Spin, Icon, Collapse, Radio, Tooltip, message } from 'antd'
import { autobind } from 'core-decorators';
import crossRequest from 'cross-request';
@ -36,7 +37,8 @@ export default class Run extends Component {
caseEnv: '',
bodyType: '',
bodyOther: '',
loading: false
loading: false,
validRes: ''
}
constructor(props) {
@ -145,15 +147,22 @@ export default class Run extends Component {
files: bodyType === 'form' ? this.getFiles(bodyForm) : {},
success: (res, header) => {
try {
if (header['content-type'].indexOf('application/json') !== -1) {
if (header['content-type'] && header['content-type'].indexOf('application/json') !== -1) {
res = typeof res === 'object' ? res : JSON.parse(res)
}
header = typeof header === 'object' ? header : JSON.parse(header)
} catch (e) {
message.error(e.message)
}
const { res_body, res_body_type } = this.props.data;
let validRes = '';
if (res_body && res_body_type === 'json' && typeof res === 'object') {
validRes = Mock.valid(JSON.parse(res_body), res)
console.log(validRes)
}
message.success('请求完成')
this.setState({res, resHeader: header})
this.setState({res, resHeader: header, validRes})
this.setState({ loading: false })
this.bindAceEditor()
},
@ -416,7 +425,7 @@ export default class Run extends Component {
render () {
const { method, domains, pathParam, pathname, query, headers, bodyForm, caseEnv, bodyType, resHeader, loading } = this.state;
const { method, domains, pathParam, pathname, query, headers, bodyForm, caseEnv, bodyType, resHeader, loading, validRes } = this.state;
const hasPlugin = this.hasCrossRequestPlugin();
const isResJson = resHeader && resHeader['content-type'] && resHeader['content-type'].indexOf('application/json') !== -1
let path = pathname;
@ -614,6 +623,12 @@ export default class Run extends Component {
value={typeof this.state.res === 'object' ? JSON.stringify(this.state.res, null, 2) : this.state.res.toString()}
autosize={{ minRows: 2, maxRows: 10 }}
></TextArea>
<div style={{display: validRes ? '' : 'none', marginTop: 6, fontWeight: 'bold'}}>返回Body验证结果</div>
<TextArea
style={{display: validRes ? '' : 'none'}}
value={validRes && (validRes.length ? JSON.stringify(validRes.map(item => item.message), null, 2) : '验证通过')}
autosize={{ minRows: 2, maxRows: 10 }}
></TextArea>
</Panel>
<Panel header="HEADERS" key="1" >
{/*<TextArea

View File

@ -2,6 +2,7 @@ import React, { Component } from 'react'
import { connect } from 'react-redux';
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'
import { Link } from 'react-router-dom'
import axios from 'axios'
import { message } from 'antd'
import { fetchInterfaceColList, setColData, fetchCaseData } from '../../../../reducer/modules/interfaceCol'
@ -122,7 +123,12 @@ export default class InterfaceCaseContent extends Component {
const data = Object.assign({}, currCase, currProject, {_id: currCase._id});
return (
<div style={{padding: '6px 0'}}>
<h1 style={{marginLeft: 8}}>{currCase.casename}</h1>
<h1 style={{marginLeft: 8}}>
{currCase.casename}
<span style={{marginLeft: 6, fontSize: 12}}>
<Link to={`/project/${currProject._id}/interface/api/${currCase.interface_id}`}>对应接口</Link>
</span>
</h1>
<div>
<Postman data={data} type="case" saveTip="更新保存修改" save={this.updateCase} ref={this.savePostmanRef} />
</div>

View File

@ -2,6 +2,7 @@ import React, { Component } from 'react'
import { connect } from 'react-redux';
import PropTypes from 'prop-types'
import { withRouter } from 'react-router'
import { Link } from 'react-router-dom'
import { Table, Tooltip } from 'antd'
import { fetchInterfaceColList, fetchCaseList, setColData } from '../../../../reducer/modules/interfaceCol'
import { formatTime } from '../../../../common.js'
@ -82,7 +83,7 @@ export default class InterfaceColContent extends Component {
render: (path, record) => {
return (
<Tooltip title="跳转到对应接口">
<a href={`/project/${record.project_id}/interface/api/${record.interface_id}`}>{path || 'fdsf'}</a>
<Link to={`/project/${record.project_id}/interface/api/${record.interface_id}`}>{path}</Link>
</Tooltip>
)
}

View File

@ -301,12 +301,13 @@ class interfaceColController extends baseController{
result.method = data.method;
result.req_body_type = data.req_body_type;
result.req_headers = data.req_headers;
result.res_body = data.res_body;
result.res_body_type = data.res_body_type;
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form)
result.req_query = this.handleParamsValue(data.req_query, result.req_query)
result.req_params = this.handleParamsValue(data.req_params, result.req_params)
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 400, e.message)

View File

@ -649,27 +649,29 @@ var interfaceColController = function (_baseController) {
result.method = data.method;
result.req_body_type = data.req_body_type;
result.req_headers = data.req_headers;
result.res_body = data.res_body;
result.res_body_type = data.res_body_type;
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form);
result.req_query = this.handleParamsValue(data.req_query, result.req_query);
result.req_params = this.handleParamsValue(data.req_params, result.req_params);
ctx.body = _yapi2.default.commons.resReturn(result);
_context6.next = 28;
_context6.next = 30;
break;
case 25:
_context6.prev = 25;
case 27:
_context6.prev = 27;
_context6.t0 = _context6['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 400, _context6.t0.message);
case 28:
case 30:
case 'end':
return _context6.stop();
}
}
}, _callee6, this, [[0, 25]]);
}, _callee6, this, [[0, 27]]);
}));
function getCase(_x6) {