Hypert - HTTP API Testing Made Easy
Hypert is an open-source Go library that simplifies testing of HTTP API clients. It provides a convenient way to record and replay HTTP interactions, making it easy to create reliable and maintainable tests for your API clients.
Features
- Record and replay HTTP interactions
- Request sanitization to remove sensitive information
- Request validation to ensure the integrity of recorded requests
- Seamless integration with Go's
http.Client
- Extensible and configurable options
Getting Started
- Install Hypert:
go get github.com/areknoster/hypert
- Use
hypert.TestClient
to create an http.Client
instance for testing:
func TestMyAPI(t *testing.T) {
httpClient := hypert.TestClient(t, true)
myAPI := NewMyAPI(httpClient, os.GetEnv("API_SECRET"))
stuff, err := myAPI.GetStuff(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("failed to get stuff: %v", err)
}
if stuff.ID != "ID-FROM-RESP" {
t.Errorf("stuff ")
}
}
After you're done with building and testing your integration, change the mode to replay
func TestMyAPI(t *testing.T) {
httpClient := hypert.TestClient(t, false)
myAPI := NewMyAPI(httpClient, os.GetEnv("API_SECRET"))
stuff, err := myAPI.GetStuff(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("failed to get stuff: %v", err)
}
if stuff.ID != "ID-FROM-RESP" {
t.Errorf("stuff ")
}
}
Now your tests:
- are deterministic
- are fast
- bring the same confidence as integration tests
Stability
I plan to maintain backward compatibility as much as possible, but breaking changes may occur before the first stable release, v1.0.0 if major issues are discovered.
Examples
Check out the examples directory for sample usage of Hypert in different scenarios.
Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you'd like to contribute code, please fork the repository and submit a pull request.
License
Hypert is released under the MIT License.