Skip to content

Commit

Permalink
recorder: specifying a cassette name is required
Browse files Browse the repository at this point in the history
Also remove the `WithCassette` option.
  • Loading branch information
dnaeon committed Aug 21, 2024
1 parent 35520f7 commit 1ae1a38
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 62 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ import (
)

func TestHelloWorld(t *testing.T) {
opts := []recorder.Option{
// Store the recorded interactions in this cassette
recorder.WithCassette("fixtures/hello-world"),
}

// Create our recorder
r, err := recorder.New(opts...)
r, err := recorder.New("fixtures/hello-world")
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 1 addition & 4 deletions examples/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ import (

func TestHTTPS(t *testing.T) {
// Start our recorder
opts := []recorder.Option{
recorder.WithCassette("fixtures/iana-reserved-domains"),
}
r, err := recorder.New(opts...)
r, err := recorder.New("fixtures/iana-reserved-domains")
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 1 addition & 4 deletions examples/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ type payload struct {
}

func TestJSON(t *testing.T) {
opts := []recorder.Option{
recorder.WithCassette("fixtures/json-content-type"),
}
r, err := recorder.New(opts...)
r, err := recorder.New("fixtures/json-content-type")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestMiddleware(t *testing.T) {
// since you want to be able to record real application behavior
t.Run("RecordRealInteractionsWithMiddleware", func(t *testing.T) {
rec, err := recorder.New(
recorder.WithCassette(cassetteName),
cassetteName,
recorder.WithMode(recorder.ModeRecordOnly),
// Use a BeforeSaveHook to remove host, remote_addr, and duration
// since they change whenever the test runs
Expand Down
5 changes: 1 addition & 4 deletions examples/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ import (

func TestSimple(t *testing.T) {
// Start our recorder
opts := []recorder.Option{
recorder.WithCassette("fixtures/golang-org"),
}
r, err := recorder.New(opts...)
r, err := recorder.New("fixtures/golang-org")
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,6 @@ type Recorder struct {
// Option is a function which configures the [Recorder].
type Option func(r *Recorder)

// WithCassette is an [Option], which configures the [Recorder] to use the given
// cassette name.
func WithCassette(name string) Option {
opt := func(r *Recorder) {
r.cassetteName = name
}

return opt
}

// WithMode is an [Option], which configures the [Recorder] to run in the
// specified mode.
func WithMode(mode Mode) Option {
Expand Down Expand Up @@ -311,8 +301,9 @@ func WithReplayableInteractions(val bool) Option {
}

// New creates a new [Recorder] and configures it using the provided options.
func New(opts ...Option) (*Recorder, error) {
func New(cassetteName string, opts ...Option) (*Recorder, error) {
r := &Recorder{
cassetteName: cassetteName,
mode: ModeRecordOnce,
realTransport: http.DefaultTransport,
passthroughs: make([]PassthroughFunc, 0),
Expand Down
52 changes: 20 additions & 32 deletions pkg/recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestRecordOnceMode(t *testing.T) {
}

// Create recorder
rec, err := recorder.New(recorder.WithCassette(cassPath))
rec, err := recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestRecordOnceMode(t *testing.T) {
}

// Re-run without the actual server
rec, err = recorder.New(recorder.WithCassette(cassPath))
rec, err = recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -239,10 +239,9 @@ func TestRecordOnceMode(t *testing.T) {

func TestReplayOnlyModeFailsWithMissingCassette(t *testing.T) {
opts := []recorder.Option{
recorder.WithCassette("missing_cassette_file"),
recorder.WithMode(recorder.ModeReplayOnly),
}
_, err := recorder.New(opts...)
_, err := recorder.New("missing_cassette_file", opts...)
if !errors.Is(err, cassette.ErrCassetteNotFound) {
t.Fatalf("expected cassette.ErrCassetteNotFound, got %v", err)
}
Expand All @@ -257,7 +256,7 @@ func TestReplayWithContextTimeout(t *testing.T) {
server := newEchoHttpServer()
serverUrl := server.URL

rec, err := recorder.New(recorder.WithCassette(cassPath))
rec, err := recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -301,7 +300,7 @@ func TestReplayWithContextTimeout(t *testing.T) {
server.Close()
rec.Stop()

rec, err = recorder.New(recorder.WithCassette(cassPath))
rec, err = recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -356,7 +355,7 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) {
}

// Create recorder
rec, err := recorder.New(recorder.WithCassette(cassPath))
rec, err := recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -382,7 +381,7 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) {
server.Close()
rec.Stop()

rec, err = recorder.New(recorder.WithCassette(cassPath))
rec, err = recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -457,10 +456,9 @@ func TestReplayWithNewEpisodes(t *testing.T) {

// Create recorder
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithMode(recorder.ModeReplayWithNewEpisodes),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -484,7 +482,7 @@ func TestReplayWithNewEpisodes(t *testing.T) {

// Re-run again with new HTTP interactions
rec.Stop()
rec, err = recorder.New(opts...)
rec, err = recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -562,10 +560,9 @@ func TestPassthroughMode(t *testing.T) {
}

opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithMode(recorder.ModePassthrough),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -630,7 +627,6 @@ func TestPassthroughHandler(t *testing.T) {

// Create recorder
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithPassthrough(func(r *http.Request) bool {
// Passthrough requests with POST method and "passthrough request" body
if r.Body == nil {
Expand All @@ -645,7 +641,7 @@ func TestPassthroughHandler(t *testing.T) {
return r.Method == http.MethodPost && b.String() == "passthrough request"
}),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -729,11 +725,10 @@ func TestAfterCaptureHook(t *testing.T) {
}

opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithHook(redactHook, recorder.AfterCaptureHook),
}

rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -808,10 +803,9 @@ func TestBeforeSaveHook(t *testing.T) {
return nil
}
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithHook(redactHook, recorder.BeforeSaveHook),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -884,7 +878,7 @@ func TestBeforeResponseReplayHook(t *testing.T) {
}

// Create recorder
rec, err := recorder.New(recorder.WithCassette(cassPath))
rec, err := recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -922,10 +916,9 @@ func TestBeforeResponseReplayHook(t *testing.T) {
}

opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithHook(hook, recorder.BeforeResponseReplayHook),
}
rec, err = recorder.New(opts...)
rec, err = recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -995,12 +988,11 @@ func TestReplayableInteractions(t *testing.T) {
}

opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithReplayableInteractions(true),
recorder.WithHook(hook, recorder.OnRecorderStopHook),
}

rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1069,10 +1061,9 @@ func TestRecordOnlyMode(t *testing.T) {

// Create recorder
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithMode(recorder.ModeRecordOnly),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1171,11 +1162,10 @@ func TestBlockRealTransportUnsafeMethods(t *testing.T) {

// Create recorder
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithMode(recorder.ModeRecordOnly),
recorder.WithBlockUnsafeMethods(true),
}
rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1194,10 +1184,9 @@ func TestBlockRealTransportUnsafeMethods(t *testing.T) {
func TestInvalidRecorderMode(t *testing.T) {
// Create recorder
opts := []recorder.Option{
recorder.WithCassette("invalid_recorder_mode"),
recorder.WithMode(recorder.Mode(-42)),
}
_, err := recorder.New(opts...)
_, err := recorder.New("invalid_recorder_mode", opts...)
if err != recorder.ErrInvalidMode {
t.Fatal("expected recorder to fail with invalid mode")
}
Expand Down Expand Up @@ -1242,11 +1231,10 @@ func TestDiscardInteractionsOnSave(t *testing.T) {
return nil
}
opts := []recorder.Option{
recorder.WithCassette(cassPath),
recorder.WithHook(hook, recorder.AfterCaptureHook),
}

rec, err := recorder.New(opts...)
rec, err := recorder.New(cassPath, opts...)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 1ae1a38

Please sign in to comment.