Moco框架

1、下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/

  下载:moco-runner-0.11.0-standalone.jar

2、moco启动实例:

  在项目上创建目录moco,拷贝下载好的jar包到新建目录,新建mock文件startup.json:

[
  {
    "description":"启动举例",
    "request":{
      "uri":"/demo"
    },
    "response":{
      "text":"demostarted"
    }

  }
]
mock文件实例

  cd命令切换到新建目录:

  mock启动命令:java -jar  moco-runner-0.11.0-standalone.jar http -p 8888 -c startup.json

3、get请求方法实现mock文件示例:

[
  {
    "description":"模拟一个没有参数get请求",
    "request":{
      "uri":"/getdemo",
      "method":"get"
    },
    "response":{
      "text":"这是一个没有参数的get请求"
    }

  },
  {
    "description":"这是一个带参数get请求",
    "request":{
      "uri":"/getwithparam",
      "method":"get",
      "queries":{
        "name":"huhansan",
        "sex":"20"
      }
    },
    "response":{
      "text":"我胡汉三又回来啦!!!!"
    }

  }

]
get方法mock

4、post请求方法实现mock文件示例:

[
  {
    "description":"模拟一个post请求",
    "request":{
      "uri":"/postdemo",
      "method":"post"
    },
    "response":{
      "text":"这是我的第一个mock的post请求"
    }
  },
  {
    "description":"这是一个带参数的post请求",
    "request":{
      "uri":"/postwithparam",
      "method":"post",
      "forms":{
        "name":"huhansan",
        "sex":"man"
      }
    },
    "response":{
      "text":"我胡汉三带着参数来啦!!!"

    }
  }

]
post方法mock文件

5、带cookie的get、post请求和返回cookie的

[
  {
    "description":"这是一个会返回cookies信息的get请求",
    "request":{
      "uri":"/getCookies",
      "method":"get"
    },
    "response":{
      "cookies":{
        "login":"true"
      },
      "text":"恭喜你获得cookies信息成功"
    }


  },


  {
    "description":"这是一个带cookies信息的get请求",
    "request":{
      "uri":"/get/with/cookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
    },
    "response":{
      "text":"这是一个需要携带cookies信息才能访问的get请求"
    }
  },
  {
    "description":"这是一个带cookies信息的post请求",
    "request":{
      "uri":"/post/with/cookies",
      "method":"post",
      "cookies":{
        "login":"true"
      },
      "json":{
        "name":"huhansan",
        "age":"18"
      }
    },
    "response":{
      "status":200,
      "json":{
        "huhansan":"success",
        "status":"1"
      }
    }
  }

]
cookie

6、请求头header:

[
  {
    "description":"这是一个带header信息的post请求",
    "request":{
      "uri":"/post/with/headers",
      "method":"post",
      "headers":{
        "content-type":"application/json"
      },
      "json":{
        "name":"wanglaosi",
        "sex":"woman"
      }
    },
    "response":{
      "json":{
        "wanglaosi":"success",
        "status":"1"
      }
    }

  }

]
header

7、重定向redirect

[
  {
    "description":"重定向到百度",
    "request":{
      "uri":"/redirect"
    },
    "redirectTo":"http://www.baidu.com"
  },
  {
    "description":"重定向到一个自己的网页上",
    "request":{
      "uri":"/redirect/topath"
    },
    "redirectTo":"/redirect/new"
  },
  {
    "description":"这是被重定向到的请求",
    "request":{
      "uri":"/redirect/new"
    },
    "response":{
      "text":"重定向成功啦"
    }
  }

]
redirect

猜你喜欢

转载自www.cnblogs.com/wangkc/p/10847863.html