Skip to content

Commit

Permalink
-feat: remove LoadCassette, add CassetteMaker (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama authored Aug 24, 2022
1 parent 9435de1 commit 1b88cd3
Show file tree
Hide file tree
Showing 30 changed files with 459 additions and 517 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ test: deps cover

lint: deps
./golangci-lint.sh || :

bin: deps
go build -o bin/govcr ./cmd/govcr/...

14 changes: 7 additions & 7 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/v9">
<a href="https://pkg.go.dev/github.com/seborama/govcr/v10">
<img src="https://img.shields.io/badge/godoc-reference-blue.svg" alt="govcr">
</a>

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

Expand Down Expand Up @@ -98,15 +98,15 @@ We use a "relaxed" request matcher because `example.com` injects an "`Age`" head
## Install

```bash
go get github.com/seborama/govcr/v9@latest
go get github.com/seborama/govcr/v10@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/v9"
import "github.com/seborama/govcr/v10"
```

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 @@ -136,7 +136,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/v9).
The code documentation can be found on [godoc](https://pkg.go.dev/github.com/seborama/govcr/v10).

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

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

```bash
go install github.com/seborama/govcr/v9/cmd/govcr@latest
go install github.com/seborama/govcr/v10/cmd/govcr@latest
```

Example usage:
Expand Down
26 changes: 19 additions & 7 deletions cassette/cassette.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -13,11 +14,11 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"

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

// Cassette contains a set of tracks.
Expand Down Expand Up @@ -50,13 +51,20 @@ type Option func(*Cassette)
// WithCassetteCrypter provides a crypter to encrypt/decrypt cassette content.
func WithCassetteCrypter(crypter Crypter) Option {
return func(k7 *Cassette) {
if k7.crypter != nil {
log.Println("notice: setting a crypter but another one had already been registered - this is incorrect usage")
}

k7.crypter = crypter
}
}

// NewCassette creates a ready to use new cassette.
func NewCassette(name string, opts ...Option) *Cassette {
k7 := Cassette{name: name, trackSliceMutex: sync.RWMutex{}}
k7 := Cassette{
name: name,
trackSliceMutex: sync.RWMutex{},
}

for _, option := range opts {
option(&k7)
Expand Down Expand Up @@ -262,6 +270,10 @@ func (k7 *Cassette) Name() string {
// readCassetteFile reads the cassette file, if present or
// returns a blank cassette.
func (k7 *Cassette) readCassetteFile(cassetteName string) error {
if cassetteName == "" {
return errors.New("a cassette name is required")
}

data, err := os.ReadFile(cassetteName) // nolint:gosec
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -384,7 +396,7 @@ func LoadCassette(cassetteName string, opts ...Option) *Cassette {

err := k7.readCassetteFile(cassetteName)
if err != nil {
panic(fmt.Sprintf("unable to load corrupted cassette '%s': %+v", cassetteName, err))
panic(fmt.Sprintf("unable to invalid / load corrupted cassette '%s': %+v", cassetteName, err))
}

// initial stats
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/v9/cassette"
"github.com/seborama/govcr/v9/cassette/track"
"github.com/seborama/govcr/v9/encryption"
"github.com/seborama/govcr/v10/cassette"
"github.com/seborama/govcr/v10/cassette/track"
"github.com/seborama/govcr/v10/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/v9/stats"
"github.com/seborama/govcr/v10/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/v9/cassette/track"
"github.com/seborama/govcr/v10/cassette/track"
)

func TestRequest_Clone(t *testing.T) {
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/v9/cassette/track"
"github.com/seborama/govcr/v10/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/v9/cassette/track/errors"
trkerr "github.com/seborama/govcr/v10/cassette/track/errors"
)

// Track is a recording (HTTP Request + Response) in a cassette.
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/v9/cassette"
"github.com/seborama/govcr/v9/encryption"
"github.com/seborama/govcr/v10/cassette"
"github.com/seborama/govcr/v10/encryption"
)

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

"github.com/seborama/govcr/v9"
"github.com/seborama/govcr/v9/stats"
"github.com/seborama/govcr/v10"
"github.com/seborama/govcr/v10/stats"
)

func TestConcurrencySafety(t *testing.T) {
const cassetteName = "temp-fixtures/TestConcurrencySafety"
const cassetteName = "temp-fixtures/TestConcurrencySafety.cassette"
threadMax := int8(50)

// create a test server
Expand All @@ -39,6 +39,7 @@ func TestConcurrencySafety(t *testing.T) {
t.Fatalf("err from w.Write(): Expected nil, got %s", err)
}
}))
defer ts.Close()

testServerClient := ts.Client()
testServerClient.Timeout = 5 * time.Second
Expand Down Expand Up @@ -77,7 +78,6 @@ func TestConcurrencySafety(t *testing.T) {
TracksPlayed: 0,
}
require.EqualValues(t, expectedStats, *vcr.Stats())
vcr.EjectCassette()

// re-run request and expect play back from vcr
vcr = createVCR(cassetteName, testServerClient)
Expand Down Expand Up @@ -116,8 +116,8 @@ func TestConcurrencySafety(t *testing.T) {

func createVCR(cassetteName string, client *http.Client) *govcr.ControlPanel {
return govcr.NewVCR(
govcr.WithClient(client),
govcr.WithCassette(cassetteName))
govcr.NewCassetteMaker(cassetteName),
govcr.WithClient(client))
}

func generateBinaryBody(sequence int8) []byte {
Expand Down
18 changes: 2 additions & 16 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/v9/cassette/track"
"github.com/seborama/govcr/v9/stats"
"github.com/seborama/govcr/v10/cassette/track"
"github.com/seborama/govcr/v10/stats"
)

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

// LoadCassette into the VCR.
// Cassette options may be provided (e.g. cryptography).
// Note: cassette.LoadCassette panics if the cassette exists but fails to load.
func (controlPanel *ControlPanel) LoadCassette(cassetteName string, opts ...CassetteOption) error {
k7Opts := ToCassetteOptions(opts...)

return controlPanel.vcrTransport().loadCassette(cassetteName, k7Opts...)
}

// SetRequestMatcher sets a new RequestMatcher to the VCR.
func (controlPanel *ControlPanel) SetRequestMatcher(requestMatcher RequestMatcher) {
controlPanel.vcrTransport().SetRequestMatcher(requestMatcher)
Expand Down Expand Up @@ -90,11 +81,6 @@ func (controlPanel *ControlPanel) HTTPClient() *http.Client {
return controlPanel.client
}

// EjectCassette from the VCR.
func (controlPanel *ControlPanel) EjectCassette() {
controlPanel.vcrTransport().ejectCassette()
}

// NumberOfTracks returns the number of tracks contained in the cassette.
func (controlPanel *ControlPanel) NumberOfTracks() int32 {
return controlPanel.vcrTransport().NumberOfTracks()
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/v9/cassette/track"
"github.com/seborama/govcr/v10/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/v9/encryption/errors"
cryptoerr "github.com/seborama/govcr/v10/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 @@ -4,7 +4,7 @@ import (
"crypto/aes"
"crypto/cipher"

cryptoerr "github.com/seborama/govcr/v9/encryption/errors"
cryptoerr "github.com/seborama/govcr/v10/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/v9/encryption"
"github.com/seborama/govcr/v10/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/v9/encryption"
"github.com/seborama/govcr/v10/encryption"
)

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

"github.com/seborama/govcr/v9"
"github.com/seborama/govcr/v9/stats"
"github.com/seborama/govcr/v10"
"github.com/seborama/govcr/v10/stats"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

vcr := govcr.NewVCR(
govcr.WithCassette(exampleCassetteName1),
govcr.NewCassetteMaker(exampleCassetteName1),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
)

Expand All @@ -36,9 +35,10 @@ func TestExample1(t *testing.T) {

// The second request will be transparently replayed from the cassette by govcr
// No live HTTP request is placed to the live server
vcr.EjectCassette()
err := vcr.LoadCassette(exampleCassetteName1)
require.NoError(t, err)
vcr = govcr.NewVCR(
govcr.NewCassetteMaker(exampleCassetteName1),
govcr.WithRequestMatcher(govcr.NewMethodURLRequestMatcher()), // use a "relaxed" request matcher
)

vcr.HTTPClient().Get("http://example.com/foo")
assert.Equal(
Expand Down
4 changes: 2 additions & 2 deletions 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/v9"
"github.com/seborama/govcr/v10"
)

const exampleCassetteName2 = "temp-fixtures/TestExample2.cassette.json"
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestExample2(t *testing.T) {

// Instantiate VCR.
vcr := govcr.NewVCR(
govcr.WithCassette(exampleCassetteName2),
govcr.NewCassetteMaker(exampleCassetteName2),
govcr.WithClient(app.httpClient),
)

Expand Down
Loading

0 comments on commit 1b88cd3

Please sign in to comment.