Skip to content

Commit

Permalink
Merge pull request #49 from Flamefork/publish-mode
Browse files Browse the repository at this point in the history
Add Mode() to recorder
  • Loading branch information
dnaeon authored Mar 2, 2020
2 parents 9384691 + 8bee2bf commit 5ccc9c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,8 @@ func (r *Recorder) AddFilter(filter cassette.Filter) {
r.cassette.Filters = append(r.cassette.Filters, filter)
}
}

// Mode returns recorder state
func (r *Recorder) Mode() Mode {
return r.mode
}
24 changes: 18 additions & 6 deletions recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ func httpTests(runID string) []recordTest {
func TestRecord(t *testing.T) {
runID, cassPath, tests := setupTests(t, "record_test")

serverURL := httpRecorderTest(t, runID, tests, cassPath, recorder.ModeRecording)
r, serverURL := httpRecorderTest(t, runID, tests, cassPath, recorder.ModeRecording)

c, err := cassette.Load(cassPath)
if err != nil {
t.Fatal(err)
}

if m := r.Mode(); m != recorder.ModeRecording {
t.Fatalf("Expected recording mode, got %v", m)
}

for i, test := range tests {
body := c.Interactions[i].Request.Body
if body != test.body {
Expand All @@ -96,12 +100,16 @@ func TestRecord(t *testing.T) {
}

// Re-run without the actual server
r, err := recorder.New(cassPath)
r, err = recorder.New(cassPath)
if err != nil {
t.Fatal(err)
}
defer r.Stop()

if m := r.Mode(); m != recorder.ModeReplaying {
t.Fatalf("Expected replaying mode, got %v", m)
}

// Use a custom matcher that includes matching on request body
r.SetMatcher(func(r *http.Request, i cassette.Request) bool {
var b bytes.Buffer
Expand All @@ -122,7 +130,7 @@ func TestRecord(t *testing.T) {
func TestModeContextTimeout(t *testing.T) {
// Record initial requests
runID, cassPath, tests := setupTests(t, "record_playback_timeout")
serverURL := httpRecorderTest(t, runID, tests, cassPath, recorder.ModeReplaying)
_, serverURL := httpRecorderTest(t, runID, tests, cassPath, recorder.ModeReplaying)

// Re-run without the actual server
r, err := recorder.New(cassPath)
Expand Down Expand Up @@ -174,7 +182,11 @@ func TestModePlaybackMissing(t *testing.T) {
func TestModeDisabled(t *testing.T) {
runID, cassPath, tests := setupTests(t, "record_disabled_test")

httpRecorderTest(t, runID, tests, cassPath, recorder.ModeDisabled)
r, _ := httpRecorderTest(t, runID, tests, cassPath, recorder.ModeDisabled)

if m := r.Mode(); m != recorder.ModeDisabled {
t.Fatalf("Expected disabled mode, got %v", m)
}

_, err := cassette.Load(cassPath)
// Expect the file to not exist if record is disabled
Expand Down Expand Up @@ -243,7 +255,7 @@ func httpRecorderTestSetup(t *testing.T, runID string, cassPath string, mode rec
return recorder, server
}

func httpRecorderTest(t *testing.T, runID string, tests []recordTest, cassPath string, mode recorder.Mode) string {
func httpRecorderTest(t *testing.T, runID string, tests []recordTest, cassPath string, mode recorder.Mode) (*recorder.Recorder, string) {
recorder, server := httpRecorderTestSetup(t, runID, cassPath, mode)
serverURL := server.URL

Expand All @@ -259,7 +271,7 @@ func httpRecorderTest(t *testing.T, runID string, tests []recordTest, cassPath s
recorder.Stop()
t.Log("recorder stopped")

return serverURL
return recorder, serverURL
}

func (test recordTest) perform(t *testing.T, url string, r *recorder.Recorder) {
Expand Down

0 comments on commit 5ccc9c8

Please sign in to comment.