After completing the initial iteration of the API endpoints implementation, an important task need to be performed which is end-to-end testing of the endpoints to ensure that the APIs are working as intended, and to enable developers to demonstrate their work at the end of each sprint. Various tools were compared and HTTP files with HTTP/Rest Client are chosen for this purpose.
Selection criteria:
- Learning curve and ease of use
- IDEs integration (Visual studio, VS Code, IntelliJ)
- CLI availability for Automation and CI/CD integration
- Integration to the existing development process (PRs, Code review, source control, etc.)
- Keep the testing process simple and easy to maintain
- Readability
- Support different HTTP methods and Headers
- Authentication support
- Environment variables
- Response validation and handling
A Mock Customers API service created using Spring Boot to illustrate the process of conducting end-to-end testing, And using VS Code as the Development environment (IDE) and Httpyac as the HTTP client.
Please find all the code and documentation in the repository here
E2E(end-to-end) testing
Let’s start by defining what is end-to-end 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 article focuses on a comprehensive end-to-end testing of the API endpoints in a practical way, and avoids going into details about the end-to-end testing methodology and different strategies that can be used to do it.
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, VS Code, IntelliJ (natively or through HTTP clients extensions [Rest Client, Httpyac, Intellj HTTP Client]) or from the command line using the (Httpyac CLI — HTTP 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 the base URL of the API endpoints and the authentication token.
For Httpyac extension in VS Code, you use different sources of the environment variables:
- System environment variables
- Json based environment variables file and you can define multiple environments per file
Create HTTP request test
As presented by the image below, the HTTP request 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.
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.
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.)
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
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
This article demonstrated a practical approach to performing 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 also highlighted a response handling and validation mechanism.