Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsRecording() and IsNewCassette() methods #76

Merged
merged 1 commit into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions recorder/recorder.go
Expand Up @@ -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
}
}
59 changes: 58 additions & 1 deletion recorder/recorder_test.go
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down