APIs testing using HTTP files and Rest Client

Ayman

APIs testing using HTTP files and Rest Client

testing-pyramid

Context

During our latest engagement with a financial sector client, and after completing the initial iteration of the API endpoints implementation, an important task was to perform end-to-end testing of the API endpoints to ensure that the APIs are working as intended, and to enable developers to demonstrate their work at the end of each sprint. We compared various tools and chose HTTP files with HTTP/Rest Client for this purpose.

Our selection criteria were:

  • Learning curve and ease of use
  • IDEs integration (Visual studio, VS Code, IntelliJ)
  • CLI availability for Automation and CI/CD integration
  • Integrated to the existing development process (PRs, Code review, source control, etc.)
  • Simplicity, Readability and maintainability of the test code
  • Support of different HTTP methods and Headers
  • Authentication support
  • Environment variables
  • Response validation and handling

I would like to walk you through the steps we took to perform end-to-end testing for the API endpoints in the solution. In this article, we going to use a sample mock Customers API service and employ it to illustrate the process of conducting e2e(end-to-end) testing, leveraging VS Code as the integrated development environment (IDE) and Httpyac as the Rest client and CLI.

Please find all the sample code and documentation in the repository here

E2E(end-to-end) testing

Let’s start by defining what is e2e testing and why we need to do it.

End-to-end testing is a methodology used to test whether the flow of an application is performing as designed from start to finish. The purpose of carrying out end-to-end tests is to identify system dependencies and to ensure that the right information is passed between various system components and systems.

This post focuses on a comprehensive practical end-to-end testing of the API endpoints, and avoid going into details about the end-to-end testing methodology and different strategies that can be used to do it.

e2e-testing

HTTP files

It is a file with a .http extension that allows you to write HTTP requests using a format that follows the standards of RFC 9110 HTTP Semantics. The file can be executed from IDEs like Visual Studio, VSCode, IntelliJ (natively or through extensions like [Rest Client, Httpyac, IntelliJ HTTP Client]) or from the command line using the (Httpyac CLIHTTP Client CLI ).

IDEs integration allows you to send HTTP requests and view the response directly in Code editor without switching to a browser or a separate application.

As it is a text file, it can be easily integrated with the existing development process (PRs, code review, source control, etc.)

Environment variables

Each IDE or Extension has its own way to define environment variables, but the concept is the same. Environment variables are used to define variables that can be used in the request URL, headers, and body.

It is very useful to define a set of environment variables for each environment (local, dev, test, prod, etc.) and use them in the HTTP files. This allows you to execute the same HTTP file against different environments without changing the request URL, headers, and body.

For Httpyac extension in VS Code, you use different sources of the environment variables:

  • System environment variables
  • VS Code settings environment variables
  • Json based files and you can define multiple environments per file

Environment-variables

Create HTTP request

As presented by the image below, the HTTP request test is composed of these parts:

  • Request line: The first line of the request contains the HTTP method, the URL, and the HTTP version.
  • Headers: The request headers are used to pass additional information about the request, and about the client itself, to the server.
  • Body: The request body is used to send data to the server.
  • Metadata: Metadata is used to add additional information about the request, such as request name, title, etc.
  • Response Handling: The response handling section is used to write the required logic for response validation and handling.

http-request-format

Execute the HTTP request

Httpyac extension allows you to select the environment variables scope and use the send UI command to execute the HTTP request.

http-request-run

Response handling and validation

The HTTP Client supports a mechanism to manipulate the response and validate it, by exposing the response as an object that can be used to access its response status, headers, and body.

In addition to a handy utility functions that can be used to validate and handle the response, these functions are exposed by an object called client and methods examples (client.test, client.assert,client.global.set, etc.)

http-request-response

Test Automation

The HTTP files can be executed from the command line using the Httpyac CLI or HTTP Client CLI. This allows you to automate the test execution and can be integrated with CI/CD pipelines.

# Install Httpyac CLI
npm install -g httpyac

# Execute the HTTP file
# https://httpyac.github.io/guide/installation_cli.html
httpyac testing/e2e-test/customers.http --all -e local -o response

http-request-run

The sample repository contains make commands as shorthand to execute the HTTP files using the Httpyac CLI automatically.

# Install Httpyac CLI
make e2e-init

# Execute the HTTP file
make e2e-local

Conclusion

In this article, we’ve demonstrated how to perform end-to-end testing for API endpoints using HTTP files and Rest Clients. We utilized the Httpyac extension for VS Code as an HTTP client for composing and executing HTTP requests. Additionally, we illustrated how to employ environment variables to define the base URL of the API endpoints and the authentication token. We’ve also highlighted a response handling and validation mechanism.

References

Feedback usabilla icon