Mock Server 之 moco-runner 使用指南二


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

moco-runner 安裝配置

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

    [ { "response" : { "text" : "Hello, Moco" } } ]
    
  • 然后寫好json后就可以啟動了

    java -jar moco-runner-<version>-standalone.jar start -p 12306 -c foo.json
    

然后就可以通過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里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM