API Testing Slides
API Testing Slides
API Testing
Nezam Academy
1.
What is API
2
What is API
3
What is API
Request
Request
Program API Server
Response
Response
4
What is API
Request
Request
Program API Server
Response
Response
Request
Request
API
Response
Response 5
2.
API VS Web Service
6
API VS Web Service
API
◎ Web service is used for REST, SOAP while API is used
Web
Service
for any style of communication.
◎ Web service supports XML while API supports XML and JSON.
◎ All Web services are APIs but all APIs are not web services.
7
3.
Online API & Offline API
8
Online API
◎ API that goes through the internet to get data = web services
Request
Request
API
Response
Response
9
Offline API
Request
Request
API
Response
Response
10
4.
HTTP
11
HTTP
12
4.
HTTP Request
13
Request Line
◎ Request Line
○ Method URI HTTP-Version
14
HTTP Request Methods
◎ GET
○ The GET method used to only retrieve data.
◎ HEAD
○ asks for a response identical to a GET request
○ checking what a GET request will return before actually making a GET
request - like before downloading a large file
◎ PUT
○ The PUT method update data on the server
◎ POST
○ The POST method submits data to the server
◎ DELETE
The DELETE method deletes the specified resource.
15
URI: Universal Resource Identifier
◎ Absolute URI
○ scheme://hostname[:port]/path
◎ Relative URI
○ /path
16
HTTP Version Number
◎ HTTP/1.0 or HTTP/1.1
◎ HTTP 0.9 did not include a version number in a request line.
◎ If a server gets a request line with no HTTP version number, it assumes
0.9.
17
Header Lines
◎ Example:
○ Accept: text/html
○ Host: www.rpi.edu
○ User-Agent: Mozilla/4.0
18
Blank Line
19
Content
20
4.
HTTP Response
21
Response Status Line
◎ Status Code is a
○ 3 digit number (for computers)
○ Message is text (for humans)
22
HTTP Response Status Code
◎ 5xx server error : the server failed to fulfil an apparently valid request
23
Response Headers
24
Blank Line
25
Content
26
6.
HTTPS
27
HTTPS
The S in HTTPS stands for "secure" HTTPS uses TLS (or SSL) to encrypt HTTP requests and
responses, instead of the text an attacker would see a bunch of seemingly random
characters. For example :
Host: www.example.com
Accept-Language: en
“t8Fw6T8UV81pQfyhDkhebbz7+oiwldr1j2gHBB3L3RFTRsQCpaSnSBZ78Vme+DpDVJPvZdZUZHpzbbc”
28
6.
Idempotency and Safety
29
Idempotency and Safety
Idempotency and safety are properties of HTTP methods. The HTTP RFC defines these
properties and tells us which HTTP methods are safe and idempotent.
◎ Safe HTTP methods
30
Safe
◎ Safe HTTP methods
○ HTTP methods are considered safe if they don't alter the server state. So safe methods can
only be used for read-only operations. The HTTP RFC defines the following methods to be
safe: GET, HEAD, OPTIONS and TRACE.
31
Idempotency
◎ Idempotent HTTP methods
○ Idempotency means that multiple identical requests will have the same outcome. So it does
not matter if a request is sent once or multiple times. The following HTTP methods are
idempotent: GET, HEAD, OPTIONS, TRACE, PUT and DELETE. All safe HTTP methods are
idempotent but PUT and DELETE are idempotent but not safe.
32
Summary
PUT No Yes
DELETE No Yes
POST No No
PATCH No No
33
API Data
1.
XML
35
XML
36
XML Example
<Employees>
<Employee>
<id> 1 </id>
<Name> Ahmad </Name>
<Position> Manager </Position>
</Employee>
</Employees>
37
1.
JSON
38
JSON
39
JSON Example
◎ String : “Ahmed”
◎ Object : {“key” : “value”} >> {“user name”: “Ahmed”}
◎ List : [“1”, “2” , “3”]
40
JSON Example
{“Employees” :
{“Employee”
{“Id” : “1” },
{“Name”: “Ahmad” },
{“Position” : “Manager” },
}
}
41
1.
SOAP API
42
SOAP API
◎ SOAP can only work with XML format. As seen from SOAP
messages, all data passed is in XML format.
43
SOAP API
◎ SOAP requests are usually sent with the POST method rather than the GET
method because they have to send so much XML data. It is more difficult to send
that data via a GET request, and so most SOAP services require the requests to be
sent using the POST protocol.
◎ For example, I will use the country info service to get a list of continents by
name. The base page for that service is here:
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso.
instead of having the request method as a GET request, you will need to set the
request method to POST and then put in the URL.
44
1.
REST API
45
REST API
◎ REST stands for Representational State Transfer
◎ REST permits different data format such as Plain text, HTML, XML, JSON, etc.
But the most preferred format for transferring data is JSON.
46
1.
Postman
47
API Testing
◎ API Testing : is a type of software testing that test API
◎ Usage :
○ Check functionality, reliability and security
◎ For example :
○ When sending request, is status code is correct ?
○ Is data in response is relevant ?
○ If send same request for 10 times, is response have same result ?
48
API Testing
49
API Testing
◎ Now we will test all methods in this API
50
Get All Students
51
Get Specific Student
52
Add new Student
◎ Choose POST method and write /Students at the end of API then we choose
Body and click on raw >> JSON from drop down
◎ Click save
◎ Create new collection
◎ Give name to request
◎ Create
56
Tests in Postman
57
Test Status code = 200
58
Test Time < 3000 millisecond
59
Test Retrieval Data
◎ Need to check that first name is Ahmed
60
Test Result
◎ After run the test we will see the result in test result below
61
Run all requests
62
Run all requests
63
Using Parameters in Postman
64
Environment Variable
◎ Variables enable you to store and reuse values in Postman. By storing a value
as a variable, you can reference it throughout your collections, environments,
requests, and test scripts.
65
Environment Variable
66
Environment Variable
◎ Now we can choose environment and use its variables using {{variable}}
67
Set Environment Variable
◎ Example : let’s add student then save his id in environment to use it in any
future request instead of write his id manually.
68
Set Environment Variable
69
Set Environment Variable
◎ In Tests :
70
Set Environment Variable
71
Set Environment Variable
72
Global Variables
pm.globals.set("SiteName", "Nezam");
73
Global Variables
74
MockAPI in Postman
75
MockAPI in Postman
76
MockAPI in Postman
77
1.
Authentication
78
Authentication
◎ The API authentication process validates the identity of the client by
sending his username and password
79
Authorization
◎ Authorization is a process of allowing or denying someone from
accessing something
80
Authentication vs Authorization
◎ Authentication tells who you are while Authorization tells what you
can do.
81
Authentication
◎ So if we try this request https://postman-echo.com/basic-auth
82
Authentication
◎ To authorize request click on authorization tab and choose basic
auth and type username:postman password:password
83
Authentication
◎ After Run
84
1.
Status Code in depth
85
Status Code in depth
◎ 5xx server error : the server failed to fulfil an apparently valid request
86
1xx Status Codes
◎ 100: “Continue” This means that the server in question has received your browser’s request
headers, and is now ready for the request body to be sent as well. For example :
◎ 101: “Switching protocols” Your browser has asked the server to change protocols, and the
◎ 201: “Created.” The server has fulfilled the browser’s request, and as a result, has created a new resource.
◎ 202: “Accepted.” The server has accepted your browser’s request but is still processing it. The request
◎ 203: “Non-Authoritative Information.” It means that the proxy server received a 200 “Everything is OK”
status code from the origin server, but has modified the response before passing it on to your browser.
88
2xx Status Codes
◎ 204: “No Content.” This code means that the server has successfully processed the request, but is not going
◎ 205: “Reset Content.” Like a 204 code, this means that how server has processed the request but is not
going to return any content. However, it also requires that your browser resets the document view.
◎ 206: “Partial Content.” A 206 code is sent when a range header causes the server to send only part of the
requested resource.
89
3xx Status Codes
◎ 300: “Multiple Choices.” Sometimes, there may be multiple possible resources the server can respond with to fulfill your
browser’s request. A 300 status code means that your browser now needs to choose between them.
◎ 301: “The requested resource has been moved permanently.” This code is delivered when a web page or resource has been
permanently replaced with a different resource. It is used for permanent URL redirection.
◎ 302: “The requested resource has moved, but was found.” This code is used to indicate that the requested resource was
found, just not at the location where it was expected. It is used for temporary URL redirection.
◎ 303: “See Other.” a 303 code tells your browser that it found the resource your browser requested via POST, PUT, or DELETE.
However, to retrieve it using GET, you need to make the appropriate request to a different URL than the one you previously
used.
90
3xx Status Codes
◎ 304: “not modified This code tells the browser that the resources stored in the browser cache. It’s used to
◎ 307: “Temporary Redirect.” This status code has replaced 302 “Found” as the appropriate action when a
resource has been temporarily moved to a different URL. Unlike the 302 status code, it does not allow the
◎ 308: “Permanent Redirect.” The 308 status code is the successor to the 301 “Moved Permanently” code. It
does not allow the HTTP method to change and indicates that the requested resource is now permanently
91
4xx Status Codes
◎ 400: “Bad Request.” The server can’t return a response due to an incorrect syntax
◎ 401: “Unauthorized” or “Authorization Required.” This is returned by the server when the target resource
◎ 402: “Payment Required.” Originally, this code was created for use as part of a digital cash system.
○ You’ve reached your daily request limit to the Google Developers API.
○ You haven’t paid your Shopify fees and your store has been temporarily deactivated.
○ Your payment via Stripe has failed, or Stripe is trying to prevent a fraudulent payment.
92
4xx Status Codes
◎ 404: “The requested resource was not found.” This is the most common error message of them all. This
code means that the requested resource does not exist, and the server does not know if it ever existed.
93