HelloGitHub- Carmen
Here is the "Explain the Open Source Project" series launched by HelloGitHub. Today I will bring you an open source and free tool to simulate the back-end API: moco
Those who have never learned back-end development can quickly get started with this open source project. The pretty girls no longer have to wait for the back-end development API, so they have more time to visit HelloGitHub to experience more interesting open source projects. Next, this article will take you to quickly get started with moco, an open source tool, so that you will no longer be stuck in the development progress of the back-end interface, so you can do it!
Project address: https://github.com/dreamhead/moco
1. What is the use of moco
I do front-end or client-side development, what use is it for me?
When the back-end API development is slow, if you want to test the display effect of the application, you don't have to wait for the back-end progress anymore. Use moco to easily simulate the back-end API.
At the beginning of the project, the product manager or the customer wants to see your application display, simulate the API and then develop the front-end to show the effect.
I do back-end development, what use is it for me?
Enterprise-level software is generally developed by multiple people, because the interfaces are dependent on each other, so if the service you rely on is slow or not running in the environment, you will not be able to test the functions you developed and will not be delivered in time Project, thus working overtime and staying up late.
Even if the services you depend on are running in the environment, the services you depend on will still be constantly tested and tuned. This process may also cause problems in your development of functional tests. A stable test interface reduces your waiting time.
2. Quick start
2.1 Preparation
JDK 1.8+ (推荐1.8版本)
2.2 Download the jar package
Click here to download the jar package
2.3 API configuration file
New hello.json
file, writes the following
[{
"description": "moco 快速开始示例",
"request": {
"uri": "/hello"
},
"response": {
"text": "Hello GitHub"
}
}]
The directory structure is as follows
├── hello.json // API 接口配置文件
├── moco-runner-1.1.0-standalone.jar // 下载的模拟 API 的工具
2.4 Run the project
Run in this directory
java -jar moco-runner-1.1.0-standalone.jar http -p 9999 -c hello.json
moco-runner-1.1.0-standalone.jar: The path of the running program (the path of the package just downloaded)
http: select the service type (http, https, socket)
-p 9999: set service port 9999
-c hello.json: Set the configuration file path (the configuration file just created)
2.5 Effect display
Visit the address in the browser
localhost:9999/hello
The effect is shown in the figure
Three, detailed usage
You should be able to simulate a simple back-end API very easily. Is it a sense of accomplishment? But if you have used or developed a back-end API, you probably know that a qualified back-end API should not be limited to this. A qualified back-end API should include: request method, request URL, request parameters, request header, request body, return status code, return prompt information, return header, and return body.
How to use moco, an open source project, to simulate a qualified back-end interface? Next, I will take you step by step to understand the detailed usage.
3.1 Basic structure
[
{
"description": "moco 基本结构",
"request": {
"uri": "/hello",
"method": "post"
},
"response": {
"text": "Hello GitHub"
}
}
]
The most layer is a json file
[]
array, a plurality of which may be encapsulated API (only one exemplary API)Because json profiles do not support comments, so this API you can write notes
description
insiderequest
Can contain all requested contentresponse
Can contain everything returned
3.2 Simulate a basic RESTful API
[{
"description": "模拟一个基本的 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": "测试成功"
},
"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": "/"
}
}
}
}]
method
: Request methodheaders
: Request headercookies
: Request Cookiesjson
: One type of the request body (alsofroms
form other types)response
The return valueheaders
,json
,cookies
is similarlatency
The simulated server freezes (because the simulated back-end API returns data almost instantaneously, here we let it freeze 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 results
Click Send to send, and check the test results in the response below
View the returned request header
View returned cookies
View global cookies
3.3 Attachment download
Sometimes we need to simulate file download, how does moco implement it?
[{
"description": "moco 附件下载",
"request": {
"uri": "/hello"
},
"response": {
"attachment":{
"filename": "demo.txt",
"file": "demo.txt"
}
}
}]
File Directory
├── hello.json // API 接口配置文件
├── moco-runner-1.1.0-standalone.jar // 模拟 API 的工具
├── demo.txt // 要下载的文件,这里可以使用相对路径
localhost:9999/hello
You can download demo.txt
the file
3.4 Polling data
If we refresh the page and want to get different content, how does moco do it?
[{
"description": "moco 轮询数据",
"request": {
"uri": "/hello"
},
"response": {
"cycle": [{
"text": "hello 1"
},
{
"text": "hello 2"
},
{
"text": "hello 3"
}
]
}
}]
Access localhost:9999/hello
will in turn get the following
hello 1
hello 2
hello 3
hello 1
hello 2
...
3.5 Redirect
Sometimes we want to redirect the page. How does moco do it?
[{
"description": "moco 重定向",
"request": {
"uri": "/hello"
},
"redirectTo": "https://hellogithub.com"
}]
Access localhost:9999/hello
will be automatically redirected tohttps://hellogithub.com
3.6 Regular expressions
moco also supports some operators, such as regular expressions.
[{
"description": "moco 正则表达式",
"request": {
"uri": {
"match": "/hello/\\w*"
}
},
"response": {
"text": "Hello GitHub"
}
}]
It can be accessed through regular expression matching links, such as
localhost:9999/hello/jarvan
localhost:9999/hello/bmft
3.7 Using templates
Sometimes we returned parameters depend on the request parameters (such as the encoding type), this time we can use template template to achieve, we can in the template req
to indicate the request sent.
{
"description": "moco 使用模板",
"request": {
"uri": "/hello",
"method": "post"
},
"response": {
"text": {
"template": "${req.method}"
}
}
}
The value returned is
{
"text": "post"
}
Four, finally
Seeing this, you must already know the basic use of the open source project moco, do you find it interesting? Here is a small suggestion. If you want to really use this open source project moco, it is recommended to refer to the official documentation to "practice". This is the fastest and most effective way to use an open source project. "Practice" is the best way to consolidate, I hope you can experience the joy of designing procedures in practice!
At this point, thanks to the friends who love open source for reading. HelloGitHub will regularly introduce interesting open source and free projects on GitHub. If you are interested in open source projects, then follow us to receive the first article push.
Follow the official account to receive the push as soon as possible
If this article promising to bring you a little help, please help me in the lower right corner point a praise it!