1. Moco是什么? 简单的模拟服务器的程序库/工具, 有请求给出响应.在地址中下载moco-runner-0.12.0-standalone.jar.

2. 使用示例

1. 创建test.json

[
  {
    "description": "First test",
    "request": {
      "uri": "/demo"
    },
    "response": {
      "text": "First Response"
    }
  }
]

2. 命令行启动服务, moco-runner-0.12.0-standalone.jar与test.json在相同目录下.


java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c test.json

3. 验证

访问 http://localhost:8888/demo

2. 模拟一个带参数的请求, para.json

[
  {
    "description": "模拟一个有参数的Get请求",
    "request": {
      "uri": "/getpara",
      "method": "get",
      "queries": {
        "name": "xiaoming",
        "age": "20"
      }
    },
    "response": {
      "text": "I am on the way."
    }
  }
]

java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c para.json

3. 模拟post请求

[
  {
    "description": "post请求",
    "request": {
      "uri": "/post",
      "method":"post"
      },
    "response": {
      "text": "First moco post request"
    }
  }
]

4. 带参的post请求

[
  {
    "description": "post请求",
    "request": {
      "uri": "/postpara",
      "method":"post",
      "forms": {
        "name": "xiaoming"
      }
    },
    "response": {
      "text": "有参数"
    }
  }
]

5. 带cookie信息, 服务端返回浏览器cookie信息

[
  {
    "description": "cookie",
    "request": {
      "uri": "/cookie",
      "method": "get",
      "cookies": {
        "login": "true"
      }
    },
    "response": {
      "text": "hello"
    }
  },
    {
        "description": "get cookies",
        "request": {
            "uri": "/getcookies",
            "method": "get"
        },
        "response": {
            "text": "getcookie success",
            "cookies": {
                "login": "true",
                "status":"10000"
            }
        }
    }
]

使用Jmeter添加cookie是必须要有的值login true localhost(域) /(路径)必不可少.

6. post json格式的

[
  {
    "description": "带cookie的post请求",
    "request": {
      "uri": "/postcookie",
      "method": "post",
      "cookies": {
        "login": "true"
      },
      "json": {
        "name": "xiaoming",
        "age": "18"
      }
    },
    "response": {
      "json": {
        "hell": "你长了"
      }
    }
  }
]

7. headers请求

[
  {
    "description": "带header的请求",
    "request": {
      "uri": "/headers",
      "method": "post",
      "headers": {
        "content-type": "application/json"
      },
      "json": {
        "name": "xiaohong",
        "age": "17"
      }
    },
    "response": {
      "json": {
        "hello": "pretty",
        "status": "200"
      }
    }
  }
]

8. 重定向地址, 重定向请求.

[
  {
    "description": "重定向网页",
    "request": {
      "uri": "/redirect",
      "method": "get"
    },
    "redirectTo": "http://huya.com"
  },
  {
    "description": "重定向请求",
    "request": {
      "uri": "/redirect/to",
      "method": "get"
    },
    "redirectTo": "/new/redirect"
  },
  {
    "description": "指定的请求",
    "request": {
      "uri": "/new/redirect",
      "method": "get"
    },
    "response": {
      "text": "请求成功了"
    }
  }
]