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

311 lines
9.8 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-27 17:16:23 +08:00
import { Button, Input, Select, Card, Alert, Spin } 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';
import URL from 'url';
2017-07-25 13:34:48 +08:00
import {
} from '../../../actions/group.js'
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
}),
{
}
)
@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: {},
search: '',
currDomain: ''
2017-07-25 13:34:48 +08:00
}
constructor(props) {
super(props)
}
2017-07-25 21:24:12 +08:00
componentWillMount() {
2017-07-26 22:01:30 +08:00
this.interfacePropsToState()
}
2017-07-25 21:24:12 +08:00
2017-07-26 22:01:30 +08:00
componentWillReceiveProps(nextProps) {
this.interfacePropsToState(nextProps)
2017-07-25 21:24:12 +08:00
}
2017-07-25 13:34:48 +08:00
@autobind
2017-07-26 22:01:30 +08:00
interfacePropsToState(nextProps) {
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-26 17:53:36 +08:00
const query = {};
2017-07-26 22:01:30 +08:00
let params = {};
let reqParams = this.props.reqParams ? this.props.reqParams : '{}';
let paramsNotJson = false;
try {
reqParams = JSON.parse(reqParams)
paramsNotJson = false;
} catch (e) {
paramsNotJson = true;
}
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-26 14:54:14 +08:00
query[key] = value;
})
2017-07-26 22:01:30 +08:00
} else if (method === 'POST') {
params = reqParams;
2017-07-26 14:54:14 +08:00
}
2017-07-26 17:53:36 +08:00
const headers = {}
2017-07-26 14:54:14 +08:00
seqGroup.forEach((headerItem) => {
2017-07-26 17:53:36 +08:00
if (headerItem.name) {
headers[headerItem.name] = headerItem.value;
}
2017-07-26 14:54:14 +08:00
})
2017-07-26 22:01:30 +08:00
this.setState({
domains,
pathname,
query,
params,
paramsNotJson,
headers,
2017-07-27 17:16:23 +08:00
currDomain: domains.prd,
loading: false
2017-07-26 22:01:30 +08:00
});
}
@autobind
testInterface() {
const { method } = this.props;
const { pathname, query, headers, params, currDomain } = this.state;
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-26 14:54:14 +08:00
query
});
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,
headers,
2017-07-26 22:01:30 +08:00
data: params,
2017-07-25 13:34:48 +08:00
success: (res, header) => {
console.log(header)
2017-07-27 17:34:46 +08:00
try {
res = JSON.parse(res);
} catch (e) {
null;
}
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
},
error: (err, header) => {
console.log(header)
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) {
const domain = this.state.domains[value];
this.setState({ currDomain: domain });
}
2017-07-25 13:34:48 +08:00
2017-07-26 22:01:30 +08:00
@autobind
changeHeader(e, key) {
const headers = JSON.parse(JSON.stringify(this.state.headers));
headers[key] = e.target.value;
this.setState({ headers });
}
2017-07-25 21:24:12 +08:00
2017-07-26 22:01:30 +08:00
@autobind
changeQuery(e, key) {
const query = JSON.parse(JSON.stringify(this.state.query));
query[key] = e.target.value;
this.setState({ query });
}
2017-07-26 17:53:36 +08:00
2017-07-26 22:01:30 +08:00
@autobind
changeParams(e, key) {
const params = JSON.parse(JSON.stringify(this.state.params));
params[key] = e.target.value;
this.setState({ params });
}
2017-07-25 21:24:12 +08:00
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-07-26 22:01:30 +08:00
render () {
const { interfaceName, method } = this.props;
const { domains, pathname, query, headers, params, paramsNotJson } = this.state;
2017-07-26 17:53:36 +08:00
const search = URL.format({
2017-07-25 21:24:12 +08:00
query
});
2017-07-27 10:12:50 +08:00
const hasPlugin = this.hasCrossRequestPlugin();
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>
<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}}>
<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'}} />
</InputGroup>
2017-07-26 17:53:36 +08:00
<InputGroup compact style={{display: 'inline-block', width: 680}}>
<Input value={method} disabled style={{display: 'inline-block', width: 80}} />
2017-07-26 22:01:30 +08:00
<Select defaultValue="prd" style={{display: 'inline-block', width: 300}} onChange={this.changeDomain}>
2017-07-26 17:53:36 +08:00
{
2017-07-26 22:01:30 +08:00
Object.keys(domains).map((key, index) => (<Option value={key} key={index}>{domains[key]}</Option>))
2017-07-26 17:53:36 +08:00
}
</Select>
2017-07-26 22:01:30 +08:00
<Input value={pathname+search} disabled style={{display: 'inline-block', width: 300}} />
2017-07-26 17:53:36 +08:00
</InputGroup>
2017-07-27 17:16:23 +08:00
<Button
onClick={this.testInterface}
type="primary"
style={{marginLeft: 10}}
loading={this.state.loading}
>发送</Button>
2017-07-27 20:24:56 +08:00
<span style={{fontSize: 12}}>请求测试真实接口</span>
2017-07-26 14:54:14 +08:00
</div>
2017-07-26 22:01:30 +08:00
<Card title="HEADERS" noHovering style={{marginTop: 10}} className={Object.keys(headers).length ? '' : 'hidden'}>
2017-07-26 18:24:06 +08:00
<div className="req-row headers">
{
Object.keys(headers).map((key, index) => {
return (
<div key={index}>
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
2017-07-26 22:01:30 +08:00
<Input value={headers[key]} onChange={e => this.changeHeader(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} />
2017-07-26 18:24:06 +08:00
</div>
)
})
}
</div>
</Card>
2017-07-26 22:01:30 +08:00
<Card title="Query" noHovering style={{marginTop: 10}} className={Object.keys(query).length ? '' : 'hidden'}>
<div className="req-row query">
2017-07-26 18:24:06 +08:00
{
2017-07-26 22:01:30 +08:00
Object.keys(query).map((key, index) => {
const value = typeof query[key] === 'object' ? JSON.stringify(query[key]) : query[key].toString();
return (
<div key={index}>
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
<Input value={value} onChange={e => this.changeQuery(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} />
</div>
)
})
}
</div>
</Card>
<Card title="Body" noHovering style={{marginTop: 10}} className={Object.keys(params).length ? '' : 'hidden'}>
<div className="req-row params">
{ paramsNotJson ?
<TextArea
value={params}
style={{margin: 10}}
autosize={{ minRows: 2, maxRows: 6 }}
></TextArea> :
Object.keys(params).map((key, index) => {
const value = typeof params[key] === 'object' ? JSON.stringify(params[key]) : params[key].toString();
2017-07-26 18:24:06 +08:00
return (
<div key={index}>
<Input disabled value={key} style={{display: 'inline-block', width: 200, margin: 10}} />{' = '}
2017-07-26 22:01:30 +08:00
<Input value={value} onChange={e => this.changeParams(e, key)} style={{display: 'inline-block', width: 200, margin: 10}} />
2017-07-26 18:24:06 +08:00
</div>
)
})
}
</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">
<div>
<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
style={{margin: 10}}
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>
)
}
}