性能测试之Moco 作为MockServer快速入门

Moco 作为MockServer快速入门

moco是个什么

一个简单搭建模拟服务器的程序库。

https://github.com/dreamhead/moco

moco 安装配置

  •  

    1、 下载jar

  • https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar

2、 编译运行

 

  • 配置java环境变量

  • 安装并配置Gradle(ref:http://www.gradle.org)

  • 然后获取源代码:https://github.com/dreamhead/moco

  • 进入代码目录, ./gradle build

  • 撰写json

     
  • 然后写好json后就可以启动了

     
  1. java-jar moco-runner-<version>-standalone.jar start-p12306-c foo.json

  2. [{"response":{"text":"Hello, Moco"}}]

然后就可以通过http://localhost:12306访问了

  •  

    3、进阶

     

  •  

    3.1 Content 根据request内容返回response

   [{

     "request":

       {

         "text":"foo"

       },

     "response":

       {

         "text":"bar"

       }

   }]

 

访问:http://localhost:12306

如果request内容太多,可以放到一个文件里面

       {

         "request" :

           {

             "file" : "foo.request"

           },

         "response" :

           {

             "text" : "bar"

           }

       }


​​​​

  • 3.2 配置文件

PS:Moco支持动态加载配置文件,所以无论你是修改还是添加配置文件都是不需要重启服务的

Moco支持在全局的配置文件中引入其他配置文件,这样就可以分服务定义配置文件

例如你有两个项目Boy和Girl项目需要使用同一个Mock Server,那么可以分别定义boy.json和girl.json配置文件,然后在全局文件中引入即可: 全局配置如下:


   [ { "context": "/boy", "include": "boy.json" }, { "context": "/girl", "include": "girl.json" } ]

在boy.json和girl.json中分别定义:

   //boy

   [ { "request" : { "uri" : "/hello" }, "response" : { "text" : "I am a boy." } } ]

   //girl

   [ { "request" : { "uri" : "/hello" }, "response" : { "text" : "I am a girl." } } ]


​​​​​

此时需要通过参数-g在加载全局配置文件(-g仅仅在3.1context章节使用)

java -jar moco-runner-<version>-standalone.jar start -p 12306 -g onecoder.json

启动成功后,我们分别通过http://localhost:12306/girl/hello 和 http://localhost:12306/boy/hello 访问服务,便可得到对应的reponse结果。 其实全局文件的引入方式还有直接include等,不过OneCoder觉得context这种方式应该比较常用,

Request参数

request里自然有很多带参数的,配置如下:

   [{ "request" : { "uri" : "/getBoy", "queries": { "name":"onecoder" } }, "response" : { "text" : "Hey." } }]

上述配置匹配的url即为:http://localhost:12306/getBoy?name=onecoder,返回值即为: Hey. 也就是说,使用这种方式你需要在开发期有固定的测试参数和参数值

对于rest风格的url,Moco支持正则匹配。

   [{ "request": { "uri": { "match": "/searchboy/\\w+" } }, "response": { "text": "Find a boy." } }]

此时,访问http://localhost:12306/searchboy/* 结尾加任何参数均可匹配到。

除了Get外,Post,Put,Delete等请求模式自然是支持的:
​​​​​

   [{

     "request" :

       {

         "method" : "post",

         "forms" :

           {

             "name" : "onecoder"

           }

       },

     "response" :

       {

         "text" : "Hi."

       }}]

对于Header、Cookies等请求信息的配置也是支持的。

  •  

    3.3 template 从0.8版本开始,Moco提供了template功能,可以动态的返回一些参数值。例如:

[

   {

   "request":{

           "uri":"/template"

                 },

   "response":{

         "text":{

             "template":"${req.queries['name']}"

             }

                   }

       }

]

 

 

此时通过url:http://localhost:12306/template?name=onecoder 访问,则会返回onecoder。 这样就可以通过template这种方式灵活的返回一些值。

  •  

    3.4 redirect

[{"request":{"uri":"/redirect"},"redirectTo":"http://www.coderli.com"}]

 

 

3.5 Asynchronous

[

   "request":{

       "uri":"/event"

   },

   "response":{

       "text":"event"

   },

   "on":{

       "complete":{

           "async":"true",

           "post":{

               "url":"http://another_siter",

               "content":"cintent"

           }

       }

   }

]

 

这样,对于/event的访问将会是异步的。要等到对http://another_siter访问结束后,才会将结果放到response里

  • 帮助网址

https://github.com/dreamhead/moco/blob/master/moco-doc/socket-apis.md https://github.com/dreamhead/moco/blob/master/moco-doc/rest-apis.md https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

 

PS: 原文MardDown格式下载

 

链接: https://pan.baidu.com/s/1zsx19iHQGYNarGo09G6PHw 密码: hnse

——————————————————

给大家推荐一个学习资料分享群(574253227),里面大牛已经为我们整理好了许多的学习资料,有自动化,接口,性能等等的学习资料!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值