diff --git a/config/tests/samples/create/harness.go b/config/tests/samples/create/harness.go index 109e968ca8..c74752efc6 100644 --- a/config/tests/samples/create/harness.go +++ b/config/tests/samples/create/harness.go @@ -322,7 +322,7 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness { testName := strings.ReplaceAll(t.Name(), "/", "_") opts := &recorder.Options{ CassetteName: filepath.Join(dir, testName), - Mode: recorder.ModeReplayOnly, + Mode: recorder.ModeRecordOnly, RealTransport: ret.Transport, } r, err := recorder.NewWithOptions(opts) diff --git a/tests/e2e/unified_test.go b/tests/e2e/unified_test.go index 2ecdc9e3d7..f5e96f6813 100644 --- a/tests/e2e/unified_test.go +++ b/tests/e2e/unified_test.go @@ -15,11 +15,14 @@ package e2e import ( + "bytes" "context" "encoding/json" "fmt" "gopkg.in/dnaeon/go-vcr.v3/cassette" "gopkg.in/dnaeon/go-vcr.v3/recorder" + "io" + "io/ioutil" "k8s.io/klog/v2" "net/http" "os" @@ -196,27 +199,40 @@ func TestAllInSeries(t *testing.T) { } h.VCRRecorder.AddHook(hook, recorder.BeforeSaveHook) - //errorInteractionMismatch := func(request cassette.Request, field string, expected string, got string) { - // t.Errorf( - // "[VCR] Error with interaction: %s %s. Field %s does not match: expected: %s, got: %s.", - // request.Method, - // request.URL, - // field, - // expected, - // got, - // ) - //} h.VCRRecorder.SetMatcher(func(r *http.Request, i cassette.Request) bool { + // We applied BeforeSaveHook, need to modify the incoming request, + // so that incoming request matches the saved request. modifiedURL := replaceFunc(r.URL.String()) - - if r.Method != i.Method { - klog.Fatalf("[VCR] Fail") + // Request saved in cassette + expected := []string{i.Method, i.URL} + // Incoming request + got := []string{r.Method, modifiedURL} + + if r.Method != i.Method || modifiedURL != i.URL { + klog.Fatalf("[VCR] Request does not match: expected: %s, got: %s.", + strings.Join(expected, " "), + strings.Join(got, " ")) return false } - if modifiedURL != i.URL { - klog.Fatalf("[VCR] Fail") - return false + // If request body exists, check body as well + if r.Body != nil && r.Body != http.NoBody { + var reqBody []byte + var err error + reqBody, err = io.ReadAll(r.Body) + if err != nil { + t.Fatal("[VCR] Failed to read request body") + } + r.Body.Close() + r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody)) + + modifiedBody := replaceFunc(string(reqBody)) + if modifiedBody != i.Body { + klog.Fatalf("[VCR] Request body does not match: expected: %s, got: %s.", + modifiedBody, + i.Body) + return false + } } return true })