2
0
mirror of https://github.com/YMFE/yapi.git synced 2025-04-24 15:30:44 +08:00

test: add mockServer matchApi test

This commit is contained in:
suxiaoxin 2017-10-20 13:30:05 +08:00
parent 94f3e8943a
commit 85fd32bdfc
3 changed files with 33 additions and 3 deletions

@ -103,6 +103,7 @@
"redux-devtools-log-monitor": "^1.3.0",
"redux-promise": "^0.5.3",
"redux-thunk": "^2.2.0",
"rewire": "^2.5.2",
"string-replace-webpack-plugin": "^0.1.3",
"style-loader": "^0.18.2",
"table-resolver": "^3.2.0",
@ -139,7 +140,9 @@
"node": ">= 7.6.0",
"npm": ">= 4.1.2"
},
"ava":{
"files": ["test/**/*.js"]
"ava": {
"files": [
"test/**/*.js"
]
}
}

@ -5,7 +5,11 @@ const mockExtra = require('../../common/mock-extra.js');
const _ = require('underscore');
const Mock = require('mockjs');
/**
*
* @param {*} apiPath /user/tom
* @param {*} apiRule /user/:username
*/
function matchApi(apiPath, apiRule) {
let apiRules = apiRule.split("/");
let apiPaths = apiPath.split("/");

@ -0,0 +1,23 @@
import test from 'ava';
const rewire = require("rewire");
const mockServer = rewire('../../server/middleware/mockServer.js');
const matchApi = mockServer.__get__('matchApi');
test('matchApi', t => {
const apiRule = '/user/:username';
t.true(matchApi('/user/tom', apiRule));
t.true(matchApi('/user/111$$%#$##$#2222222222!!!!!!!', apiRule))
t.false(matchApi('/user/a/', apiRule))
t.false(matchApi('/use/a', apiRule))
const apiRule_2 = '/user/:username/kk';
t.true(matchApi('/user/aa/kk', apiRule_2));
t.true(matchApi('/user/!!!###kksdjfks***/kk', apiRule_2));
t.false(matchApi('/user/aa/aa', apiRule_2));
const apiRule_3 = '/user/:sdfsdfj/ttt/:sdkfjkj';
t.true(matchApi('/user/a/ttt/b', apiRule_3));
t.false(matchApi('/user/a/ttt2/b', apiRule_3))
});