Mock Server 之 moco-runner 使用指南二


文章出处http://blog.csdn.net/crisschan/article/details/53335234

moco-runner 安装配置

http://www.gradle.org)
  • 然后获取源代码:https://github.com/dreamhead/moco
  • 进入代码目录, 
    ./gradle build
  • 撰写json

    [ { "response" : { "text" : "Hello, Moco" } } ]
    
  • 然后写好json后就可以启动了

    java -jar moco-runner--standalone.jar start -p 12306 -c foo.json
    
  • 然后就可以通过http://localhost:12306访问了

    http://localhost:12306

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

    {
      "request" :
        {
          "file" : "foo.request"
        },
      "response" :
        {
          "text" : "bar"
        }
    }
    
  • 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等请求信息的配置也是支持的。

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

    http://another_siter访问结束后,才会将结果放到response里