This commit is contained in:
2024-03-01 20:28:14 +08:00
commit 076c21dc36
491 changed files with 84482 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
import test from 'ava';
import {
ltrim,
rtrim,
trim,
handleParams,
verifyPath,
sandbox,
handleVarPath
} from '../../server/utils/commons.js';
test('trim', t => {
t.is(trim(" a b ksjdfk "), 'a b ksjdfk');
t.is(trim(1), '1')
});
test('ltrim', t => {
t.is(ltrim(" a b ksjdfk "), 'a b ksjdfk ');
t.is(ltrim(1), '1')
});
test('rtrim', t => {
t.is(rtrim(" a b ksjdfk "), ' a b ksjdfk');
t.is(rtrim(1), '1')
});
test('handleParams', t=>{
t.deepEqual(handleParams({
a: ' s k ',
b: " a123456 "
}, {
a: 'string',
b: 'number'
}), {
a: 's k',
b: 0
})
})
test('verifyPath', t=>{
t.false(verifyPath('a/b'));
t.true(verifyPath('/a:b/t/.api/k_-/tt'))
t.true(verifyPath('/a:b/t/.api/k_-/tt/'))
})
test('sandbox', t=>{
t.deepEqual(sandbox({
a: 1
}, 'a=2'), {a : 2});
})
test('handleVarPath', t=>{
let result = [];
let pathname = '/a/:id'
handleVarPath(pathname, result);
t.deepEqual(result, [{
name: 'id',
desc: ''
}])
})
test('handleVarPath2', t=>{
let result = [];
let pathname = '/a/{id}'
handleVarPath(pathname, result);
t.deepEqual(result, [{
name: 'id',
desc: ''
}])
})
test('handleVarPath4', t=>{
let result = [];
let pathname = '/a/id={id}/tt/:sub/kk'
handleVarPath(pathname, result);
t.deepEqual(result, [{
name: 'sub',
desc: ''
}, {
name: 'id',
desc: ''
}])
})

View File

@@ -0,0 +1,52 @@
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.truthy(matchApi('/user/tom', apiRule));
t.truthy(matchApi('/user/111$$%#$##$#2222222222!!!!!!!', apiRule))
t.false(matchApi('/user/a/', apiRule))
t.false(matchApi('/use/a', apiRule))
const apiRule_2 = '/user/:username/kk';
t.truthy(matchApi('/user/aa/kk', apiRule_2));
t.truthy(matchApi('/user/!!!###kksdjfks***/kk', apiRule_2));
t.false(matchApi('/user/aa/aa', apiRule_2));
const apiRule_3 = '/user/:sdfsdfj/ttt/:sdkfjkj';
t.truthy(matchApi('/user/a/ttt/b', apiRule_3));
t.false(matchApi('/user/a/ttt2/b', apiRule_3))
const apiRule_4 = '/user/{aaa}/ttt/{bbbb}';
t.truthy(matchApi('/user/a/ttt/b', apiRule_4));
t.false(matchApi('/user/a/ttt2/b', apiRule_4))
const apiRule_5 = '/user/{aaa}/ttt/{bbbb}';
let r5 = matchApi('/user/ac/ttt/bd', apiRule_5);
t.deepEqual(r5, {
aaa: 'ac',
bbbb: 'bd',
__weight: 2
});
const apiRule_6 = '/user/a1={aaa}/ttt/b1={bbbb}';
let r6 = matchApi('/user/a1=aaa/ttt/b1=111q', apiRule_6);
t.deepEqual(r6, {
aaa: 'aaa',
bbbb: '111q',
__weight: 2
});
const apiRule_7 = '/user/a1={aaa}/ttt/b1={bbbb}/xxx/yyy';
let r7 = matchApi('/user/a1=aaa/ttt/b1=111q/xxx/yyy', apiRule_7);
t.deepEqual(r7, {
aaa: 'aaa',
bbbb: '111q',
__weight: 4
});
});