QA GUIDE / API TESTING

API testing: the definitive guide.

Everything you need to start testing APIs well: what an API is, how REST and HTTP work, what to validate, and which tools to use.

What is API?

API stands for Application Programming Interface. It is a collection of functions and procedures that allows communication between two applications or libraries.

Think of an API as a connector that enables data flow between different systems. For example, all data connects to an organization through APIs, whether from customers, suppliers, partners, or applications.

In simple terms, an API is an interface between different software programs or services.

API Explained with an Example

Imagine you go to a restaurant:

  • Client - You (the customer)
  • API - The waiter
  • System - The kitchen

The API (waiter) takes your order and tells the system (kitchen) what to do (prepare food), then returns with the response you asked for (your food).

Types of APIs

  • Simple Object Access Protocol (SOAP)
  • Remote Procedure Call (RPC)
  • Representational State Transfer (REST)

In this guide, we'll focus primarily on Web APIs, particularly REST APIs.

What is API Testing?

API testing involves testing APIs and their integration with services. It's one of the most challenging types of testing because missing certain test cases can cause significant problems in production after full integration, making debugging difficult.

This guide focuses on REST API testing, which involves validating APIs, testing error codes, and performing load testing.

What is REST API?

REST is an acronym for REpresentational State Transfer, and statelessness is key. An API can be considered RESTful if it follows these six constraints defined by Roy Fielding:

1. Uniform Interface

Defines the interface between clients and servers. Clients and servers must communicate and agree on certain rules based on resources (using the same format like JSON, XML, HTML) with proper encoding.

2. Stateless

APIs in REST are stateless, meaning neither client nor server needs to worry about the state of the request or response.

3. Cacheable

Clients can cache responses, which should define themselves as cacheable either implicitly or explicitly.

4. Client-Server

Client and server are separate entities that can be replaced and developed independently as long as the interface remains unchanged.

5. Layered System

There can be any number of intermediary systems between client and server without affecting their communication.

6. Code on Demand

Servers can store code or logic and transfer it when needed rather than relying on client-side logic.

If an API fulfills all these constraints, it can be considered a REST API.

Who Uses REST?

  • Twitter API
  • LinkedIn API
  • Slack API
  • And many more...

REST API vs SOAP API

REST

  • Uses POX, RSS, JSON, etc.
  • HTTP GET, POST, PUT, DELETE methods
  • Resource URI based
  • Application level protocol

SOAP

  • Uses SOAP (WS+)
  • SMTP, HTTP POST, MQ protocols
  • Endpoint URI based
  • Uses WSDL for service description

SOAP (Simple Object Access Protocol) is a messaging protocol that allows programs running on disparate operating systems to communicate using HTTP and XML.

What to Test in API Testing?

  • Validate keys with minimum and maximum range of APIs (e.g., maximum and minimum length)
  • Perform XML and JSON schema validation
  • Verify all keys are present in JSON/XML responses
  • Test how API error codes are handled

Why Perform API Testing?

  • Many services rely on hundreds of interconnected APIs - if one fails, the service may not work
  • The internet uses millions of APIs that need thorough testing
  • Developers can create buggy APIs that need validation
  • API validation is crucial before going live to production

HTTP Fundamentals

HTTP is an application layer protocol designed within the framework of the Internet protocol suite.

In HTTP, a client requests a resource (HTML page, file, etc.) from a server, and the server responds using the same protocol. HTTP is stateless, meaning each request is independent and doesn't know about previous requests.

HTTP Request Structure

http://www.domain.com:1234/path/to/resource?a=b&x=y

This URL can be broken down into:

  • Protocol: http
  • Host: www.domain.com
  • Port: 1234
  • Resource path: /path/to/resource
  • Query parameters: ?a=b&x=y

What are Cookies?

Cookies are small text files with ID tags stored in your browser's directory. They record user browsing activity, pages visited, and contain domain names and lifetime information.

GET /spec.html HTTP/1.1
Host: www.example.org
Cookie: theme=light; sessionToken=abc123

What is Authentication?

Authentication is the process of presenting credentials (username, password, secret key) to a system for validation. In API terms, authentication protects content so only valid users with proper credentials can access API endpoints.

Types of Authentication:

  • Basic authentication: Credentials encoded with Base64
    curl --header "Authorization: Basic am9objpzZWNyZXQ=" my-website.com
  • Digest Authentication: Password transmitted in encrypted form
  • OAuth: Allows approval of one application interacting with another without sharing passwords

HTTP Methods Explained

Method Description
GET Request to read a web page
HEAD Request to read a web page's header
PUT Request to store a web page
POST Append to a named resource
DELETE Remove the web page
TRACE Echo the incoming request
CONNECT Reserved for future use
OPTIONS Query certain options

GET Method

Used to retrieve all resources in a collection. GET requests can be cached, remain in browser history, can be bookmarked, but should never be used for sensitive data and have length restrictions.

GET /test?name=value1 HTTP/1.1
Host: scrolltest.com
User-Agent: my browser details
Accept: text/html
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

POST Method

Used to create a new resource in a collection. POST requests are never cached, don't remain in browser history, cannot be bookmarked, and have no restrictions on data length.

POST /test/t.php HTTP/1.1
Host: scrolltest.com
name1=value1&name2=value2

PUT Method

Puts a file or resource at a specific URI, replacing any existing resource. PUT responses are not cacheable.

PUT /bool/foo.txt HTTP/1.1
Host: www.foo.com
Content-Type: plain/text

This is a testing content for the text file foo.txt

PATCH Method

Updates a partial resource when you only need to update one field.

PATCH /groups/api/v1/groups/(group id)
with request body like (action:activate | deactivate)

DELETE & OPTIONS Methods

DELETE removes a resource. OPTIONS returns available HTTP methods and other options.

HEAD & TRACE Methods

HEAD retrieves only header information. TRACE returns traces of the request.

How to Test an API

API testing can be done manually or using tools. Let's examine an example API:

https://api.chucknorris.io/jokes/random

Sample response:

{
  "category": null,
  "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
  "id": "i-v_AlnY89yrx6622e26c0",
  "url": "https://api.chucknorris.io/jokes/i-v_AlnY89yrx6622e26c0",
  "value": "Chuck Norris can play the violin with a piano"
}

Keys to verify: category, icon_url, id, url, and value with their corresponding values as strings or numbers.

API Testing Tools

There are several API testing tools available. I recommend starting with Postman, which provides features like command-line support, CI/CD integration, and API monitoring with test case support.

  • Runscope.com
  • Postman CI/CD
  • Katalon using CI/CD
  • SoapUI CI/CD
  • Rest Assured CI/CD

API Testing Using Postman

Postman is an API testing tool used by developers and testers with features like global variables, mock requests, environments, and API monitoring.

Download Postman for macOS, Windows, or Linux: https://www.getpostman.com

Major Features of Postman:

  • Collections: Executable API description format for running requests, testing, debugging, and creating automated tests
  • Workspaces: Collaboration spaces for teams to share collections, set permissions, and manage participation
  • Built-in Tools: Everything developers need to work with APIs including design, mocking, debugging, test automation, documentation, monitoring, and publishing