API Testing Basics
API Testing Basics
API Testing 1
What is an API?
An API (Application Programming Interface) is a set of rules, protocols, and
tools that allow different software applications to communicate with each other.
It specifies how software components should interact and is used to request
and exchange data between systems. APIs are the backbone of modern web
development, enabling functionalities like fetching data from a server, sending
data, or interacting with various services such as payment gateways,
databases, and cloud services.
1. Protocol (Scheme)
The protocol refers to the communication method used to transfer data over
the web. It defines how the data will be transmitted from the client to the server.
Common Protocols:
Example:
api.example.com in https://api.example.com
This domain indicates the server responsible for handling requests for the
API. It's part of the base URL used in all API calls.
3. Path
The path specifies the specific resource or endpoint on the server that the API
is interacting with. It is the part of the URL that comes after the domain and
usually corresponds to a specific functionality or object within the API.
Example:
/users/1234 in https://api.example.com/users/1234
Common Patterns:
Example:
https://api.example.com/users?age=25&city=NewYork
In this example:
Characteristics:
Filtering: https://api.example.com/products?category=electronics
Pagination: https://api.example.com/items?page=2&limit=10
Sorting: https://api.example.com/orders?sort=desc
API Testing
API Testing is the process of testing APIs to ensure they function as expected,
meet business requirements, and are free of bugs. Since APIs are integral to
the functionality of applications, API testing is essential for ensuring data
integrity, security, and performance.
2. Security Testing: Ensuring that the API is secure against threats like
unauthorized access or data breaches.
4. Load and Stress Testing: Testing the limits of the API to see how it behaves
under heavy load or stress.
5. Error Handling: Ensuring the API returns appropriate error messages when
something goes wrong (e.g., invalid input, resource not found).
6. Data Validation: Ensuring that the API returns the correct data, formatted
properly, and within the expected range.
Protocol: https
Domain: api.example.com
Path: /users/1234
Query: age=25&city=NewYork
What is a Client?
The client is the front-end or user interface of an application where end users
interact with the system. It is responsible for making requests to a server
(typically via an API), displaying results to the user, and capturing user input.
Types of Clients:
Web Clients: Browsers like Chrome, Firefox, Safari, etc., where users
interact with web applications.
Mobile Clients: Mobile apps for Android and iOS that interact with APIs
over the network.
Client Responsibilities:
User Interaction: Clients provide a graphical user interface (GUI) where
users can input data and view output.
Data Collection: Clients gather user data (like form submissions, clicks,
etc.).
API Requests: Clients send requests to the API to fetch or modify data from
the server.
Data Display: After receiving data from the API, the client presents it in a
meaningful way to the user.
What is an API?
An API (Application Programming Interface) is a set of rules and protocols that
allows software systems to communicate with each other. APIs define the
API as an Interface: An API acts as a bridge between the client and the
server, allowing the client to access and interact with the server’s data,
functionality, or services without needing to understand the internal
workings of the server.
Types of APIs:
API Responsibilities:
Request Handling: APIs receive HTTP requests from the client (e.g., GET ,
POST , PUT , DELETE ).
Business Logic: APIs may perform some business logic or data validation
before sending or updating data in the database.
Data Interaction: APIs interact with the database (or other services) to
retrieve, insert, update, or delete data.
Response to Client: After processing the request, the API sends a response
back to the client in a structured format (usually JSON or XML).
Example: A weather app (client) may use a weather API to fetch the current
weather for a given city. The API processes the request and returns the
weather data (temperature, humidity, etc.) to the client, which then displays it to
the user.
3. Database
What is a Database?
A database is a structured collection of data that is stored and managed in a
way that allows efficient retrieval, insertion, updating, and deletion. Databases
Types of Databases:
Database Responsibilities:
Data Storage: Databases store the application’s persistent data (e.g., user
accounts, products, order history).
Data Integrity: Ensure that data remains consistent, accurate, and secure.
Databases have mechanisms like constraints and foreign keys to enforce
data integrity.
Data Retrieval: Databases allow for querying data via SQL or other query
languages.
A user interacts with the client (e.g., filling out a form or clicking a button).
The client then makes a request to the API. For example, the client might
send a GET request to fetch user details.
Example Request:
The API receives the client request and processes it. If the request involves
retrieving data, the API will query the database. For instance, if the request
is to get details about user 1234 , the API will need to access the database to
fetch the relevant data.
Example Response:
{
"id": 1234,
"name": "John Doe",
"email": "[email protected]"
}
To fulfill the request, the API communicates with the database. For example,
if the API is requested to provide user data, it will use SQL (in a relational
database) or a query language specific to a NoSQL database to retrieve
that data.
Example SQL Query:
Example of Interaction:
The user enters their login credentials on the client interface (e.g., a
login form on a website or mobile app).
The client sends a POST request to the API with the login credentials.
Example Request:
POST https://api.example.com/login
Content-Type: application/json
Body:
{
"username": "johndoe",
"password": "password123"
}
2. API:
The API receives the request and performs necessary checks (e.g.,
validate the credentials).
The API queries the database to check if the username and password
match any records.
3. Database:
The database retrieves the relevant user data (e.g., user ID, name,
email) from the users table.
4. API Response:
The API returns a success message and user data if the credentials are
valid.
Example Response:
{
"status": "success",
5. Client:
The client displays the user’s details or redirects the user to the main
dashboard, showing the retrieved information.
Conclusion
In summary:
The Client is the interface that users interact with to input data and see
results.
The API acts as the intermediary that processes requests from the client
and interacts with the database or other services to fetch or update data.
The Database stores the application’s data and provides it to the API when
requested.
URN -
3.path
4.Query (After Question mark )