REST ASSURED
REST ASSURED
APIs are like messengers between applications. Testing ensures these messages are
sent, received, and processed correctly.
In API testing, tools like Postman or Rest Assured are used to simulate these processes
and verify their accuracy without needing a UI (like the WhatsApp app).
Key Differences:
- Data format: REST supports JSON, making it faster, while SOAP only supports XML.
- Complexity: REST is simpler and more flexible; SOAP is more complex and rigid.
- Performance: REST is faster and lightweight compared to SOAP's heavy XML-based
messaging.
Rest Assured makes it easy to automate and validate APIs without requiring complex
setup or external tools.
What is REST?
- REST (Representational State Transfer) is an architectural style for building web
services. It relies on stateless communication and uses standard HTTP methods for
interactions. RESTful APIs allow clients to communicate with servers and access
resources in a simple and scalable way.
REST principles:
- Statelessness: Each request from a client to a server must contain all the information
the server needs to understand and respond to the request. The server does not store
any session information.
- Uniform Interface: The API should have a consistent and standardized way of
interacting, making it easier for developers to understand and use.
- Client-Server architecture: The client and server are independent of each other. The
client sends requests, and the server processes them and sends back responses.
Example:
In WhatsApp, when you send a message (client), the server processes it and sends it to
the recipient. The client doesn’t need to know how the server works, it just interacts
with the API.
Example Explanation:
1. Status Code: Shows if the request was successful.
2. Response Body: Contains the data returned from the server (e.g., list of messages).
3. Response Headers: Provides information about the response, like content type or
server details.
Example in code (using Rest Assured for extracting data from a JSON response):
Explanation:
1. In the JSON example, we use `jsonPath()` to access elements in the JSON response
(like the sender and message).
2. In the XML example, we use `xmlPath()` to access elements in the XML response.
This approach helps you extract specific data easily from the response, which is useful
when validating or processing API responses in your tests.