feat: test server

This commit is contained in:
zwjamnsss 2017-09-14 17:14:01 +08:00
parent 9688a3e847
commit 5e71882ced
4 changed files with 211 additions and 2 deletions

View File

@ -1,3 +1,3 @@
module.exports = function(){
}
};

View File

@ -24,6 +24,8 @@ app.use(router.allowedMethods());
websocket(app);
app.use( async (ctx, next) => {
if( /^\/(?!api)[a-zA-Z0-9\/\-_]*$/.test(ctx.path) ){
ctx.path = "/"

164
server/controllers/test.js Normal file
View File

@ -0,0 +1,164 @@
const yapi = require('../yapi.js');
const baseController = require('./base.js');
class interfaceColController extends baseController{
constructor(ctx) {
super(ctx);
}
/**
* 测试 get
* @interface /test/get
* @method GET
* @returns {Object}
* @example
*/
async testGet(ctx){
try {
let query = ctx.query;
ctx.body = yapi.commons.resReturn(query);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 post
* @interface /test/post
* @method POST
* @returns {Object}
* @example
*/
async testPost(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn(params);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 单文件上传
* @interface /test/single/upload
* @method POST
* @returns {Object}
* @example
*/
async testSingleUpload(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn({res: '上传成功'});
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 文件上传
* @interface /test/files/upload
* @method POST
* @returns {Object}
* @example
*/
async testFilesUpload(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn({res: '上传成功'});
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 put
* @interface /test/put
* @method PUT
* @returns {Object}
* @example
*/
async testPut(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn(params);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 delete
* @interface /test/delete
* @method DELETE
* @returns {Object}
* @example
*/
async testDelete(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn(params);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 head
* @interface /test/head
* @method HEAD
* @returns {Object}
* @example
*/
async testHead(ctx){
try{
let query = ctx.query;
ctx.body = yapi.commons.resReturn(query);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 options
* @interface /test/options
* @method OPTIONS
* @returns {Object}
* @example
*/
async testOptions(ctx){
try{
let query = ctx.query;
ctx.body = yapi.commons.resReturn(query);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 测试 patch
* @interface /test/patch
* @method PATCH
* @returns {Object}
* @example
*/
async testPatch(ctx){
try{
let params = ctx.request.body;
ctx.body = yapi.commons.resReturn(params);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
}
module.exports = interfaceColController

View File

@ -3,6 +3,7 @@ const interfaceController = require('./controllers/interface.js');
const groupController = require('./controllers/group.js');
const userController = require('./controllers/user.js');
const interfaceColController = require('./controllers/interfaceCol.js');
const testController = require('./controllers/test.js');
const yapi = require('./yapi.js');
const projectController = require('./controllers/project.js');
@ -47,6 +48,10 @@ const INTERFACE_CONFIG = {
col: {
prefix: '/col/',
controller: interfaceColController
},
test: {
prefix: '/test/',
controller: testController
}
};
@ -347,7 +352,44 @@ const routerConfig = {
path: "del_case",
method: "get"
}
]
],
"test": [{
action: "testPost",
path: "post",
method: "post"
}, {
action: "testGet",
path: "get",
method: "get"
}, {
action: "testPut",
path: "put",
method: "put"
}, {
action: "testDelete",
path: "delete",
method: "del"
}, {
action: "testHead",
path: "head",
method: "head"
}, {
action: "testOptions",
path: "options",
method: "options"
}, {
action: "testPatch",
path: "patch",
method: "patch"
}, {
action: "testFilesUpload",
path: "files/upload",
method: "post"
}, {
action: "testSingleUpload",
path: "single/upload",
method: "post"
}]
}
for(let ctrl in routerConfig){
@ -365,6 +407,7 @@ for(let ctrl in routerConfig){
* @param {*} action controller_action_name
*/
function createAction(controller, action, path, method) {
console.log(method)
router[method]("/api" + INTERFACE_CONFIG[controller].prefix + path, async (ctx) => {
let inst = new INTERFACE_CONFIG[controller].controller(ctx);