HelloGitHub- garvin

Here is HelloGitHub’s “Explain Open Source project” series, today brings you an open source free simulation of back-end API tool: Moco

Those who have not learned backend development can also quickly get started with this open source project, and they don’t have to wait for back-end development API, so they can have more time to browse HelloGitHub to experience more interesting open source projects. This article will give you a quick start on moco, an open source tool that will get you off the back end of your interface development schedule.

Project address: github.com/dreamhead/m…

What is the use of MOCO

I do front-end or client development, what’s the use for me?

  1. If you want to test your application presentation when back-end API development is slow, you don’t have to wait for the back-end to progress and use MOCO to easily simulate the back-end API.
  2. At the beginning of the project, product managers or customers will want to see your application in action.

I do back-end development. What’s the use for me?

  1. Enterprise-level software tends to be multi-player because the interfaces are interdependent, so if the services you rely on are slow or not running in the environment, you can’t test the features you’re developing, deliver the project on time, and stay up late.
  2. Even if the services you rely on are running in the environment, the services you rely on are constantly tested and tuned, and this process can cause problems when you develop functional tests. A stable test interface to reduce your waiting time.

Start fast

2.1 Preparations

JDK 1.8+ (recommended version 1.8)Copy the code

2.2 Downloading jar Packages

Click here to download the JAR package

2.3 API Configuration file

Create a hello.json file and write the following

[{
	"description": "Moco Quick Start Example"."request": {
		"uri": "/hello"
	},
	"response": {
		"text": "Hello GitHub"}}]Copy the code

The directory structure is as follows

├ ─ ─ hello. Json// API interface configuration file├ ─ ─ moco - runner1.1. 0-standalone.jar       // Download a tool to simulate the API
Copy the code

2.4 Running Projects

Run in that directory

Java-jar moco-runner-1.1.0-standalone. Jar HTTP -p 9999 -c hello. JsonCopy the code
  • Moco-runner -1.1.0-standalone. Jar: Path to run the program (path to the package you just downloaded)

  • HTTP: Select the service type (HTTP, HTTPS, or socket)

  • -p 9999: sets the service port 9999

  • -c hello.json: Set the configuration file path (newly created configuration file)

2.5 Effect Display

Access the address in your browser

localhost:9999/hello
Copy the code

The effect is shown below

Three, detailed usage

You should be able to easily emulate a simple back-end API. Isn’t that rewarding? But if you use or develop back-end apis, you probably know that a qualified back-end API should be more than that. A qualified backend API should include: request method, request URL, request parameters, request header, request body, return status code, return prompt, return header, and return body.

How do you use moCO, an open source project, to simulate a qualified back-end interface? Here is a step-by-step guide to the details.

3.1 Basic Structure

[{"description": "Moco basic Structure"."request": {
      "uri": "/hello"."method": "post"
    },
    "response": {
      "text": "Hello GitHub"}}]Copy the code
  • The top layer of the JSON file is one[]Array that can encapsulate multiple apis (the example has only one API)
  • Since JSON configuration files do not support comments, you can write comments to this APIdescriptioninside
  • requestCan contain everything requested
  • responseYou can include everything returned

3.2 Simulate a basic RESTful API

[{
	"description": "Emulating a basic RESTful API"."request": {
		"uri": "/hello2"."method": "post"."headers": {
			"Content-Type": "application/json"."Accept": "application/json"."token": "header.playload.signature"."Accept-Charset": "utf8"
		},
		"cookies": {
			"login": "true"
		},
		"json": {
			"name": "zhangsan"."age": 13}},"response": {
		"json": {
			"message": "Test successful"
		},
		"latency": {
			"duration": 2."unit": "second"
		},
		"headers": {
			"Content-Type": "application/json"."token": "new-header.new-playload.new-signature"
		},
		"cookies": {
			"login": {
				"value": "true"."domain": "localhost"."secure": "true"."httpOnly": "true"."path": "/"}}}}]Copy the code
  • method: Request method
  • headers: request header
  • cookies: request Cookies
  • json: a type of request body (andfromsForms, etc.)
  • responseThe return value ofheadersjson,cookiesA similar
  • latencyThe emulated server is stuck (since the emulated back-end API returns data almost instantaneously, here we let it stall for 2 seconds)

test

Here we use the open source and free API testing software Postman on GitHub for testing

(1) URL, request method, request header and Cookies

(2) Request body (JSON)

(3) Test effect

Click Send to Send, and view the test results in response below

View the returned request header

View the returned Cookies

Viewing global Cookies

3.3 Attachment Download

Sometimes we need to simulate a file download. How does MoCO do that?

[{
	"description": "Moco Attachment Download"."request": {
		"uri": "/hello"
	},
	"response": {
		"attachment": {"filename": "demo.txt"."file": "demo.txt"}}}]Copy the code

File directory

├─ hello.json // API Config file ├─ Moco-runner - 1.0-standalone. Jar // Simulation API tools ├─ demoCopy the code

Localhost :9999/hello to download the demo. TXT file

3.4 Polling data

How does MOCO work if we want to refresh the page to get different content?

[{
	"description": "Moco Polling data"."request": {
		"uri": "/hello"
	},
	"response": {
		"cycle": [{
				"text": "hello 1"
			},
			{
				"text": "hello 2"
			},
			{
				"text": "hello 3"}}}]]Copy the code

A visit to localhost:9999/hello returns the following in sequence

hello 1
hello 2
hello 3
hello 1
hello 2.Copy the code

3.5 the redirection

Sometimes we want to redirect the page how does Moco do that?

[{
	"description": "Moco Redirection"."request": {
		"uri": "/hello"
	},
	"redirectTo": "https://hellogithub.com"
}]
Copy the code

Accessing localhost:9999/hello is automatically redirected to https://hellogithub.com

3.6 Regular Expressions

Moco also supports operators such as regular expressions.

[{
	"description": "Moco regular expressions"."request": {
		"uri": {
			"match": "/hello/\\w*"}},"response": {
		"text": "Hello GitHub"}}]Copy the code

Can be accessed via regular expression matched links, such as

localhost:9999/hello/jarvan
localhost:9999/hello/bmft
Copy the code

3.7 Using a Template

Sometimes our return parameters depend on the request parameters (such as the encoding type). In this case, we can use the template template, in which we can represent the sent request by req.

{
    "description": "Moco use template"."request": {
        "uri": "/hello"."method": "post"
    },
    "response": {
        "text": {
            "template": "${req.method}"}}}Copy the code

The value returned is

{
  "text": "post"
}
Copy the code

Four, the last

It’s interesting that you already know the basics of the open source project MoCO. Here’s a tip. If you want to actually use moCO, refer to the official documentation to “practice.” This is the fastest and most effective way to use open source projects. “Practice” is the best way to consolidate, I hope you can experience the happiness of designing procedures in practice!

At this point, thank you for loving open source partners reading. The public account HelloGitHub regularly introduces interesting open source and free projects on GitHub. If you are interested in open source projects, then follow us to receive the first post.

Reference:

GitHub: Official project documentation

Open Source China: Moco can easily build a test server