Interface and postman

1. What is an interface

Official: An interface is a service provided by hardware or software to the outside world.

Hardware interface: USB interface.

Software interface: api (application program interface)

 

cookie: generated on the server, saved on the client

session: generated on the server, stored on the server, but can be transmitted to the client through a cookie. sessionID

token: Authentication code, which can also be transmitted through cookies.

 

2. Why do you need an interface

Professional: Because the data inside the project can be modified externally.

 

3. Data format returned by the interface

json is a data format: integer, string

1.json format

{"error_code":"0","msg":"withdrawal success","data":"detailed data"}

error_code: error code, 0 success, 40001 error code

msg: Chinese description of the error code

data: specific data

 

JSON consists of key-value pairs {} and arrays[].

 

2.xml format

<xml>

<error_code>0</error_code>

<msg>Withdrawal succeeded</msg>

<data>Detailed data</data>

</xml>

 

3.html format

<html>

<head>

<title></title>

</head>

<body>

data

</body>

</html>

 

Fourth, the interface protocol

http: starts with http. return json format

webservice: starts with http and ends with ?wsdl. (web service description, language), returns the xml format.

duboo protocol: duboo: at the beginning, return json format

 

What is the http protocol?

Also known as: Hypertext Transfer Protocol, the main function is: the exchange of data between the browser and the server. Interaction data is divided into two parts: request and response.

request: request line, request header, blank line, request body

Response: response line, response headers, blank line, response body

Request line: request method, request address url, protocol

GET http://localhost/phpwind/index.php?m=u&c=login HTTP/1.1

Request header:

Host: localhost (host)

Connection: keep-alive (connection state, keep alive)

User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 (client-side user)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 (the client receives type of data)

Referer: http://localhost/phpwind/(来源)

Accept-Encoding: gzip, deflate, br (the compression method received by the client)

Accept-Language: zh-CN,zh;q=0.9 (language received by the client)

Cookie: Pycharm-a7943e9=c6763d48-d1f4-4903-b72d-738219304462; csrf_token=00af6710d4cf234e; z92_visitor=J4FIxKqz%2BYgWDGcoJzFV70Y1b2ka02%2BjPcVw6nwmEVE%3D; z92_lastvisit=6998%091593863870%09%2Fphpwind%2F

(cookie sent by client to server)

Skip a line

request body

----------------------------------------------------------------------------------------------------------------

Response line: protocol, status code, status information

200 success

30X redirect

40X The requested resource could not be found

50X server error

HTTP/1.1 200 OK

response header

Date: Sat, 04 Jul 2020 13:08:20 GMT (response time)

Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45 (Server)

X-Powered-By: PHP/5.4.45

Set-Cookie: z92_lastvisit=6998%091593868101%09%2Fphpwind%2Findex.php%3Fm%3Du%26c%3Dlogin; expires=Sun, 04-Jul-2021 13:08:21 GMT; path=/

Set-Cookie: z92_visitor=Ig2%2Bvhubk1sSLrLMujkCLPyXnWu%2FLhE41lYKXObcN8cC7bqaj1foBQ%3D%3D; path=/

Vary: Accept-Encoding (cookie sent by server to client)

Content-Length: 15322 (response in bytes)

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html;charset=utf-8 (type of response data)

Skip a line

response body

<!doctype html>

<html>

<head>

<meta charset="UTF-8" />

<title>登录 - phpwind 9.0 - Powered by phpwind</title>

<meta http-equiv="X-UA-Compatible" content="chrome=1">

 

Five, the implementation of the interface testing process in the enterprise

1. Check the api documentation, check the request address and sending method. Protocol, token, request parameters, what is the return value

2. Interface test plan, scheme, use case I and review.

Interface test solution (solution how to test):

Normal example:

1-2 pieces,

get (generally used to get data),

post (generally used to submit data),

put (generally used to modify data),

delete (generally used to delete data)

Counterexample:

Authentication code exception: authentication code is empty, wrong, expired....

Exception for parameters:

Parameters: required, abnormal length, abnormal type, abnormal format.

Error code coverage.

Other exceptions:

pagination (0, first page, middle page, last page, negative number, Chinese, special characters, letters)

3. Use the interface tool to perform interface testing

4. Submit the interface test report.

 

6. Perform interface testing

There are four types of post request parameters, and their options in postman.

 

get: pass parameters in params

post: in Body

form-data: pass key-value pairs and files.

x-www-....: pass key-value pairs

raw:传xml,json,javascript,html,txt

binaly: Pass binary files.

 

What is the difference between a get request and a post request?

1. The get request is generally to obtain data, and the post request is generally to submit the data.

2. The get request is not safe, and the post request is safe.

3. The essential difference: the way of passing parameters is different

get passes parameters in the form of ? after the request address, and then separates multiple parameters with &.

post it is passed parameters in the body.

 

7. Description of the Postman request tab and response tab.

Request tab:

params: parameters are passed in the get method.

headers: request headers

body: post parameters are passed.

Pre-request Script: The script before the request

Tests: Assertions after the request.

cookies: The dictionary data sent by the client to the server.

Code: Generate interface automation test scripts.

 

Response tab:

Body: return data

Pretty: Display the response in JSON or XML format.

Raw: Display in text format

PreView: Display as HTML web page

Cookies: Dictionary data sent by the server to the client.

Headers: response headers

Test Results: The result of the assertion

 

8. Assertions in Postman

Assertion: Used to determine whether the interface request is successful!

Minimum two:

Status Assertion: 200

Business Assertion: There can be more than one.

 

//Status assertion:

//Assert status code is 200

pm.test("Status code is 200"function () {

    pm.response.to.have.status(200);

});

// business assertion

//Assert that the returned result contains a string

pm.test("Body matches string"function () {

    pm.expect(pm.response.text()).to.include("access_token");

});

//Check the value of the returned JOSN data.

pm.test("Your test name"function () {

    var jsonData = pm.response.json();

    pm.expect(jsonData.expires_in).to.eql(7200);

});

//Assert the returned result is equal to a string

pm.test("Body is correct"function () {

    pm.response.to.have.body("{\"errcode\":0,\"errmsg\":\"ok\"}");

});

//Assert whether the response header contains Content-Type

pm.test("Content-Type is present"function () {

    pm.response.to.have.header("Connection");

});

//Assert that the request time of the interface is less than 200ms

pm.test("Response time is less than 200ms"function () {

    pm.expect(pm.response.responseTime).to.be.below(200);

});

 

Nine, global variables and environment variables

How is the associated interface tested? (How can the return value of the previous interface be used as the parameter of the next interface?)

1. Get the value through JSON in the Tests assertion of the previous interface

// extract via JSON

var jsData = JSON.parse(responseBody)

pm.globals.set("new_token",jsData.access_token);

 

//Extract by regular expression

var access_token = responseBody.match(new RegExp('"access_token":"(.+?)"'))[1]

pm.globals.set("new_token",access_token);

 

2. Get global variables through { { new_token }} in the next interface .

 

Development environment: for development

Test environment: for testing

Pre-release environment: pre-release the project before it goes live

Online environment: for users

 

How to get value when multiple levels of nesting

{

    "tags": [

        {

            "id"2,

            "name""Star Group" ,

            "count"0

        },

var jsData = JSON.parse(responseBody)

pm.globals.set("new_token",jsData.tags[0].id);

 

10. Project combat

{ {$timestamp}} server timestamp

{ {$randomInt}} random number from 0-1000

{ {$guid}} randomly generates a string

 

Postman: Born for Interfaces

newman: born for postman, execute non-GUI way

 

newman run "e:\yongli.json" -e "e:\\huanjing.json" -g "e:\quanju.json" -r cli,html,json,junit --reporter-html-export "e:\result.html"

 

Interface signature: sign

csrf_token: Authentication code

sign = MD5(appkey+parameter name+timestamp)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324328607&siteId=291194637