第四節(jié) Mock框架

Moco框架基本介紹

下載地址:
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/
可以下載jar包

Moco啟動(dòng)以及第一個(gè)demo
啟動(dòng)命令
java -jar ./moco-runner-0.11.0-standalone.jar 服務(wù)類型 -p 端口號 -c json配置文件
例如

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8809 -c startup.json

第一個(gè)demo

1、創(chuàng)建startup.json文件
2、填入如下代碼塊,并啟動(dòng)moco
3、瀏覽器輸入本地網(wǎng)址127.0.0.1:8009/demo
4、這個(gè)是一個(gè)不帶參數(shù)的get方法

[
  {
    "description": "這是我們的第一個(gè)mock例子",
    "request": {
      "uri": "/demo"
    },
    "response": {
      "text": "第一個(gè)基本框架說明"
    }
  }
]

注意:修改json文件不用重新部署,服務(wù)熱部署,會(huì)自動(dòng)重啟

不帶參數(shù)的get方法

[
  {
    "description": "模擬一個(gè)沒有參數(shù)的get請求",
    "request": {
      "uri": ",/getdemo",
      "method": "get"
    },
    "response": {
      "text": "這是一個(gè)沒有參數(shù)的GET請求"
    }
  }
]

帶參數(shù)的get方法

[
  {
    "description": "模擬一個(gè)沒有參數(shù)的get請求",
    "request": {
      "uri": "/getdemo",
      "method": "get",
      "queries": {
        "name": "1",
        "age": "2"
      }
    },
    "response": {
      "text": "這是一個(gè)有參數(shù)的GET請求"
    }
  }
]

不帶參數(shù)的post方法

[
  {
    "description": "模擬一個(gè)沒有參數(shù)的post請求",
    "request": {
      "uri": "/postdemo",
      "method": "post"
    },
    "response": {
      "text": "這是一個(gè)沒參數(shù)的post請求"
    }
  }
]

帶參數(shù)的post方法

[
  {
    "description": "模擬一個(gè)有參數(shù)的post請求",
    "request": {
      "uri": "/postdemo",
      "method": "post",
      "forms": {
        "name": "1",
        "age": "2"
      }
    },
    "response": {
      "text": "這是一個(gè)有參數(shù)的post請求"
    }
  }
]

請求中帶cookies信息的get請求

[
 {
    "description":"這是一個(gè)request請求中帶cookies信息的get請求",
    "request":{
      "uri":"/getwithcookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
    },
    "response":{
      "text":"返回這是一個(gè)request請求中帶cookies信息的get請求"
    }
  }
]

請求中帶cookies的post請求

[
{
   "description":"這是一個(gè)request請求中帶cookies、使用json格式傳參、返回結(jié)果為json格式的post請求",
   "request":{
     "uri":"/postwithcookiesandjson",
     "method":"post",
     "cookies":{
       "login":"true"
     },
     "json":{
       "name":"qinzhenxia",
       "age":"28"
     }
   },
   "response":{
     "status":200,
     "json":{
       "code":"0",
       "msg":"success",
       "p2pdata":{
         "name":"moguzhixing",
         "address":"beisanhuan"
       }

     }
   }
 }
]

response返回中帶有cookies信息的get請求

[
{
    "description":"模擬response返回中帶有cookies信息的get請求",
    "request":{
      "uri":"/getcookies"
    },
    "response":{
      "cookies":{
        "login":"true",
        "token":"1234567890"

      },
      "json":{
        "name":"zhoujielun",
        "age":"38"
      }
    }
  }
]

有重定向的接口(redirectTo)

[
  {
    "description":"模擬重定向接口",
    "request":{
      "uri":"/redirectto"
    },
    "redirectTo":"/redirectedpath"
  },
  {
    "description":"模擬被重定向的接口",
    "request":{
      "uri":"/redirectedpath"
    },
    "response":{
      "text":"返回重定向結(jié)果",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }

]

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容