测试基础-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官方文档 

 

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