Mock test using Moco framework

Moco framework: an open source project above github, which can simulate http, https and other protocols.
github website: moco source code

1. Start the simulation server

Download Moco_ URL of jar package: Moco jar package download
Command to start the impersonation server:

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8899 -c startup1.json

Jar package: moco-runner-0.11.0-standalone.jar And JSON file: startup1.json are in the same path, so the current path. / moco-runner-0.11.0-standalone.jar

http: indicates using http protocol
8899: port number

[
  {
    "description": "This is our first moco example",
    "request": {
      "uri": "/demo"
    },
    "response": {
      "text": "first moco frame demo"
    }
  }

2. Implementation of get method with parameters

Hot deployment of service: when the json configuration file is repaired, the service automatically restarts and updates without affecting the user's use.

Enter web address: http://localhost : 8899 / getwithparam? Name = huhansan & age = 18 to access get requests with parameters.
json file configuration for get requests with and without parameters:
Parameter: "queries"

[
  {
    "description": "Simulate a parameter free get request",
    "request": {
      "uri": "/getdemo",
      "method": "get"
    },
    "response": {
      "text": "This one has no parameters get request"
    }
  },
  {
    "description": "Simulate a parametric get request",
    "request": {
      "uri": "/getWithParam",
      "method": "get",
      "queries": {
        "name": "huhansan",
        "age": "18"
      }
    },
    "response": {
      "text": "I Hu Hansan is back!!!"
    }
  }
]

3. Implementation of post method of http protocol in Moco framework

The browser can only use Get to access the service.
If you need to use the Post method to access the service, you can use JMeter to use the Post method to access it.

No parameters & json configuration file for Post request with parameters:
Parameter: "forms"

[
  {
    "description": "Simulate a non parametric post request",
    "request": {
      "uri": "/postDemo",
      "method": "post"
    },
    "response": {
      "text": "This is my first mock Of post request"
    }
  },
  {
    "description": "This is a parameter post request",
    "request": {
      "uri": "postWithParam",
      "method": "post",
      "forms": {
        "name": "huhansan",
        "sex": "male"
      }
    },
    "response": {
      "text": "I Hu Hansan came back with the parameters!!!"
    }
  }
]

4. Add Cookies to Moco framework

Fill in Cooki: name, value, domain (server address), path.
JMeter can be used to add Cookie parameters.
The json data is configured in the Post request, which can be configured in both request and response. The json request data is written to the requested BodyDate.
json configuration of get and post requests with Cookies parameter:

[
  {
    "description": "This is a belt cookie Informative get request",
    "request": {
      "uri": "get/with/cookies",
      "method": "get",
      "cookies": {
        "login": "true"
      }
    },
    "response": {
      "text": "This is a need to carry cookies Information access get request"
    }
  },
  {
    "description": "This is a belt cookies Informative post request",
    "request": {
      "uri": "/post/with/cookies",
      "method": "post",
      "cookies": {
        "login": "true"
      },
      "json": {
        "name": "huhansan",
        "age": "18"
      }
    },
    "response": {
      "status": 200,
      "json": {
        "huhansan":"success",
        "status": "1"
      }
    }
  }
]

5. Add Headers information to Moco framework (in request)

Header information: it is generally the requirement for data format.
json configuration file with header information, json data:

[
  {
    "description": "This is a belt header Informative post request",
    "request": {
      "uri": "post/with/headers",
      "method": "post",
      "headers": {
        "conten-type": "application/json"
      },
      "json": {
        "name": "wanglaosi",
        "sex": "female"
      }
    },
    "response": {
      "json": {
        "wanglaosi": "success",
        "status": "1"
      }
    }
  }
]

6. Redirect by mock framework

"redirectTo": redirect to another web page

[
  {
    "description": "Redirect to Baidu",
    "request": {
      "uri": "/redirect"
    },
    "redirectTo": "http://www.baidu.com"
  },

  {
    "description": "Redirect to a web page of your own",
    "request": {
      "uri": "redirect/topath"
    },
    "redirectTo": "redirect/new"
  },

  {
    "description": "This is the request that was redirected to",
    "request": {
      "uri": "redirect/new"
    },
    "response": {
      "text": "Redirect succeeded!!!"
    }
  }
]

Keywords: JSON github Java

Added by kavisiegel on Fri, 19 Jun 2020 07:39:04 +0300