feat: 增强mock

This commit is contained in:
suxiaoxin 2017-08-31 18:04:45 +08:00
parent 8144052368
commit 26b01d96ed
9 changed files with 95 additions and 65 deletions

View File

@ -6,7 +6,7 @@ import { autobind } from 'core-decorators';
import crossRequest from 'cross-request';
import mockEditor from '../../containers/Project/Interface/InterfaceList/mockEditor'
import URL from 'url';
const MockExtra = require('common/mock-extra.js')
import './Postman.scss'
const { TextArea } = Input;
@ -158,8 +158,29 @@ export default class Run extends Component {
}
const { res_body, res_body_type } = this.props.data;
let validRes = '';
let query = {};
this.state.query.forEach(item=>{
query[item.name] = item.value;
})
let body = {};
if(this.state.bodyType === 'form'){
this.state.bodyForm.forEach(item=>{
body[item.name] = item.value;
})
}else if(this.state.bodyType === 'json'){
try{
body = JSON.parse(this.state.bodyOther);
}catch(e){
body = {}
}
}
if (res_body && res_body_type === 'json' && typeof res === 'object') {
validRes = Mock.valid(JSON.parse(res_body), res)
let tpl = MockExtra(JSON.parse(res_body), {
query: query,
body: body
})
console.log(tpl, this.state)
validRes = Mock.valid(tpl, res)
console.log(validRes)
}

View File

@ -4,6 +4,7 @@ import _ from 'underscore'
import constants from '../../../../constants/variable.js'
import { handlePath } from '../../../../common.js'
import {
Form, Select, Input, Tooltip,
Button, Row, Col, Radio, Icon, AutoComplete, Switch
@ -89,6 +90,7 @@ class InterfaceEditForm extends Component {
if (!err) {
if (values.res_body_type === 'json') values.res_body = this.state.res_body;
values.method = this.state.method;
values.req_params = values.req_params || [];
let isfile = false, isHavaContentType = false;
if (values.req_body_type === 'form') {
values.req_body_form.forEach((item) => {

View File

@ -4,6 +4,7 @@ require('brace/mode/javascript');
require('brace/theme/xcode');
require("brace/ext/language_tools.js");
var json5 = require('json5');
const MockExtra = require('common/mock-extra.js')
var langTools = ace.acequire("ace/ext/language_tools"),
wordList = [
@ -67,7 +68,7 @@ function run(options) {
curData.text = json;
curData.format = true;
curData.jsonData = obj;
curData.mockData = Mock.mock(obj);
curData.mockData = Mock.mock(MockExtra(obj, {}));
curData.mockText = JSON.stringify(curData.mockData, null, " ");
} catch (e) {
curData.format = e.message;

View File

@ -1,70 +1,67 @@
var mockjs = require('mockjs');
var strRegex = /\${([a-zA-Z0-9_\.]+)\}/g
var varSplit = ".";
var mockSplit = "|";
var strRegex = /\${([a-zA-Z0-9_\.]+)\}/g;
var varSplit = '.';
var mockSplit = '|';
function mock(mockJSON, context) {
context = context || {};
var filtersMap = {
regexp: handleRegexp
}
return mockjs.mock(parse(mockJSON))
context = context || {};
var filtersMap = {
regexp: handleRegexp
};
function parse(p, c) {
c = c || {};
for (var i in p) {
if (!p.hasOwnProperty(i)) {
continue;
}
if (typeof p[i] === 'object') {
c[i] = (p[i].constructor === Array) ? [] : {};
parse(p[i], c[i]);
} else {
p[i] = handleStr(p[i]);
var filters = i.split(mockSplit), newFilters = [].concat(filters);
if (filters.length > 1) {
for (var f = 1, l = filters.length, index; f < l; f++) {
if (filters[f] in filtersMap) {
if ((index = newFilters.indexOf(filters[f])) !== -1) {
newFilters.splice(index, 1);
}
c[newFilters.join(mockSplit)] = filtersMap[filters[f]].call(p, p[i])
return parse(mockJSON);
function parse(p, c) {
c = c || {};
for (var i in p) {
if (!p.hasOwnProperty(i)) {
continue;
}
if (typeof p[i] === 'object') {
c[i] = (p[i].constructor === Array) ? [] : {};
parse(p[i], c[i]);
} else {
p[i] = handleStr(p[i]);
var filters = i.split(mockSplit), newFilters = [].concat(filters);
if (filters.length > 1) {
for (var f = 1, l = filters.length, index; f < l; f++) {
if (filters[f] in filtersMap) {
if ((index = newFilters.indexOf(filters[f])) !== -1) {
newFilters.splice(index, 1);
}
c[newFilters.join(mockSplit)] = filtersMap[filters[f]].call(p, p[i]);
}
}
} else {
c[i] = p[i];
}
}
}
} else {
c[i] = p[i]
}
}
return c;
}
return c;
}
function handleRegexp(item) {
return new RegExp(item)
}
function handleStr(str) {
if(typeof str !== 'string' ||str.indexOf("{") === -1 || str.indexOf("}") === -1 || str.indexOf("$") === -1){
return str;
function handleRegexp(item) {
return new RegExp(item);
}
str = str.replace(strRegex, function (matchs, name) {
var names = name.split(varSplit);
var data = context;
names.forEach(function(n){
if(data === false) return false;
if(n in data){
data = data[n];
}else{
data = false;
function handleStr(str) {
if (typeof str !== 'string' || str.indexOf('{') === -1 || str.indexOf('}') === -1 || str.indexOf('$') === -1) {
return str;
}
})
if(data === false){
return matchs;
}
return data;
})
return str;
}
str = str.replace(strRegex, function (matchs, name) {
var names = name.split(varSplit);
var data = context;
names.forEach(function (n) {
if (data === '') return '';
if (n in data) {
data = data[n];
} else {
data = '';
}
});
return data;
});
return str;
}
}
module.exports = mock;

View File

@ -351,6 +351,7 @@ class interfaceController extends baseController {
data.req_params = params.req_params;
} else {
data.type = 'static'
data.req_params = [];
}
if (!_.isUndefined(params.req_query)) {

View File

@ -3,6 +3,7 @@ import projectModel from '../models/project.js';
import interfaceModel from '../models/interface.js';
import mockExtra from '../../common/mock-extra.js';
import _ from 'underscore';
import Mock from 'mockjs';
function matchApi(apiPath, apiRule) {
@ -96,7 +97,7 @@ module.exports = async (ctx, next) => {
body: ctx.request.body
}
);
return ctx.body = res;
return ctx.body = Mock.mock(res);
} catch (e) {
yapi.commons.log(e, 'error')
return ctx.body = {

View File

@ -698,6 +698,7 @@ var interfaceController = function (_baseController) {
data.req_params = params.req_params;
} else {
data.type = 'static';
data.req_params = [];
}
if (!_underscore2.default.isUndefined(params.req_query)) {

View File

@ -28,6 +28,10 @@ var _underscore = require('underscore');
var _underscore2 = _interopRequireDefault(_underscore);
var _mockjs = require('mockjs');
var _mockjs2 = _interopRequireDefault(_mockjs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function matchApi(apiPath, apiRule) {
@ -189,7 +193,7 @@ module.exports = function () {
query: ctx.request.query,
body: ctx.request.body
});
return _context.abrupt('return', ctx.body = res);
return _context.abrupt('return', ctx.body = _mockjs2.default.mock(res));
case 60:
_context.prev = 60;

View File

@ -92,7 +92,8 @@ module.exports = {
exports: [
'./index.js'
],
modifyWebpackConfig: function (baseConfig) {
modifyWebpackConfig: function (baseConfig) {
var ENV_PARAMS = {};
switch (this.env) {
case 'local':
@ -113,7 +114,8 @@ module.exports = {
//初始化配置
baseConfig.devtool = 'cheap-module-eval-source-map'
baseConfig.context = path.resolve(__dirname, "client");
baseConfig.context = path.resolve(__dirname, './client');
baseConfig.resolve.alias.common = '/common';
baseConfig.output.prd.path = 'static/prd';
baseConfig.output.prd.publicPath = '';
baseConfig.output.prd.filename = '[name]@[chunkhash][ext]'