Charles common function record summary

1. Charles mock data:

Premise: Make sure that the mobile phone has successfully linked with the Charles agent on the PC.

In our usual business development, there are often cases where the end-end has been developed, but the back-end interface is not yet ready; or the interface needs to change data frequently to test various cases. In this way, if we rely too much on the interface, our development efficiency will definitely be greatly reduced, so we need mock data to improve development efficiency.

There are many open source mock frameworks on the Internet, such as Apifox , Apipost, etc. If you are interested, you can study it. Today I will introduce the use of Charles for data mocking.

1. Save the response data:

Select the interface → right click → select Save Response → save.

This saves the response data to a local file.

2. Turn on map mapping:

Select Tools → Map Local → check Enable Map Local 

 After selecting Add, the following figure appears, and fill in the interface information according to the prompts:

 This completes the response mock data mapping.

3. Modify the mock data:

For example: I changed the title field in the response data in step 1 from "register to receive 80RMB" to "login to receive 80RMB" , and only modify the locally stored response mock data and save it:

 You can see that the returned data has been changed.

4. Cancel the mock data:

If the interface is ok at this time and you want to cancel the mock data, follow the steps below:

Select Tools → Map Local → [Uncheck] Enable Map Local

2. Charles weak network test:

Charles can simulate various networks by modifying Throttle Setting, such as: 2G/3G/4G, etc. You can also manually set the network speed limit, etc. It is a better solution for us to develop/test simulate weak network environment and reproduce weak network problems:

The steps to use are as follows:

  1. Proxy → Throttle Setting
  2. Check Enable Setting
  3. Only for elected hosts Check it to add the interface that needs speed limit. If it is not checked, it means that all interfaces will implement the following speed limit settings
  4. Throttle preset can choose the network that has already been set up [after selecting this item for general weak network testing, the following content does not need to be modified]
  5. Requests such as opening apps/webpages will slow down. You can adjust the speed of the network by modifying the Throttle preset.

The opening and closing of Throttle can also be quickly operated in the following ways:

  • Method 1: Proxy →Start/Stop Throttle
  • Method 2: Charles kettle email→Throttling

  • Method 3: Click the little turtle on the upper part of the Charles panel--it turns green to open; gray to close.

3. Charles copy and use the curl command:

 In daily business development, we can use the curl command to send GET/POST requests directly on the terminal to quickly obtain the data returned by the server to view the data. So how to quickly obtain accurate and complete curl requests, you can quickly obtain them in Charles:

1. Select the corresponding interface (right click) → 2. Copy cURL Request

GET requests generally get (similar to) the following instructions:

curl -H  "Content-Type: application/json;charset=UTF-8" -H "appVersion: 923" -H "User-Agent: okhttp/3.8.0" -H "Host: www.testview.com" --compressed "https://www.testview.com/config/dialog-comm-test?deviceType=2&version=0&deviceId=8999899"

 If it's a POST, you get (something like) the following command:

curl -H  "Content-Type: application/json;charset=UTF-8" -H "appVersion: 923" -H "User-Agent: okhttp/3.8.0" -H "Host: www.testview.com" --data-binary "{\"type\":4,\"uid\":\"456\"}" --compressed "https://www.testview.com/config/dialog-comm-test?deviceType=2&version=0&deviceId=8999899"

Copy to the terminal → press Enter to quickly obtain the server data, and you can also perform data joint debugging by modifying the request parameters, which greatly improves the development efficiency.

reference: 

The most complete and detailed explanation of Linux curl command

4. Charles modify request parameters:

 If we want to view different data returned by the server by modifying the request parameters in our usual development and testing, we can do it in Charles in the following two ways:

method one,

1. Select the corresponding interface → 2. Click the pen as shown in the figure → 3. Modify the request parameters in the Compose area → 4. Execute

 Method two,

1. Select the corresponding interface → 2. Select Breakpoints【Breakpoint】

Then execute the interface request [real data request or click the arrow below]:

The breakpoint debugging Breakpoints page will appear, and then perform the following operations:

1. Select EditRequest → 2. Select Headers → 3. Modify request parameters in area 3 → 4. Execute

 This method is modified through breakpoint debugging, and specific breakpoint settings can also be performed in the steps below:

Five, Charles simulation blocking (Block) interface:

1. Select the corresponding interface (right click) → 2. Block List → 3. Re-request the interface:

After blocking the interface, re-request the interface data, and the response result is as follows:

It will be found that no data is returned, indicating that the block is successful.

This method can be used for some fault-tolerant tests, and then optimize the code to avoid displaying the bottom-up or fault-tolerant pages we want when the interface is really abnormal. 

Continuously updating...

Guess you like

Origin blog.csdn.net/u012440207/article/details/127389031