laitimes

Mock data moco framework used

author:Sweet potatoes in the vegetable garden

First, moco introduction

The moco framework is an open source project on GitHub that emulates the http, https, and Socket protocols.

Mock testing is a test method that is created for testing purposes for some objects that are not easy to construct or are not easy to obtain, or that have not yet been developed.

Second, the download and launch of the moco framework

Download: https://github.com/dreamhead/moco

Here my moco directory structure is under the same folder, like this:

Mock data moco framework used

Startup: java -jar moco-runner-1.0.0-standalone.jar http -p 2222 -c get_data.json (start the mock service with http protocol, service port 2222)

3. Simulate GET requests

The following is the simulated get request has been written to simulate the data, including the request uri, method, parameters, response content

[
  {
    "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":"这是带参数的get请求"
    }
  }
]           

Here I simulate the first get request with the browser, and the response is as follows:

Mock data moco framework used

Here I use postman to simulate a second get request with request parameters, and the response is as follows:

Mock data moco framework used

4. Simulate POST requests

Here are a few ways to request a post request:

1. Post request without parameters

2. Post request with parameters

3. Post request with cookie

4. Post request with header

The simulated return response data is as follows

[
  {
    "description":"不带参数的post请求",
    "request":{
      "uri":"/postDemo",
      "method":"post"
    },
    "response":{
      "text":"这是不带参数的post请求"
    }
  },
  {
    "description":"带参数的post请求",
    "request":{
      "uri":"/postDemoWithParam",
      "method":"post",
      "forms":{
        "param1":"one",
        "param2":"two"
      }
    },
    "response":{
      "json":{"name":"han","age":30,"address":"beijing daxing"}
    }
  },
  {
    "description":"带cookie的Post请求",
    "request":{
        "uri":"/postDemoWithCookies",
        "method":"post",
        "cookies":{
            "login":"true"
        },
        "json":{
            "name":"hi",
            "age":"3"
        }
    },
    "response":{
        "status":"200",
        "json":{
            "name":"success",
            "status":"1"
        }
    }
  },

  {
    "description":"带header的post请求",
    "request":{
        "uri":"/withHeader",
        "method":"post",
        "headers":{
            "content-type":"application/json"
        },
        "json":{
            "name":"xiaoming",
            "age":"18"
        }
    },
    "response":{
        "json":{
            "message":"success",
            "status":"1"
        }
    }
  }
]           

The following is a simulated request with postman

Mock data moco framework used

2. Post request with parameters (application/x-www-form-urlencoded used here)

Mock data moco framework used

3. Post request with cookie (although the simulated request is written as cookies, it must be written as a cookie when requesting, do not add "s")

Mock data moco framework used
Mock data moco framework used

In summary, you may wish to try moco to simulate the request data when the mock data.

Read on