From 31c706faa178d034859c1e51061f14a7d0c9e68e Mon Sep 17 00:00:00 2001 From: Marin Atanasov Nikolov Date: Fri, 19 Aug 2022 19:36:42 +0300 Subject: [PATCH] Add IsRecording() and IsNewCassette() methods --- recorder/recorder.go | 30 ++++++++++++++++++++ recorder/recorder_test.go | 59 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/recorder/recorder.go b/recorder/recorder.go index 10e2f10..ab228e6 100644 --- a/recorder/recorder.go +++ b/recorder/recorder.go @@ -478,3 +478,33 @@ func (rec *Recorder) GetDefaultClient() *http.Client { return client } + +// IsNewCassette returns true, if the recorder was started with a +// new/empty cassette. Returns false, if it was started using an +// existing cassette, which was loaded. +func (rec *Recorder) IsNewCassette() bool { + return rec.cassette.IsNew +} + +// IsRecording returns true, if the recorder is recording +// interactions, returns false otherwise. Note, that in some modes +// (e.g. ModeReplayWithNewEpisodes and ModeRecordOnce) the recorder +// might be recording new interactions. For example in ModeRecordOnce, +// we are replaying interactions only if there was an existing +// cassette, and we are recording it, if the cassette is a new one. +// ModeReplayWithNewEpisodes would replay interactions, if they are +// present in the cassette, but will also record new ones, if they are +// not part of the cassette already. In these cases the recorder is +// considered to be recording for these modes. +func (rec *Recorder) IsRecording() bool { + switch { + case rec.options.Mode == ModeRecordOnly || rec.options.Mode == ModeReplayWithNewEpisodes: + return true + case rec.options.Mode == ModeReplayOnly || rec.options.Mode == ModePassthrough: + return false + case rec.options.Mode == ModeRecordOnce && rec.IsNewCassette(): + return true + default: + return false + } +} diff --git a/recorder/recorder_test.go b/recorder/recorder_test.go index 900d1ff..d323b60 100644 --- a/recorder/recorder_test.go +++ b/recorder/recorder_test.go @@ -164,6 +164,14 @@ func TestRecordOnlyMode(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + + if !rec.IsNewCassette() { + t.Fatal("recorder is not using a new cassette") + } + // Run tests ctx := context.Background() client := rec.GetDefaultClient() @@ -254,6 +262,10 @@ func TestReplayWithContextTimeout(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + tests := []testCase{ { method: http.MethodGet, @@ -294,6 +306,11 @@ func TestReplayWithContextTimeout(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + // This time the recording should only be replaying + if rec.IsRecording() != false { + t.Fatal("recorder should not be recording") + } + defer rec.Stop() client = rec.GetDefaultClient() @@ -340,6 +357,10 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) { t.Fatal(err) } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + if rec.Mode() != recorder.ModeRecordOnce { t.Fatal("recorder is not in the correct mode") } @@ -367,6 +388,10 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != false { + t.Fatal("recorder should not be recording") + } + newTests := []testCase{ { method: http.MethodHead, @@ -436,6 +461,10 @@ func TestReplayWithNewEpisodes(t *testing.T) { t.Fatal(err) } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + if rec.Mode() != recorder.ModeReplayWithNewEpisodes { t.Fatal("recorder is not in the correct mode") } @@ -522,6 +551,7 @@ func TestPassthroughMode(t *testing.T) { server := newEchoHttpServer() serverUrl := server.URL + defer server.Close() cassPath, err := newCassettePath("test_passthrough_mode") if err != nil { @@ -536,12 +566,15 @@ func TestPassthroughMode(t *testing.T) { if err != nil { t.Fatal(err) } - defer server.Close() if m := rec.Mode(); m != recorder.ModePassthrough { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != false { + t.Fatal("recorder should not be recording") + } + // Run tests ctx := context.Background() client := rec.GetDefaultClient() @@ -602,6 +635,10 @@ func TestPassthroughHandler(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + // Add a passthrough handler which does not record any // requests with a specific body. rec.AddPassthrough(func(r *http.Request) bool { @@ -687,6 +724,10 @@ func TestFilter(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + // Add a filter which replaces each request body in the stored // cassette: dummyBody := "[REDACTED]" @@ -759,6 +800,10 @@ func TestPreSaveFilter(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + // Add a save filter which replaces each request body in the stored cassette dummyBody := "[REDACTED]" rec.AddPreSaveFilter(func(i *cassette.Interaction) error { @@ -824,6 +869,10 @@ func TestReplayableInteractions(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + // Configure replayable interactions rec.SetReplayableInteractions(true) @@ -889,6 +938,10 @@ func TestWithCustomMatcher(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != true { + t.Fatal("recorder is not recording") + } + // Run tests first in RecordOnce mode, so we capture the // interactions ctx := context.Background() @@ -922,6 +975,10 @@ func TestWithCustomMatcher(t *testing.T) { t.Fatal("recorder is not in the correct mode") } + if rec.IsRecording() != false { + t.Fatal("recorder should not be recording") + } + // Set replayable interactions to true, so that we can match // against the already recorded interactions. rec.SetReplayableInteractions(true)