From 178042cb97a77b93666d4f3cc417a72c95b96b68 Mon Sep 17 00:00:00 2001 From: jmdalmeida Date: Thu, 5 Nov 2020 18:13:06 +0000 Subject: [PATCH] feat: allow interactions to be replayed --- .gitignore | 2 ++ cassette/cassette.go | 4 +++- recorder/recorder.go | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5b15ac5..e61714e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ _testmain.go # Ignore test code coverage coverage.txt + +.idea/ diff --git a/cassette/cassette.go b/cassette/cassette.go index 5dbe109..e610dee 100644 --- a/cassette/cassette.go +++ b/cassette/cassette.go @@ -125,6 +125,8 @@ type Cassette struct { Mu sync.RWMutex `yaml:"mu,omitempty"` // Interactions between client and server Interactions []*Interaction `yaml:"interactions"` + // ReplayableInteractions defines whether to allow interactions to be replayed or not + ReplayableInteractions bool `yaml:"-"` // Matches actual request with interaction requests. Matcher Matcher `yaml:"-"` @@ -176,7 +178,7 @@ func (c *Cassette) GetInteraction(r *http.Request) (*Interaction, error) { c.Mu.Lock() defer c.Mu.Unlock() for _, i := range c.Interactions { - if !i.replayed && c.Matcher(r, i.Request) { + if (c.ReplayableInteractions || !i.replayed) && c.Matcher(r, i.Request) { i.replayed = true return i, nil } diff --git a/recorder/recorder.go b/recorder/recorder.go index 83109d3..b5d4628 100644 --- a/recorder/recorder.go +++ b/recorder/recorder.go @@ -277,6 +277,13 @@ func (r *Recorder) SetMatcher(matcher cassette.Matcher) { } } +// SetReplayableInteractions defines whether to allow interactions to be replayed or not. +func (r *Recorder) SetReplayableInteractions(replayable bool) { + if r.cassette != nil { + r.cassette.ReplayableInteractions = replayable + } +} + // AddPassthrough appends a hook to determine if a request should be ignored by the // recorder. func (r *Recorder) AddPassthrough(pass Passthrough) {