Skip to content

Commit

Permalink
Integrated trackRecordingMutators
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Jul 26, 2019
1 parent 2fd95ea commit 35dcba5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
11 changes: 4 additions & 7 deletions cassette/cassette.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,13 @@ func transformInterfacesInJSON(jsonString []byte) []byte {
return []byte(regex.ReplaceAllString(string(jsonString), `$1"$2",`))
}

// RecordNewTrackToCassette saves a new track using the specified details to a cassette.
func RecordNewTrackToCassette(cassette *Cassette, req *Request, resp *Response, httpErr error) error {
// create track
t := NewTrack(req, resp, httpErr)

// AddTrackToCassette saves a new track using the specified details to a cassette.
func AddTrackToCassette(cassette *Cassette, aTrack *Track) error {
// mark track as replayed since it's coming from a live Request!
t.Replayed(true)
aTrack.Replayed(true)

// add track to cassette
cassette.AddTrack(t)
cassette.AddTrack(aTrack)

// save cassette
return cassette.save()
Expand Down
2 changes: 1 addition & 1 deletion controlpanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (controlPanel *ControlPanel) LoadCassette(cassetteName string) error {

// LoadCassette into the VCR.
func (controlPanel *ControlPanel) AddMutators(trackMutators ...TrackMutator) {
controlPanel.vcrTransport().addMutators(trackMutators...)
controlPanel.vcrTransport().AddMutators(trackMutators...)
}

// Player returns the http.Client that contains the VCR.
Expand Down
10 changes: 8 additions & 2 deletions govcr_wb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ func TestRoundTrip_SavesMutatedCassetteTracks(t *testing.T) {
}
require.EqualValues(t, expectedStats, actualStats)

// TODO now need to verify what is on the cassette has been mutated
err = vcr.LoadCassette(cassetteName)
assert.NoError(t, err)

t.Fatal("implement me - SHOULD TEST TRACKREPLAYMUTATOR")
for trackNum, aTrack := range vcr.vcrTransport().cassette.Tracks {
require.EqualValues(t, "GET has been mutated", aTrack.Request.Method, "track #%d", trackNum)
require.EqualValues(t, "200 OK has been mutated", aTrack.Response.Status, "track #%d", trackNum)
require.EqualValues(t, "ErrType was mutated", aTrack.ErrType, "track #%d", trackNum)
require.EqualValues(t, "ErrMsg was mutated", aTrack.ErrMsg, "track #%d", trackNum)
}
}

func makeHTTPCalls_WithSuccess(testServerURL string, vcr *ControlPanel, t *testing.T) stats.Stats {
Expand Down
4 changes: 4 additions & 0 deletions pcb.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (pcbr *pcb) mutateTrack(t *cassette.Track) {
pcbr.trackRecordingMutators.Mutate(t)
}

func (pcbr *pcb) AddMutators(mutators ...TrackMutator) {
pcbr.trackRecordingMutators = pcbr.trackRecordingMutators.Add(mutators...)
}

// RequestMatcher is an interface that exposes the method to perform request comparison.
// request comparison involves the HTTP request and the track request recorded on cassette.
type RequestMatcher interface {
Expand Down
2 changes: 1 addition & 1 deletion vcrsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func WithCassette(cassetteName string) Setting {
// a cassette to load.
func WithTrackRecordingMutators(trackRecordingMutators ...TrackMutator) Setting {
return func(vcrConfig *VCRSettings) {
vcrConfig.trackRecordingMutators.Add(trackRecordingMutators...)
vcrConfig.trackRecordingMutators = vcrConfig.trackRecordingMutators.Add(trackRecordingMutators...)
}
}

Expand Down
8 changes: 4 additions & 4 deletions vcrtransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (t *vcrTransport) RoundTrip(httpRequest *http.Request) (*http.Response, err

newTrack := cassette.NewTrack(request, response, reqErr)
t.pcb.mutateTrack(newTrack)
if err := cassette.RecordNewTrackToCassette(t.cassette, request, response, reqErr); err != nil {
log.Printf("RoundTrip failed to RecordNewTrackToCassette: %v\n", err)
if err := cassette.AddTrackToCassette(t.cassette, newTrack); err != nil {
log.Printf("RoundTrip failed to AddTrackToCassette: %v\n", err)
}

return httpResponse, reqErr
Expand Down Expand Up @@ -68,8 +68,8 @@ func (t *vcrTransport) ejectCassette() {
t.cassette = nil
}

func (t *vcrTransport) addMutators(mutators ...TrackMutator) {
t.pcb.trackRecordingMutators.Add(mutators...)
func (t *vcrTransport) AddMutators(mutators ...TrackMutator) {
t.pcb.AddMutators(mutators...)
}

func (t *vcrTransport) stats() *stats.Stats {
Expand Down

0 comments on commit 35dcba5

Please sign in to comment.