Moco as a mockserver quick start

Source: Internet
Author: User
Moco Exploration What's a Moco?

A simple structure of the simulation server library Ortools.

Https://github.com/dreamhead/moco
Moco Installation Configuration 1. Download Jar
Https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0-standalone.jar
2. Compile and runConfigure Java environment variables to install and configure Gradle (ref:http://www.gradle.org) and get the source code: Https://github.com/dreamhead/moco into the code directory,
./gradle Build

Compose JSON

[{"Response": {"text": "Hello, Moco"}}]

And then write the JSON, and then you can start.

Java-jar Moco-runner-<version>-standalone.jar start-p 12306-c Foo.json

Then you can access the 3, advanced 3.1 Content via http://localhost:12306

Return response based on request content

[{'
  request ':
    {
      ' text ': ' foo '
    },
  ' response ':
    {
      ' text ': ' Bar '
    }
}]

Visit: http://localhost:12306

If the request content is too much, you can put it in a file

{"
  request":
    {
      "file": "Foo.request"
    },
  "response":
    {
      "text": "Bar"
    }
}
3.2 Configuration Files

Ps:moco supports dynamic loading of configuration files, so whether you modify or add a profile, you do not need to restart the service

Moco supports the introduction of additional configuration files in a global configuration file so that you can define profiles for services

For example, if you have two projects boy and girl projects need to use the same mock Server, you can define Boy.json and Girl.json profiles separately, and then introduce them in the global file: The global configuration is as follows:

[{context: '/boy ', ' include ': ' Boy.json '}, {' context ': '/girl ', ' include ': ' Girl.json '}]

In Boy.json and Girl.json, respectively, define:

Boy
[{"Request": {"uri": "/hello"}, "response": {"text": "I am a Boy."}]
Girl
[{"Request": {"uri": "/hello"}, "response": {"text": "I am a Girl."}]

The global configuration file needs to be loaded via parameter-G (-g only used in the 3.1context section)

Java-jar Moco-runner-<version>-standalone.jar start-p 12306-g Onecoder.json

After the successful start, we can get the corresponding reponse result by accessing the service through Http://localhost:12306/girl/hello and Http://localhost:12306/boy/hello respectively. In fact, the introduction of the global file has direct include, etc., but onecoder feel that the context of this approach should be more commonly used,

Request parameters

Request naturally has a lot of parameters, configuration as follows:

[{"Request": {"uri": "/getboy", "Queries": {"name": "Onecoder"}}, "response": {"text": "Hey."}]

The URL that matches the above configuration is: Http://localhost:12306/getBoy?name=onecoder, the return value is: Hey. In other words, you need to have fixed test parameters and parameter values in the development period.

The rest-style Url,moco support is a regular match.

[{"Request": {"uri": {"match": "/searchboy/\\w+"}}, "response": {"text": "Find a Boy."}]

At this point, the access http://localhost:12306/searchboy/* end plus any parameters can be matched to.

In addition to get, Post,put,delete and other request modes are naturally supported:

[{"
  request":
    {
      "method": "Post",
      "forms":
        {
          "name": "Onecoder"
        }
    },
  " Response ": 
    {
      " text ":" Hi. "
    }}]

The configuration of request information such as header and cookies is also supported. 3.3 Template

Starting with version 0.8, Moco provides a template feature that dynamically returns some parameter values. For example:

[
    {"
    request": {
             "uri": "/template"
                   },
    "response": {
           "text": {"
               template": "${ req.queries[' name '} '}}}
]

At this point, the Onecoder is returned by Url:http://localhost:12306/template?name=onecoder access. This makes it possible to return some values flexibly in this way by template. 3.4 redirect

[{"Request": {"uri": "/redirect"}, "Redirectto": "Http://www.coderli.com"}]
3.5 Asynchronous
[
    ' request ': {
        ' uri ': '/event '
    },
    ' response ': {
        ' text ': ' Event '
    },
    ' on ': {
        ' Complete ': {
            "async": "True",
            "post": {
                "url": "Http://another_siter",
                "content": "Cintent"
            }
        }
    }
] 

In this way, access to/event will be asynchronous. To wait until the end of the Http://another_siter visit, the results will be placed in the response Help URL

Https://github.com/dreamhead/moco/blob/master/moco-doc/socket-apis.md
Https://github.com/dreamhead/moco/blob/master/moco-doc/rest-apis.md
Https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.