上期讲过了​​moco API模拟框架视频讲解(上)​​​、​​moco API模拟框架视频讲解(中)​​​,本期分享内容是​​mocoserver​​对象的创建和​​moco api​​的实践。​​mocoserver​​比较简单,常用的参数基本​​port​​和​​logmonitor​​,其他的基本用不到。​​moco API​​实践内容比较少,因为录制时间不够了,并未把所有的​​request​​和​​response​​演示完,有兴趣可以自己尝试一下,十分推荐。

moco API模拟框架视频讲解(下)

moco API模拟框架视频讲解(下)_性能测试


演示代码



package com.fun

import com.fun.base.bean.Result
import com.fun.moco.MocoServer
import com.github.dreamhead.moco.HttpServer

class DeleteNull extends MocoServer {


public static void main(String[] args) {
HttpServer server = getServer(8088, "1.log")

server.get(urlMatcher("/abc+")).response("funtest")

server.get(urlOnly("/ab")).response(obRes(Result.success(getJson("2323=3323"))))

server.get(urlStartsWith("/test")).response(limit("fun", "tester", 3000))

server.request(both(urlStartsWith("/te"), eqArgs("q", "fun"))).response(obRes(Result.success(getJson("name=fun"))))

server.get(urlStartsWith("/error")).response(setStatus(404))

server.response("haha")

MocoServer drive = run(server)


waitForKey("fan")

drive.stop()
}


}

MocoServer代码




package com.fun.moco


import com.github.dreamhead.moco.HttpServer
import com.github.dreamhead.moco.MocoMonitor
import com.github.dreamhead.moco.MocoRequestHit
import com.github.dreamhead.moco.RequestHit
import com.github.dreamhead.moco.Runner

import static com.github.dreamhead.moco.Moco.httpServer
import static com.github.dreamhead.moco.Moco.log

/**
* 获取server的工具类,提供了计数监视器和日志监视器
* 这里的继承关系为了更方便调用mocorequest和mocoresponse的静态方法
*/
class MocoServer extends MocoResponse {

def array = []

/**
* 获取httpserver对象,端口号12345
* @return
*/
static HttpServer getServer() {
httpServer 12345, getLogMonitor()
}

/**
* 获取httpserver对象
* @param port
* @return
*/
static HttpServer getServer(int port) {
httpServer port, getLogMonitor()
}

/**
* 获取httpserver对象
* @param mocoMonitors
* @return
*/
static HttpServer getServer(MocoMonitor mocoMonitors) {
httpServer 12345, mocoMonitors
}

/**
* 获取httpserver对象
* @param port 端口
* @param logName 日志文件名
* @param configs 配置
* @return
*/
static HttpServer getServer(final int port, String logName, MocoMonitor configs) {
httpServer port, getLogMonitor(logName), configs
}

/**
* 获取httpserver对象
* @param port 端口
* @param logName 日志文件名
* @return
*/
static HttpServer getServer(final int port, String logName) {
httpServer port, getLogMonitor(logName)
}

/**
* 获取日志监视器,在log_path下面
* @param logName
* @return
*/
static def getLogMonitor(String logName) {
log LOG_Path + logName, DEFAULT_CHARSET
}

/**
* 获取日志监视器,默认在控制台显示
* @return
*/
static def getLogMonitor() {
log()
}

/**
* 获取计数监视器,计数器在做测试的时候用到,确认服务启动且接口调用正常
* @return
*/
static RequestHit getHitMonitor() {
MocoRequestHit.requestHit()
}

/**
* 启动所有服务
* @param httpServer
* @return
*/
static MocoServer run(HttpServer... httpServer) {
def server = new MocoServer()
httpServer.each { x -> server.array << Runner.runner(x) }
server.start()
}

/**
* 启动服务
* @return
*/
def start() {
array.each { x -> x.start() }
this
}

/**
* 结束服务
* @return
*/
def stop() {
array.each { x -> x.stop() }
}
}

moco API模拟框架视频讲解(下)_json_02