Hoverfly in a Nutshell: Lightweight API Mocking for Developers
What is Hoverfly?
β
Intercept & record real API calls for later replay
β
Simulate APIs without manually defining every response
β
Mock HTTPS services (without self-signed cert nightmares)
β
Test latency, failures, and rate limits effortlessly
β
Run inside CI/CD pipelines without headaches
It works by sitting between your app and the real API, capturing traffic, and playing it back like a time-traveling API recorder. π°οΈ
How Hoverfly Works
Hoverfly runs in different modes, depending on your use case:
- Capture Mode β Records real API traffic.
- Simulate Mode β Replays recorded interactions as mock responses.
- Modify Mode β Intercepts requests and modifies responses on the fly.
- Synthesize Mode β Generates dynamic responses based on request parameters.
Unlike WireMock (which requires defining responses manually), Hoverfly can learn from real traffic and then act as a stand-in API.
Setting Up Hoverfly
π οΈ 1. Install Hoverfly
You can grab Hoverfly from the official releases or install it via:
Linux & macOS
|
|
Windows (via Chocolatey)
|
|
Now, check if itβs working:
|
|
π 2. Start Hoverfly
Run Hoverfly in simulation mode:
|
|
This starts Hoverfly as a local API mock server on port 8500.
Now, any request sent to Hoverfly can be intercepted, recorded, or simulated!
π¬ 3. Capture Real API Calls
Hoverfly can record API interactions, so you donβt have to define every response manually.
Start recording:
|
|
Now, send real API requests through Hoverfly:
|
|
Hoverfly captures the request and response, storing them for later playback.
Stop capturing:
|
|
π 4. Replay API Responses (Simulation Mode)
Now, restart Hoverfly in simulate mode:
|
|
Now, calling the same API doesnβt hit the real serverβHoverfly replays the response from memory:
|
|
Boom! π Now you can test without needing internet access or the real API.
Simulating Errors & Slow Responses
Want to test how your app handles timeouts or rate limits? Hoverfly makes it stupidly simple.
π Add Artificial Delays
|
|
Now, every request will take 2 seconds to respond.
π₯ Simulate a 500 Error
Modify a response on the fly:
|
|
Now, requests to /api/v1/users
return a 500 error instead of real data.
Using Hoverfly in CI/CD Pipelines
One of Hoverflyβs killer features is how easily it integrates into CI/CD workflows.
πΉ Mock APIs in automated tests so your CI doesnβt fail due to flaky services.
πΉ Record once, replay foreverβno need to hit real APIs during testing.
πΉ Run in Docker or Kubernetes for scalable service virtualization.
For example, in JUnit (Java), you can mock API responses:
|
|
Now your tests pass even if the real API is down! π
Hoverfly vs. WireMock vs. Mountebank
So, which tool should you use?
Feature | Hoverfly | WireMock | Mountebank |
---|---|---|---|
Mock REST APIs? | β Yes | β Yes | β Yes |
Mock HTTPS APIs? | β Yes (Built-in) | β οΈ Harder | β οΈ Needs workarounds |
Capture API Calls? | β Yes | β No | β Yes |
Modify Responses Dynamically? | β Yes | β No | β Yes |
Simulate Network Delays? | β Yes | β Yes | β Yes |
Supports TCP & SMTP? | β No | β No | β Yes |
Best for? | Recording & replaying APIs | Manual API mocking | Mocking multiple protocols |
πΉ Use WireMock if you want precise, code-defined API mocks.
πΉ Use Mountebank if you need mocking for HTTP, TCP, or SMTP.
πΉ Use Hoverfly if you need API recording, HTTPS support, and easy CI/CD integration.
Why Hoverfly is Awesome
β
Effortless API Recording β No need to manually write mocks.
β
First-Class HTTPS Support β Works without painful cert setups.
β
Dynamic Response Modification β Easily tweak responses on the fly.
β
CI/CD Friendly β Runs in Docker, Kubernetes, and headless environments.
β
Tiny & Fast β Uses minimal resources compared to heavyweight mocking tools.
π Key Ideas
Key Idea | Summary |
---|---|
What is Hoverfly? | A lightweight, proxy-based API mocking tool. |
Why use it? | Capture & replay API calls, simulate failures, and speed up testing. |
How to use it? | Install Hoverfly, record traffic, replay responses. |
Supports HTTPS? | Yes, built-in support. |
Best for? | Automating API testing, CI/CD pipelines, and simulating real-world failures. |