yapi/client/containers/AddInterface/InterfaceTest/InterfaceTest.js

512 lines
16 KiB
JavaScript
Raw Normal View History

2017-07-25 13:34:48 +08:00
import React, { Component } from 'react'
2017-07-25 21:24:12 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2017-07-30 17:12:21 +08:00
import { Button, Input, Select, Card, Alert, Spin, Icon, message } from 'antd'
2017-07-25 13:34:48 +08:00
import { autobind } from 'core-decorators';
import crossRequest from 'cross-request';
2017-07-25 21:24:12 +08:00
import { withRouter } from 'react-router';
2017-07-30 11:10:33 +08:00
import axios from 'axios';
2017-07-25 21:24:12 +08:00
import URL from 'url';
2017-07-25 13:34:48 +08:00
import {
2017-08-08 10:07:55 +08:00
} from '../../../reducer/modules/group.js'
2017-07-25 13:34:48 +08:00
import './InterfaceTest.scss'
2017-07-26 14:54:14 +08:00
const { TextArea } = Input;
2017-07-26 17:53:36 +08:00
const InputGroup = Input.Group;
const Option = Select.Option;
2017-07-26 14:54:14 +08:00
2017-07-25 21:24:12 +08:00
@connect(
state => ({
reqParams: state.addInterface.reqParams,
method: state.addInterface.method,
url: state.addInterface.url,
seqGroup: state.addInterface.seqGroup,
interfaceName: state.addInterface.interfaceName,
interfaceProject: state.addInterface.project
2017-07-28 15:12:55 +08:00
})
2017-07-25 21:24:12 +08:00
)
@withRouter
2017-07-25 13:34:48 +08:00
export default class InterfaceTest extends Component {
static propTypes = {
2017-07-25 21:24:12 +08:00
reqParams: PropTypes.string,
method: PropTypes.string,
url: PropTypes.string,
interfaceName: PropTypes.string,
seqGroup: PropTypes.array,
match: PropTypes.object,
interfaceProject: PropTypes.object
2017-07-25 13:34:48 +08:00
}
state = {
2017-07-26 17:53:36 +08:00
res: '',
2017-07-26 22:01:30 +08:00
method: 'GET',
domains: [],
pathname: '',
query: {},
params: {},
paramsNotJson: false,
headers: {},
2017-07-30 17:12:21 +08:00
currDomain: '',
paramsType: 'from'
2017-07-25 13:34:48 +08:00
}
constructor(props) {
super(props)
}
2017-07-25 21:24:12 +08:00
componentWillMount() {
2017-07-28 17:28:32 +08:00
this.getInterfaceState()
2017-07-26 22:01:30 +08:00
}
2017-07-25 21:24:12 +08:00
2017-07-26 22:01:30 +08:00
componentWillReceiveProps(nextProps) {
2017-07-28 17:28:32 +08:00
this.getInterfaceState(nextProps)
2017-07-25 21:24:12 +08:00
}
2017-07-25 13:34:48 +08:00
@autobind
2017-07-28 17:28:32 +08:00
getInterfaceState(nextProps) {
2017-07-26 22:01:30 +08:00
const props = nextProps || this.props;
const { method, url, seqGroup, interfaceProject } = props;
const { prd_host, basepath, protocol, env } = interfaceProject;
const pathname = (basepath + url).replace(/\/+/g, '/');
const domains = {prd: protocol + '://' + prd_host};
env.forEach(item => {
domains[item.name] = item.domain;
})
2017-07-26 14:54:14 +08:00
2017-07-30 11:10:33 +08:00
const query = [];
2017-07-30 17:12:21 +08:00
let params = [];
2017-07-26 22:01:30 +08:00
let reqParams = this.props.reqParams ? this.props.reqParams : '{}';
let paramsNotJson = false;
try {
2017-07-30 17:12:21 +08:00
reqParams = JSON.parse(reqParams);
// paramsNotJson = false;
2017-07-26 22:01:30 +08:00
} catch (e) {
2017-07-30 17:12:21 +08:00
// paramsNotJson = true;
reqParams = {};
message.error('请求参数不是 JSON 格式');
2017-07-26 22:01:30 +08:00
}
2017-07-26 14:54:14 +08:00
if (method === 'GET') {
Object.keys(reqParams).forEach(key => {
2017-07-26 22:01:30 +08:00
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString();
2017-07-30 11:10:33 +08:00
query.push({key, value})
2017-07-26 14:54:14 +08:00
})
2017-07-26 22:01:30 +08:00
} else if (method === 'POST') {
2017-07-30 17:12:21 +08:00
// params = reqParams;
Object.keys(reqParams).forEach(key => {
const value = typeof reqParams[key] === 'object' ? JSON.stringify(reqParams[key]) : reqParams[key].toString();
query.push({key, value, type: 'text'})
})
2017-07-26 14:54:14 +08:00
}
2017-07-30 17:12:21 +08:00
const headers = []
2017-08-01 14:02:06 +08:00
let contentTypeIndex = -1;
seqGroup.forEach((headerItem, index) => {
2017-07-26 17:53:36 +08:00
if (headerItem.name) {
2017-08-01 14:02:06 +08:00
// TODO 'Content-Type' 排除大小写不同格式影响
if (headerItem.name === 'Content-Type'){
contentTypeIndex = index;
headerItem.value = headerItem.value || 'application/x-www-form-urlencoded';
}
2017-07-30 17:12:21 +08:00
headers.push({name: headerItem.name, value: headerItem.value});
2017-07-26 17:53:36 +08:00
}
2017-07-26 14:54:14 +08:00
})
2017-08-01 14:02:06 +08:00
if (contentTypeIndex === -1) {
headers.push({name: 'Content-Type', value: 'application/x-www-form-urlencoded'});
}
2017-07-26 14:54:14 +08:00
2017-07-26 22:01:30 +08:00
this.setState({
2017-07-28 17:28:32 +08:00
method,
2017-07-26 22:01:30 +08:00
domains,
pathname,
query,
params,
paramsNotJson,
headers,
2017-07-27 17:16:23 +08:00
currDomain: domains.prd,
2017-07-30 17:12:21 +08:00
loading: false,
paramsType: 'form'
2017-07-26 22:01:30 +08:00
});
}
@autobind
2017-07-30 11:10:33 +08:00
requestInterface() {
2017-07-28 17:28:32 +08:00
const { headers, params, currDomain, method, pathname, query } = this.state;
2017-07-26 22:01:30 +08:00
const urlObj = URL.parse(currDomain);
2017-07-26 14:54:14 +08:00
const href = URL.format({
2017-07-26 22:01:30 +08:00
protocol: urlObj.protocol || 'http',
host: urlObj.host,
pathname,
2017-07-30 11:10:33 +08:00
query: this.getQueryObj(query)
2017-07-26 14:54:14 +08:00
});
2017-07-27 17:16:23 +08:00
this.setState({ loading: true })
2017-07-25 13:34:48 +08:00
crossRequest({
2017-07-26 14:54:14 +08:00
url: href,
method,
2017-07-31 10:30:45 +08:00
headers: this.getHeadersObj(headers),
data: this.arrToObj(params),
2017-08-01 14:02:06 +08:00
files: this.getFiles(params),
2017-07-28 15:12:55 +08:00
success: (res) => {
2017-07-27 17:34:46 +08:00
try {
2017-07-28 15:12:55 +08:00
res = JSON.parse(res)
2017-07-27 17:34:46 +08:00
} catch (e) {
2017-07-28 15:12:55 +08:00
null
2017-07-27 17:34:46 +08:00
}
2017-07-26 17:22:12 +08:00
this.setState({res})
2017-07-27 17:16:23 +08:00
this.setState({ loading: false })
2017-07-27 17:34:46 +08:00
},
2017-07-28 15:12:55 +08:00
error: (err) => {
2017-07-27 17:34:46 +08:00
this.setState({res: err || '请求失败'})
this.setState({ loading: false })
2017-07-25 13:34:48 +08:00
}
})
}
2017-07-26 22:01:30 +08:00
@autobind
changeDomain(value) {
2017-07-28 17:28:32 +08:00
this.setState({ currDomain: value });
}
@autobind
selectDomain(value) {
this.setState({ currDomain: value });
2017-07-26 22:01:30 +08:00
}
2017-07-25 13:34:48 +08:00
2017-07-26 22:01:30 +08:00
@autobind
2017-07-30 17:12:21 +08:00
changeHeader(e, index, isName) {
2017-07-26 22:01:30 +08:00
const headers = JSON.parse(JSON.stringify(this.state.headers));
2017-07-30 17:12:21 +08:00
const v = e.target.value;
if (isName) {
headers[index].name = v;
} else {
headers[index].value = v;
}
2017-07-26 22:01:30 +08:00
this.setState({ headers });
}
2017-07-30 17:12:21 +08:00
@autobind
addHeader() {
const { headers } = this.state;
this.setState({headers: headers.concat([{name: '', value: ''}])})
}
@autobind
deleteHeader(index) {
const { headers } = this.state;
this.setState({headers: headers.filter((item, i) => +index !== +i)});
}
2017-08-01 14:02:06 +08:00
@autobind
setContentType(type) {
const headersObj = this.getHeadersObj(this.state.headers);
headersObj['Content-Type'] = type;
this.setState({headers: this.objToArr(headersObj, 'name')})
}
2017-07-25 21:24:12 +08:00
2017-07-26 22:01:30 +08:00
@autobind
2017-07-30 11:10:33 +08:00
changeQuery(e, index, isKey) {
2017-07-26 22:01:30 +08:00
const query = JSON.parse(JSON.stringify(this.state.query));
2017-07-30 11:10:33 +08:00
const v = e.target.value;
if (isKey) {
query[index].key = v;
} else {
query[index].value = v;
}
2017-07-26 22:01:30 +08:00
this.setState({ query });
}
2017-07-30 11:10:33 +08:00
@autobind
addQuery() {
const { query } = this.state;
this.setState({query: query.concat([{key: '', value: ''}])})
}
@autobind
deleteQuery(index) {
const { query } = this.state;
this.setState({query: query.filter((item, i) => +index !== +i)});
}
2017-07-26 17:53:36 +08:00
2017-07-26 22:01:30 +08:00
@autobind
2017-07-30 17:12:21 +08:00
changeParams(e, index, type) {
2017-07-26 22:01:30 +08:00
const params = JSON.parse(JSON.stringify(this.state.params));
2017-07-30 17:12:21 +08:00
switch (type) {
case 'key':
params[index].key = e.target.value
break;
case 'type':
params[index].type = e
break;
case 'value':
2017-08-01 14:02:06 +08:00
if (params[index].type === 'file') {
params[index].value = e.target.id
} else {
params[index].value = e.target.value
}
2017-07-30 17:12:21 +08:00
break;
default:
break;
}
2017-08-01 14:02:06 +08:00
if (type === 'type' && e === 'file') {
this.setContentType('multipart/form-data')
}
2017-07-26 22:01:30 +08:00
this.setState({ params });
}
2017-07-30 17:12:21 +08:00
@autobind
addParams() {
const { params } = this.state;
this.setState({params: params.concat([{key: '', value: '', type: 'text'}])})
}
@autobind
deleteParams(index) {
const { params } = this.state;
this.setState({params: params.filter((item, i) => +index !== +i)});
}
2017-07-25 21:24:12 +08:00
2017-07-28 17:28:32 +08:00
@autobind
changeMethod(value) {
this.setState({ method: value });
}
@autobind
changePath(e) {
const path = e.target.value;
const urlObj = URL.parse(path, true);
this.setState({
2017-08-01 15:53:41 +08:00
query: this.objToArr(urlObj.query),
2017-07-28 17:28:32 +08:00
pathname: urlObj.pathname
})
}
2017-07-30 17:12:21 +08:00
@autobind
changeParamsType(value) {
this.setState({paramsType: value})
}
2017-07-27 10:12:50 +08:00
hasCrossRequestPlugin() {
const dom = document.getElementById('y-request');
return dom.getAttribute('key') === 'yapi';
}
2017-07-26 17:53:36 +08:00
2017-08-01 14:02:06 +08:00
objToArr(obj, key, value) {
const keyName = key || 'key';
const valueName = value || 'value';
const arr = []
Object.keys(obj).forEach((_key) => {
if (_key) {
arr.push({[keyName]: _key, [valueName]: obj[_key]});
}
})
return arr;
}
2017-07-31 10:30:45 +08:00
arrToObj(arr) {
const obj = {};
arr.forEach(item => {
2017-08-01 14:02:06 +08:00
if (item.key && item.type !== 'file') {
2017-07-31 10:30:45 +08:00
obj[item.key] = item.value || '';
}
})
return obj;
}
2017-08-01 14:02:06 +08:00
getFiles(params) {
const files = {};
params.forEach(item => {
if (item.key && item.type === 'file') {
files[item.key] = item.value
}
})
return files;
}
2017-07-30 11:10:33 +08:00
getQueryObj(query) {
const queryObj = {};
query.forEach(item => {
if (item.key) {
queryObj[item.key] = item.value || '';
}
})
return queryObj;
}
2017-07-31 10:30:45 +08:00
getHeadersObj(headers) {
const headersObj = {};
headers.forEach(item => {
if (item.name && item.value) {
headersObj[item.name] = item.value;
}
})
return headersObj;
}
2017-07-30 11:10:33 +08:00
2017-08-01 14:02:06 +08:00
@autobind
fileChange(e, index) {
console.log(e)
console.log(index)
}
2017-07-26 22:01:30 +08:00
render () {
2017-07-28 17:28:32 +08:00
const { interfaceName } = this.props;
2017-07-30 17:12:21 +08:00
const { method, domains, pathname, query, headers, params, currDomain, paramsType } = this.state;
2017-07-27 10:12:50 +08:00
const hasPlugin = this.hasCrossRequestPlugin();
2017-07-30 11:10:33 +08:00
const search = decodeURIComponent(URL.format({query: this.getQueryObj(query)}));
console.log(axios)
window.axios = axios
2017-07-25 21:24:12 +08:00
2017-07-25 13:34:48 +08:00
return (
2017-07-26 14:54:14 +08:00
<div className="interface-test">
2017-07-27 10:12:50 +08:00
<div style={{padding: '0 20%'}}>
{ hasPlugin ? '' :
<Alert
message={
<div>
温馨提示当前正在使用接口测试服务请安装我们为您免费提供的&nbsp;
<a
target="blank"
href="https://chrome.google.com/webstore/detail/cross-request/cmnlfmgbjmaciiopcgodlhpiklaghbok?hl=en-US"
>
测试增强插件 [点击获取]
</a>
</div>
}
type="warning"
/>
}
</div>
2017-07-26 14:54:14 +08:00
<div className="interface-name">{interfaceName}</div>
2017-07-28 17:28:32 +08:00
{/* url */}
2017-07-26 14:54:14 +08:00
<div className="req-part">
2017-07-26 17:53:36 +08:00
<div className="req-row href">
2017-07-27 12:13:18 +08:00
<InputGroup compact style={{display: 'inline-block', width: 680, border: 0, background: '#fff', marginBottom: -4}}>
2017-07-30 11:10:33 +08:00
<Input value="Method" disabled style={{display: 'inline-block', width: 80, border: 0, background: '#fff'}} />
<Input value="Domain" disabled style={{display: 'inline-block', width: 300, border: 0, background: '#fff'}} />
<Input value="Basepath + Url + [Query]" disabled style={{display: 'inline-block', width: 300, border: 0, background: '#fff'}} />
2017-07-27 12:13:18 +08:00
</InputGroup>
2017-07-26 17:53:36 +08:00
<InputGroup compact style={{display: 'inline-block', width: 680}}>
2017-07-28 17:28:32 +08:00
<Select value={method} style={{display: 'inline-block', width: 80}} onChange={this.changeMethod} >
<Option value="GET">GET</Option>
<Option value="POST">POST</Option>
</Select>
<Select value={currDomain} mode="combobox" filterOption={() => true} style={{display: 'inline-block', width: 300}} onChange={this.changeDomain} onSelect={this.selectDomain}>
2017-07-26 17:53:36 +08:00
{
2017-07-28 17:28:32 +08:00
Object.keys(domains).map((key, index) => (<Option value={domains[key]} key={index}>{key + '' + domains[key]}</Option>))
2017-07-26 17:53:36 +08:00
}
</Select>
2017-07-28 17:28:32 +08:00
<Input value={pathname + search} onChange={this.changePath} spellCheck="false" style={{display: 'inline-block', width: 300}} />
2017-07-26 17:53:36 +08:00
</InputGroup>
2017-07-27 17:16:23 +08:00
<Button
2017-07-30 11:10:33 +08:00
onClick={this.requestInterface}
2017-07-27 17:16:23 +08:00
type="primary"
style={{marginLeft: 10}}
loading={this.state.loading}
>发送</Button>
2017-07-27 20:59:54 +08:00
<span style={{fontSize: 12, color: 'rgba(0, 0, 0, 0.25)'}}>请求测试真实接口</span>
2017-07-26 14:54:14 +08:00
</div>
2017-07-28 17:28:32 +08:00
2017-07-30 11:10:33 +08:00
<Card title="Query" noHovering style={{marginTop: 10}}>
{
query.map((item, index) => {
return (
<div key={index}>
<Input value={item.key} onChange={e => this.changeQuery(e, index, true)} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={item.value} onChange={e => this.changeQuery(e, index)} style={{display: 'inline-block', width: 200, margin: 10}} />
<Icon type="close" className="icon-btn" onClick={() => this.deleteQuery(index)} />
</div>
)
})
}
2017-07-31 10:30:45 +08:00
<Button type="primary" icon="plus" onClick={this.addQuery} style={{margin: 10}}>Add query parameter</Button>
2017-07-26 18:24:06 +08:00
</Card>
2017-07-30 17:12:21 +08:00
<Card title="HEADERS" noHovering style={{marginTop: 10}} >
2017-07-28 17:28:32 +08:00
<div className="req-row headers">
2017-07-26 18:24:06 +08:00
{
2017-07-30 17:12:21 +08:00
headers.map((item, index) => {
2017-07-26 22:01:30 +08:00
return (
<div key={index}>
2017-07-30 17:12:21 +08:00
<Input value={item.name} onChange={e => this.changeHeader(e, index, true)} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={item.value} onChange={e => this.changeHeader(e, index)} style={{display: 'inline-block', width: 200, margin: 10}} />
<Icon type="close" className="icon-btn" onClick={() => this.deleteHeader(index)} />
2017-07-26 22:01:30 +08:00
</div>
)
})
}
2017-07-31 10:30:45 +08:00
<Button type="primary" icon="plus" onClick={this.addHeader} style={{margin: 10}}>Add header</Button>
2017-07-26 22:01:30 +08:00
</div>
</Card>
2017-07-30 17:12:21 +08:00
<Card title="Body" noHovering style={{marginTop: 10}}>
2017-07-26 22:01:30 +08:00
<div className="req-row params">
2017-07-31 10:30:45 +08:00
<div>
<Select style={{margin: 10, float: 'right'}} defaultValue={paramsType} onChange={this.changeParamsType} className={method === 'POST' ? 'floatfix' : 'floatfix hidden'}>
<Option value="text">Text</Option>
<Option value="file">File</Option>
<Option value="form">Form</Option>
</Select>
</div>
2017-07-30 17:12:21 +08:00
{ method === 'POST' && paramsType !== 'form' && paramsType !== 'file' &&
<div>
<TextArea
value={params}
style={{margin: 10}}
autosize={{ minRows: 2, maxRows: 6 }}
></TextArea>
<div>{paramsType}</div>
</div>
}
{
method === 'POST' && paramsType === 'form' && (
<div>
{
params.map((item, index) => {
return (
<div key={index}>
<Input value={item.key} onChange={e => this.changeParams(e, index, 'key')} style={{display: 'inline-block', width: 200, margin: 10}} />
[<Select value={item.type} onChange={e => this.changeParams(e, index, 'type')}>
<Option value="file">File</Option>
<Option value="text">Text</Option>
</Select>]{' = '}
{item.type === 'file' ?
2017-08-01 14:02:06 +08:00
<Input type="file" id={'file_' + index} onChange={e => this.changeParams(e, index, 'value')} multiple style={{display: 'inline-block', width: 200, margin: 10}} /> :
2017-07-30 17:12:21 +08:00
<Input value={item.value} onChange={e => this.changeParams(e, index, 'value')} style={{display: 'inline-block', width: 200, margin: 10}} />
}
</div>
)
})
}
2017-07-31 10:30:45 +08:00
<Button type="primary" icon="plus" onClick={this.addParams} style={{margin: 10}}>Add form parameter</Button>
2017-07-30 17:12:21 +08:00
</div>
)
}
{
method === 'POST' && paramsType === 'file' && (
<div>
<Input type="file"></Input>
</div>
)
}
{
method !== 'POST' && (
2017-07-31 10:30:45 +08:00
<div style={{margin: 10}}>GET 请求没有 Body</div>
2017-07-30 17:12:21 +08:00
)
2017-07-26 18:24:06 +08:00
}
</div>
</Card>
2017-07-25 21:24:12 +08:00
</div>
2017-07-26 22:01:30 +08:00
<Card title="返回结果" noHovering style={{marginTop: 10}}>
2017-07-27 17:16:23 +08:00
<Spin spinning={this.state.loading}>
<div className="res-part">
2017-07-27 20:59:54 +08:00
<div style={{padding: 10}}>
2017-07-27 17:16:23 +08:00
<TextArea
2017-07-27 17:34:46 +08:00
value={typeof this.state.res === 'object' ? JSON.stringify(this.state.res, null, 2) : this.state.res.toString()}
2017-07-27 17:16:23 +08:00
autosize={{ minRows: 2, maxRows: 6 }}
></TextArea>
</div>
2017-07-26 18:24:06 +08:00
</div>
2017-07-27 17:16:23 +08:00
</Spin>
2017-07-26 18:24:06 +08:00
</Card>
2017-07-25 13:34:48 +08:00
</div>
)
}
}