Skip to content

Commit

Permalink
Change RequestMatchers to work like Mutators (#104)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Change RequestMatchers to work like Mutators
  • Loading branch information
seborama authored Aug 28, 2022
1 parent a5f71b9 commit 3d1c95c
Show file tree
Hide file tree
Showing 31 changed files with 197 additions and 238 deletions.
48 changes: 23 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<img src="https://github.com/seborama/govcr/actions/workflows/codeql-analysis.yml/badge.svg?branch=master" alt="govcr">
</a>

<a href="https://pkg.go.dev/github.com/seborama/govcr/v12">
<a href="https://pkg.go.dev/github.com/seborama/govcr/v13">
<img src="https://img.shields.io/badge/godoc-reference-blue.svg" alt="govcr">
</a>

<a href="https://goreportcard.com/report/github.com/seborama/govcr/v12">
<img src="https://goreportcard.com/badge/github.com/seborama/govcr/v12" alt="govcr">
<a href="https://goreportcard.com/report/github.com/seborama/govcr/v13">
<img src="https://goreportcard.com/badge/github.com/seborama/govcr/v13" alt="govcr">
</a>
</p>

Expand Down Expand Up @@ -77,7 +77,7 @@ This project is an adaptation for Google's Go / Golang programming language.
func TestExample1() {
vcr := govcr.NewVCR(
govcr.NewCassetteLoader("MyCassette1.json"),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
govcr.WithRequestMatchers(govcr.NewMethodURLRequestMatchers()...), // use a "relaxed" request matcher
)

vcr.Client.Get("http://example.com/foo")
Expand All @@ -97,15 +97,15 @@ We use a "relaxed" request matcher because `example.com` injects an "`Age`" head
## Install

```bash
go get github.com/seborama/govcr/v12@latest
go get github.com/seborama/govcr/v13@latest
```

For all available releases, please check the [releases](https://github.com/seborama/govcr/releases) tab on github.

And your source code would use this import:

```go
import "github.com/seborama/govcr/v12"
import "github.com/seborama/govcr/v13"
```

For versions of **govcr** before v5 (which don't use go.mod), use a dependency manager to lock the version you wish to use (perhaps v4)!
Expand Down Expand Up @@ -135,7 +135,7 @@ go get gopkg.in/seborama/govcr.v4

**govcr** is a wrapper around the Go `http.Client`. It can record live HTTP traffic to files (called "**cassettes**") and later replay HTTP requests ("**tracks**") from them instead of live HTTP calls.

The code documentation can be found on [godoc](https://pkg.go.dev/github.com/seborama/govcr/v12).
The code documentation can be found on [godoc](https://pkg.go.dev/github.com/seborama/govcr/v13).

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

Expand All @@ -161,7 +161,7 @@ This structure contains parameters for configuring your **govcr** recorder.
Settings are populated via `With*` options:

- Use `WithClient` to provide a custom http.Client otherwise the default Go http.Client will be used.
- See `vcrsettings.go` for more options such as `WithRequestMatcher`, `WithTrackRecordingMutators`, `WithTrackReplayingMutators`, ...
- See `vcrsettings.go` for more options such as `WithRequestMatchers`, `WithTrackRecordingMutators`, `WithTrackReplayingMutators`, ...
- TODO: `WithLogging` enables logging to help understand what **govcr** is doing internally.

[(toc)](#table-of-content)
Expand All @@ -178,7 +178,7 @@ This may be the case when the request contains a timestamp or a dynamically chan

You can create your own matcher on any part of the request and in any manner (like ignoring or modifying some headers, etc).

The input parameters received by a `RequestMatcherFunc` are scoped to the `RequestMatcher`. It is safe to modify them as needed. This affects the other `RequestMatcherFunc`'s in the `RequestMatcher`. But it does **not** permeate to the original incoming HTTP request or the tracks read from or written to the cassette.
The input parameters received by a `RequestMatcher` are scoped to the `RequestMatchers`. This affects the other `RequestMatcher`'s. But it does **not** permeate throughout the VCR to the original incoming HTTP request or the tracks read from or written to the cassette.

[(toc)](#table-of-content)

Expand Down Expand Up @@ -437,7 +437,7 @@ vcr := govcr.NewVCR(
The command is located in the `cmd/govcr` folder, to install it:

```bash
go install github.com/seborama/govcr/v12/cmd/govcr@latest
go install github.com/seborama/govcr/v13/cmd/govcr@latest
```

Example usage:
Expand Down Expand Up @@ -473,20 +473,18 @@ This example shows how to handle situations where a header in the request needs
This could be necessary because the header value is not predictable or changes for each request.

```go
vcr.SetRequestMatcher(
govcr.NewRequestMatcherCollection(
govcr.DefaultMethodMatcher,
govcr.DefaultURLMatcher,
func(httpRequest, trackRequest *track.Request) bool {
// we can safely mutate our inputs:
// mutations affect other RequestMatcherFunc's but _not_ the
// original HTTP request or the cassette Tracks.
httpRequest.Header.Del("X-Custom-Timestamp")
trackRequest.Header.Del("X-Custom-Timestamp")

return govcr.DefaultHeaderMatcher(httpRequest, trackRequest)
},
),
vcr.SetRequestMatchers(
govcr.DefaultMethodMatcher,
govcr.DefaultURLMatcher,
func(httpRequest, trackRequest *track.Request) bool {
// we can safely mutate our inputs:
// mutations affect other RequestMatcher's but _not_ the
// original HTTP request or the cassette Tracks.
httpRequest.Header.Del("X-Custom-Timestamp")
trackRequest.Header.Del("X-Custom-Timestamp")

return govcr.DefaultHeaderMatcher(httpRequest, trackRequest)
},
)
```

Expand All @@ -508,7 +506,7 @@ How you specifically tackle this in practice really depends on how the API you a
// See TestExample3 in tests for fully working example.
vcr := govcr.NewVCR(
govcr.NewCassetteLoader(exampleCassetteName3),
govcr.WithRequestMatcherFuncs(
govcr.WithRequestMatchers(
func(httpRequest, trackRequest *track.Request) bool {
// Remove the header from comparison.
// Note: this removal is only scoped to the request matcher, it does not affect the original HTTP request
Expand Down
10 changes: 5 additions & 5 deletions cassette/cassette.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v12/compression"
cryptoerr "github.com/seborama/govcr/v12/encryption/errors"
govcrerr "github.com/seborama/govcr/v12/errors"
"github.com/seborama/govcr/v12/stats"
"github.com/seborama/govcr/v13/cassette/track"
"github.com/seborama/govcr/v13/compression"
cryptoerr "github.com/seborama/govcr/v13/encryption/errors"
govcrerr "github.com/seborama/govcr/v13/errors"
"github.com/seborama/govcr/v13/stats"
)

// Cassette contains a set of tracks.
Expand Down
6 changes: 3 additions & 3 deletions cassette/cassette_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/cassette"
"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v12/encryption"
"github.com/seborama/govcr/v13/cassette"
"github.com/seborama/govcr/v13/cassette/track"
"github.com/seborama/govcr/v13/encryption"
)

func Test_cassette_GzipFilter(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cassette/cassette_wb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/seborama/govcr/v12/stats"
"github.com/seborama/govcr/v13/stats"
)

func Test_cassette_NumberOfTracks_PanicsWhenNoCassette(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cassette/track/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v13/cassette/track"
)

func TestRequest_Clone(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cassette/track/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"regexp"
)

// Predicate is a function signature that takes a Track and returns a boolean.
// It is used to construct conditional mutators.
type Predicate func(trk *Track) bool
type Predicate func(*Track) bool

// Any accepts one or more predicates and returns a new predicate that will evaluate
// to true when any the supplied predicate is true, otherwise false.
Expand Down
2 changes: 1 addition & 1 deletion cassette/track/mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v13/cassette/track"
)

func Test_Mutator_On(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cassette/track/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/pkg/errors"

trkerr "github.com/seborama/govcr/v12/cassette/track/errors"
trkerr "github.com/seborama/govcr/v13/cassette/track/errors"
)

// Track is a recording (HTTP Request + Response) in a cassette.
Expand Down
2 changes: 1 addition & 1 deletion cassette/track/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v13/cassette/track"
)

func TestTrack_ToHTTPResponse(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/govcr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/pkg/errors"

"github.com/seborama/govcr/v12/cassette"
"github.com/seborama/govcr/v12/encryption"
"github.com/seborama/govcr/v13/cassette"
"github.com/seborama/govcr/v13/encryption"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12"
"github.com/seborama/govcr/v12/stats"
"github.com/seborama/govcr/v13"
"github.com/seborama/govcr/v13/stats"
)

func TestConcurrencySafety(t *testing.T) {
Expand Down
15 changes: 10 additions & 5 deletions controlpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package govcr
import (
"net/http"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v12/stats"
"github.com/seborama/govcr/v13/cassette/track"
"github.com/seborama/govcr/v13/stats"
)

// ControlPanel holds the parts of a VCR that can be interacted with.
Expand All @@ -18,9 +18,14 @@ func (controlPanel *ControlPanel) Stats() *stats.Stats {
return controlPanel.vcrTransport().stats()
}

// SetRequestMatcher sets a new RequestMatcher to the VCR.
func (controlPanel *ControlPanel) SetRequestMatcher(requestMatcher RequestMatcher) {
controlPanel.vcrTransport().SetRequestMatcher(requestMatcher)
// SetRequestMatchers sets a new set of RequestMatcher's to the VCR.
func (controlPanel *ControlPanel) SetRequestMatchers(requestMatcher ...RequestMatcher) {
controlPanel.vcrTransport().SetRequestMatchers(requestMatcher...)
}

// AddRequestMatchers sets a new set of RequestMatcher's to the VCR.
func (controlPanel *ControlPanel) AddRequestMatchers(requestMatcher ...RequestMatcher) {
controlPanel.vcrTransport().AddRequestMatchers(requestMatcher...)
}

// SetReadOnlyMode sets the VCR to read-only mode (true) or to normal read-write (false).
Expand Down
2 changes: 1 addition & 1 deletion controlpanel_wb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v13/cassette/track"
)

func TestControlPanel_SetRecordingMutators(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion encryption/.study/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"

cryptoerr "github.com/seborama/govcr/v12/encryption/errors"
cryptoerr "github.com/seborama/govcr/v13/encryption/errors"
)

// nolint: deadcode
Expand Down
2 changes: 1 addition & 1 deletion encryption/aesgcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/pkg/errors"

cryptoerr "github.com/seborama/govcr/v12/encryption/errors"
cryptoerr "github.com/seborama/govcr/v13/encryption/errors"
)

// NewAESGCMWithRandomNonceGenerator creates a new Cryptor initialised with an
Expand Down
2 changes: 1 addition & 1 deletion encryption/aesgcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/encryption"
"github.com/seborama/govcr/v13/encryption"
)

func TestCryptor_AESGCM(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion encryption/chacha20poly1305_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/seborama/govcr/v12/encryption"
"github.com/seborama/govcr/v13/encryption"
)

func TestCryptor_ChaCha20Poly1305(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions examples/Example1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"os"
"testing"

"github.com/seborama/govcr/v12"
"github.com/seborama/govcr/v12/stats"
"github.com/stretchr/testify/assert"

"github.com/seborama/govcr/v13"
"github.com/seborama/govcr/v13/stats"
)

const exampleCassetteName1 = "temp-fixtures/TestExample1.cassette.json"
Expand All @@ -17,7 +18,7 @@ func TestExample1(t *testing.T) {

vcr := govcr.NewVCR(
govcr.NewCassetteLoader(exampleCassetteName1),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
govcr.WithRequestMatchers(govcr.NewMethodURLRequestMatchers()...), // use a "relaxed" request matcher
)

// The first request will be live and transparently recorded by govcr since the cassette is empty
Expand All @@ -37,7 +38,7 @@ func TestExample1(t *testing.T) {
// No live HTTP request is placed to the live server
vcr = govcr.NewVCR(
govcr.NewCassetteLoader(exampleCassetteName1),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
govcr.WithRequestMatchers(govcr.NewMethodURLRequestMatchers()...), // use a "relaxed" request matcher
)

vcr.HTTPClient().Get("http://example.com/foo")
Expand Down
2 changes: 1 addition & 1 deletion examples/Example2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/seborama/govcr/v12"
"github.com/seborama/govcr/v13"
)

const exampleCassetteName2 = "temp-fixtures/TestExample2.cassette.json"
Expand Down
6 changes: 3 additions & 3 deletions examples/Example3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/google/uuid"
"github.com/seborama/govcr/v12"
"github.com/seborama/govcr/v12/cassette/track"
"github.com/seborama/govcr/v13"
"github.com/seborama/govcr/v13/cassette/track"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,7 +21,7 @@ func TestExample3(t *testing.T) {
// Instantiate VCR.
vcr := govcr.NewVCR(
govcr.NewCassetteLoader(exampleCassetteName3),
govcr.WithRequestMatcherFuncs(
govcr.WithRequestMatchers(
func(httpRequest, trackRequest *track.Request) bool {
// Remove the header from comparison.
// Note: this removal is only scoped to the request matcher, it does not affect the original HTTP request
Expand Down
8 changes: 4 additions & 4 deletions examples/Example4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"os"
"testing"

"github.com/seborama/govcr/v12"
"github.com/seborama/govcr/v12/encryption"
"github.com/seborama/govcr/v12/stats"
"github.com/seborama/govcr/v13"
"github.com/seborama/govcr/v13/encryption"
"github.com/seborama/govcr/v13/stats"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,7 +22,7 @@ func TestExample4(t *testing.T) {
WithCipher(
encryption.NewChaCha20Poly1305WithRandomNonceGenerator,
"test-fixtures/TestExample4.unsafe.key"),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
govcr.WithRequestMatchers(govcr.NewMethodURLRequestMatchers()...), // use a "relaxed" request matcher
)

// The first request will be live and transparently recorded by govcr since the cassette is empty
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/seborama/govcr/v12
module github.com/seborama/govcr/v13

go 1.17

Expand Down
Loading

0 comments on commit 3d1c95c

Please sign in to comment.