Proxying
Proxying in API Mocking: Definition, Use Cases, and How it
TLDR: Proxying is the practice of redirecting API requests through an intermediary server that forwards those requests to the actual server and then returns the responses to the client.
What is Proxying?
Proxy servers function as intermediaries between clients and end servers, handling requests and responses. In API development, a proxy server can be used to monitor, modify, or log traffic between a client and an API. This is particularly useful for debugging, testing, and monitoring APIs in real time without affecting the actual API or its users.
Proxies can blend mock responses with live API responses, enabling comprehensive testing without impacting the actual API or production data. This approach is beneficial for parallel development and ensuring resilient API client behavior.
When Should Dev Teams Use Proxying?
It is difficult to make dependent APIs respond in a desired way when testing an application. However, proxying makes it possible to test edge cases, errors, security, and performance in a way that wouldn’t be possible in a live API:
- Testing Edge Cases: Proxying provides a potential workaround by blending mock and live API responses. This makes it possible to test edge cases and failure scenarios that couldn’t be replicated in a live API without negatively impacting users.
- Debugging and Errors: Proxying allows developers to inspect and analyze the traffic between the client and the API. This can be invaluable for debugging issues, performance tuning, and ensuring that requests and responses conform to expected formats and protocols.
- Security Testing: Proxy servers can be utilized to simulate various security scenarios, such as man-in-the-middle attacks. This ensures that the API and client are resilient to common attack methodologies.
- Performance Testing: By routing requests through a proxy, developers can simulate different network conditions, such as latency or bandwidth constraints. This helps dev teams understand how the API and client handle these scenarios and engineer improvements in response.
API Mocking and Proxying
Proxying in API mocking is usually configured on a per-stub basis. That’s how it’s handled in our mocking solution, WireMock Cloud.
When a stub is configured to serve a proxy response in WireMock Cloud, all of the normal request-matching rules apply. Instead of returning a mocked response, however, the response is forwarded to the target. By using WireMock to proxy all but a few requests, it becomes possible to test scenarios that cannot be produced in the live target API.
On a technical level, the proxy achieves this by intercepting requests and either routing them to the real API or handling them locally to return predefined or random responses. The proxy can also modify requests before forwarding them to the API or back to the client, depending on how it has been configured.