Merge branch 'dev' of gitlab.corp.qunar.com:mfe/yapi into dev

This commit is contained in:
zwjamnsss 2017-10-25 17:28:33 +08:00
commit 7dc5c152e5
3 changed files with 113 additions and 3 deletions

View File

@ -236,6 +236,7 @@ export default class Run extends Component {
data: reqBody,
files: bodyType === 'form' ? this.getFiles(bodyForm) : {},
file: bodyType === 'file' ? 'single-file' : null,
timeout: 8240000, //因浏览器限制,超时时间最多为两分钟
success: (res, header, third) => {
console.log('suc', third);
this.setState({

View File

@ -1,4 +1,4 @@
var strRegex = /\${(body|query)([a-zA-Z0-9_\.]*)\}/i;
var strRegex = /\${([a-zA-Z]+)\.?([a-zA-Z0-9_\.]*)\}/i;
var varSplit = '.';
var mockSplit = '|';
var Mock = require('mockjs');
@ -66,10 +66,14 @@ function mock(mockJSON, context) {
let matchs = str.match(strRegex);
if(matchs){
let name = matchs[1] + matchs[2];
let name = matchs[1] + (matchs[2]? '.' + matchs[2] : '');
if(!name) return str;
var names = name.split(varSplit);
var data = context;
if(typeof context[names[0]] === undefined){
return str;
}
names.forEach(function (n) {
if (data === '') return '';
if (n in data) {

View File

@ -2,6 +2,7 @@ import test from 'ava';
const rewire = require("rewire");
const mockServer = rewire('../../server/middleware/mockServer.js');
const matchApi = mockServer.__get__('matchApi');
const mockExtra = require('../../common/mock-extra.js');
test('matchApi', t => {
const apiRule = '/user/:username';
@ -20,4 +21,108 @@ test('matchApi', t => {
t.false(matchApi('/user/a/ttt2/b', apiRule_3))
});
});
test('mock-extra', t=>{
let data = '@string ${body.a}';
t.is(mockExtra(data), '@string ${body.a}');
let data2 = {
a:'@string',
b:{
t:'${body.a}'
}
}
t.deepEqual(mockExtra(data2,{
body: {
a: 3
}
}), {
a:'@string',
b:{
t:3
}
}, 'message');
//test object
let data3 = {
a:'@string',
b:{
t:'${body}'
}
}
t.deepEqual(mockExtra(data3,{
body: {
a: 3,
t: 5
}
}), {
a:'@string',
b:{
t:{
a: 3,
t: 5
}
}
}, 'message');
//test array
let data4 = {
a:'@string',
b:{
t:'${query.arr}'
}
}
t.deepEqual(mockExtra(data4, {query: {
arr: [1,2,3]
}}), {
a: '@string',
b:{
t: [1,2,3]
}
}, 'message');
//test var
let data5 = {
a:'@string',
b:{
t:'${ttt.arr}'
}
}
t.deepEqual(mockExtra(data5, {ttt: {
arr: [1,2,3]
}}), {
a: '@string',
b:{
t: [1,2,3]
}
}, 'message');
//test var
let data6 = {
a:'@string',
b:{
"ttt|regexp":'a|b'
}
}
//test regexp
t.deepEqual(mockExtra(data6, {ttt: {
arr: [1,2,3]
}}), {
a: '@string',
b:{
ttt: /a|b/
}
}, 'message');
})