Comments (4)
For me there are two real use cases:
- To record traffic of an existing API
- To simulate the contract of an API which is yet to be written
When it comes to testing my preferred approach is to mock out any HTTP calls in my unit tests and only hit a real endpoint during integration tests. For integration testing I prefer to use cucumber so I would write a test something like this...
@dataapi
Feature: Data API
In order to ensure my service integrates correctly
As a developer
I want to be able to test the service handles requests to the data api
Background:
Given hoverfly is running and using the recording 'data_api.json'
Scenario: Requesting data fetches the data from the data API
Given I make a request to '/data/34'
Then the service should make an upstream request to the data api # validates call to hoverfly
And the service should return the correct data # check response from my api
Scenario: When the upstream returns no data return not found
Given I make a request to '/data/badrequest'
Then the service should make an upstream request to the data api # validates call to hoverfly
And the service should return status code 404 # check response from my api
This is obviously quite a simplified example but it allows me to spin up a stack including the system under test with docker and then execute an integration suite written BDD style.
I have been playing round this morning and wrote a little application which sits in front of hoverfly as a mock api which proxies all requests to hoverfly. This enables me to make requests to http://myserver/myurl without the requirement for setting an http_proxy.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
var (
proxyURL string
simulateURL string
)
func main() {
proxyURL = os.Args[1]
simulateURL = os.Args[2]
fmt.Println("running on port 9000")
http.HandleFunc("/", rootHandler)
fmt.Println("Listening for connections on port", 9000)
http.ListenAndServe(fmt.Sprintf(":%v", 9000), nil)
}
func rootHandler(w http.ResponseWriter, req *http.Request) {
proxyURL, _ := url.Parse(proxyURL)
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
defer req.Body.Close()
path := fmt.Sprintf("%v%v", simulateURL, req.URL.Path)
fmt.Printf("Making request for: %v\n", path)
request, err := http.NewRequest(req.Method, path, req.Body)
for k, v := range req.Header {
if k != "Content-Length" {
request.Header.Set(k, v[0])
}
}
if err != nil {
http.Error(w, "Unable to create request", http.StatusInternalServerError)
}
resp, err := client.Do(request)
if err != nil {
http.Error(w, "Unable to execute request", http.StatusInternalServerError)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Unable to read body", http.StatusInternalServerError)
}
w.WriteHeader(resp.StatusCode)
w.Write(body)
}
from hoverfly.
I've been thinking about such a change. A developer is likely to use Hoverfly in capture mode on existing services for short periods (hours) but is likely to want to use Hoverfly in simulation mode in testing and development for much longer (days). Having to have proxy settings set to use Hoverfly permanently could be an imposition.
from hoverfly.
One other thing I would add is that given I have a request like this...
"request": {
"path": "/events/v1/register",
"method": "POST",
"destination": "dev.acet.io",
"scheme": "http",
"query": "",
"body": "{\"event_name\":\"test\", \"callback_url\":\"http://test\"}"
}
it would be good to be able to specify regex in the request options such as...
```json
"request": {
"path": "/events/v1/register",
"method": "POST",
"destination": "dev.acet.io",
"scheme": "http",
"query": "",
"body": "{\"event_name\":\"regex(.*)\", \"callback_url\":\"http://regex(.*)\"}"
}
from hoverfly.
As of v0.7.0 (9bf0b1f), Hoverfly can be ran as a webserver with support for simulate mode. The use case we imagine is that you use the proxy to capture simulation data and then would then deploy Hoverfly with the data as a webserver along side their application in a test environment.
from hoverfly.
Related Issues (20)
- Version mismatch between the website docs and the release package HOT 2
- Hoverfly 1.5.0 seems not to forward form content in capture mode HOT 3
- Validating Response from Real Service and returning Simulated data. HOT 2
- Hoverfly not installing via "brew" on Mac HOT 5
- Templating: Invalid example of 'replace' helper method HOT 3
- Vulnerabilities CVE-2022-41717 and CVE-2022-41723 in Hoverfly v1.5.2 HOT 7
- json matcher is space sensitive HOT 1
- Performance reports of various modes HOT 1
- Capture GraphQL response HOT 1
- Hoverfly mismatch HOT 1
- Hoverfly Docker Image in GIT HOT 2
- Support request matching for missing parameters HOT 1
- Vulnerabilities CVE-2023-39325 , CVE-2023-3978 and CVE-2023-44487 in Hoverfly v1.6.0 HOT 3
- Issue with Jsonpath matcher and in getting data from request body having json field name with dot(.) eg "first.name" HOT 1
- runners are running behind proxy, so we need to set http proxy, https proxy and no proxy variables provided by our organization so where can we set hoverfly proxy variables HOT 1
- Invalid data when using template HOT 3
- Query parameters with semicolon ignored HOT 3
- Hoverfly tries to connect to stubbed server in simulation mode and fails HOT 1
- HTTPS error when using Go core package to setup hoverfly instance HOT 3
- Responses with status code 204 over HTTPS not working as expected
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hoverfly.