測試基礎-10-Moco使用

前言

上一篇搭建好了moco環境,本篇爲moco中request和response的具體使用

 

moco使用(request部分)

1)description字段,在所有JSONAPI中,可以使用description來描述這個會話的目的。它只是用作註釋,在運行時將被忽略。

//test.json
[
   {
        "description": "any response",
        "response": {
            "text": "foo"
        }
    }
]

2)request字段,根據請求進行響應

//test.json
[
   {
      "request" :
      {
         "text" : "foo"
      },
      "response" :
      {
         "text" : "bar"
      }
   }
]

3)request-uri字段,根據不同url響應不同信息

//test.json
[
   {
      "request" :
      {
         "uri" : "/foo"
      },
      "response" :
      {
         "text" : "bar"
      }
   }
]

瀏覽器輸入:localhost:12306/foo,會返回“bar”

 

4)request-queries字段,請求帶參數

//test.json
[
   {
      "request" :
      {
         "uri" : "/foo",
          "queries" : 
         {
            "param" : "blah"
         }
       },
      "response" :
       {
          "text" : "bar"
      }
   }
]

瀏覽器輸入:localhost:12306/foo?param=blah,會返回“bar”

 

5)request-method字段,請求方法(以get爲例,其他方法與其操作一致)

//test.json
[
   {
      "request" :
      {
         "method": "get",
         "uri" : "/foo"
       },
      "response" :
       {
          "text" : "bar"
      }
   }
]

6)request-headers字段,設置請求頭信息

//test.json
[
   {
      "request" :
      {
         "method": "post",
         "headers" :{
            "content-type" : "application/json"
         }
       },
      "response" :
       {
          "text" : "bar"
      }
   }
]

7)request-cookies字段,設置cookie

//test.json
[
   {
      "request" :
      {
         "uri": "/cookie",
         "cookies" :{
            "login" : "true"
         }
       },
      "response" :
       {
          "text" : "success"
      }
   }
]

8)request-forms字段,設置表單

//test.json
[
   {
      "request" :
      {
         "method" : "post", 
         "forms" :{
            "name" : "foo"
           }
       },
      "response" :
       {
          "text" : "bar"
      }
   }
]

9)request-text-xml字段,請求是一個xml文件

//test.json
[
   {
      "request" :
      {
         "uri" : "/xml",
         "text" :{
            "xml" : "<request><parameters><id>1</id></parameters></request>"
           }
       },
      "response" :
       {
          "text" : "foo"
      }
   }
]

如果 XML 文件很大的話,可以放在一個 xml 文件中(request-file-xml字段),然後通過引用文件路徑的方式來發起請求:

//test.json
[
   {
      "request" :
      {
         "uri" : "/xml",
         "file" :{
            "xml" : "your_file.xml"
           }
       },
      "response" :
       {
          "text" : "foo"
      }
   }
]

10)request-xpaths字段,對於XML/HTML請求,MOCO允許我們將請求與xpath匹配。

//test.json
[
   {
      "request" :
      {
         "method" : "post",
          "xpaths" :{
            "/request/parameters/id/text()" : "1"
         }
      },
      "response" :
      {
         "text" : "bar"
      }
   }
]

 11)request-text-json字段,請求數據格式爲json格式

//test.json
[
   {
      "request" :
       {
          "uri": "/json",
          "text":{
             "json": "{'foo':'bar'}"
          }
       },
      "response" :
      {
         "text" : "foo"
      }
   }
]

request-json字段可以直接請求json格式

//test.json
[
   {
      "request" :
       {
          "uri": "/json",
          "json":{
             "foo": "bar"
          }
       },
      "response" :
      {
         "text" : "foo"
      }
   }
]

request-file-json字段,請求json數據存放在json文件上傳

//test.json
[
   {
      "request" :
       {
          "uri": "/json",
          "file":{
             "json": "your_file.json"
          }
       },
      "response" :
      {
         "text" : "foo"
      }
   }
]

12)request中的匹配

①match正則匹配

//test.json
[
   {
      "request" :
       {
          "uri":{
             "match": "/\\w*/foo"
          }
       },
      "response" :
      {
         "text" : "bar"
      }
   }
]

匹配任意類似http://localhost:12306/xxx/foo的請求,並返回:bar。其中的 /\\w* 表示以 / 開始,之後是任意數量的數字或字母。

②startsWith 匹配開頭

//test.json
[
   {
      "request" :
       {
          "uri":{
             "startsWith": "/foo"
          }
       },
      "response" :
      {
         "text" : "bar"
      }
   }
]

匹配以/foo開頭的url

③startsWith 匹配結尾

//test.json
[
   {
      "request" :
       {
          "uri":{
             "endsWith": "foo"
          }
       },
      "response" :
      {
         "text" : "bar"
      }
   }
]

匹配以foo結尾的url

④contain包含

//test.json
[
   {
      "request" :
       {
          "uri":{
             "contain": "foo"
          }
       },
      "response" :
      {
         "text" : "bar"
      }
   }
]

匹配包含foo的url

 

moco使用(response部分)

1)response-text字段,直接返回的是文字內容,上方的都是

2)response-file字段,返回的內容存放在文件中

//test.json
[
   {
      "request" :
       {
          "text": "foo"
       },
      "response" :
      {
         "file" : "bar.response"
      }
   }
]

3)response-status字段,返回響應狀態碼

//test.json
[
   {
      "request" :
       {
          "text": "foo"
       },
      "response" :
      {
         "status" : 200
      }
   }
]

4)response-headers字段,設置返回響應頭信息

//test.json
[
   {
      "request" :
       {
          "text": "foo"
       },
      "response" :
      {
         "headers" : {
            "content-type" : "application/json"
         }
      }
   }
]

5)response-proxy字段,我們也可以使用指定的URL進行響應,就像使用代理一樣

//test.json
[
   {
      "request" :
       {
          "text": "foo"
       },
      "response" :
      {
         "proxy" : "http://www.github.com"
      }
   }
]

實際上,代理比這更強大。它可以將整個請求轉發到目標URL,包括HTTP方法、版本、頭、內容等。

6)response-proxy-failover字段,除了基本功能外,代理還支持故障轉移,這意味着如果遠程服務器暫時不可用,服務器將知道從本地配置進行恢復。

//test.json
[
   {
      "request" :
       {
          "text": "foo"
       },
      "response" :
      {
         "proxy" : {
            "url" : "http://localhost:12306/unknown",
            "failover" : "failover.json"
         }
      }
   }
]

7)response-cookies字段,響應中返回cookie信息

//test.json
[
   {
      "request" :
       {
          "uri" : "/cookie"
       },
      "response" :
      {
         "cookies" :{
            "login" : "true"
         }
      }
   }
]

8)response-json字段,響應返回json數據

//test.json
[
   {
      "request" :
       {
          "uri": "/json"
       },
      "response" :
      {
         "json" :{
            "foo" : "bar"
         }
      }
   }
]

 

Redirect重定向

我們可以簡單地將請求重定向到不同的URL。

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

 

更多使用參考:moco官方文檔 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章