Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to quickly simulate the back-end interface

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to quickly simulate the back-end interface". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to quickly simulate the back-end interface.

What is the use of moco

I do front-end or client-side development, what is the use to me?

When the back-end API development is slow, if you want to test the application demonstration, you don't have to wait for the back-end progress and 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 show, so you can develop the front end and show the effect by simulating the API.

I do back-end development, what is the use to me?

Enterprise software is generally developed by multiple people, because interfaces are interdependent, so if the services you rely on are slow or not running in the environment, you will not be able to test the functions you have developed. and then can't deliver the project in time, so you stay up late.

Even if the service you rely on runs in the environment, the service you rely on will continue to be tested and tuned, which can lead to problems with your development functional tests. A stable test interface to reduce your waiting time.

2. Quick start 2.1preparations JDK 1.8 + (recommended version 1.8) 2.2 download jar package

Click here to download the jar package

2.3 API profile

Create a new hello.json file and write the following

[{"description": "moco Quick start example", "request": {"uri": "/ hello"}, "response": {"text": "Hello GitHub"}}]

The directory structure is as follows

├── hello.json / / API interface configuration file ├── moco-runner-1.1.0-standalone.jar / / download tools to simulate API 2.4 run the project

Run under this directory

Java-jar moco-runner-1.1.0-standalone.jar http-p 9999-c hello.jsonmoco-runner-1.1.0-standalone.jar: the path to run the program (the path to the package you just downloaded)

Http: select a service type (http, https, socket)

-p 9999: set service port 9999

-c hello.json: set the configuration file path (the configuration file you just created)

2.5 effect display

Access the address in the browser

Localhost:9999/hello

The effect is shown in the figure

Third, detailed usage

You should easily simulate a simple back-end API just now, don't you have a sense of accomplishment? But if you have used or developed a back-end API, you may know that a qualified back-end API should not be limited to that. A qualified backend API should include: request method, request URL, request parameters, request header, request body, return status code, return prompt, return return and return body, and so on.

How to use moco as an open source project to simulate a qualified back-end interface? Next, I will show you the detailed usage step by step.

3.1 basic structure [{"description": "moco basic structure", "request": {"uri": "/ hello", "method": "post"}, "response": {"text": "Hello GitHub"}}]

The top layer of the json file is a [] array that can encapsulate multiple API (there is only one API in the example)

Since the json configuration file does not support annotations, you can write comments for this API into description.

Request can contain all the contents of the request

Response can contain all the content returned

Simulate a basic RESTful API [{"description": "simulate 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": "/"}]

Method: request method

Headers: request header

Cookies: request Cookies

Json: a type of request body (as well as froms forms, etc.)

Response headers json cookies

Latency simulation server stutter (because the simulated back-end API returns data almost instantly, here we let it stutter for 2 seconds)

test

Here we use Postman, the open source and free API testing software on GitHub, to test.

(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 the response below

View the returned request header

View the returned Cookies

View global Cookies

3.3 attachment download

Sometimes we need to simulate file download, how to achieve moco?

[{"description": "moco attachment download", "request": {"uri": "/ hello"}, "response": {"attachment": {"filename": "demo.txt", "file": "demo.txt"}]

File directory

├── hello.json / / API interface configuration file ├── moco-runner-1.1.0-standalone.jar / / simulate the API tool ├── demo.txt / / the file to be downloaded, here you can use the relative path

Download the demo.txt file by localhost:9999/hello

3.4 polling data

How can moco be implemented if we refresh the page and want to get different content?

[{"description": "moco polling data", "request": {"uri": "/ hello"}, "response": {"cycle": [{"text": "hello 1"}, {"text": "hello 2"}, {"text": "hello 3"}]}]

When you visit localhost:9999/hello, you will get the following content in turn

Hello 1 hello 2 hello 3 hello 1 hello 2... 3.5 redirect

Sometimes we want to redirect the page. How can moco be implemented?

[{"description": "moco redirection", "request": {"uri": "/ hello"}, "redirectTo": "https://hellogithub.com"}]

Accessing localhost:9999/hello will automatically redirect to https://hellogithub.com

3.6 regular expression

Moco also supports some operators, such as regular expressions.

[{"description": "moco regular expression", "request": {"uri": {"match": "/ hello/\\ w*"}}, "response": {"text": "Hello GitHub"}}]

Can be accessed through links that match regular expressions, such as

Localhost:9999/hello/jarvan localhost:9999/hello/bmft3.7 uses templates

Sometimes our return parameters depend on the request parameters (such as encoding type), so we can use the template template to represent the sent request through req in the template.

{"description": "moco use template", "request": {"uri": "/ hello", "method": "post"}, "response": {"text": {"template": "${req.method}"}

The returned value is

{"text": "post"} Thank you for reading. The above is the content of "how to simulate the back-end interface quickly". After the study of this article, I believe you have a deeper understanding of how to quickly simulate the back-end interface, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report