使用 Moco 搭建一个模拟服务器

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

moco-runner-0.11.0-standalone.jar

2、新建一个package,将下载下来的jar包直接拷进来 + 新建一个first.json文件

3、进入到jar包同级目录下,运行命令:java -jar moco-runner-0.11.0-standalone.jar http -p 8089 -c first.json

指定运行编码格式:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.11.0-standalone.jar 协议类型 -p 端口号 -c json配置文件

Moco Options:
moco [server type] -p port -c [configuration file]

server type: http, https, socket

D:\workspace\interface-autotest\moco\src\main\java\com\wy\mocodemo>java -jar moco-runner-0.11.0-standalone.jar http -p 8089 -c first.json

4、访问路径http://localhost:8089/demo

get添加参数 使用 “queries”
post添加参数 forms(key-value格式)、json(json格式)

first.json实例:

[
  {
    "description": "这是第一个moco例子,默认mothed 为get",
    "request": {
      "uri": "/demo"
    },
    "response": {
      "text": "这是一个moco的demo",
      "headers": {
        "Content-Type": "text/html;charset=UTF-8"
      }
    }
  },
  {
    "description": "带参的get请求",
    "request": {
      "uri": "/getDemoWithParam",
      "method": "get",
      "queries": {
        "name": "hi",
        "age": "3"
      }
    },
    "response": {
      "text": "这是带参的get请求,url地址的参数要和queries的参数值一样"
    }
  },
  {
    "description": "不带参的 post 请求",
    "request": {
      "uri": "/postDemo",
      "method": "post"
    },
    "response": {
      "text": "这是不带参的post请求"
    }
  },
  {
    "description": "参数格式为key-value的 post 请求",
    "request": {
      "uri": "/postDemoWithParam",
      "method": "post",
      "forms": {
        "name": "zhangsan",
        "age": "10"
      }
    },
    "response": {
      "text": "参数格式为key-value的 post 请求"
    }
  },
  {
    "description": "参数格式为json的 post 请求",
    "request": {
      "uri": "/postDemoWithParaJson",
      "method": "post",
      "json": {
        "name": "zhangsan",
        "age": "10"
      }
    },
    "response": {
      "headers": {
        "Content-Type": "application/json;charset=gbk"
      },
      "text": "这是参数格式为json的post请求"
    }
  },
  {
    "description": "这是带cookies 和 json 参数的Post请求",
    "request": {
      "uri": "/postWithCookiesAndParam",
      "headers": {
        "Content-Type": "application/json;charset=gbk"
      },
      "json": {
        "name": "zhangsan",
        "age": 10
      },
      "method": "post",
      "cookies": {
        "login": "true"
      }
    },
    "response": {
      "status": "200",
      "headers": {
        "Content-Type": "application/json;charset=gbk"
      },
      "json": {
        "text": "需要带cookies 和 json 参数的Post请求",
        "name": "zhangsanhhah",
        "age": "100"
      }
    }
  },
  {
    "description": "获取cookies的get请求",
    "request": {
      "uri": "/getCookies",
      "method": "get"
    },
    "response": {
      "headers": {
        "Content-Type": "application/json;charset=gbk"
      },
      "cookies": {
        "status": "2001",
        "user_name": "w454246n",
        "login": "true"
      },
      "text": "获取cookies成功hha"
    }
  },
  {
    "description": "重定向到百度",
    "request": {
      "uri": "/redirect"
    },
    "redirectTo": "http://www.baidu.com",
    "response": {
      "text": "redirectTo baidu"
    }
  },
  {
    "description": "重定向 到其他方面",
    "request": {
      "uri": "/redirect/otherPath"
    },
    "redirectTo": "/demo"
  }
]

你可能感兴趣的:(使用 Moco 搭建一个模拟服务器)