接口模拟之moco框架

2019-10-30  本文已影响0人  Xyxtank

接口模拟之moco框架

一、背景

在软件开发中,一般都是前后端分离,但前后端开发进度因为各种原因往往无法匹配,导致前端想调用接口时,后端却还没有开发完毕,此时前端迫切需要模拟出后端反馈的数据,继续推进前端开发工作进度。

在测试工作中,同样存在这样的矛盾,测试工作会受到后端接口开发的限制,所以测试同样需要模拟接口返回数据,推进测试工作进度。

二、环境准备

三、环境搭建

C:\Users\Administrator>java

执行命令,若显示如下信息,则服务正常启动。

java -jar moco-runner-0.12.0-standalone.jar http -p 8801 -c demo.json

打开浏览器,验证服务是否正常启动,在浏览器中输入访问地址127.0.0.1:8801/demo,注意/demo是demo.json文件中配置的访问接口。

四、常用请求响应格式示例

1、第一个demo示例
[
  {
    "description":"这是一个moco例子",
    "request":{
        "uri":"/demo"
    },
    "response":
    {
        "text":"Hello,Moco",
        "status": "200"
    }
  }
]
2、get请求,不带参数
[
    {
    "description":"这是一个get请求,不带参数",
    "request":{
        "uri":"/WithGet",
        "method": "get"
    },
    "response":
    {
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        },
        "text":"这是一个GET请求,不带参数",
        "status": "200"
    }
  }
]
3、get请求,带参数
[
    {
    "description":"这是一个get请求,带参数",
    "request":{
        "uri":"/GetWithQuery",
        "method": "get",
        "queries": {
            "username": "john",
            "password": "123123"
        }
    },
    "response":
    {
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        },
        "text":"这是一个GET请求,带参数",
        "status": "200"
    }
  },
]
4、post请求,带参数
[
    {
    "description":"这是一个post请求,带参数",
    "request":{
        "uri":"/PostWithQuery",
        "method": "post",
        "queries": {
            "username": "john",
            "password": "123123"
        }
    },
    "response":
    {
        "text":"这是一个POST请求,带参数",
        "status": "200",
        "headers": {
            "Content-Type": "text/plain; charset=GBK"
        }
    }
  }
]
5、post请求,不带参数
[
    {
        "description": "这是一个post请求,不带参数",
        "request": {
            "uri": "/WithPost",
            "method": "post"
        },
        "response": {
            "text": "这是一个post请求,不带参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
6、post请求,带cookie参数
[
    {
        "description": "这是一个post请求,带cookie参数",
        "request": {
            "uri": "/PostWithCookie",
            "method": "post",
            "cookies": {
                "token": "123123"
            }
        },
        "response": {
            "text": "这是一个post请求,带cookie参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
7、post请求,带forms参数
[
    {
        "description": "这是一个post请求,带forms参数",
        "request": {
            "uri": "/PostWithForm",
            "method": "post",
            "forms": {
                "username": "jake",
                "password": "123123"
            }
        },
        "response": {
            "text": "这是一个post请求,带forms参数",
            "status": "200",
            "headers": {
                "Content-Type": "text/plain; charset=GBK"
            }
        }

    }
]
8、重定向
[
    {
        "description": "这是一个重定向",
        "request": {
            "uri": "/redirect",
            "method": "get"

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

    }
]
上一篇下一篇

猜你喜欢

热点阅读