Skip to content

Commit 31c706f

Browse files
committed
Add IsRecording() and IsNewCassette() methods
1 parent 4015d92 commit 31c706f

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

recorder/recorder.go

+30
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,33 @@ func (rec *Recorder) GetDefaultClient() *http.Client {
478478

479479
return client
480480
}
481+
482+
// IsNewCassette returns true, if the recorder was started with a
483+
// new/empty cassette. Returns false, if it was started using an
484+
// existing cassette, which was loaded.
485+
func (rec *Recorder) IsNewCassette() bool {
486+
return rec.cassette.IsNew
487+
}
488+
489+
// IsRecording returns true, if the recorder is recording
490+
// interactions, returns false otherwise. Note, that in some modes
491+
// (e.g. ModeReplayWithNewEpisodes and ModeRecordOnce) the recorder
492+
// might be recording new interactions. For example in ModeRecordOnce,
493+
// we are replaying interactions only if there was an existing
494+
// cassette, and we are recording it, if the cassette is a new one.
495+
// ModeReplayWithNewEpisodes would replay interactions, if they are
496+
// present in the cassette, but will also record new ones, if they are
497+
// not part of the cassette already. In these cases the recorder is
498+
// considered to be recording for these modes.
499+
func (rec *Recorder) IsRecording() bool {
500+
switch {
501+
case rec.options.Mode == ModeRecordOnly || rec.options.Mode == ModeReplayWithNewEpisodes:
502+
return true
503+
case rec.options.Mode == ModeReplayOnly || rec.options.Mode == ModePassthrough:
504+
return false
505+
case rec.options.Mode == ModeRecordOnce && rec.IsNewCassette():
506+
return true
507+
default:
508+
return false
509+
}
510+
}

recorder/recorder_test.go

+58-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ func TestRecordOnlyMode(t *testing.T) {
164164
t.Fatal("recorder is not in the correct mode")
165165
}
166166

167+
if rec.IsRecording() != true {
168+
t.Fatal("recorder is not recording")
169+
}
170+
171+
if !rec.IsNewCassette() {
172+
t.Fatal("recorder is not using a new cassette")
173+
}
174+
167175
// Run tests
168176
ctx := context.Background()
169177
client := rec.GetDefaultClient()
@@ -254,6 +262,10 @@ func TestReplayWithContextTimeout(t *testing.T) {
254262
t.Fatal("recorder is not in the correct mode")
255263
}
256264

265+
if rec.IsRecording() != true {
266+
t.Fatal("recorder is not recording")
267+
}
268+
257269
tests := []testCase{
258270
{
259271
method: http.MethodGet,
@@ -294,6 +306,11 @@ func TestReplayWithContextTimeout(t *testing.T) {
294306
t.Fatal("recorder is not in the correct mode")
295307
}
296308

309+
// This time the recording should only be replaying
310+
if rec.IsRecording() != false {
311+
t.Fatal("recorder should not be recording")
312+
}
313+
297314
defer rec.Stop()
298315
client = rec.GetDefaultClient()
299316

@@ -340,6 +357,10 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) {
340357
t.Fatal(err)
341358
}
342359

360+
if rec.IsRecording() != true {
361+
t.Fatal("recorder is not recording")
362+
}
363+
343364
if rec.Mode() != recorder.ModeRecordOnce {
344365
t.Fatal("recorder is not in the correct mode")
345366
}
@@ -367,6 +388,10 @@ func TestRecordOnceWithMissingEpisodes(t *testing.T) {
367388
t.Fatal("recorder is not in the correct mode")
368389
}
369390

391+
if rec.IsRecording() != false {
392+
t.Fatal("recorder should not be recording")
393+
}
394+
370395
newTests := []testCase{
371396
{
372397
method: http.MethodHead,
@@ -436,6 +461,10 @@ func TestReplayWithNewEpisodes(t *testing.T) {
436461
t.Fatal(err)
437462
}
438463

464+
if rec.IsRecording() != true {
465+
t.Fatal("recorder is not recording")
466+
}
467+
439468
if rec.Mode() != recorder.ModeReplayWithNewEpisodes {
440469
t.Fatal("recorder is not in the correct mode")
441470
}
@@ -522,6 +551,7 @@ func TestPassthroughMode(t *testing.T) {
522551

523552
server := newEchoHttpServer()
524553
serverUrl := server.URL
554+
defer server.Close()
525555

526556
cassPath, err := newCassettePath("test_passthrough_mode")
527557
if err != nil {
@@ -536,12 +566,15 @@ func TestPassthroughMode(t *testing.T) {
536566
if err != nil {
537567
t.Fatal(err)
538568
}
539-
defer server.Close()
540569

541570
if m := rec.Mode(); m != recorder.ModePassthrough {
542571
t.Fatal("recorder is not in the correct mode")
543572
}
544573

574+
if rec.IsRecording() != false {
575+
t.Fatal("recorder should not be recording")
576+
}
577+
545578
// Run tests
546579
ctx := context.Background()
547580
client := rec.GetDefaultClient()
@@ -602,6 +635,10 @@ func TestPassthroughHandler(t *testing.T) {
602635
t.Fatal("recorder is not in the correct mode")
603636
}
604637

638+
if rec.IsRecording() != true {
639+
t.Fatal("recorder is not recording")
640+
}
641+
605642
// Add a passthrough handler which does not record any
606643
// requests with a specific body.
607644
rec.AddPassthrough(func(r *http.Request) bool {
@@ -687,6 +724,10 @@ func TestFilter(t *testing.T) {
687724
t.Fatal("recorder is not in the correct mode")
688725
}
689726

727+
if rec.IsRecording() != true {
728+
t.Fatal("recorder is not recording")
729+
}
730+
690731
// Add a filter which replaces each request body in the stored
691732
// cassette:
692733
dummyBody := "[REDACTED]"
@@ -759,6 +800,10 @@ func TestPreSaveFilter(t *testing.T) {
759800
t.Fatal("recorder is not in the correct mode")
760801
}
761802

803+
if rec.IsRecording() != true {
804+
t.Fatal("recorder is not recording")
805+
}
806+
762807
// Add a save filter which replaces each request body in the stored cassette
763808
dummyBody := "[REDACTED]"
764809
rec.AddPreSaveFilter(func(i *cassette.Interaction) error {
@@ -824,6 +869,10 @@ func TestReplayableInteractions(t *testing.T) {
824869
t.Fatal("recorder is not in the correct mode")
825870
}
826871

872+
if rec.IsRecording() != true {
873+
t.Fatal("recorder is not recording")
874+
}
875+
827876
// Configure replayable interactions
828877
rec.SetReplayableInteractions(true)
829878

@@ -889,6 +938,10 @@ func TestWithCustomMatcher(t *testing.T) {
889938
t.Fatal("recorder is not in the correct mode")
890939
}
891940

941+
if rec.IsRecording() != true {
942+
t.Fatal("recorder is not recording")
943+
}
944+
892945
// Run tests first in RecordOnce mode, so we capture the
893946
// interactions
894947
ctx := context.Background()
@@ -922,6 +975,10 @@ func TestWithCustomMatcher(t *testing.T) {
922975
t.Fatal("recorder is not in the correct mode")
923976
}
924977

978+
if rec.IsRecording() != false {
979+
t.Fatal("recorder should not be recording")
980+
}
981+
925982
// Set replayable interactions to true, so that we can match
926983
// against the already recorded interactions.
927984
rec.SetReplayableInteractions(true)

0 commit comments

Comments
 (0)