Response and assertion

In “Sending HTTP Requests,” we looked at viewing response data in APIPOST.

API Request Response

After clicking the send button, if there is data returned, the returned data, response time, response code, Cookie and so on will be displayed.

Note: The returned data defaults to == beautify == mode for viewing in JSON XML format. You can view types of other types by toggling == native == or == preview == mode.

Returns the Headers

In addition to viewing results, ApiPost also provides powerful test validation capabilities. Here we can also use assertions to validate response results.

The response results are displayed in separate screens

After APIPOST version 5.4, “split screen display of response results” is supported to increase workspace space.

image.png

What is an assertion?

Collaborative development, version upgrade, server upgrade, interface return may be different from what we expected due to some bugs. In order to facilitate the development and testing personnel can find bugs faster, is conducive to the whole product quality and progress assurance. We launched assertions.

How are assertions used?

  1. Define test cases
  2. Verify test cases

For example, the interface returns:

{ "errcode": 0, "errstr": "success", "post": [], "get": [], "request": [], "put": "", "header": { "Host": "echo.apipost.cn", "Connection": "keep-alive", "Content-Length": "0", "Accept": "application/json, text/javascript, */*; Q =0.01", "accept-encoding ": "gzip, deflate, br"," accept-language ": "zh-cn "," content-type ": "application/json", "Cookie": "PHPSESSID=n3k73k06o6ghnie4e9re4rbf0t", "Origin": "Https://echo.apipost.cn", "the user-agent: Mozilla / 5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, Like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"}}Copy the code

Define test cases:

apt.assert('response.raw.status==200');
apt.assert('response.raw.type=="json"');
apt.assert('response.json.errcode==0');
apt.assert('response.raw.responseTime<100');
apt.assert('response.json.header.Host=="echo.apipost.cn"');
Copy the code

After clicking the Send button:

Green means the test passes, red means the test fails.

Special note: == Each test case is a single line, not a newline. = =

Example: apt. Assert (‘ response. Json. The header. The Host = = “echo. Apipost. Cn” ‘);

1) response. Json. The header. The Host said the json response below the Host field in the header array, 2) must be 1, will pass.

Common test cases can be obtained by executing scripts after:

More response parameter variables

Response. raw: raw response data

Call example:

Response. Raw. The status / / response status code (200, 301, 404, etc.) the response. Raw. The responseTime / / response time (milliseconds) response. Raw. Type / / response types (json, etc.) The response. Raw. The responseText / / response textCopy the code

Response. json: Response data in JSON format

Call example as above:

Response.json.data. token // Response.json.data ["token"]Copy the code

Response. headers: indicates the response head

Call example:

Response.headers. Server // Response.headers ["server"]Copy the code

Response. cookies: Indicates a response cookie

Call example:

The response. Cookies. PHPSESSION / / also can response. Cookies [" PHPSESSION "]Copy the code

Common assertion expressions

1. Check if the Response Body contains a string

apt.assert('response.raw.responseText=="test"'); / / check whether the response text is equal to the test string apt. Assert (' response. Raw. The responseText. IndexOf (" test ") > - 1 '); // Check whether the response text contains the test stringCopy the code

2. Check whether a value in the returned JSON is equal to the expected value

apt.assert('response.json.hasOwnProperty("errcode")'); // Check whether the returned JSON object contains the errcode field apt.assert(' Response.json. errcode=="success"'); / / testing errcode column is equal to the success of a json object string apt. Assert (' response. Json. Errcode. IndexOf (" success ") > - 1 '); // Check whether the errcode field of the returned JSON object contains the success string apt.assert('response.json.errcode! ="success"'); // Check if the errcode field of the returned JSON object is not equal to the success string apt.assert('response.json.errcode>=1'); // Check whether the errcode field of the returned JSON object is greater than 1 apt.assert(' Response.json. errcode==null'); // Check whether the errcode field of the returned JSON object is nullCopy the code

Test whether an element of response Headers exists (e.g. Content-type)

apt.assert('response.headers.hasOwnProperty("content-type")');
Copy the code

4. Verify that the value of the Status code is 200

apt.assert('response.raw.status==200');
Copy the code

5. Verify whether Response time (request time) is greater than a certain value

apt.assert('response.raw.responseTime>=100');
Copy the code

6. Verify that the return type is JSON

apt.assert('response.raw.type=="json"');
Copy the code