@@ -10,6 +10,7 @@ import (
1010 "net/url"
1111 "os"
1212 "path/filepath"
13+ "reflect"
1314 "regexp"
1415 "strings"
1516 "testing"
@@ -74,8 +75,7 @@ func compareJSONFields(expected, actualI interface{}) bool {
7475
7576 return compareJSONFieldsStrings (expected .(string ), actual )
7677 default :
77- // Consider equality when not handled
78- return true
78+ return reflect .DeepEqual (expected , actualI )
7979 }
8080}
8181
@@ -116,39 +116,41 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
116116 if actualRequest .Body == nil || actualRequest .ContentLength == 0 {
117117 if cassetteRequest .Body == "" {
118118 return true // Body match if both are empty
119- } else if _ , isFile := actualRequest .Body .(* os.File ); isFile {
119+ }
120+
121+ if _ , isFile := actualRequest .Body .(* os.File ); isFile {
120122 return true // Body match if request is sending a file, maybe do more check here
121123 }
122124
123125 return false
124126 }
125127
126- actualBody , err := actualRequest .GetBody ()
128+ actualBodyReader , err := actualRequest .GetBody ()
127129 if err != nil {
128130 panic (fmt .Errorf ("cassette body matcher: failed to copy actualRequest body: %w" , err )) // lintignore: R009
129131 }
130132
131- actualRawBody , err := io .ReadAll (actualBody )
133+ actualBody , err := io .ReadAll (actualBodyReader )
132134 if err != nil {
133135 panic (fmt .Errorf ("cassette body matcher: failed to read actualRequest body: %w" , err )) // lintignore: R009
134136 }
135137
136138 // Try to match raw bodies if they are not JSON (ex: cloud-init config)
137- if string (actualRawBody ) == cassetteRequest .Body {
139+ if string (actualBody ) == cassetteRequest .Body {
138140 return true
139141 }
140142
141143 actualJSON := make (map [string ]interface {})
142144 cassetteJSON := make (map [string ]interface {})
143145
144- err = xml .Unmarshal (actualRawBody , new (interface {}))
146+ // match if content is xml
147+ err = xml .Unmarshal (actualBody , new (interface {}))
145148 if err == nil {
146- // match if content is xml
147149 return true
148150 }
149151
150- if ! json .Valid (actualRawBody ) {
151- values , err := url .ParseQuery (string (actualRawBody ))
152+ if ! json .Valid (actualBody ) {
153+ values , err := url .ParseQuery (string (actualBody ))
152154 if err != nil {
153155 panic (fmt .Errorf ("cassette body matcher: failed to parse body as url values: %w" , err )) // lintignore: R009
154156 }
@@ -162,9 +164,9 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
162164 return compareFormBodies (values , cassetteRequest .Form )
163165 }
164166
165- err = json .Unmarshal (actualRawBody , & actualJSON )
167+ err = json .Unmarshal (actualBody , & actualJSON )
166168 if err != nil {
167- panic (fmt .Errorf ("cassette body matcher: failed to parse json body: %w" , err )) // lintignore: R009
169+ panic (fmt .Errorf ("cassette body matcher: failed to parse request body as json : %w" , err )) // lintignore: R009
168170 }
169171
170172 err = json .Unmarshal ([]byte (cassetteRequest .Body ), & cassetteJSON )
@@ -182,9 +184,9 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
182184 return compareJSONBodies (cassetteJSON , actualJSON )
183185}
184186
185- // cassetteMatcher is a custom matcher that check equivalence of a played request against a recorded one
187+ // CassetteMatcher is a custom matcher that check equivalence of a played request against a recorded one
186188// It compares method, path and query but will remove unwanted values from query
187- func cassetteMatcher (actual * http.Request , expected cassette.Request ) bool {
189+ func CassetteMatcher (actual * http.Request , expected cassette.Request ) bool {
188190 expectedURL , _ := url .Parse (expected .URL )
189191 actualURL := actual .URL
190192 actualURLValues := actualURL .Query ()
@@ -297,7 +299,7 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
297299 }(r )
298300
299301 // Add custom matcher for requests and cassettes
300- r .SetMatcher (cassetteMatcher )
302+ r .SetMatcher (CassetteMatcher )
301303
302304 // Add a filter which removes Authorization headers from all requests:
303305 r .AddHook (func (i * cassette.Interaction ) error {
0 commit comments