diff --git a/examples/fixtures/middleware.yaml b/examples/fixtures/middleware.yaml index 1cc0e0a..29ef1ef 100644 --- a/examples/fixtures/middleware.yaml +++ b/examples/fixtures/middleware.yaml @@ -9,8 +9,8 @@ interactions: content_length: 0 transfer_encoding: [] trailer: {} - host: 127.0.0.1:60761 - remote_addr: 127.0.0.1:60762 + host: "" + remote_addr: "" request_uri: /request1 body: "" form: {} @@ -37,7 +37,7 @@ interactions: - VALUE status: 200 OK code: 200 - duration: 42ns + duration: 0s - id: 1 request: proto: HTTP/1.1 @@ -46,9 +46,9 @@ interactions: content_length: 0 transfer_encoding: [] trailer: {} - host: 127.0.0.1:60761 - remote_addr: 127.0.0.1:60763 - request_uri: /request2 + host: "" + remote_addr: "" + request_uri: /request2?query=example body: "" form: {} headers: @@ -56,7 +56,7 @@ interactions: - gzip User-Agent: - Go-http-client/1.1 - url: http://go-vcr/request2 + url: http://go-vcr/request2?query=example method: GET response: proto: HTTP/1.1 @@ -66,7 +66,9 @@ interactions: trailer: {} content_length: -1 uncompressed: false - body: OK + body: |- + query=example + OK headers: Content-Type: - text/plain; charset=utf-8 @@ -74,7 +76,7 @@ interactions: - VALUE status: 200 OK code: 200 - duration: 41ns + duration: 0s - id: 2 request: proto: HTTP/1.1 @@ -83,8 +85,8 @@ interactions: content_length: 9 transfer_encoding: [] trailer: {} - host: 127.0.0.1:60761 - remote_addr: 127.0.0.1:60764 + host: "" + remote_addr: "" request_uri: /postform body: key=value form: @@ -117,7 +119,7 @@ interactions: - VALUE status: 200 OK code: 200 - duration: 375ns + duration: 0s - id: 3 request: proto: HTTP/1.1 @@ -126,8 +128,8 @@ interactions: content_length: 15 transfer_encoding: [] trailer: {} - host: 127.0.0.1:60761 - remote_addr: 127.0.0.1:60765 + host: "" + remote_addr: "" request_uri: /postdata body: '{"key":"value"}' form: {} @@ -158,4 +160,4 @@ interactions: - VALUE status: 200 OK code: 200 - duration: 125ns + duration: 0s diff --git a/examples/middleware_test.go b/examples/middleware_test.go index 17a0ec5..8a1c15d 100644 --- a/examples/middleware_test.go +++ b/examples/middleware_test.go @@ -14,43 +14,31 @@ import ( func TestMiddleware(t *testing.T) { cassetteName := "fixtures/middleware" - createHandler := func(middleware func(http.Handler) http.Handler) http.Handler { - mux := http.NewServeMux() - - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("KEY", "VALUE") - - body, _ := io.ReadAll(r.Body) - if len(body) > 0 { - w.Write(body) - } else { - w.Write([]byte("OK")) - } - }) - - if middleware != nil { - handler = middleware(handler).ServeHTTP - } - - mux.Handle("/", handler) - return mux - } // 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) { - recorder, err := recorder.NewWithOptions(&recorder.Options{ - CassetteName: cassetteName, - Mode: recorder.ModeRecordOnly, - BlockRealTransportUnsafeMethods: false, + rec, err := recorder.NewWithOptions(&recorder.Options{ + CassetteName: cassetteName, + Mode: recorder.ModeRecordOnly, + SkipRequestLatency: true, }) 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(recorder.Middleware) - defer recorder.Stop() + handler := createHandler(rec.Middleware) + defer rec.Stop() server := httptest.NewServer(handler) defer server.Close() @@ -60,7 +48,7 @@ func TestMiddleware(t *testing.T) { t.Errorf("error making request: %v", err) } - _, err = http.Get(server.URL + "/request2") + _, err = http.Get(server.URL + "/request2?query=example") if err != nil { t.Errorf("error making request: %v", err) } @@ -80,3 +68,31 @@ func TestMiddleware(t *testing.T) { cassette.TestServerReplay(t, cassetteName, createHandler(nil)) }) } + +// createHandler will return an HTTP handler with optional middleware. It will respond to +// simple requests for testing +func createHandler(middleware func(http.Handler) http.Handler) http.Handler { + mux := http.NewServeMux() + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("KEY", "VALUE") + + query := r.URL.Query().Encode() + if query != "" { + w.Write([]byte(query + "\n")) + } + + body, _ := io.ReadAll(r.Body) + if len(body) > 0 { + w.Write(body) + } else { + w.Write([]byte("OK")) + } + }) + + if middleware != nil { + handler = middleware(handler).ServeHTTP + } + + mux.Handle("/", handler) + return mux +}