dnaeon / go-vcr

Record and replay your HTTP interactions for fast, deterministic and accurate tests
BSD 2-Clause "Simplified" License
1.26k stars 78 forks source link

Add ability to skip certain interactions from getting saved to disk #80

Closed sergiught closed 2 years ago

sergiught commented 2 years ago

Describe the problem you'd like to have solved

I'd like to have a way to specify which kind of Interaction to skip from being recorded on disk inside a cassette. This would be useful for example if I don't want the cassette spammed with 429s.

Describe the ideal solution

Perhaps something like:

recorder.AddHook(
    func(i *cassette.Interaction) error {
        if i.Response.Code == http.StatusTooManyRequests {
            i.Skip()
            return nil
        }
        return nil
    },
    recorder.BeforeSaveHook,
)

Alternatives and current workarounds

Current workaround would be to manually remove them from the cassette file.

Additional context

Thanks a lot for maintaining this! It's a great project. 👍🏻

dnaeon commented 2 years ago

@sergiught , please check #81 and let me know what do you think?

Example hook you could set looks like this.

    hook := func(i *cassette.Interaction) error {
        if i.Request.Method == http.MethodPost && i.Request.Body == "foo" {
            i.DiscardOnSave = true
        }

        return nil
    }