on main help sign in
fix(test): improve go-vcr request matching logic

Normalize line endings to avoid issues on Windows.

commit4926d2c481857eee651d223be66e950266b9881b authorAndrey Nering <andreynering@users.noreply.github.com> dateFri 24 Oct 2025, 17:53 (1 year ago) parent0ca5de0
1 file changed+15−3
internal/agent/recorder_test.go+15−3
internal/agent/recorder_test.go+15−3
@@ -58,14 +58,16 @@ func customMatcher(t *testing.T) recorder.MatcherFunc {
5858 // Some providers can sometimes generate JSON requests with keys in
5959 // a different order, which means a direct string comparison will fail.
6060 // Falling back to deserializing the content if we don't have a match.
61− if string(reqBody) == i.Body { // hot path
61+ requestContent := normalizeLineEndings(reqBody)
62+ cassetteContent := normalizeLineEndings(i.Body)
63+ if requestContent == cassetteContent {
6264 return true
6365 }
6466 var content1, content2 any
65− if err := json.Unmarshal(reqBody, &content1); err != nil {
67+ if err := json.Unmarshal([]byte(requestContent), &content1); err != nil {
6668 return false
6769 }
68− if err := json.Unmarshal([]byte(i.Body), &content2); err != nil {
70+ if err := json.Unmarshal([]byte(cassetteContent), &content2); err != nil {
6971 return false
7072 }
7173 return reflect.DeepEqual(content1, content2)
@@ -102,3 +104,13 @@ func hookRemoveHeaders(i *cassette.Interaction) error {
102104 }
103105 return nil
104106}
107+
108+// normalizeLineEndings does not only replace `\r\n` into `\n`,
109+// but also replaces `\\r\\n` into `\\n`. That's because we want the content
110+// inside JSON string to be replaces as well.
111+func normalizeLineEndings[T string | []byte](s T) string {
112+ str := string(s)
113+ str = strings.ReplaceAll(str, "\r\n", "\n")
114+ str = strings.ReplaceAll(str, `\r\n`, `\n`)
115+ return str
116+}