Skip to content

Commit

Permalink
Rename HTTPMiddleware and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean authored and dnaeon committed Aug 19, 2024
1 parent ab3bb92 commit 3916d5f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ defer r.Stop() // Make sure recorder is stopped once done with it
## Server Side

VCR testing can also be used for creating server-side tests. Use the
`recorder.Middleware` with an HTTP handler in order to create fixtures from
`recorder.HTTPMiddleware` with an HTTP handler in order to create fixtures from
incoming requests and the handler's responses. Then, these requests can be
replayed and compared against the recorded responses to create a regression test.

Expand Down
32 changes: 15 additions & 17 deletions examples/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/url"
"testing"

"gopkg.in/dnaeon/go-vcr.v3/cassette"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -18,26 +18,24 @@ func TestMiddleware(t *testing.T) {
// In a real-world scenario, the recorder will run outside of unit tests
// since you want to be able to record real application behavior
t.Run("RecordRealInteractionsWithMiddleware", func(t *testing.T) {
rec, err := recorder.NewWithOptions(&recorder.Options{
CassetteName: cassetteName,
Mode: recorder.ModeRecordOnly,
SkipRequestLatency: true,
})
rec, err := recorder.New(
recorder.WithCassette(cassetteName),
recorder.WithMode(recorder.ModeRecordOnly),
// Use a BeforeSaveHook to remove host, remote_addr, and duration
// since they change whenever the test runs
recorder.WithHook(func(i *cassette.Interaction) error {
i.Request.Host = ""
i.Request.RemoteAddr = ""
i.Response.Duration = 0
return nil
}, recorder.BeforeSaveHook),
)
if err != nil {
t.Errorf("error creating recorder: %v", err)
}

// Use a BeforeSaveHook to remove host, remote_addr, and duration
// since they change whenever the test runs
rec.AddHook(func(i *cassette.Interaction) error {
i.Request.Host = ""
i.Request.RemoteAddr = ""
i.Response.Duration = 0
return nil
}, recorder.BeforeSaveHook)

// Create the server handler with recorder middleware
handler := createHandler(rec.Middleware)
handler := createHandler(rec.HTTPMiddleware)
defer rec.Stop()

server := httptest.NewServer(handler)
Expand Down
4 changes: 2 additions & 2 deletions pkg/recorder/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"net/http/httptest"
)

// Middleware intercepts and records all incoming requests and the server's response
func (rec *Recorder) Middleware(next http.Handler) http.Handler {
// HTTPMiddleware intercepts and records all incoming requests and the server's response
func (rec *Recorder) HTTPMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := newPassthrough(w)

Expand Down
2 changes: 1 addition & 1 deletion pkg/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (rec *Recorder) RoundTrip(req *http.Request) (*http.Response, error) {
return rec.executeAndRecord(req, nil)
}

// executeAndRecord is used internally by the Middleware to allow recording a response on the server side
// executeAndRecord is used internally by the HTTPMiddleware to allow recording a response on the server side
func (rec *Recorder) executeAndRecord(req *http.Request, serverResponse *http.Response) (*http.Response, error) {
// Passthrough mode, use real transport
if rec.mode == ModePassthrough {
Expand Down

0 comments on commit 3916d5f

Please sign in to comment.