Skip to content

Commit 1918034

Browse files
committed
wip
1 parent d5838ca commit 1918034

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

internal/acctest/acctest_test.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,37 +101,31 @@ var secretPatchBodyRequest = `{
101101

102102
var integertestBodyRequest = `{
103103
"akey": "avalue",
104-
"nested_integer": {
105-
"integers": [
106-
1,
107-
2,
108-
3
109-
]
110-
}
104+
"integers": [
105+
1,
106+
2,
107+
3
108+
]
111109
}
112110
`
113111

114112
var integertestBodyCassette = `{
115113
"akey": "avalue",
116-
"nested_integer": {
117-
"integers": [
118-
4,
119-
5,
120-
6
121-
]
122-
}
114+
"integers": [
115+
4,
116+
5,
117+
6
118+
]
123119
}
124120
`
125121

126122
var integerBodyRequestOutOfOrder = `{
127123
"akey": "avalue",
128-
"nested_integer": {
129-
"integers": [
130-
2,
131-
1,
132-
3
133-
]
134-
}
124+
"integers": [
125+
2,
126+
1,
127+
3
128+
]
135129
}
136130
`
137131

@@ -171,6 +165,23 @@ var nestedSliceOfSlicesCassette = `{
171165
}
172166
`
173167

168+
var simpleSliceOfStringsRequest = `{
169+
"strings": [
170+
"1",
171+
"2",
172+
"3"
173+
]
174+
}
175+
`
176+
var simpleSliceOfStringsCassette = `{
177+
"strings": [
178+
"3",
179+
"2",
180+
"1"
181+
]
182+
}
183+
`
184+
174185
// we don't use httptest.NewRequest because it does not set the GetBody func
175186
func newRequest(method, url string, body io.Reader) *http.Request {
176187
req, err := http.NewRequestWithContext(context.Background(), method, url, body)
@@ -275,6 +286,17 @@ var testBodyMatcherCases = []struct {
275286
},
276287
shouldMatch: true,
277288
},
289+
// compare simple slice of strings
290+
{
291+
requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(simpleSliceOfStringsRequest)),
292+
cassetteBody: &cassette.Request{
293+
URL: "https://api.scaleway.com/iam/v1alpha1/users",
294+
Method: http.MethodPost,
295+
Body: simpleSliceOfStringsCassette,
296+
ContentLength: int64(len(simpleSliceOfStringsCassette)),
297+
},
298+
shouldMatch: true,
299+
},
278300
}
279301

280302
func TestCassetteMatcher(t *testing.T) {

internal/acctest/compare.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ import (
99

1010
// compareJSONFields compare two given json fields
1111
// it will recurse on map[string]interface{} and []interface{}
12-
// and compare string fields with compareJSONFieldsStrings
1312
func compareJSONFields(requestValue, cassetteValue interface{}) bool {
1413
switch requestValue := requestValue.(type) {
1514
case string:
16-
return compareJSONFieldsStrings(requestValue, cassetteValue.(string))
15+
return compareFieldsStrings(requestValue, cassetteValue.(string))
1716
case map[string]interface{}:
1817
return compareJSONBodies(requestValue, cassetteValue.(map[string]interface{}))
1918
case []interface{}:
20-
return compareJSONSlices(requestValue, cassetteValue.([]interface{}))
21-
case []string:
22-
return compareStringSlices(requestValue, cassetteValue.([]string))
19+
return compareSlices(requestValue, cassetteValue.([]interface{}))
2320
default:
2421
return reflect.DeepEqual(requestValue, cassetteValue)
2522
}
@@ -74,7 +71,7 @@ func compareFormBodies(request, cassette url.Values) bool {
7471
continue
7572
}
7673

77-
if !compareJSONFields(requestValue, cassette[key]) {
74+
if !compareStringSlices(requestValue, cassette[key]) {
7875
return false
7976
}
8077
}
@@ -91,9 +88,9 @@ func compareFormBodies(request, cassette url.Values) bool {
9188
return true
9289
}
9390

94-
// compareJSONFieldsStrings compare two strings from request JSON bodies
91+
// compareFieldsStrings compare two strings from request JSON bodies
9592
// has special case when string are terraform generated names
96-
func compareJSONFieldsStrings(expected, actual string) bool {
93+
func compareFieldsStrings(expected, actual string) bool {
9794
if expected == actual {
9895
return true
9996
}
@@ -145,7 +142,7 @@ func compareStringSlices(request, cassette []string) bool {
145142
})
146143

147144
for i, v := range request {
148-
if !compareJSONFieldsStrings(v, cassette[i]) {
145+
if !compareFieldsStrings(v, cassette[i]) {
149146
return false
150147
}
151148
}
@@ -155,8 +152,8 @@ func compareStringSlices(request, cassette []string) bool {
155152

156153
// compareJSONSlices compares two slices of interface{}
157154
// if the slices are comparable (string or float64), it will sort them and compare them
158-
// it returns true in case of slice of map[string]interface{} because it is not possible to sort them
159-
func compareJSONSlices(request, cassette []interface{}) bool {
155+
// it returns true in case of slice of map[string]interface{} because it is impossible to sort
156+
func compareSlices(request, cassette []interface{}) bool {
160157
if len(request) != len(cassette) {
161158
return false
162159
}
@@ -165,23 +162,18 @@ func compareJSONSlices(request, cassette []interface{}) bool {
165162
return true
166163
}
167164

168-
// try to infer the type of the slice, sort it and compare all elements
169165
switch request[0].(type) {
170166
case string:
171-
sort.Slice(request, func(i, j int) bool {
172-
return request[i].(string) < request[j].(string)
173-
})
174-
sort.Slice(cassette, func(i, j int) bool {
175-
return cassette[i].(string) < cassette[j].(string)
176-
})
177-
178-
for i := range request {
179-
if !compareJSONFieldsStrings(request[i].(string), cassette[i].(string)) {
180-
return false
181-
}
167+
requestStrings := make([]string, len(request))
168+
for i, v := range request {
169+
requestStrings[i] = v.(string)
170+
}
171+
cassetteStrings := make([]string, len(cassette))
172+
for i, v := range cassette {
173+
cassetteStrings[i] = v.(string)
182174
}
183175

184-
return true
176+
return compareStringSlices(requestStrings, cassetteStrings)
185177
case float64:
186178
sort.Slice(request, func(i, j int) bool {
187179
return request[i].(float64) < request[j].(float64)

internal/acctest/vcr.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ func cassetteBodyMatcher(request *http.Request, cassette cassette.Request) bool
119119
requestValues.Del(key)
120120
}
121121

122-
// Compare url values
123122
return compareFormBodies(requestValues, cassette.Form)
124123
}
125124

0 commit comments

Comments
 (0)