首页 > 数据库 > 数据库开发技术 > moco API模拟框架视频讲解(下)
上期讲过了moco API模拟框架视频讲解(上)、moco API模拟框架视频讲解(中),本期分享内容是mocoserver对象的创建和moco api的实践。
mocoserver比较简单,常用的参数基本port和logmonitor,其他的基本用不到。
moco API实践内容比较少,因为录制时间不够了,并未把所有的request和response演示完,有兴趣可以自己尝试一下,十分推荐。
package com.funimport com.fun.base.bean.Resultimport com.fun.moco.MocoServerimport com.github.dreamhead.moco.HttpServerclass 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()}}
package com.fun.mocoimport com.github.dreamhead.moco.HttpServerimport com.github.dreamhead.moco.MocoMonitorimport com.github.dreamhead.moco.MocoRequestHitimport com.github.dreamhead.moco.RequestHitimport com.github.dreamhead.moco.Runnerimport static com.github.dreamhead.moco.Moco.httpServerimport 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() }}}
Fhaohaizi@163.com。原文作者:FunTester;如有侵权,请联系管理员删除。