@@ -10,6 +10,7 @@ import (
10
10
"net/url"
11
11
"os"
12
12
"path/filepath"
13
+ "reflect"
13
14
"regexp"
14
15
"strings"
15
16
"testing"
@@ -74,8 +75,7 @@ func compareJSONFields(expected, actualI interface{}) bool {
74
75
75
76
return compareJSONFieldsStrings (expected .(string ), actual )
76
77
default :
77
- // Consider equality when not handled
78
- return true
78
+ return reflect .DeepEqual (expected , actualI )
79
79
}
80
80
}
81
81
@@ -116,39 +116,41 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
116
116
if actualRequest .Body == nil || actualRequest .ContentLength == 0 {
117
117
if cassetteRequest .Body == "" {
118
118
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 {
120
122
return true // Body match if request is sending a file, maybe do more check here
121
123
}
122
124
123
125
return false
124
126
}
125
127
126
- actualBody , err := actualRequest .GetBody ()
128
+ actualBodyReader , err := actualRequest .GetBody ()
127
129
if err != nil {
128
130
panic (fmt .Errorf ("cassette body matcher: failed to copy actualRequest body: %w" , err )) // lintignore: R009
129
131
}
130
132
131
- actualRawBody , err := io .ReadAll (actualBody )
133
+ actualBody , err := io .ReadAll (actualBodyReader )
132
134
if err != nil {
133
135
panic (fmt .Errorf ("cassette body matcher: failed to read actualRequest body: %w" , err )) // lintignore: R009
134
136
}
135
137
136
138
// 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 {
138
140
return true
139
141
}
140
142
141
143
actualJSON := make (map [string ]interface {})
142
144
cassetteJSON := make (map [string ]interface {})
143
145
144
- err = xml .Unmarshal (actualRawBody , new (interface {}))
146
+ // match if content is xml
147
+ err = xml .Unmarshal (actualBody , new (interface {}))
145
148
if err == nil {
146
- // match if content is xml
147
149
return true
148
150
}
149
151
150
- if ! json .Valid (actualRawBody ) {
151
- values , err := url .ParseQuery (string (actualRawBody ))
152
+ if ! json .Valid (actualBody ) {
153
+ values , err := url .ParseQuery (string (actualBody ))
152
154
if err != nil {
153
155
panic (fmt .Errorf ("cassette body matcher: failed to parse body as url values: %w" , err )) // lintignore: R009
154
156
}
@@ -162,9 +164,9 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
162
164
return compareFormBodies (values , cassetteRequest .Form )
163
165
}
164
166
165
- err = json .Unmarshal (actualRawBody , & actualJSON )
167
+ err = json .Unmarshal (actualBody , & actualJSON )
166
168
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
168
170
}
169
171
170
172
err = json .Unmarshal ([]byte (cassetteRequest .Body ), & cassetteJSON )
@@ -182,9 +184,9 @@ func cassetteBodyMatcher(actualRequest *http.Request, cassetteRequest cassette.R
182
184
return compareJSONBodies (cassetteJSON , actualJSON )
183
185
}
184
186
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
186
188
// 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 {
188
190
expectedURL , _ := url .Parse (expected .URL )
189
191
actualURL := actual .URL
190
192
actualURLValues := actualURL .Query ()
@@ -297,7 +299,7 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
297
299
}(r )
298
300
299
301
// Add custom matcher for requests and cassettes
300
- r .SetMatcher (cassetteMatcher )
302
+ r .SetMatcher (CassetteMatcher )
301
303
302
304
// Add a filter which removes Authorization headers from all requests:
303
305
r .AddHook (func (i * cassette.Interaction ) error {
0 commit comments