mock-server

1 Overview:

moco is to build a simple simulation server library / tool, mainly for testing integrated. This has received a lot of attention on Github Java-based development of open source projects. mock-server use is developed in the development process, the need to rely on part of the interface, but they did not provide the environment, or so the situation.

For example, the front-end development time in the development of the page, you need to provide server-side API interface to the server at this time is not developed, or that did not set up a test environment, this time a front-end development on their own mock api server, provide for ourselves call interface return data.

2, operating mode:

moco currently support a variety of use, they are the two most basic way: API and run independently. This article explains sever stand-alone building.

Stand-alone: ​​moco mainly through the configuration into a json file, start moco time, you need to specify the configuration file paths used, so the configuration can take effect.

3, MOCO shortcomings:

Use moco is very simple, configuration is also very convenient, is currently provides http, rest, socket services. But it is only able to stub out the interface, to simulate a simple scene. If you are receiving a request to do some processing, To query the database, calculates, or some complex operations can not do anything. So if the choice moco, depends on whether developers just need a simple simulation server

4, MOCO ready to build:

Step1: Configure jdk: open cmd window through java -version check for the successful installation of jdk.

Jdk download address is: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html , select the version for your computer system to download and install.

Jdk安装方法:
● windows:http://jingyan.baidu.com/article/bea41d435bc695b4c41be648.html
● linux:http://www.cnblogs.com/caosiyang/archive/2013/03/14/2959087.html

If the following message appears the instructions to install correctly. 

Step 2: Download moco-runner jar file, the current version is 0.11.0:
https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/moco-runner-0.11.0 -standalone.jar

MOCO foundation to build
Step1: create a new folder will be placed in 2.2 download moco-runner-0.11.0-standalone.jar files in this directory, and create json file in the directory named foo, this document as moco profile.

Step2: write a configuration file, as described in the following way to configure your moco services:

Step3: foo.json moco service start command input through a configuration file:
Java -jar moco-runner- <version> -C foo.json 12306 -standalone.jar HTTP -p
where <version> Jar package is downloaded replacing the version 0.11.0 here, so execution: the Java -jar Moco-Runner-0.11.0-standalone.jar HTTP foo.json -c -p 12306
-p port specify Moco service.

Moco-runner successful start is as follows:

It should be noted that the above command is executed, you need to cd to the directory where the Jar file, then foo.json files need to be placed in the same directory for the job, otherwise it will error. Or you can use the Jar file by file and .json absolute path.

json file path relative path may also be used, such as: ./ api / foo.json

Access moco-runner sever a postman:

If someone would like to visit, you need to provide native IP on it.

After starting the service, is bound to stub out according to the needs of a wide variety of interfaces feedback, moco service can detect the change of the configuration file, if you modified the configuration file without restarting moco, service can still take effect.

5, Advanced Operation:

Reference moco official document: https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md, the following few of the most common configuration.

1.url: Set uri in the request, the visit HTTP: // localhost: 12306 / foo , you can get to the content bar.
[
    {
        "Request": {
          "URI": "/ foo"
        },
         "Response": {
          "text": "bar"
        }
    }
]

2. Request with parameters, launch a browser and access the HTTP: // localhost: 12306 / foo parm = Blash?
[
    {
      "Request": {
          "uri": "/ foo",
          "Queries": {
              "param ":" Blash "
            }
        },
      " Response ": {
          " text ":" bar "
        }
    }
]

3.返回特定的status
[
    {
    "request" :
    {
      "uri" : "/foo"
     },
    "response" :
    {
      "status" : 503
    }
    } 
]

4.返回json数据
[
  {
      "request" :{
              "uri": "/getJson"              
       },
      "response" :{
          "json": {
                "foo": "bar"
               }              
      }
   }
]    

The different parameters returned by the different request, the following different parameters returned by the POST and GET.
[
   {
     "Request": {
     "URI": "/ test1",
     "Method": "the POST"
     },
     "Response": {
     "JSON": {
     "code": "200 is",
     "desc": "the POST Success "
     }
   }
   },   
   {
      " Request ": {
      " URI ":" / test1 ",
      " Method ":" the GET ",
    },
      " Response ": {
      " JSON ": {
      " code ":" 200 is ",
      " desc ":" GET success "



6.Json请求参数
[
   {
        "request": {
            "uri": "/json",
            "json": {
                "foo": "bar"
            }
        },
        "response": {
            "text": "foo"
        }
    }

]

 

6, the configuration file:

moco support the introduction of other profiles in the configuration file, so you can Excellent service custom profiles, easy to manage.

For example, you have two different paths API: http://xxx.com/path1/login  and  http://xxx.com/path2/pay  (login and payment interface).

Spoken in the previous section, we can write the two profile login and pay (or write together) are provided in the request for the url / path1 / login and / path2 / pay. If you need to test the interface a lot, is not conducive to management, and path1, path2 so confusing distributed in a different configuration file, for later want to change is also very convenient.

● correct posture should look like this:

Also written login.json and pay.json two configuration files, and then write a configuration file, configuration is as follows:

// config.json
[
   {"context":"/path1", "include":"login.json"},
   {"context":"/path2", "include":"pay.json"}
]

login and pay two files are no special requirements, and the wording as before. such as:

// pay.json
[
    {
        "request": {
            "uri": "/pay",
            "method": "POST",
            "json": {
                "phone":"18688886666",
                "password":"123456"
            }
        },
        "response": {
            "json": {
                "state":"0"
            }
        }
    }
]
// login.json
[
    {
        "request": {
            "uri": "/login",
            "method": "POST",
            "json": {
                "amount":"100"
            }
        },
        "response": {
            "json": {
                "state":"0"
            }
        }
    }
]

Then the command to start the service is moco:
the Java -jar moco-runner- <Version> -standalone.jar HTTP -p 12306 -g config.json

It should be noted that the last specified parameter is -g config.json!

If you just want to introduce more json file, then global configuration file may not use the context field. For example:
// do not use config.json context field.
[
    { "The include": "login.json"},
    { "the include": "pay.json"}
]

Guess you like

Origin www.cnblogs.com/summerxye/p/10975259.html