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

199
test/common/common.test.js Normal file
View File

@@ -0,0 +1,199 @@
import test from 'ava';
import {
handleParamsValue,
schemaValidator
} from '../../common/utils.js';
test('handleParamsValue', t => {
const json = JSON.stringify({
t: 1,
obj: {
name: "dd",
value: "vvvv"
}
})
t.is(handleParamsValue(" aaaa | length"), 'aaaa | length');
t.is(handleParamsValue("{{aaaa |upper }}"), 'AAAA')
t.is(handleParamsValue(json), json)
t.is(handleParamsValue(' {{ dkkdjf }}'), 'dkkdjf')
t.is(handleParamsValue(' {{ dkkdjf | upper | kkk }}'), '{{ dkkdjf | upper | kkk }}')
t.is(handleParamsValue('aaa {{ aaaa | upper }} bbbb'), 'aaa AAAA bbbb')
t.is(handleParamsValue('aaa {{ aaaa | upper }} bbbb,aaa {{ aaaa | upper }} bbbb'), 'aaa AAAA bbbb,aaa AAAA bbbb')
t.is(handleParamsValue("{{aaaa | length}}"), 4);
t.is(handleParamsValue("{{4444 | number}}"), 4444);
});
test('schemaValidator', t => {
const schema1 = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"errcode": {
"type": "number"
},
"errmsg": {
"type": "string"
},
"data": {
"type": "object",
"properties": {}
}
},
"required": [
"errcode",
"errmsg"
]
};
const data1 = {
"errcode": 0,
"errmsg": "成功!",
"data": {}
}
t.is(schemaValidator(schema1, data1).valid, true);
const schema2 ={
"type": "object",
"required": [
"id",
"category",
"status"
],
"properties": {
"id": {
"type": "integer",
"format": "int64",
"minimum": 1,
"maximum": 4,
"enum": [
2,
3,
4
],
"exclusiveMinimum": true,
"exclusiveMaximum": true,
"description": "所有功能"
},
"category": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64",
"minimum": 1,
"maximum": 3,
"exclusiveMinimum": true,
"description": "exclusiveMinimum"
},
"type": {
"type": "string",
"pattern": "\\d",
"default": "12",
"minLength": 1,
"maxLength": 2,
"description": "正则, 长度限制"
},
"name": {
"type": "string",
"enum": [
"小明",
"小风"
],
"description": "枚举"
},
"formate": {
"type": "string",
"format": "ctitle",
"description": "formate"
},
"boolean": {
"type": "boolean"
},
"array": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item": {
"type": "boolean"
}
},
"required": [
"item"
]
},
"description": "uniqueItems",
"uniqueItems": true
},
"array2": {
"type": "array",
"items": {
"type": "integer",
"enum": [
2
],
"minimum": 1,
"maximum": 2,
"description": "枚举和最大值最小值"
},
"minItems": 1,
"maxItems": 2,
"description": "最大个数和最小个数"
}
},
"xml": {
"name": "Category"
},
"$$ref": "#/definitions/Category",
"required": [
"id",
"name",
"boolean"
]
},
"status": {
"type": "number",
"description": "枚举",
"enum": [
23.9,
34.9
]
}
},
"xml": {
"name": "Pet"
},
"$$ref": "#/definitions/Pet"
}
const data2 = {
"id": 2,
"category": {
"id": 2,
"type": "8",
"name": "小明",
"formate": "任治导具",
"boolean": false,
"array": [
{
"item": true
},
{
"item": false
}
],
"array2": [
2,
2
]
},
"status": 23.9
}
t.is(schemaValidator(schema2, data2).valid, true);
})

View File

@@ -0,0 +1,129 @@
import test from 'ava';
import mergeJsonSchema from '../../common/mergeJsonSchema';
test('base', t=>{
let schema1 = {
type: 'string',
default: 'xxx'
}
let schema2 = {
type: 'string',
format: 'email'
}
let result = mergeJsonSchema(schema1, schema2)
t.deepEqual(result, {
type:'string',
default: 'xxx',
format: 'email'
})
})
test('object', t=>{
let schema1 = {
"type": "object",
"title": "empty object",
"xxx": 1,
"properties": {
"field_1": {
"type": "string",
"format": "email"
}
}
}
let schema2 = {
"type": "object",
"title": "empty object",
"properties": {
"field_1": {
"type": "string",
"description": "dd"
}
}
}
let result = mergeJsonSchema(schema1, schema2)
t.deepEqual(result, {
"type": "object",
"title": "empty object",
"xxx": 1,
"properties": {
"field_1": {
"type": "string",
"format": "email",
"description": "dd"
}
}
})
})
test('array', t=>{
let schema1 = {
"type": "object",
"title": "empty object",
"properties": {
"field_1": {
"type": "array",
"tt":1,
"items": {
"type": "object",
"xxx": "2",
"properties": {
"field_3": {
"format": 'ttt',
"type": "string"
}
}
}
}
}
}
let schema2 = {
"type": "object",
"title": "empty object",
"properties": {
"field_1": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field_3": {
"type": "string",
"enum": [1,2]
}
}
}
}
}
}
let result = mergeJsonSchema(schema1, schema2)
t.deepEqual(result, {
"type": "object",
"title": "empty object",
"properties": {
"field_1": {
"type": "array",
"tt":1,
"items": {
"type": "object",
"xxx": "2",
"properties": {
"field_3": {
"format": 'ttt',
"type": "string",
"enum": [1,2]
}
}
}
}
}
})
})

180
test/lib.test.js Normal file
View File

@@ -0,0 +1,180 @@
import test from 'ava';
const rewire = require("rewire");
const lib = rewire('../common/lib.js');
const plugin = rewire('../common/plugin.js')
const initPlugins = plugin.initPlugins;
test('initPlugins', t=>{
plugin.__set__("getPluginConfig", function(){
return {
server: true,
client: true
}
})
let configs = initPlugins(['a', 'b'], 'exts');
t.deepEqual(configs, [{
name: 'a',
enable: true,
server: true,
client: true
}, {
name: 'b',
enable: true,
server: true,
client: true
}])
})
test('initPlugins2', t=>{
plugin.__set__("getPluginConfig", function(){
return {
server: true,
client: false
}
})
let configs = initPlugins(['a', 'b'], 'exts');
t.deepEqual(configs, [{
name: 'a',
enable: true,
server: true,
client: false
}, {
name: 'b',
enable: true,
server: true,
client: false
}])
})
test('initPlugins3', t=>{
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: true
}
})
let configs = initPlugins(['a', {name: 'a'}], 'exts');
t.deepEqual(configs, [{
name: 'a',
enable: true,
server: false,
client: true
}])
})
test('initPlugins3', t=>{
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: true
}
})
let configs = initPlugins([{
name: 'a',
options: {
a:1,
t:{
c:3
}
}
}], 'exts');
t.deepEqual(configs, [{
name: 'a',
enable: true,
server: false,
client: true,
options: {
a:1,
t:{
c:3
}
}
}])
})
test('initPlugins3', t=>{
plugin.__set__("getPluginConfig", function(){
return {
server: false,
client: false
}
})
let configs = initPlugins(['a', 'b'], 'exts');
t.deepEqual(configs, [])
})
test('testJsonEqual', t=>{
let json1 = {
a:"1",
b:2,
c:{
t:3,
x: [11,22]
}
};
let json2 = {
c:{
x: [11,22],
t:3
},
b:2,
a:"1"
}
t.true(lib.jsonEqual(json1, json1));
})
test('testJsonEqualBase', t=>{
t.true(lib.jsonEqual(1,1));
})
test('testJsonEqualBaseString', t=>{
t.true(lib.jsonEqual('2', '2'));
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch({a:'aaaaa', b:2}, {a:'aaaaa'}))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch({a:1, b:2, c: {t:'ttt'}}, {c: {t:'ttt'}}))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch({}, undefined))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch(undefined, {}))
})
test('isDeepMatch', t=>{
t.false(lib.isDeepMatch(undefined, {a:1}))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch({ t: 1,
b: '2',
ip: '127.0.0.1',
interface_id: 1857,
ip_enable: true,
params: { a: 'x', b: 'y' },
res_body: '111',
code: 1 }, {t:'1'}))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch({ t:[{a: 1}]}, { t:[{a: 1}]}))
})
test('isDeepMatch', t=>{
t.false(lib.isDeepMatch({ t:[{a: 1, b: 12}]}, { t:[{a: 1}]}))
})
test('isDeepMatch', t=>{
t.true(lib.isDeepMatch([{a: 1}], [{a: 1}]))
})

107
test/mock-extra.test.js Normal file
View File

@@ -0,0 +1,107 @@
import test from 'ava';
const mockExtra = require('../common/mock-extra.js');
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');
})

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
});
});

1054
test/swagger.v2.json Normal file

File diff suppressed because it is too large Load Diff

1027
test/swagger.v3.json Normal file

File diff suppressed because it is too large Load Diff