Skip to content

Commit deb8b16

Browse files
author
刘柱寨
committed
base interface
1 parent 2f405ec commit deb8b16

11 files changed

+405
-44
lines changed

DATA_FORMAT.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CaseSet数据
2+
`{
3+
"name":"foo bar", //名称,
4+
"origin":{
5+
"pre":"https://liveamusecommpre.lizhi.fm",
6+
"dev":"http://172.17.33.213:7102",
7+
"prod":"https://liveamusecomm.lizhi.fm"
8+
},
9+
"global_input":{ //可为空
10+
"mount_id":"23412341234", //可有默认值,双下滑线需要系统处理
11+
"token":"__TOKEN",
12+
},
13+
"interface":[{
14+
"name":"api_mount_list",
15+
"input": { //额外的输入框
16+
17+
},
18+
"path":"/mount/getMountList",
19+
"method": "GET",
20+
"application_json" : {
21+
22+
},
23+
"headers":{
24+
//额外的header
25+
},
26+
"assert":"RET.rcode === 0" //可为空
27+
28+
},{
29+
"path":"/mount/getUserInfo",
30+
"para": {
31+
"token":"${TOKEN}",
32+
"userId":"${UID}"
33+
},
34+
"assert":[
35+
"RET.rcode === 0"
36+
]
37+
}
38+
],
39+
40+
"cases":[{
41+
"describe":"购买座驾后有改id",
42+
"process":" api_buy_mount('12223232'); api_user_info().contain('12222232');",
43+
}
44+
]
45+
}
46+
`

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"axios": "^0.18.0",
67
"react": "^16.6.1",
78
"react-dom": "^16.6.1",
8-
"react-scripts": "2.1.1"
9+
"react-scripts": "2.1.1",
10+
"vconsole": "^3.2.0"
911
},
1012
"scripts": {
1113
"start": "react-scripts start",

src/App.css

+3-28
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22
text-align: center;
33
}
44

5-
.App-logo {
6-
animation: App-logo-spin infinite 20s linear;
7-
height: 40vmin;
8-
}
9-
10-
.App-header {
11-
background-color: #282c34;
12-
min-height: 100vh;
13-
display: flex;
14-
flex-direction: column;
15-
align-items: center;
16-
justify-content: center;
17-
font-size: calc(10px + 2vmin);
18-
color: white;
19-
}
20-
21-
.App-link {
22-
color: #61dafb;
23-
}
24-
25-
@keyframes App-logo-spin {
26-
from {
27-
transform: rotate(0deg);
28-
}
29-
to {
30-
transform: rotate(360deg);
31-
}
32-
}
5+
.meta-info {
6+
text-align: left;
7+
}

src/App.js

+151-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,161 @@
11
import React, { Component } from 'react';
2-
import logo from './logo.svg';
2+
import GlobalInput from './component/GlobalInput';
3+
import Interface from './component/Interface';
4+
import datasource from './test_data';
35
import './App.css';
6+
import VConsole from 'vconsole';
7+
import Axios from 'axios';
8+
9+
const vcon = new VConsole();
10+
vcon.setOption('maxLogNumber', 500);
411

512
class App extends Component {
13+
constructor(props) {
14+
super(props);
15+
this.state = {
16+
tc:JSON.parse(datasource),
17+
origin:"dock",
18+
status:[],//0 空闲,1正在执行, 2 成功, 3失败 state describe
19+
};
20+
this.onGlobalInputChanged = this.onGlobalInputChanged.bind(this);
21+
this.onApiInputChanged = this.onApiInputChanged.bind(this);
22+
this.onRunApi = this.onRunApi.bind(this);
23+
this.onRunAll = this.onRunAll.bind(this);
24+
}
25+
componentDidMount() {
26+
27+
}
28+
29+
//---------------------------support function -----------------------//
30+
currentOrigin() {
31+
return this.state.tc.origin[this.state.origin];
32+
}
33+
34+
apiFromIndex(index) {
35+
const {tc} = this.state;
36+
return tc.interface[index];
37+
}
38+
39+
varValue(api, token) {
40+
if (api.input.hasOwnProperty(token)) {
41+
return api.input[token];
42+
}
43+
else {
44+
var res = this.state.tc.global_input[token];
45+
if(res === undefined) {
46+
res = '';
47+
}
48+
return res;
49+
}
50+
}
51+
52+
wholeUrl(api) {
53+
var reg = /\$\{.+?\}/g;
54+
var retArray;
55+
var path = api.path;
56+
while((retArray = reg.exec(api.path)) !== null) {
57+
var target = retArray[0].substring(2, retArray[0].length-1);
58+
path = path.replace(retArray[0], this.varValue(api, target));
59+
}
60+
return this.currentOrigin() + path;
61+
}
62+
63+
requestApi(api, index) {
64+
var {status} = this.state;
65+
status[index] = {
66+
state:1,
67+
describe:'',
68+
}
69+
this.setState({
70+
status:status,
71+
});
72+
Axios({
73+
method:api.method,
74+
url:this.wholeUrl(api),
75+
76+
}).then((response)=>{
77+
var RET = response.data;
78+
var selfAssert = eval(api.assert);
79+
console.log(`api index ${index} ,assert result:${selfAssert}`);
80+
var newStatus = this.state.status;
81+
newStatus[index] = {
82+
state:selfAssert?2:3,
83+
describe:JSON.stringify(RET),
84+
};
85+
this.setState({
86+
status:newStatus,
87+
});
88+
}).catch((error)=>{
89+
console.log(error);
90+
var newStatus = this.state.status;
91+
var errorDict = error;
92+
if (error.response) {
93+
errorDict = error.response;
94+
if(error.response.data) {
95+
errorDict = error.response.data;
96+
}
97+
}
98+
newStatus[index] = {
99+
state:3,
100+
describe:JSON.stringify(error.response.data),
101+
};
102+
this.setState({
103+
status:newStatus,
104+
});
105+
}
106+
);
107+
}
108+
109+
//----------------------------event response -----------------------//
110+
onRunAll(){
111+
const {tc} = this.state;
112+
tc.interface.forEach(
113+
(element, index) => {
114+
this.requestApi(element, index);
115+
});
116+
}
117+
118+
onGlobalInputChanged(key,value) {
119+
const {tc} = this.state;
120+
tc.global_input[key] = value;
121+
this.setState({
122+
tc:tc,
123+
});
124+
}
125+
126+
onApiInputChanged(index,key, value) {
127+
const {tc} = this.state;
128+
tc.interface[index].input[key] = value;
129+
this.setState({
130+
tc:tc,
131+
});
132+
}
133+
134+
onRunApi(index) {
135+
console.log(`run api index:${index}`);
136+
var api = this.apiFromIndex(index);
137+
this.requestApi(api,index);
138+
}
139+
140+
141+
6142
render() {
143+
const {tc} = this.state;
7144
return (
8145
<div className="App">
9-
<header className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<p>
12-
Edit <code>src/App.js</code> and save to reload.
13-
</p>
14-
<a
15-
className="App-link"
16-
href="https://reactjs.org"
17-
target="_blank"
18-
rel="noopener noreferrer"
19-
>
20-
Learn React
21-
</a>
22-
</header>
146+
<h1>{tc.name}</h1>
147+
<div className="meta-info">
148+
<div>环境:{this.state.origin}, origin:{this.currentOrigin()}</div>
149+
<GlobalInput global_input={tc.global_input}
150+
onGlobalInputChanged={this.onGlobalInputChanged}
151+
/>
152+
<button onClick={this.onRunAll}>全部运行</button>
153+
</div>
154+
<Interface interface ={tc.interface}
155+
onApiInputChanged={this.onApiInputChanged}
156+
onRunApi = {this.onRunApi}
157+
status ={this.state.status}
158+
/>
23159
</div>
24160
);
25161
}

src/component/GlobalInput.css

Whitespace-only changes.

src/component/GlobalInput.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, {Component} from 'react';
2+
import './GlobalInput.css';
3+
4+
class GlobalInput extends Component {
5+
render() {
6+
var inputs = Object.keys(this.props.global_input).map(
7+
(ele, index, arr)=> {
8+
return (<div key={ele}>
9+
{ele} <input type="text" value = {this.props.global_input[ele]}
10+
onChange={(e)=>this.props.onGlobalInputChanged(ele, e.target.value)}/>
11+
</div>)
12+
}
13+
)
14+
15+
return (<div>
16+
{inputs}
17+
</div>)
18+
}
19+
}
20+
21+
export default GlobalInput;

src/component/Interface.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.api-detail {
2+
text-align: left;
3+
border:solid 1px gray;
4+
margin-top:10px;
5+
}
6+

0 commit comments

Comments
 (0)