@@ -49,6 +49,128 @@ var fooMemberCreationBody = `{
4949}
5050`
5151
52+ var secretPatchBodyCassette = `{
53+ "environment_variables": {
54+ "foo": "bar"
55+ },
56+ "privacy": "unknown_privacy",
57+ "protocol": "unknown_protocol",
58+ "secret_environment_variables": [
59+ {
60+ "key": "foo_secret",
61+ "value": "bar_secret"
62+ },
63+ {
64+ "key": "test_secret",
65+ "value": "updated_secret"
66+ },
67+ {
68+ "key": "first_secret",
69+ "value": null
70+ }
71+ ],
72+ "http_option": "unknown_http_option",
73+ "sandbox": "unknown_sandbox"
74+ }
75+ `
76+
77+ var secretPatchBodyRequest = `{
78+ "environment_variables": {
79+ "foo": "bar"
80+ },
81+ "privacy": "unknown_privacy",
82+ "protocol": "unknown_protocol",
83+ "secret_environment_variables": [
84+ {
85+ "key": "first_secret",
86+ "value": null
87+ },
88+ {
89+ "key": "foo_secret",
90+ "value": "bar_secret"
91+ },
92+ {
93+ "key": "test_secret",
94+ "value": "updated_secret"
95+ }
96+ ],
97+ "http_option": "unknown_http_option",
98+ "sandbox": "unknown_sandbox"
99+ }
100+ `
101+
102+ var integertestBodyRequest = `{
103+ "akey": "avalue",
104+ "nested_integer": {
105+ "integers": [
106+ 1,
107+ 2,
108+ 3
109+ ]
110+ }
111+ }
112+ `
113+
114+ var integertestBodyCassette = `{
115+ "akey": "avalue",
116+ "nested_integer": {
117+ "integers": [
118+ 4,
119+ 5,
120+ 6
121+ ]
122+ }
123+ }
124+ `
125+
126+ var integerBodyRequestOutOfOrder = `{
127+ "akey": "avalue",
128+ "nested_integer": {
129+ "integers": [
130+ 2,
131+ 1,
132+ 3
133+ ]
134+ }
135+ }
136+ `
137+
138+ var nestedSliceOfSlicesRequest = `{
139+ "akey": "avalue",
140+ "nested_lists": [
141+ [
142+ "1",
143+ "2",
144+ "3"
145+ ],
146+ [
147+ "4",
148+ "5",
149+ "6"
150+ ]
151+ }
152+ }
153+ `
154+
155+ var nestedSliceOfSlicesCassette = `{
156+ "akey": "avalue",
157+ "nested_slice_of_slices": {
158+ "integers_array": [
159+ [
160+ "4",
161+ "5",
162+ "6"
163+ ],
164+ [
165+ "1",
166+ "2",
167+ "3"
168+ ]
169+ ]
170+ },
171+ }
172+ `
173+
52174// we don't use httptest.NewRequest because it does not set the GetBody func
53175func newRequest (method , url string , body io.Reader ) * http.Request {
54176 req , err := http .NewRequestWithContext (context .Background (), method , url , body )
@@ -64,43 +186,102 @@ var testBodyMatcherCases = []struct {
64186 cassetteBody * cassette.Request
65187 shouldMatch bool
66188}{
189+ // create bar compare with foo
190+ // {
191+ // requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
192+ // cassetteBody: &cassette.Request{
193+ // URL: "https://api.scaleway.com/iam/v1alpha1/users",
194+ // Method: http.MethodPost,
195+ // Body: fooMemberCreationBody,
196+ // ContentLength: int64(len(fooMemberCreationBody)),
197+ // },
198+ // shouldMatch: false,
199+ // },
200+ // // create bar compare with bar
201+ // {
202+ // requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(barMemberCreationBody)),
203+ // cassetteBody: &cassette.Request{
204+ // URL: "https://api.scaleway.com/iam/v1alpha1/users",
205+ // Method: http.MethodPost,
206+ // Body: barMemberCreationBody,
207+ // ContentLength: int64(len(barMemberCreationBody)),
208+ // },
209+ // shouldMatch: true,
210+ // },
211+ // // simple http get
212+ // {
213+ // requestBody: newRequest(http.MethodGet, "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b", nil),
214+ // cassetteBody: &cassette.Request{
215+ // URL: "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b",
216+ // Method: http.MethodGet,
217+ // Body: "",
218+ // ContentLength: 0,
219+ // },
220+ // shouldMatch: true,
221+ // },
222+ // // patch secret with nested slices of map[string]interface{} in different order
223+ // // we cannot user deep equal because the order of the slices is different although the values are the same
224+ // // it is not possible to sort them because they are not comparable (map[string]interface{})
225+ // {
226+ // requestBody: newRequest(http.MethodPatch, "https://api.scaleway.com/secrets/v1/secrets/123", strings.NewReader(secretPatchBodyRequest)),
227+ // cassetteBody: &cassette.Request{
228+ // URL: "https://api.scaleway.com/secrets/v1/secrets/123",
229+ // Method: http.MethodPatch,
230+ // Body: secretPatchBodyCassette,
231+ // ContentLength: int64(len(secretPatchBodyCassette)),
232+ // },
233+ // shouldMatch: true,
234+ // },
235+ // // compare nested slices of different integers
236+ // {
237+ // requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integertestBodyRequest)),
238+ // cassetteBody: &cassette.Request{
239+ // URL: "https://api.scaleway.com/iam/v1alpha1/users",
240+ // Method: http.MethodPost,
241+ // Body: integertestBodyCassette,
242+ // ContentLength: int64(len(integertestBodyCassette)),
243+ // },
244+ // shouldMatch: false,
245+ // },
246+ // // compare nested slices of same integers in different order
247+ // {
248+ // requestBody: newRequest(http.MethodPost, "https://api.scaleway.com/iam/v1alpha1/users", strings.NewReader(integerBodyRequestOutOfOrder)),
249+ // cassetteBody: &cassette.Request{
250+ // URL: "https://api.scaleway.com/iam/v1alpha1/users",
251+ // Method: http.MethodPost,
252+ // Body: integertestBodyRequest,
253+ // ContentLength: int64(len(integertestBodyRequest)),
254+ // },
255+ // shouldMatch: true,
256+ // },
257+ // // compare nested slices of slices of strings
67258 {
68- requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (barMemberCreationBody )),
259+ requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (nestedSliceOfSlicesRequest )),
69260 cassetteBody : & cassette.Request {
70261 URL : "https://api.scaleway.com/iam/v1alpha1/users" ,
71262 Method : http .MethodPost ,
72- Body : fooMemberCreationBody ,
73- ContentLength : int64 (len (fooMemberCreationBody )),
263+ Body : nestedSliceOfSlicesCassette ,
264+ ContentLength : int64 (len (nestedSliceOfSlicesCassette )),
74265 },
75266 shouldMatch : false ,
76267 },
77268 {
78- requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (barMemberCreationBody )),
269+ requestBody : newRequest (http .MethodPost , "https://api.scaleway.com/iam/v1alpha1/users" , strings .NewReader (nestedSliceOfSlicesRequest )),
79270 cassetteBody : & cassette.Request {
80271 URL : "https://api.scaleway.com/iam/v1alpha1/users" ,
81272 Method : http .MethodPost ,
82- Body : barMemberCreationBody ,
83- ContentLength : int64 (len (barMemberCreationBody )),
84- },
85- shouldMatch : true ,
86- },
87- {
88- requestBody : newRequest (http .MethodGet , "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b" , nil ),
89- cassetteBody : & cassette.Request {
90- URL : "https://api.scaleway.com/iam/v1alpha1/users/6867048b-fe12-4e96-835e-41c79a39604b" ,
91- Method : http .MethodGet ,
92- Body : "" ,
93- ContentLength : 0 ,
273+ Body : nestedSliceOfSlicesRequest ,
274+ ContentLength : int64 (len (nestedSliceOfSlicesRequest )),
94275 },
95276 shouldMatch : true ,
96277 },
97278}
98279
99280func TestCassetteMatcher (t * testing.T ) {
100- for _ , test := range testBodyMatcherCases {
281+ for i , test := range testBodyMatcherCases {
101282 shouldMatch := acctest .CassetteMatcher (test .requestBody , * test .cassetteBody )
102283 if shouldMatch != test .shouldMatch {
103- t .Errorf ("expected %v, got %v" , test .shouldMatch , shouldMatch )
284+ t .Errorf ("test %d: expected %v, got %v" , i , test .shouldMatch , shouldMatch )
104285 }
105286 }
106287}
0 commit comments