API Replay
What is API Replay? When Would You Use It?
The short answer: API replay is a technique for capturing and reusing real API request and response data to test and debug applications.
API replay, also known as "API recording" or "API replay testing," captures actual API traffic during a real session and then replays those requests and responses in a testing environment.
This technique allows developers to test and validate their code with real-world data, ensuring the application behaves as intended under the same conditions it would encounter in production. API replay can be particularly useful for regression testing, performance testing, and identifying issues that may not be evident with mock data.
When Should You Use API Replay?
The API replay technique is almost exclusively used for a range of testing-related purposes, such as:
Regression testing: When you want to ensure that new code changes do not break existing functionality, API replay allows you to run previous requests and verify that the responses remain consistent.
For example, a dev team might release a new version of their application and use the API replay technique to run the same API requests captured from the previous version. They compare the responses to ensure no unexpected changes or regressions have been introduced.
Performance testing: By replaying a large volume of real API requests, dev teams can simulate realistic load conditions and assess how their application performs under stress.
For example, a dev team might capture API traffic during peak usage hours and replay this data in a controlled environment to identify performance bottlenecks and optimize their application for better scalability.
Reproducing bugs: When a bug is reported in production, replaying the exact sequence of API requests that led to the issue helps developers reproduce and diagnose the problem more accurately.
For example, an end-user may report a bug where a specific API request results in an unexpected error. Using the API replay technique, the dev team can capture the exact request and response data, replay it in their testing environment, and quickly identify the cause of the issue.
API Replay in WireMock
Open source WireMock supports API replay via its recording and playback functionality. It provides two main approaches:
- Recording: You can start recording, make requests to an API through WireMock, and then stop recording. This captures the requests and responses and generates stub mappings that can be replayed later.
- Snapshotting: With snapshotting, you configure WireMock to proxy to the target API, generate some traffic, and then trigger WireMock to generate stub mappings from the requests and responses it has captured.
Both approaches result in WireMock creating stub mappings that will replay the same responses when matching requests are made in the future.
WireMock provides a web UI for recording as well as a Java DSL and REST API for more advanced configuration options. Some key features include:
- Filtering which requests to record/snapshot based on URL, method, headers, etc.
- Capturing specific request headers
- Extracting response bodies to separate files based on size thresholds
- Generating scenarios from repeated identical requests with different responses
- Customizing stub generation behavior with transformers
In WireMock Cloud, replay is handled by recording stubs in the web UI.