moco框架的使用

1.概述

Moco可以通过简单的配置request和response 对象,达到模拟后端返回的效果;

  • 支持HTTP、HTTPS、socket协议;
  • 支持在request中设置Headers、cookies、statusCode;
  • 支持get、post、put、delete请求;
  • 无需环境配置,只需要java运行环境即可;

2.下载安装

moco安装使用只需要下载moco jar包,即可使用;

moco jar包下载地址:

https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/1.1.0/

下载moco-runner-1.1.0-standalone.jar文件即可;
二、moco的启动及demo
1、moco的启动
1)、到github上下载moco-runner-0.12.0-standalone.jar 包【这里下载了当前最新版本】
下载地址https://github.com/dreamhead/moco
2)、moco的启动
在命令行中执行如下命令:
java -jar moco-runner-0.12.0-standalone.jar 协议类型 -p 端口号 -c json配置文件
2、demo
1)将jar包放在某个目录下。
2)json配置文件:demo.json

[

  {

    "description":"这是我们的第一个mock例子",

    "request":{

      "uri":"/demo"

    },

    "response":

    {

      "text":"Hello,Moco"

    }

  }

]

3)在命令行中执行
java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c demo.json
备注:这个例子中 moco-runner-0.12.0-standalone.jar 与demo.json文件在同一目录下
执行结果如下:说明启动成功了

image.png

4)在浏览器中输入http://127.0.0.1:8888/demo 进行访问,得到响应结果
image.png

三、常用请求
以下是json配置文件内容
1、get请求


[

  {

    "description":"不带参数的get请求",

    "request":{

      "uri":"/withGetDemo",

      "method":"get"

    },

    "response":{

      "text":"这是不带参数的get请求"

    }

  },

  {

    "description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",

    "request":{

      "uri":"/wihtGetDemobyParam",

      "method":"get",

      "queries":{

        "p1":"hh",

        "p2":"good"

      }

    },

    "response":{

      "text":"this is a get method with paramter"

    }

  }

]

2、post请求


[

  {

    "description":"post 请求",

    "request":{

      "uri":"/postDemo",

      "method":"post"

    },

    "response":{

      "text":"This is post request"

    }

  },

  {

    "description":"带参数的post请求",

    "request":{

      "uri":"/postDemoWithParam",

      "method":"post",

      "forms":{

        "param1":"one",

        "param2":"two"

      }

    },

    "response":{

      "text":"this is post request with param"

    }

  }

]

3、post请求,(请求参数为json格式、请求带cookies)


[

  {

    "description":"这是一个带cookies的Post请求",

    "request":{

      "uri":"/postDemoWithCookies",

      "cookies":{

        "login":"true"

      },

      "json":{

        "name":"hi",

        "age":"3"

      }

    },

    "response":{

      "status":"200",

      "json":{

        "name":"success",

        "status":"1"

      }

    }

  }

]

4、请求带header


[

  {

    "description":"带header请求",

    "request": {

      "uri": "/withHeader",

      "method": "post",

      "headers": {

        "content-type": "application/json"

      },

      "json": {

        "name": "xiaoming",

        "age": "18"

      }

    },

      "response":{

        "json":{

          "message":"success",

          "status":"1"

        }

      }

    }

]

5、请求重定向


[

  {

    "description":"重定向到百度",

    "request":{

      "uri":"/redirect",

      "method":"get"

    },

    "redirectTo":"http://www.baidu.com"

  },

  {

    "description":"这是被重定向的请求",

    "request":{

      "uri":"/toRedirect"

    },

    "response":{

      "text":"this is the redirect page"

    }

  },

  {

    "description":"重定向到自己的网页上",

    "request":{

      "uri":"/myStation"

    },

    "redirectTo":"/toRedirect"

  }

]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容