Mock之Moco框架使用

从github上下载jar包,https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.1/,选择最大的

moco-runner-0.11.1-standalone.jar。

启动命令:

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c json配置文件

-p 就是port 端口号, -c 是一个json文件。

moco模拟请求返回中文乱码问题,添加响应头编码格式为gbk,就可以解决返回结果中文乱码

 "headers":{
        "Content-Type":"text/html;charset=gbk"
      }

[
  {
    "description":"这是我们的第一个mock例子",
    "request":{
      "uri":"/demo"
    },
    "response":{
      "text":"第一个moco框架",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }
]

一、第一个mock框架

[
  {
    "description":"这是我的第一个mock例子",
    "request":{
      "uri":"/demo"
    },
    "response":{
      "text":"第一个mock框架demo",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }
]

二、带参数的get方法,http://127.0.0.1:8899/getwithparam1?name=xiaoqiang&age=18

[
{
    "description":"这是一个带参数的get请求",
    "request":{
      "uri":"/getwithparam1",
      "method":"get",
      "queries":{
        "name":"xiaoqiang",
        "age":"18"
      }
    },
    "response":{
      "text":"我是一个带参数的get请求",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }
]

三、post请求

[
{
    "description":"这是一个带参数的post请求",
    "request":{
    "uri":"/postwithparam",
      "method":"post",
      "forms":{
        "name":"xiaoqinag",
        "age":"23"
      }
    },
    "response":{
      "text":"小强小强",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }
]

 四、带cookie的请求

[
  {
    "description":"这是一个会返回cookies信息的get请求",
    "request":{
      "uri":"/getCookies",
      "method":"get"

    },
    "response":{
      "cookies":{
        "login":"true"
      },
      "text":"恭喜你获得cookies信息成功",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },
  {
    "description":"这是一个带cookies信息的get请求",
    "request":{
      "uri":"/get/with/cookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
    },
    "response":{
      "text":"这是一个需要携带cookies信息才能访问的get请求",
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  },
  {
    "description":"这是一个带cookies信息的post请求",
    "request":{
      "uri":"/post/with/cookies",
      "method":"post",
      "cookies":{
        "login":"true"
      },
      "json":{
        "name":"xiaoqiang",
        "age":"18"
      }
    },
    "response":{
      "status":200,
      "json":{
        "huhansan":"success",
        "status":"1"
      },
      "headers":{
        "Content-Type":"text/html;charset=gbk"
      }
    }
  }

]

五、带请求头

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

六、重定向的请求

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

]

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值