Mock service moco series (2) - Json format, File file, Header, Cookie, solving Chinese garbled characters

Table of contents

  • 1. Json format

  • 2. File file

  • 3、Header

  • 4、Cookie

  • 5. Solve Chinese garbled characters

1. Json format

1. Create the 04Json.json configuration file.

json is in Json format.

The content is as follows:

[
    {
        "description":"Json格式",
        "request":{
            "uri":"/json",
            "method":"get"
        },
        "response":{
            "json":{
                "username":"admin",
                "password":"123456"
            }
        }
    }
]

 

2. Enter the command to start the moco service.

java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 04Json.json

 

3. Postman accesses the moco service address.

Open the installed Postman.

The protocol type is selected as GET

Access address: http://localhost:8083/json

picture

 

Click Send, and the access result displays: output in Json format.

picture

 

 

2. File file

 

 

1. Create 05File.json, data.json, data2.json configuration files.

The request part file reads the data.json file as a request parameter.

The response part file reads the data2.json file as the response content.

05File.json content is as follows:

[
    {
        "description":"File文件",
        "request":{
            "uri":"/file",
            "method":"post",
            "file":{
                "json":"data.json"
            }
        },
        "response":{
            "file":"data2.json"
        }
    }
]

 

The content of data.json is as follows:

{
    "username":"admin",
    "password":"123456"
}

 

The content of data2.json is as follows:

{
    "username":"administrator",
    "password":"abcdef"
}

 

2. Enter the command to start the moco service.

java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 05File.json

 

3. Postman accesses the moco service address.

Open the installed Postman.

The protocol type is selected as POST

Access address: http://localhost:8083/file

Body adds data.json as a request parameter.

picture

 

Click Send, and the access result shows that the content of the data2.json file is the response content.

picture

 

 

3、Header

 

 

1. Create the 06Header.json configuration file.

headers: information header.

Add content-type to Json format in the headers of the request part, and send data in Json format.

The headers of the response part add the custom parameter Self-Header and its corresponding value, and the response content is in Json format.

The content is as follows:

[
    {
        "description":"Header",
        "request":{
            "uri":"/header",
            "method":"post",
            "headers":{
                "content-type":"application/json"
            },
            "json":{
                "username":"admin",
                "password":"123456"
            }
        },
        "response":{
            "headers":{
                "Self-Header":"MySelfHeader"
            },
            "json":{
                "username":"administrator",
                "password":"abcdef"
            }
        }
    }
]

 

2. Enter the command to start the moco service.

java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 06Header.json

 

3. Postman accesses the moco service address.

Open the installed Postman.

The protocol type is selected as POST

Access address: http://localhost:8083/header

Headers add key Content-Type, value application/json.

picture

 

Body adds the requested Json data.

picture

 

Click Send, and the access result shows:

Body content display is output in Json format.

picture

 

Headers displays information about the custom parameter Self-Header and its corresponding value.

picture

 

 

4、Cookie

1. Create the 07Cookie.json configuration file.

The configuration file has 2 interfaces (Get request with Cookie, Post request with Cookie).

cookies: cookie information.

status: Response status code.

The content is as follows:

[
    {
        "description":"Cookie(Get请求)",
        "request":{
            "uri":"/get/cookie",
            "method":"get",
            "cookies":{
                "login":"true"
            }
        },
        "response":{
            "text":"Moco Cookie"
        }
    },
    {
        "description":"Cookie(Post请求)",
        "request":{
            "uri":"/post/cookie",
            "method":"post",
            "cookies":{
                "login":"true"
            },
            "json":{
                "username":"admin",
                "password":"123456"
            }
        },
        "response":{
            "status":200,
            "json":{
                "admin":"success",
                "status":"1"
            }
        }
    }
]

 

2. Enter the command to start the moco service.

java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 07Cookie.json

 

3. Postman accesses the moco service address.

Open the installed Postman.

(1) Cookie (Get request)

The protocol type is selected as GET

Access address: http://localhost:8083/get/cookie

Headers add key Cookie, value login=true.

picture

 

Click Send, and the access result displays: output in Text format.

picture

 

(2) Cookie (Post request)

The protocol type is selected as POST

Access address: http://localhost:8083/post/cookie

Headers add key Cookie, value login=true.

Headers add key Content-Type, value application/json.

picture

 

Body adds the requested Json data.

picture

 

Click Send, and the access result shows:

Body content display is output in Json format.

picture

 

The status code is 200.

picture

 

 

5. Solve Chinese garbled characters

 

 

1. Questions:

When the response content contains Chinese (for example: the returned response content), the browser displays garbled characters when viewing it, as shown in the figure:

picture

 

2. Solve:

Add and set the encoding format to "GBK" in the configuration file response.

"headers":{"Content-Type":"text/html;charset=gbk"}

 

1. Create the 08CharsetGBK.json configuration file.

The content is as follows:

[
    {
        "description":"解决中文乱码",
        "request":{
            "uri":"/demo",
            "method":"get"
        },
        "response":{
            "headers":{
                "Content-Type":"text/html;charset=gbk"
            },
            "text":"返回的响应内容"
        }
    }
]

 

2. Enter the command to start the moco service.

java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 08CharsetGBK.json

 

3. The browser accesses the moco service address.

Access address: http://localhost:8083/demo

The result of the visit shows: Chinese display is normal.

picture

If you think the article is not bad, please  like, share , watch, bookmark  , because this will be the strongest motivation for me to continue to output more high-quality articles!

Here I recommend a software testing exchange group created by myself, QQ: 642830685. The group will share software testing resources, test interview questions and testing industry information from time to time. You answer questions.

Guess you like

Origin blog.csdn.net/m0_53918927/article/details/116667979#comments_27795007