Testing an API involves ensuring that it works as intended, responds correctly to different inputs, and meets the established requirements. Special tools can be used to facilitate the testing process.
Request sending
Sending a request in an API involves a client sending an HTTP request to an API endpoint. This includes details like headers, parameters, and sometimes a request body, in order to initiate an action or retrieve data from the server.
Basic API testing tools like Postman, cURL, Swagger, and libraries in various programming languages provide the capability to send requests to our restaurant website API. Swagger facilitates collaboration between QA teams and developers. Developers can add documentation to the API using Swagger UI, and QA can send requests directly from it. Here's an example of Swagger's collection: https://petstore.swagger.io/.
These tools enable you to get data, add items, update info, and check different scenarios. By thoroughly testing the API, you can ensure its effective operation and a pleasing user experience.
Postman is a highly popular API testing tool due to its ease of use and multitude of features. You can quickly create requests by entering the endpoint URL, choosing the HTTP method, and adding any necessary headers or parameters. Postman lets you send multiple requests together within a collection, which saves time and effort. You can also add tests to ensure the proper responses are received.
cURL is great if you like using the command line. Although it doesn't have a graphic user interface like Postman, cURL serves well as a command-line tool. You can assemble requests by giving the endpoint URL, HTTP method, and any extra headers or parameters in the terminal.
Response parsing
Parsing a response in an API entails figuring out and extracting important data from the received response for use in your app or system.
When testing an API, you need to ensure that the server's responses are accurate and expected. This lets you focus on specific details, check assumptions, and confirm the API's correctness. For example, if we have a restaurant website API, and we want to get the names and ratings of all restaurants in a certain location. You can do this by parsing the response data. A tool for handling JSON responses is JSONPath. It gives us a simple way to go through JSON responses. Here's an example:
Request:
GET /restaurants?location=NewYorkResponse:
{
"restaurants": [
{
"name": "Tasty Treats",
"rating": 4.5
},
{
"name": "Foodie Haven",
"rating": 4.8
},
{
"name": "Burger Paradise",
"rating": 4.2
}
]
}Using JSONPath, we can get the names and ratings like this:
$.restaurants[*].name
$.restaurants[*].ratingThis gives us a list of restaurant names and their ratings. We can use this data for further tests or analysis.
XMLPath enables you to parse XML responses. Let's think about an XML response from our restaurant website API:
GET /restaurants?location=LondonResponse:
<restaurants>
<restaurant>
<name>Delicious Delights</name>
<rating>4.6</rating>
</restaurant>
<restaurant>
<name>Culinary Corner</name>
<rating>4.9</rating>
</restaurant>
<restaurant>
<name>Flavorful Fusion</name>
<rating>4.4</rating>
</restaurant>
</restaurants>With XMLPath, we can get the names and ratings like this:
/restaurants/restaurant/name
/restaurants/restaurant/ratingLike JSONPath, XPath helps us get the needed data easily.
Collection management
Collection management in API usually allows you to perform actions on a group of resources or items, such as creating, retrieving, updating, or deleting multiple items at once.
Postman provides collection management features. You can make collections to put your API requests into groups based on things like the endpoint, the function of the request, or the setting. This keeps your requests tidy and easy to find. You can also set the order in which the requests should execute, which is helpful when one request depends on another.
In Postman, collection management is about organizing API requests, tests, and documentation together in a collection. You can create, modify, and arrange collections; run tests; and check responses. This gives you a good way to handle API-related tasks and simplifies the testing and creation processes.
Collection management in Insomnia involves creating, modifying, and arranging collections; setting up authentication; establishing environment variables; and adjusting other settings associated with the API requests in the collection. It allows you to test and develop APIs in a clear way. Remember that tool features and terms can change, so it's advisable to consult the most recent Insomnia documentation for any updates.
Request parametrization
This refers to altering the data we send to the API server so we can test different scenarios. Say we have an API endpoint that gets info about restaurants based on certain criteria, like location or food type. In our tests, we might want to try different criteria to see what the API does.
We can use Postman or cURL for this. For simplicity, we'll use Postman. First, we identify the API endpoint to test. For our restaurant website, let's assume it's "https://api.example.com/restaurants". This endpoint expects certain criteria in the request to give specific restaurant info.
In Postman, we can make a GET request to this endpoint. In the request, we can add search criteria such as location or food type. For instance, adding ?location=NewYork means we are searching for restaurants in New York.
The good thing about request parametrization is that we can experiment with different criteria and see the API's responses. We can test "?location=NewYork for American food, ?location=London for European food", and so on.
By modifying these criteria, we can do lots of tests to make sure the API works right. We can also see if the API gives the accurate results for the criteria we give. This helps us find any issues with the API.
Also, request parametrization lets us automate testing. We can make test sets with different criteria and run them regularly to ensure the API maintains consistency over time. Conveniently, requests can be easily transferred from Postman to cURL, and vice versa.
Integration
Integration essentially involves ensuring different components, systems, or software work well together. With our restaurant website, it's about perfectly coordinating the reservation system, online ordering system, and payment area. For example, within the reservation system integration: we can link the website with a reservation management tool to facilitate easy table booking for customers. This integration synchronously updates booking details on both the website and the tool, thereby avoiding any mix-ups or double booking.
The online ordering system integration focuses on letting customers order food online from the website. The key is to smoothly connect the website with the restaurant's ordering system. Customers can then view the menu, add items to their cart, and pay without problems. The integration ensures orders go straight to the restaurant's system, which lowers the chance of errors or delay.
Integrating with a reliable payment gateway lets the website securely handle online payments and accept different payment methods (cards, digital wallets, etc.). The integration ensures that payment info is kept safe and secure, so customers don't need to worry about data theft or fraud.
Conclusion
Postman and Insomnia are very useful for testers because they provide a nice environment for testing APIs. Whether it's sending requests, checking responses, handling collections, or automating tests, these tools aid in improving the testing process, enhancing precision, and making collaborative work easier.