diff --git a/client/components/Postman/Postman.js b/client/components/Postman/Postman.js index 03c0cca6..83f2f4f3 100755 --- a/client/components/Postman/Postman.js +++ b/client/components/Postman/Postman.js @@ -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({ diff --git a/common/mock-extra.js b/common/mock-extra.js index 51d51f81..11c146a5 100755 --- a/common/mock-extra.js +++ b/common/mock-extra.js @@ -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) { diff --git a/test/server/mockServer.test.js b/test/server/mockServer.test.js index 28b04c54..c7008386 100644 --- a/test/server/mockServer.test.js +++ b/test/server/mockServer.test.js @@ -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)) -}); \ No newline at end of file +}); + +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'); + + + + + +}) +