HTTP mock for Golang: record and replay HTTP/HTTPS interactions for offline testing

Overview

govcr

A Word Of Warning

I'm in the process of partly rewriting govcr to offer better support for cassette mutations. This is necessary because when I first designed govcr, I wanted cassettes to be immutable as much as golang can achieve this. Since then, I have received requests to permit cassette mutations at recording time.

The next release of govcr will bring breaking changes for those who are using govcr v4 or older. In exchange for the inconvenience, it will bring new features and a refreshed code base for future enhancements.

If you're happy with govcr as it is, use a dependency manager to lock the version of govcr you wish to use!

End Of: A Word Of Warning

Records and replays HTTP / HTTPS interactions for offline unit / behavioural / integration tests thereby acting as an HTTP mock.

This project was inspired by php-vcr which is a PHP port of VCR for ruby.

This project is an adaptation for Google's Go / Golang programming language.

Install

go get github.com/seborama/govcr

For all available releases, please check the releases tab on github.

You can pick a specific major release for compatibility. For example, to use a v4.x release, use this command:

go get gopkg.in/seborama/govcr.v4

And your source code would use this import:

import "gopkg.in/seborama/govcr.v4"

Glossary of Terms

VCR: Video Cassette Recorder. In this context, a VCR refers to the overall engine and data that this project provides. A VCR is both an HTTP recorder and player. When you use a VCR, HTTP requests are replayed from previous recordings (tracks saved in cassette files on the filesystem). When no previous recording exists for the request, it is performed live on the HTTP server the request is intended to, after what it is saved to a track on the cassette.

cassette: a sequential collection of tracks. This is in effect a JSON file saved under directory ./govcr-fixtures (default). The cassette is given a name when creating the VCR which becomes the filename (with an extension of .cassette).

Long Play cassette: a cassette compressed in gzip format. Such cassettes have a name that ends with '.gz'.

tracks: a record of an HTTP request. It contains the request data, the response data, if available, or the error that occurred.

PCB: Printed Circuit Board. This is an analogy that refers to the ability to supply customisations to some aspects of the behaviour of the VCR (for instance, disable recordings or ignore certain HTTP headers in the request when looking for a previously recorded track).

Documentation

govcr is a wrapper around the Go http.Client which offers the ability to replay pre-recorded HTTP requests ('tracks') instead of live HTTP calls.

govcr can replay both successful and failed HTTP transactions.

The code documentation can be found on godoc.

When using govcr's http.Client, the request is matched against the tracks on the 'cassette':

  • The track is played where a matching one exists on the cassette,
  • or the request is executed live to the HTTP server and then recorded on cassette for the next time (unless option DisableRecording is used).

When multiple matching tracks exist for the same request on the cassette (this can be crafted manually inside the cassette or can be simulated when using RequestFilters), the tracks will be replayed in the same order as they were recorded. See the tests for an example (TestPlaybackOrder).

When the last request matching track has been replayed, govcr cycles back to the first track again and so on.

Cassette recordings are saved under ./govcr-fixtures (by default) as *.cassette files in JSON format.

You can enable Long Play mode that will compress the cassette content. This is enabled by using the cassette name suffix .gz. The compression used is standard gzip.

It should be noted that the cassette name will be of the form 'MyCassette.gz" in your code but it will appear as "MyCassette.cassette.gz" on the file system. You can use gzip to compress and de-compress cassettes at will. See TestLongPlay() in govcr_test.go for an example usage. After running this test, notice the presence of the file govcr-fixtures/TestLongPlay.cassette.gz. You can view its contents in various ways such as with the gzcat command.

VCRConfig

This structure contains parameters for configuring your govcr recorder.

VCRConfig.CassettePath - change the location of cassette files

Example:

    vcr := govcr.NewVCR("MyCassette",
        &govcr.VCRConfig{
            CassettePath: "./govcr-fixtures",
        })

VCRConfig.DisableRecording - playback or execute live without recording

Example:

    vcr := govcr.NewVCR("MyCassette",
        &govcr.VCRConfig{
            DisableRecording: true,
        })

In this configuration, govcr will still playback from cassette when a previously recorded track (HTTP interaction) exists or execute the request live if not. But in the latter case, it won't record a new track as per default behaviour.

VCRConfig.Logging - disable logging

Example:

    vcr := govcr.NewVCR("MyCassette",
        &govcr.VCRConfig{
            Logging: false,
        })

This simply redirects all govcr logging to the OS's standard Null device (e.g. nul on Windows, or /dev/null on UN*X, etc).

VCRConfig.RemoveTLS - disable TLS recording

Example:

    vcr := govcr.NewVCR("MyCassette",
        &govcr.VCRConfig{
            RemoveTLS: true,
        })

As RemoveTLS is enabled, govcr will not record the TLS data from the HTTP response on the cassette track.

Features

  • Record extensive details about the request, response or error (network error, timeout, etc) to provide as accurate a playback as possible compared to the live HTTP request.

  • Recordings are JSON files and can be read in an editor.

  • Custom Go http.Client's can be supplied.

  • Custom Go http.Transport / http.RoundTrippers.

  • http / https supported and any other protocol implemented by the supplied http.Client's http.RoundTripper.

  • Hook to define HTTP headers that should be ignored from the HTTP request when attempting to retrieve a track for playback. This is useful to deal with non-static HTTP headers (for example, containing a timestamp).

  • Hook to transform the Header / Body of an HTTP request to deal with non-static data. The purpose is similar to the hook for headers described above but with the ability to modify the data.

  • Hook to transform the Header / Body of the HTTP response to deal with non-static data. This is similar to the request hook however, the header / body of the request are also supplied (read-only) to help match data in the response with data in the request (such as a transaction Id).

  • Ability to switch off automatic recordings. This allows to play back existing records or make a live HTTP call without recording it to the cassette.

  • Record SSL certificates.

Filter functions

In some scenarios, it may not possible to match tracks the way they were recorded.

For instance, the request contains a timestamp or a dynamically changing identifier, etc.

In other situations, the response needs a transformation to be receivable.

Building on from the above example, the response may need to provide the same identifier that the request sent.

Filters help you deal with this kind of practical aspects of dynamic exchanges.

Refer to examples/example6.go for advanced examples.

Influencing request comparison programatically at runtime.

RequestFilters receives the request Header / Body to allow their transformation. Both the live request and the recorded request on track are filtered in order to influence their comparison (e.g. remove an HTTP header to ignore it completely when matching).

Transformations are not persisted and only for the purpose of influencing comparison.

Runtime transforming of the response before sending it back to the client.

ResponseFilters is the flip side of RequestFilters. It receives the response Header / Body to allow their transformation. Unlike RequestFilters, this influences the response returned from the request to the client. The request header is also passed to ResponseFilter but read-only and solely for the purpose of extracting request data for situations where it is needed to transform the Response (such as to retrieve an identifier that must be the same in the request and the response).

Examples

Example 1 - Simple VCR

When no special HTTP Transport is required by your http.Client, you can use VCR with the default transport:

package main

import (
    "fmt"

    "github.com/seborama/govcr"
)

const example1CassetteName = "MyCassette1"

// Example1 is an example use of govcr.
func Example1() {
    vcr := govcr.NewVCR(example1CassetteName, nil)
    vcr.Client.Get("http://example.com/foo")
    fmt.Printf("%+v\n", vcr.Stats())
}

If the cassette exists and a track matches the request, it will be played back without any real HTTP call to the live server.

Otherwise, a real live HTTP call will be made and recorded in a new track added to the cassette.

Tip:

To experiment with this example, run it at least twice: the first time (when the cassette does not exist), it will make a live call. Subsequent executions will use the track on cassette to retrieve the recorded response instead of making a live call.

Example 2 - Custom VCR Transport

Sometimes, your application will create its own http.Client wrapper or will initialise the http.Client's Transport (for instance when using https).

In such cases, you can pass the http.Client object of your application to VCR.

VCR will wrap your http.Client with its own which you can inject back into your application.

package main

import (
    "crypto/tls"
    "fmt"
    "net/http"
    "time"

    "github.com/seborama/govcr"
)

const example2CassetteName = "MyCassette2"

// myApp is an application container.
type myApp struct {
    httpClient *http.Client
}

func (app myApp) Get(url string) {
    app.httpClient.Get(url)
}

// Example2 is an example use of govcr.
// It shows the use of a VCR with a custom Client.
// Here, the app executes a GET request.
func Example2() {
    // Create a custom http.Transport.
    tr := http.DefaultTransport.(*http.Transport)
    tr.TLSClientConfig = &tls.Config{
        InsecureSkipVerify: true, // just an example, not recommended
    }

    // Create an instance of myApp.
    // It uses the custom Transport created above and a custom Timeout.
    myapp := &myApp{
        httpClient: &http.Client{
            Transport: tr,
            Timeout:   15 * time.Second,
        },
    }

    // Instantiate VCR.
    vcr := govcr.NewVCR(example2CassetteName,
        &govcr.VCRConfig{
            Client: myapp.httpClient,
        })

    // Inject VCR's http.Client wrapper.
    // The original transport has been preserved, only just wrapped into VCR's.
    myapp.httpClient = vcr.Client

    // Run request and display stats.
    myapp.Get("https://example.com/foo")
    fmt.Printf("%+v\n", vcr.Stats())
}

Example 3 - Custom VCR, POST method

Please refer to the source file in the examples directory.

This example is identical to Example 2 but with a POST request rather than a GET.

Example 4 - Custom VCR with a RequestFilters

This example shows how to handle situations where a header in the request needs to be ignored (or the track would not match and hence would not be replayed).

For this example, logging is switched on. This is achieved with Logging: true in VCRConfig when calling NewVCR.

package main

import (
    "fmt"
    "strings"
    "time"

    "net/http"

    "github.com/seborama/govcr"
)

const example4CassetteName = "MyCassette4"

// Example4 is an example use of govcr.
// The request contains a custom header 'X-Custom-My-Date' which varies with every request.
// This example shows how to exclude a particular header from the request to facilitate
// matching a previous recording.
// Without the RequestFilters, the headers would not match and hence the playback would not
// happen!
func Example4() {
    vcr := govcr.NewVCR(example4CassetteName,
        &govcr.VCRConfig{
            RequestFilters: govcr.RequestFilters{
                govcr.RequestDeleteHeaderKeys("X-Custom-My-Date"),
            },
            Logging: true,
        })

    // create a request with our custom header
    req, err := http.NewRequest("POST", "http://example.com/foo", nil)
    if err != nil {
        fmt.Println(err)
    }
    req.Header.Add("X-Custom-My-Date", time.Now().String())

    // run the request
    vcr.Client.Do(req)
    fmt.Printf("%+v\n", vcr.Stats())
}

Tip:

Remove the RequestFilters from the VCRConfig and re-run the example. Check the stats: notice how the tracks no longer replay.

Example 5 - Custom VCR with a RequestFilters and ResponseFilters

This example shows how to handle situations where a transaction Id in the header needs to be present in the response. This could be as part of a contract validation between server and client.

Note: This is useful when some of the data in the request Header / Body needs to be transformed before it can be evaluated for comparison for playback.

package main

import (
    "fmt"
    "strings"
    "time"

    "net/http"

    "github.com/seborama/govcr"
)

const example5CassetteName = "MyCassette5"

// Example5 is an example use of govcr.
// Supposing a fictional application where the request contains a custom header
// 'X-Transaction-Id' which must be matched in the response from the server.
// When replaying, the request will have a different Transaction Id than that which was recorded.
// Hence the protocol (of this fictional example) is broken.
// To circumvent that, we inject the new request's X-Transaction-Id into the recorded response.
// Without the ResponseFilters, the X-Transaction-Id in the header would not match that
// of the recorded response and our fictional application would reject the response on validation!
func Example5() {
    vcr := govcr.NewVCR(example5CassetteName,
        &govcr.VCRConfig{
            RequestFilters: govcr.RequestFilters{
                govcr.RequestDeleteHeaderKeys("X-Transaction-Id"),
            },
			ResponseFilters: govcr.ResponseFilters{
				// overwrite X-Transaction-Id in the Response with that from the Request
				govcr.ResponseTransferHeaderKeys("X-Transaction-Id"),
			},
            Logging: true,
        })

    // create a request with our custom header
    req, err := http.NewRequest("POST", "http://example.com/foo5", nil)
    if err != nil {
        fmt.Println(err)
    }
    req.Header.Add("X-Transaction-Id", time.Now().String())

    // run the request
    resp, err := vcr.Client.Do(req)
    if err != nil {
        fmt.Println(err)
    }

    // verify outcome
    if req.Header.Get("X-Transaction-Id") != resp.Header.Get("X-Transaction-Id") {
        fmt.Println("Header transaction Id verification failed - this would be the live request!")
    } else {
        fmt.Println("Header transaction Id verification passed - this would be the replayed track!")
    }

    fmt.Printf("%+v\n", vcr.Stats())
}

More examples

Refer to examples/example6.go for advanced examples.

All examples are in the examples directory.

Stats

VCR provides some statistics.

To access the stats, call vcr.Stats() where vcr is the VCR instance obtained from NewVCR(...).

Run the examples

Please refer to the examples directory for examples of code and uses.

Observe the output of the examples between the 1st run and the 2nd run of each example.

The first time they run, they perform a live HTTP call (Executing request to live server).

However, on second execution (and sub-sequent executions as long as the cassette is not deleted) govcr retrieves the previously recorded request and plays it back without live HTTP call (Found a matching track). You can disconnect from the internet and still playback HTTP requests endlessly!

Make utility

make examples

Manually

cd examples
go run *.go

Output

First execution - notice the stats show that a track was recorded (from a live HTTP call).

Second execution - no track is recorded (no live HTTP call) but 1 track is loaded and played back.

Running Example1...
1st run =======================================================
{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
2nd run =======================================================
{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
Complete ======================================================

Running Example2...
1st run =======================================================
{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
2nd run =======================================================
{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
Complete ======================================================

Running Example3...
1st run =======================================================
{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
2nd run =======================================================
{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
Complete ======================================================

Running Example4...
1st run =======================================================
2018/10/25 00:12:56 INFO - Cassette 'MyCassette4' - Executing request to live server for POST http://www.example.com/foo
2018/10/25 00:12:56 INFO - Cassette 'MyCassette4' - Recording new track for POST http://www.example.com/foo as POST http://www.example.com/foo
{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
2nd run =======================================================
2018/10/25 00:12:56 INFO - Cassette 'MyCassette4' - Found a matching track for POST http://www.example.com/foo
{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
Complete ======================================================

Running Example5...
1st run =======================================================
2018/10/25 00:12:56 INFO - Cassette 'MyCassette5' - Executing request to live server for POST http://www.example.com/foo5
2018/10/25 00:12:56 INFO - Cassette 'MyCassette5' - Recording new track for POST http://www.example.com/foo5 as POST http://www.example.com/foo5
Header transaction Id verification failed - this would be the live request!
{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
2nd run =======================================================
2018/10/25 00:12:56 INFO - Cassette 'MyCassette5' - Found a matching track for POST http://www.example.com/foo5
Header transaction Id verification passed - this would be the replayed track!
{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
Complete ======================================================

Run the tests

make test

or

go test -race -cover

Bugs

  • None known

Improvements

  • When unmarshaling the cassette fails, rather than fail altogether, it would be preferable to revert to live HTTP call.

  • The code has a number of TODO's which should either be taken action upon or removed!

Limitations

Go empty interfaces (interface{})

Some properties / objects in http.Response are defined as interface{}. This can cause json.Unmarshall to fail (example: when the original type was big.Int with a big interger indeed - json.Unmarshal attempts to convert to float64 and fails).

Currently, this is dealt with by converting the output of the JSON produced by json.Marshal (big.Int is changed to a string).

Support for multiple values in HTTP headers

Repeat HTTP headers may not be properly handled. A long standing TODO in the code exists but so far no one has complained :-)

HTTP transport errors

govcr also records http.Client errors (network down, blocking firewall, timeout, etc) in the track for future play back.

Since errors is an interface, when it is unmarshalled into JSON, the Go type of the error is lost.

To circumvent this, govcr serialises the object type (ErrType) and the error message (ErrMsg) in the track record.

Objects cannot be created by name at runtime in Go. Rather than re-create the original error object, govcr creates a standard error object with an error string made of both the ErrType and ErrMsg.

In practice, the implications for you depend on how much you care about the error type. If all you need to know is that an error occurred, you won't mind this limitation.

Mitigation: Support for common errors (network down) has been implemented. Support for more error types can be implemented, if there is appetite for it.

Contribute

You are welcome to submit a PR to contribute.

Please follow a TDD workflow: tests must be present and avoid toxic DDT (dev driven testing).

Issues
  • Version tags don't all work with Go modules

    Version tags don't all work with Go modules

    In an empty package, trying to load govcr with Go modules grabs some older versions:

    $ cat go.mod 
    module example
    
    go 1.12
    
    $ go get gopkg.in/seborama/govcr.v2
    $ go get gopkg.in/seborama/govcr.v3
    $ go get gopkg.in/seborama/govcr.v4
    go: finding gopkg.in/seborama/govcr.v4 latest
    
    $ cat go.mod 
    module example
    
    go 1.12
    
    require (
    	gopkg.in/seborama/govcr.v2 v2.2.1 // indirect
    	gopkg.in/seborama/govcr.v3 v3.0.1 // indirect
    	gopkg.in/seborama/govcr.v4 v4.0.0-20190303132007-6b478b087ffb // indirect
    )
    

    The latest v2 tag seems to be 2.4.2, but it doesn't get found because it's missing the v prefix (note: it's also missing from gopkg.in.) The latest v3 tag is v3.2 and the latest v4 tag is v4.4. However, modules seem to require semver which needs 3-component versions, i.e., quoting from the Modules wiki page:

    Modules must be semantically versioned according to semver, usually in the form v(major).(minor).(patch), such as v0.1.0, v1.2.3, or v1.5.0-rc.1. The leading v is required. If using Git, tag released commits with their versions. Public and private module repositories and proxies are becoming available (see FAQ below).

    So if these were re-tagged with a trailing .0 (e.g., v4.4.0), I think this would work (though I'm not totally sure, Go modules are fancy new things.)

    opened by QuLogic 13
  • Add advanced filtering

    Add advanced filtering

    When running tests where multiple request types are used, it if quite difficult to set up proper filtering.

    To help this, I made it possible to chain multiple filters and check URL and method.

    Furthermore the URL can now be modified.

    For future-proofing the Request and Response structs can now be extended with more stuff without breaking compatibility.

    If you like this, I can add some tests for the new functionality.

    opened by klauspost 11
  • json: unsupported type: func(...

    json: unsupported type: func(...

    I'm using govcr but when I have logging turned on I see the following message:

    2018/07/25 06:21:15 json: unsupported type: func(string, []uint8, int) ([]uint8, bool)
    

    Also, after running the test I don't see any new files at the CassettePath I have configured.

    opened by leighmcculloch 6
  • HTTPS requests recording

    HTTPS requests recording

    Hi @seborama!

    I tried your lib, but had a problem with it that forced me to use https://github.com/dnaeon/go-vcr instead.

    My use case: I inject your *http.Client into gorequest.SuperAgent and that into library that I maintain. All requests are HTTPS. The problem is, the records contain the body encrypted. Firstly, that's hard to maintain. Secondly, it doesn't match for request body, so I can't replay.

    opened by pkopac 5
  • Remove

    Remove "Path" from cassettes

    It seems that for no real reason, the full path is stored in a cassette:

    {
      "Name": "TestUnauth.gz",
      "Path": "/home/klaus/gopath/src/github.com/app/testdata",
      "Tracks": [ ....
    

    This creates problems for me, when I run the cassette as part of a CI test, since the cassette is no longer at this path. Therefore I get an unexpected mkdir /home/klaus: permission denied when it wants to add to the cassette.

    I don't see any reason to have this information inside the cassette since it must be provided anyway for loading it, and it only causes problems. So I'd propose to remove it.

    opened by klauspost 4
  • Add Long Play (compression) to cassettes.

    Add Long Play (compression) to cassettes.

    To play back a cassette with "long play" it must have been recorded with it.

    In the test example it reduces the size from 76k to a little over 2k.

    In the test setup I removed wipeCassette bool parameter since it wasn't used.

    opened by klauspost 3
  • Allow multiple filter cases

    Allow multiple filter cases

    This change should be backwards compatible.

    Please, ensure your pull request meet these guidelines:

    • [x] My code is written in TDD (test driven development) fashion.
    • [x] My code adheres to Go standards I have run make lint, or I have used https://goreportcard.com/ and I have taken the necessary compliance actions.
    • [ ] I have provided / updated examples.
    • [ ] I have updated [README.md].

    Thanks for your PR, you're awesome! :+1:

    opened by klauspost 2
  • Handle concurrent roundtrips better

    Handle concurrent roundtrips better

    Typically you would expect an http.RoundTripper to handle concurrent requests.

    If we look at the exiting Roundtripper it will cause race conditions on concurrent access.

    A "simple" solution could be to put a mu sync.Mutex in the vcrTransport and add t.mu.Lock() / defer t.mu.Unlock() to the RoundTrip function. This could serialize roundtrips so only one can run at the time. It will not guarantee any order of recordings, but if you do concurrent requests you wouldn't expect to.

    opened by klauspost 2
  • Add option to remove TLS from recording

    Add option to remove TLS from recording

    TLS is often not needed for responses and take up a lot of space.

    Add RemoveTLS option to VCRConfig struct. Default value false keeps existing behaviour.

    opened by klauspost 2
  • Using govcr as audit tool

    Using govcr as audit tool

    Hi @seborama!

    Thank you for govcr, is a fantastic library.

    I am planning to use this library for 2 purposes:

    1. Mock test responses
    2. Store the cassettes as a audit tool for a project

    The second use is not the intended library use. Do you know a better library to store network requests in golang? If not, what will be the best strategy to turn govcr agnostic to persistance (i.e. .cassette files, databases, etc..)?

    Thanks,

    opened by llonchj 2
  • Fix usage when body is binary

    Fix usage when body is binary

    The conversion of a []byte to a string can (and does) fail if the byte content is not valid unicode. To avoid this, pass around the []byte. Allows binary body payloads to function correctly.

    opened by richardwilkes 2
  • Skip Saving tracks on Error Responses ( 404, 402, etc.. )

    Skip Saving tracks on Error Responses ( 404, 402, etc.. )

    Feature Details:

    Allow to skip saving tracks where API call returns an error response such as 404 etc..

    Please, ensure your pull request meet these guidelines:

    • [✔️] My code is written in TDD (test driven development) fashion.
    • [✔️] My code adheres to Go standards I have run make lint, or I have used https://goreportcard.com/ and I have taken the necessary compliance actions.
    • [✔️] I have provided / updated examples.

    Thanks for your PR, you're awesome! :+1:

    opened by aqadir-ah 0
  • Add ability to remove sensitive information from responses

    Add ability to remove sensitive information from responses

    This will allow filtering of responses before they are saved.

    We also allow for updates to Trailer, TLS and ContentLength.

    Small refactor to move the filtering upstream from the cassette (as requested in the code comment)

    Please, ensure your pull request meet these guidelines:

    Fixes #42

    • [x] My code is written in TDD (test driven development) fashion.
    • [x] My code adheres to Go standards I have run make lint, or I have used https://goreportcard.com/ and I have taken the necessary compliance actions.
    • [x] I have provided / updated examples.
    • [x] I have updated [README.md].

    Thanks for your PR, you're awesome! :+1:

    opened by klauspost 3
  • WIP - TrackFilters

    WIP - TrackFilters

    TrackFilters allow modifying what is stored in tracks. An example use case is filtering Authorization headers, cookies and request/response bodies.

    Possible solution for #42, requesting feedback before I continue. Example use cases:

    func TestClientWrapper(c *http.Client, cassetteName string) *http.Client {
    	cfg := govcr.VCRConfig{
    		CassettePath:     "./testdata/govcr-fixtures",
    		Client:           c,
    		RemoveTLS:        true,
    		Logging:          testing.Verbose(),
    		DisableRecording: !IsDevMode(),
    	}
    
    	cfg.TrackFilters.Add(
    		govcr.TrackRequestDeleteHeaderKeys("Authorization"),
    		govcr.TrackResponseDeleteHeaderKeys("Set-Cookie"),
    		govcr.TrackFilter(func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
    			req.URL.Host = "mock.okta.example.com"
    			return req, resp
    		}),
    
    		govcr.TrackRequestChangeBody(func(b []byte) []byte {
    			client := CreateClientOptions{}
    			if err := json.Unmarshal(b, &client); err != nil {
    				log.Fatal(err)
    			}
    			client.ClientName = "vault-client-MOCK-3e9c0c50-6b3b-9335-87c3-9055e07019aa"
    			b, err := json.Marshal(&client)
    			if err != nil {
    				log.Fatal(err)
    			}
    
    			return b
    		}).OnMethod(http.MethodPost).OnPath("/oauth2/v1/clients/?$"),
    		govcr.TrackResponseChangeBody(func(b []byte) []byte {
    			client := ClientResponse{}
    			if err := json.Unmarshal(b, &client); err != nil {
    				log.Fatal(err)
    			}
    			client.ClientID = "0oaftpbw8rpuExJeA356"
    			client.ClientSecret = "MOCK-CLIENT-SECRET"
    			client.ClientName = "vault-client-MOCK-3e9c0c50-6b3b-9335-87c3-9055e07019aa"
    			client.ClientIDIssuedAt = 1554750248
    			b, err := json.Marshal(&client)
    			if err != nil {
    				log.Fatal(err)
    			}
    
    			return b
    		}).OnMethod(http.MethodPost).OnPath("/oauth2/v1/clients/?$").OnStatus(http.StatusCreated),
    
    		govcr.TrackFilter(func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
    			req.URL.Path = "/oauth2/v1/clients/0oaftpbw8rpuExJeA356/lifecycle/newSecret"
    			return req, resp
    		}).OnMethod(http.MethodPost).OnPath("/oauth2/v1/clients/[^/]+/lifecycle/newSecret"),
    		govcr.TrackResponseChangeBody(func(b []byte) []byte {
    			client := ClientResponse{}
    			if err := json.Unmarshal(b, &client); err != nil {
    				log.Fatal(err)
    			}
    			client.ClientID = "0oaftpbw8rpuExJeA356"
    			client.ClientSecret = "MOCK-CLIENT-NEW-SECRET"
    			client.ClientName = "vault-client-MOCK-3e9c0c50-6b3b-9335-87c3-9055e07019aa"
    			client.ClientIDIssuedAt = 1554750248
    			b, err := json.Marshal(&client)
    			if err != nil {
    				log.Fatal(err)
    			}
    
    			return b
    		}).OnMethod(http.MethodPost).OnPath("/oauth2/v1/clients/[^/]+/lifecycle/newSecret").OnStatus(http.StatusOK),
    
    		govcr.TrackRequestChangeBody(func(b []byte) []byte {
    			client := CreateClientOptions{}
    			if err := json.Unmarshal(b, &client); err != nil {
    				log.Fatal(err)
    			}
    			client.ClientName = "vault-client-MOCK-3e9c0c50-6b3b-9335-87c3-9055e07019aa-updated"
    			b, err := json.Marshal(&client)
    			if err != nil {
    				log.Fatal(err)
    			}
    
    			return b
    		}).OnMethod(http.MethodPut).OnPath("/oauth2/v1/clients/.+"),
    		govcr.TrackFilter(func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
    			req.URL.Path = "/oauth2/v1/clients/nonexistent-MOCK-f69b6323-bb5d-0caf-682b-600f23750e66"
    			return req, resp
    		}).OnMethod(http.MethodPut).OnPath("/oauth2/v1/clients/nonexistent-.*"),
    
    		govcr.TrackFilter(func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
    			req.URL.Path = "/oauth2/v1/clients/0oaftpbw8rpuExJeA356"
    			return req, resp
    		}).OnMethod(http.MethodPut).OnPath("/oauth2/v1/clients/[^n].*"),
    
    		govcr.TrackFilter(func(req *http.Request, resp *http.Response, err error) (*http.Request, *http.Response) {
    			req.URL.Path = "/oauth2/v1/clients/0oaftpbw8rpuExJeA356"
    			return req, resp
    		}).OnMethod(http.MethodDelete).OnPath("/oauth2/v1/clients/.+"),
    	)
    	vcr := govcr.NewVCR(cassetteName, &cfg)
    	return vcr.Client
    }
    
    opened by michaelw 3
  • Ability to remove sensitive information from responses

    Ability to remove sensitive information from responses

    Sensitive headers (e.g., Set-Cookie) are stored in the cassette currently. Is there a way to ensure they are not stored?

    Currently, I am injecting a custom RoundTripper:

    func (tr *testScrubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    	resp, err := tr.Original.RoundTrip(req)
    	if err == nil {
    		// Scrub sensitive material here
    		resp.Header.Del("Set-Cookie")
    	}
    	return resp, err
    }
    

    But it would be nice if we could use govcr's ResponseFilter machinery for this.

    opened by michaelw 0
Releases(v5.0.0-alpha.2)
  • v5.0.0-alpha.2(Jul 28, 2019)

    This pre-release introduces govcr v5 alpha.

    Also, some changes to package organisation.

    The README is not up-to-date with the code:

    • incorrect NewVCR usage
    • filter functions are not yet implemented in this release
    • this release does have support for track recording mutators
    Source code(tar.gz)
    Source code(zip)
  • v5.0.0-alpha.1(Jul 26, 2019)

    This pre-release introduces govcr v5 alpha. This is for testing only as it does not support request / response filters on playback yet. Use v4 for a stable release.

    Basic functionality should be working fine:

    • recording cassettes
    • mutation of tracks on recording
    • replaying tracks
    • compression logic is implemented but I think I haven't fully surfaced it yet
    Source code(tar.gz)
    Source code(zip)
  • v4.5.0(Jul 26, 2019)

  • v4.4.0(Mar 3, 2019)

  • v4.2.0(Jan 3, 2019)

  • v4.0.0(Dec 14, 2018)

  • v3.2.0(Nov 3, 2018)

    See the README.md for details.

    Thanks to @klauspost for his contribution!

    See the tests for examples, in particular:

    • TestLongPlay() in govcr_test.go
    • govcr_example4_test.go
    Source code(tar.gz)
    Source code(zip)
  • v3.1.0(Oct 25, 2018)

    This is to help migrating from v2 to v3.

    Thanks to @klauspost for another nice contribution.

    Note that the helper RequestExcludeHeaderFunc will be removed on or after April 25th 2019!

    Source code(tar.gz)
    Source code(zip)
  • v3.0.0(Oct 24, 2018)

    This release replaces ExcludeHeaderFunc and FilterFunc's with RequestFilters and ResponseFilters.

    Filters are much more flexible and simpler to use. They also offer the possibility to more easily re-use Filters and create re-usable "recipes" of Filters.

    Many thanks to @klauspost for his great work!

    Here's a simple example:

    govcr.VCRConfig{
    	RequestFilters: govcr.RequestFilters{
    		govcr.RequestDeleteHeaderKeys("X-Custom-My-Date"),
    	},
    }
    

    Refer to examples/example6.go for advanced examples.

    Source code(tar.gz)
    Source code(zip)
  • v2.4.2(Oct 20, 2018)

  • v2.4.1(Jul 18, 2018)

  • v2.3.0(Mar 3, 2017)

  • v2.2.0(Feb 17, 2017)

  • v2.0.0(Sep 12, 2016)

    This release changes the API and hence is tagged as v2.

    It removes the v1 style FilterFuncs and consolidates them in two new FilterFuncs: RequestFilterFunc and ResponseFilterFunc.

    RequestFilterFunc receives the request Header / Body to allow their transformation. Both the live request and the replayed request are filtered at comparison time. Transformations are not persisted and only for the purpose of influencing comparison.

    ResponseFilterFunc is the flip side of RequestFilterFunc. It receives the response Header / Body to allow their transformation. Unlike RequestFilterFunc, this influences the response returned from the request to the client. The request header is also passed to ResponseFilterFunc but read-only and solely for the purpose of extracting request data for situations where it is needed to transform the Response.

    Source code(tar.gz)
    Source code(zip)
  • v1.6.0(Sep 10, 2016)

  • v1.1.0(Jul 22, 2016)

    Via VCRConfig.CassettePath

    Quick example:

        vcr := govcr.NewVCR("MyCassette",
            &govcr.VCRConfig{
                CassettePath: "./govcr-fixtures",
            })
    
    

    Note: If you omit CassettePath or use "" then the default path is ./govcr-fixtures.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Jul 25, 2019)

Owner
Seb C
Seb C
Sql mock driver for golang to test database interactions

Sql driver mock for Golang sqlmock is a mock library implementing sql/driver. Which has one and only purpose - to simulate any sql driver behavior in

DATA-DOG 4.5k Jun 25, 2022
Vault mock - Mock of Hashicorp Vault used for unit testing

vault_mock Mock of Hashicorp Vault used for unit testing Notice This is a person

Elliot Rotenstein 0 Jan 19, 2022
CLI tool to mock TCP connections. You can use it with Detox, Cypress or any other framework to automatically mock your backend or database.

Falso It is a CLI that allows you to mock requests/responses between you and any server without any configuration or previous knowledge about how it w

Sorin 3 Mar 15, 2022
Mock-the-fck - Mock exercise for human

Mock the fck Originally, Mockery-example Example case for mockery issue #128 fil

Mike Cat 0 Jan 21, 2022
Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.

Fortio Fortio (Φορτίο) started as, and is, Istio's load testing tool and now graduated to be its own project. Fortio is also used by, among others, Me

Fortio (Φορτίο) 2.6k Jun 24, 2022
A Go implementation of Servirtium, a library that helps test interactions with APIs.

Servirtium is a server that serves as a man-in-the-middle: it processes incoming requests, forwards them to a destination API and writes the response into a Markdown file with a special format that is common across all of the implementations of the library.

Servirtium 6 Jun 16, 2022
Go-interactions - Easy slash commands for Arikawa

go-interactions A library that aims to make dealing with discord's slash command

null 7 May 26, 2022
Merge Mock - testing tool for the Ethereum Merge

MergeMock Experimental debug tooling, mocking the execution engine and consensus node for testing. work in progress Quick Start To get started, build

Diederik Loerakker 33 Jun 17, 2022
A mock of Go's net package for unit/integration testing

netmock: Simulate Go network connections netmock is a Go package for simulating net connections, including delays and disconnects. This is work in pro

Lucas Wolf 1 Oct 27, 2021
mock server to aid testing the jaguar-java client API

stripe-mock stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's test mode to make test suites

knr.data 1 Dec 24, 2021
Mock object for Go http.ResponseWriter

mockhttp -- Go package for unit testing HTTP serving Unit testing HTTP services written in Go means you need to call their ServeHTTP receiver. For thi

Tv 21 Mar 22, 2022
A basic lightweight HTTP client for Go with included mock features.

A basic lightweight HTTP client for Go with included mock features. Features Support almost all http method like G

Andres Mijares 1 May 2, 2022
📡 mock is a simple, cross-platform, cli app to simulate HTTP-based APIs.

mock ?? mock is a simple, cross-platform, cli app to simulate HTTP-based APIs. About mock Mock allows you to spin up a local http server based of a .m

Bruno Schaatsbergen 8 May 6, 2022
A tool that integrates SQL, HTTP,interface,Redis mock

Mockit 目标:将mock变得简单,让代码维护变得容易 分支介绍 main 主分支,覆盖了单元测试 light 轻分支,去除了单元测试,简化了依赖项,方便其他团队使用 常见Mock难点 不同中间件,mock库设计模式不一致,学习代价高,差异化明显 mock方案强依赖服务端,无法灵活解耦 单元测试

SHIHUO 14 Apr 19, 2022
Just Dance Unlimited mock-up server written on Golang and uses a popular Gin framework for Go.

BDCS Just Dance Unlimited mock-up server written on Golang and uses a popular Gin framework for Go. Features Security Authorization works using UbiSer

Mikhail 0 Nov 10, 2021
siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

Re 249 Jun 27, 2022
A yaml data-driven testing format together with golang testing library

Specimen Yaml-based data-driven testing Specimen is a yaml data format for data-driven testing. This enforces separation between feature being tested

Design it, Run it 0 Jan 31, 2022
mockery - A mock code autogenerator for Golang

mockery - A mock code autogenerator for Golang

Vektra 3.6k Jun 26, 2022
A simple mock server configurable via JSON, built using GoLang.

GoMock Server A simple mock server configurable via JSON, built using GoLang. How To A file name endpoint.json must be placed in the context root, wit

Miguel Alexandre 0 Jan 9, 2022