diff --git a/README.md b/README.md index 2073620..f2aa4a0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/middleware_test.go b/examples/middleware_test.go index 8a1c15d..f3f85d8 100644 --- a/examples/middleware_test.go +++ b/examples/middleware_test.go @@ -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) { @@ -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) diff --git a/pkg/recorder/middleware.go b/pkg/recorder/middleware.go index 34cc18a..f5ed5c3 100644 --- a/pkg/recorder/middleware.go +++ b/pkg/recorder/middleware.go @@ -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) diff --git a/pkg/recorder/recorder.go b/pkg/recorder/recorder.go index 1a614c0..ec5ff68 100644 --- a/pkg/recorder/recorder.go +++ b/pkg/recorder/recorder.go @@ -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 {