Git Product home page Git Product logo

hypert's Introduction

Hypert - HTTP API Testing Made Easy

ci-img pkg-img reportcard-img coverage-img tag-img license-img

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

  1. Install Hypert:
go get github.com/areknoster/hypert
  1. Use hypert.TestClient to create an http.Client instance for testing:
func TestMyAPI(t *testing.T) {
	httpClient := hypert.TestClient(t, true) // true to record real requests
	// Use the client to make API requests. 
	// The requests and responses would be stored in ./testdata/TestMyAPI
	myAPI := NewMyAPI(httpClient, os.GetEnv("API_SECRET")) 
	// Make an API request with your adapter.
	// use static arguments, so that validation against recorded requests can happen
	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)
	}
    // Assertions on the actual API response
	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) // false to replay stored requests
    // Now client would validate requests against what's stored in ./testdata/TestMyAPI/*.req.http 
    // and load the response from  ./testdata/TestMyAPI/*.resp.http
    myAPI := NewMyAPI(httpClient, os.GetEnv("API_SECRET"))
    // HTTP requests are validated against what was prevously recorded. 
    // This behaviour can be customized using WithRequestValidator option
    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)
    }
    // Same assertions that were true on actual API responses should be true for replayed API responses.
	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.


hypert's People

Contributors

areknoster avatar dependabot[bot] avatar

Stargazers

Mohamed Yussuf avatar Tomasz Czubocha avatar Hamov Oleg avatar NLS avatar Oleksandr Nosov avatar Pedro Maia Costa avatar Krzysztof Niepokojczycki avatar Meir Blumenfeld avatar Rodrigo V. Del Monte avatar Mattia Zignale avatar Lysak Yaroslav avatar Sven Hohlfeld avatar Christian Korneck avatar Nikolay Lukyanov avatar Aleksey Khorev avatar Tanmoy Sen Gupta avatar Ben Woodward avatar Antares avatar Sanaf avatar Aleksei Badyaev avatar Clayton Kehoe avatar David Pedersen avatar Egor Voronov avatar Liviu Costea avatar

Watchers

Pedro Maia Costa avatar  avatar

hypert's Issues

Default naming scheme directory defaults to GO pkg location

Discussed in #5

Originally posted by pnmcosta May 15, 2024
Hi Arek,

Great work with this, I have a use case where it's really handy to use, but have come across what I believe is an issue, but let me know if you need any more info about my setup, or possibly I'm doing something wrong?

The default naming scheme in my WSL setup with a Fedora distro and go 1.22.3 seems to be always setting the request storage directory to the package's go pkg directory:

hypert: using sequential naming scheme in /root/go/pkg/mod/github.com/areknoster/[email protected]/testdata/TestDo directory

This happens with running go test ./... -v directly on the package's directory, but also when running from VSCode and/or the repo's root.

I have worked around it by specifying a naming scheme and setting a relative path for the directory instead, and then it stores it under my package's directory:

ns, err := hypert.NewSequentialNamingScheme("./testdata")
if err != nil {
	t.Fatal(err)
}
httpClient := hypert.TestClient(t, true, hypert.WithNamingScheme(ns)) // during the development recordModeOn is set to true

Is this the expected behaviour/usage?

Thank you,
Pedro

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.