Api Testing Notes
Api Testing Notes
API testing
Functional ( System)
Functional testing of an API is done to validate the correctness of the API response for a given
request. For any given method, API response is what needs to be validated. Response is not
only the response body, but also the returned status code.
Integration
Many times an API is meant to call another API or trigger another action. In these cases
performing an overall call sequencing and validating them at the same time would be
considered as integration testing.
Security
As the name suggests, security testing of APIs deals with the security of the API under test. Let
it be about who is calling the API (client or other application), can the requested data be
manipulated before reaching the server, and the response data is securely transferred to the
requesting party. Validating the implemented security mechanisms like Basic Authorization,
OAuth or 2 way authentication.
Performance
Protocols
There are many different protocols available, but most widely used and accepted ones are:
REST
Representational State Transfer is a web architecture concept, which represents the current
state of the requested resource. Client or requesting entity makes a logical request with the
required data for a specific resource to the server, server then responds with the current state of
the resource without keeping or storing any information of the request.
Methods
Psychology
Biggest challenge in API testing is the psychology of the people who have not yet done it or just
started to do it. Making a shift from testing UI based components to API has a psychological
fear that it is too technical in nature. Maybe beauces it doesn’t give the comfort of an interacting
GUI and things happen at the back end.
All it requires is the basic conceptual knowledge of the client-server architecture. Web services
functioning and a very good understanding of the system under test.
Documentation
Most of the time documentation for the APIs under test is either unavailable or not exhaustive. It
fails to provide the relevant and required information to the tester. This leads to a lesser
confidence in the tester even if the test coverage is fairly good.
Understanding the design architecture for these applications in terms of how it structures the
web services and other application components helps you visualize the skeleton and open a
vast number of opportunities for you to explore. It provides you with a better understanding of
each cog in the complete machinery.
Consider this analogy, you went to a burger joint to get a burger. As a customer you want to
order a burger. You place your order (think of your order as a request to an application) with an
attendant (thing of attendant as the entry point API of the application) at the dedicated counter
(think of this counter as the dedicated host of the application or DNS). You give your details
(think of details as request data) about the burger you want to the attendant, attendant takes the
details and pass it to the next attending component (it could be a central dashboard of all the
requests or a person taking all the orders and triaging, think of this component as load balancer
at high level).
As a customer you get to know that your order will be available in 5 minutes and then you wait
for it with the provided token (think of it as a session token). But for the attendant the
understanding is different, she knows the exact working of how the burger would be made and
presented.
She knows…
● Who is arranging the buns
● Who is preparing the required patty
● Who is preparing the side items - French fries and soft drink
● Who is responsible for arranging the items into a burger, putting it together with the side
item and then finally presenting it to the counter where the customer has been waiting for
it
She also knows the usual time taken by each component and hence an approximate time is
conveyed to the customer. Which is fallible as the components performance may vary
depending on multiple factors.
Key points to take away from the above analogy would be that
● Customer has limited information about the burger, he just knows the kind of burger he
would get, taste, size and look of it. He is limited to change the details of required burger
and expect a predefined burger in return
● Attendant on the other hand, has much more information about the entire process. She
understands the system in detail as in how every component interacts with each other.
With the information attendant has, it provides her with opportunities to explore the
system at a granular level.
Point here is that we would be better equipped to be an API tester if we are being an attendant
rather than a customer.
In order to be a good attendant it is necessary to understand the different patterns for structuring
the application components.
There are different structures in which these components are arranged, let's have a quick look
at these structures/patterns
Before dwelling into the different patterns, for a reference point let's assume that we are
developing an e-commerce application. For simplicity, we will assume the application has a User
interface to search for an item, once searched option to add the item in the cart. In this, we
would have components such as User Interface, search engine, inventory management and
checkout.
Monolithic Architecture
Pros:
Cons:
Once the application grow in size, existing pros tends to turn into cons
● Developer works on IDE to write the code, once the application grows, code base also
grows and hence developer gets a slow IDE, impacting the performance of developers
○ For same reason, it becomes harder to debug and pinpoint the issues
○ Larger code base also intimidate the new developers working on the application
● Deployment becomes a task
○ From above example, larger application means larger WAR file, it will take more
and more time for the application to start up and hence increases the deployment
time
○ Entire application needs to be redeployed even if there is a slight change or
update in one of the components
● Scale is not easy
○ It becomes very hectic to scale individual components. Individual components
may require different resources, some might be CPU intensive some might be
memory intensive. It becomes hard to identify this and scale accordingly
○ Teams working on components need to interact with each other and manage the
changes and updates. This also reduces the overall performance. It is ideal to
divide the teams component wise and application as well
○ Monolithic architecture requires long term commitment to the tech stack. It is not
feasible to simply adapt to the newer technologies for a component. As it might
break the other components or not be compatible at all
● One faulty component can bring down the entire application
Microservice Architecture
In micro services architecture all the components are deployed as micros services which in turn
talks to each other for any logical requirements. From the above example, all the components
such as User interface, search engine, inventory management and checkout would be deployed
as separate services.
Pros:
● Independent deployment and changes
○ Micro services architecture allows developers to work independently, enhancing
the performance and faster go to market cycle
○ Maintainability is easy
● Quick to understand
○ Smaller services are easier to understand
● No long term commitment to tech stack
● Debugging and fault isolation is much easier, allows developers to work freely to
enhance the performance of the services
Cons:
● Distributed system
○ Developers have an extra overhead of handling distributed systems. It adds lots
of complexities as compared to monolithic architecture
■ Inter communication between services
■ Fallback mechanisms
○ Added operational costs of maintaining and deploying distributed systems
○ Increased memory and infrastructure
API Gateway
Before discussing the API gateway, let's understand the system where an API gateway is
needed. During our discussion we have assumed an imaginary e-commerce application with
different components. In real life we all know that in general an e-commerce application is
available at different platforms, you can access the same application in your web browser, your
mobile device and other devices like tablets or iPads.
As a user if you come to the application in your web browser, then you would be able to see lots
of information for a searched item along with other related information while in your mobile
application the information might be less.
Also, you might have noticed that the applications or sites you visited earlier are now showing
up in your social media feeds as well. So what and how is it happening?
Having a One API and Fit all scenario is very complex and having a separate set of services for
each platform is too costly.
In general applications have common micro services with ability to provide any permutation of
data required in its scope. It is designed to provide a different set of data points based on the
request made. For example a web application requires much more information for a product
details than a third party app (like a social media - instagram). Third part app might only require
product name, pricing and hyperlink to navigate to the application itself.
In short an application gets requests with varying inputs and it is required to provide the
appropriate data. An API GATEWAY is a component which acts as an entry point to an
application.
Api Gateway does computation and forwards the request to appropriate micro service.
Sometimes it simply acts as a proxy and passes the request as it is.
As discussed micro services might use various communication protocols to interact. Hence, api
gateway might also be required to take input from platforms in more known/common protocols
and convert them into specific services protocol.
Pros:
● Abstracts the application components from the client
● Discovery for services and components overhead is reduced
● Common and web friendly protocol for communication
○ Abstraction from the specific protocols used by the services internally
● Saves time for clients as they need to interact with one component which in turns take
care of distributed call internally
○ Reduces the complexity at client by having computational logic in API gateway
● Specific API for clients for faster and optimised communication
Cons:
● It adds to the complexity of the system by having one more component to manage
○ New updates
○ Deployments
Communication Style
In a microservice architecture, various services need to interact with each other. There are
various communication styles available, which one to use depends on the context of interaction
between the services. Let’s have a quick look at those.
Micro services need to interact with each other in real time and require tight coupling. Where
any requests made have to be served in real time. Synchronous communication is important to
the functioning of the application.
RPI could be used to solve the above problems, request/reply protocol is used by client and
service.
Messaging
As a generic understanding, API calls are made with a request to get a suitable response, what
an API does when a request is made is something that we need to understand. Understanding
the actions when an API call is done helps identify granular details and unearth better and more
test coverage
It involves an API which makes a call to a web service and the service uses data from the API
and writes the required information in a database.
Various connecting points in the scenario are API, web service and database. To test such APIs
testers should keep in mind all the connecting elements.
Various validations on such API
● logs of web service,
● database queries and data validations
● Validations based on the API signature
It involves an API which makes a call and write a message to a specified messaging service
Various connecting points in the scenario are API, web service messaging service like
(rabbitMQ & Kafka). The service which is going to consume the published message.
Various validations on such API
● Message being published
○ Type and details of a message
○ Data validations of messages
● Validating the messages through messaging service itself
● Nature of messages
○ Duration
○ Single consumer/Multiple consumer
● Validation of logs - web service whose API is called and web service which consumed
the message
○ Validation of any action which happens post consumption of the messages
It involves an API which consumes the messages from a messaging service, and uses the
message content as the request input. There could be numerous possibilities about what the
API does post reading the message. It is vital to understand the purpose of the API and decide
the testing areas accordingly.
● What does API do post reading the message
● Various data validations for varying messages
○ Wrong message
○ Incomplete message
● What happens if the API consumes same data again n again
● Logs of the service for which API is consuming the messages
Such APIs are the most complex one as it comes with much more interacting elements. Like,
API itself, web services or APIs which are being called, logs of API and other services being
called, any Database operation
Various validations on such API
● Various data validations for the API in hand
● Validations of actions taken by API
○ Direct action taken by API, like DB operation
○ Call to other web services or APIs
○ Logs of API being called and other subsequent services/APIs
Would like to conclude by highlighting the importance of knowing the system in depth is what
makes you a better backend/API tester.
Understanding the latest technologies and their usage would help you alot in understanding the
end to end flows and decide on the run what all cases need to be covered for better coverage.