red, orange, yellow cubes, and API letters Or application programming interface

API Testing Primer: An Introduction

A word about API applications

The traditional API application design pattern combines all the API’s methods into a single application, making one single monolithic API application. This monolithic API architecture would then be installed on as many servers as needed to serve the amount of traffic needed. Any change to any one method in the monolithic API would necessitate a regression test of all APIs in the application, possibly requiring considerable time and slowing the release cadence. 

The right-hand diagram shows an example of the microservice API architecture. With the microservice API architecture an individual API application serves only a handful of methods. This API design allows for much more flexible server deployments where only the most utilized API application needs to be scaled to multiple servers and the least used can use a separate scaling schema. This architecture also lends itself to faster releases as only the API application and its respective methods that were changed need to be tested.

API Testing Tools

  • Postman
    • Postman is a browser-based UI platform for testing APIs. Users can enter the endpoints, define the headers, provide parameters, and payloads to execute an API request and see the response. User can organize their API requests into collections and share them with other users.
  • Hoppscotch
    • Hoppscotch is similar to Postman but can be installed locally or on your own server for browser-based access. This gives you all the benefits of Postman without all the disadvantages of cloud based products you have no control over.
  • Swagger
    • Swagger is another browser-based API testing solution. The difference with Swagger is that the API requests are mostly set up automatically using a JSON definition of the API. Development teams may provide Swagger endpoints which list all the available API methods that can easily be accessed.
  • Insomnia
    • Insomnia is another browser-based API testing solution that offers a more simplistic and easy to use design.
  • SoapUI
    • SoapUI is a free desktop API testing tool that has a long history in the software QA community. SOAP based APIs can be tested with this tool.
  • ReadyAPI
    • Ready API is a commercial tool for API testing offering functional testing, performance testing, and other tools.
  • RestAssured
    • RestAssured is a Java library for testing APIs making this a code-based solution to API testing without a UI. Code-based API solutions may appeal more to those looking for a build runtime or CI/CD testing solution.
  • Karate DSL
    • Karate DSL is another code-based API testing solution built on the Cucumber library with test cases defined in Javascript. Code-based API solutions may appeal more to those looking for a build runtime or CI/CD testing solution.
  • JMeter
    • JMeter is a desktop/server application for API functional and load testing. JMeter provides a UI for creating tests which can then be run from either the UI or command line. JMeter tests can be set up to access external data sources, implement flow controls and logic, and run custom coded assertions.
  • K6
    • K6 is a code-based API load testing application. Scripts are written in Javascript.
  • Selenium, Cypress, Playwright
    • Selenium, Cypress, Playwright are popular UI test automation tools that can also be used to code-based API testing. If you have an existing UI test framework using the same solution for API testing may provide an advantage.

API Testing Types

Let’s get a brief description of the different types of testing that are executed during API testing. The rest of this API testing series of blog posts will dive deeper into each of these. It is important to understand that each of these types do not live in isolation and there can be some overlap in the testing and the kind of tests you actually use for each. For instance you may run a functional test to verify an integration is working. These types of tests define the objective, not necessarily the process.

Integration Testing

  • Databases
  • Authorization systems
  • Third party data providers
  • Email Handlers
  • Payment Providers
  • Logging and Error Handling Systems
  • Caching Schemes
  • Scheduled Job Execution
  • Queuing Services

 

Contract Testing

  • HTTP methods (POST, GET, PATCH, DELETE)
    • A definition of which HTTP methods are supported by the API.
  • Endpoint URLs and paths
    • The domain the API can be accessed at as well as the path to access the API
  • Headers
    • The HTTP headers that are required by the API for the request to be served. This can include authentication headers, content-type, and user agent data.
  • Parameters
    • The listing of parameters and data types that the API requires for requests to be processed.
  • Post body format
    • The specification for the data that is being sent to the API such as if it requires XML or JSON and the schema of the data with respect to parent and child objects.
  • HTTP status codes
    • Numeric codes that will be included in the HTTP Status header that define the response contents. Some examples include 200 for a successful response or 500 for a server error.
  • Headers
    • A listing of response headers such as content-type and cache status information
  • Response Body
    • The specification for the data that is being sent by the API such as if it is XML or JSON and the schema of the data with respect to parent and child objects.

Functional Testing

  • Parameter testing
    • Testing each request parameter and parameter value. For instance if a parameter supports an enumerated list of values then each values will be tested.
  • Response testing
    • Similar to the request parameter testing, each response field will be tested including verifying that each request value is included in the response.
  • Data Validation
    • During this testing we check to verify that the data that is returned in a response is correct. This may include comparing the API response data with the source data such as the database that the data is sourced from. The objective of this verification is to confirm that the data in the database and the application of any business rules produces the same data that is in the API response.
  • Error Handling
    • Error handling testing involves sending invalid requests to the API to verify that it properly handles the requests. The types of invalid requests may include unauthorized, improperly formatted, improperly encoded, or requests that violate business rules.
  • Boundary Testing
    • Boundary testing is testing to verify that parameter values near or over a limit are properly handled by the API. For instance if an API parameter takes a value of 0 – 100 we will test that the correct response is provided when value ‘0’ and ‘100’ are sent. If an API accepts a CSV file as input with a limit of 50,000 records then we test with a CSV file with 50,000 records in it. We also test response fields with regard to boundary testing; verifying that given response field will return values at the minimum and maximum scopes.
  • Cache Policy Testing
    • Cache testing may or may not be required depending on if the API is caching any API responses on the server. An API may cache responses to increase performance and reduce the load on the database. API responses that retrieve data that does not frequently change are good candidates for caching. Testing involves making a request to an API (HTTP GET method) that caches the response and then repeating the request to verify that the data is cached and retrieved from the cache correctly. Further testing would involve updating or deleting the data that is served in the response and then making the GET request again to verify that the API serves the updated data or does not respond with the record if it was deleted.
  • Performance Testing
    • During functional testing we will get our first look at performance and load testing. Together with the product owners and development team a standard response time will need to be defined. Then during functional testing we will evaluate the API calls with different parameter values, responses, and business rules to verify that the API meets the response time requirements. The APIs must meet their performance requirements as a prerequisite for load testing to be performed.

Load Testing

Load testing involves executing multiple simultaneous API requests to simulate usage by multiple users of a client application that is using the API. The objective of this testing is to discover if the API can serve multiple simultaneous requests and still remain performant and not suffer any functional degradation. This testing is also used to test any automated scaling policies put in place on the server to increase the number of API application instances when traffic increases during peak usage.

  • The user or business flows to be included in the load test are determine and set. These can be flows such as first time user experience (FTUE), user registration, general search, navigation, and usage, checkout, and payment flows.
  • Discover which APIs are called during each of these flows.
  • Determine how many API requests per second are generated by a typical user executing these flows.
  • Determine the the expected amount of simultaneous users that are expected to execute these flows at peak usage periods.
  • Once the JMeter script has been created that simulates the API usage from the flows that we defined in the first set, use JMeter to simulate the number of users (called ‘threads’ in JMeter) that generate the expected number of API requests per second by the number of simultaneous users making API requests at peak usage

 

Another way of determining the target load is to investigate server logs from past API usage in a production environment to determine the number of requests per second that were generated and then set up JMeter to reproduce that load.

Security Testing

  • Authentication and Authorization Testing
    • Verification of the authentication process for API access. This can include the testing of authentication tokens, session tokens, and expiration of tokens.
  • Input Validation
    • Input validation testing may focus on verifying that the API is resistant to SQL injection and other injection attacks.
  • Rate Limiting
    • Verifying that the API detects excessive requests coming from a single source and appropriately limits the sender from making excessive requests to the API.
  • Logging and monitoring
    • Investigating any logging and monitoring policies that are put in place to ensure that they can detect and report attacks on the API.

What’s Next?

In our next blog post in this series we will discuss Integration Testing in more detail including the methods for testing and some of the tools used.