forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params_test.go
310 lines (264 loc) · 7.46 KB
/
params_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package stripe_test
import (
"net/url"
"reflect"
"testing"
stripe "github.com/stripe/stripe-go"
. "github.com/stripe/stripe-go/testing"
)
func TestCheckinRangeQueryParamsAppendTo(t *testing.T) {
{
values := &stripe.RequestValues{}
// Try it with an empty set of parameters
params := &stripe.RangeQueryParams{}
params.AppendTo(values, "created")
if !values.Empty() {
t.Fatalf("Expected request values to be empty")
}
}
{
values := &stripe.RequestValues{}
// Try it with an empty set of parameters
params := &stripe.RangeQueryParams{
GreaterThan: 10,
GreaterThanOrEqual: 20,
LesserThan: 30,
LesserThanOrEqual: 40,
}
params.AppendTo(values, "created")
{
value := values.Get("created[gt]")
if len(value) != 1 || value[0] != "10" {
t.Fatalf(
"Expected encoded value of 10 for created[gt] but got %v.",
value)
}
}
{
value := values.Get("created[gte]")
if len(value) != 1 || value[0] != "20" {
t.Fatalf(
"Expected encoded value of 10 for created[gte] but got %v.",
value)
}
}
{
value := values.Get("created[lt]")
if len(value) != 1 || value[0] != "30" {
t.Fatalf(
"Expected encoded value of 10 for created[lt] but got %v.",
value)
}
}
{
value := values.Get("created[lte]")
if len(value) != 1 || value[0] != "40" {
t.Fatalf(
"Expected encoded value of 10 for created[lte] but got %v.",
value)
}
}
}
}
func TestRequestValues(t *testing.T) {
values := &stripe.RequestValues{}
actual := values.Encode()
expected := ""
if expected != actual {
t.Fatalf("Expected encoded value of %v but got %v.", expected, actual)
}
if !values.Empty() {
t.Fatalf("Expected values to be empty.")
}
values = &stripe.RequestValues{}
values.Add("foo", "bar")
actual = values.Encode()
expected = "foo=bar"
if expected != actual {
t.Fatalf("Expected encoded value of %v but got %v.", expected, actual)
}
if values.Empty() {
t.Fatalf("Expected values to not be empty.")
}
got := values.Get("foo")
if len(got) != 1 {
t.Fatalf("Expected 1 result from Get, got %v.", len(got))
}
if got[0] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[0])
}
values = &stripe.RequestValues{}
values.Add("foo", "bar")
values.Add("foo", "bar")
values.Add("baz", "bar")
actual = values.Encode()
expected = "foo=bar&foo=bar&baz=bar"
if expected != actual {
t.Fatalf("Expected encoded value of %v but got %v.", expected, actual)
}
got = values.Get("foo")
if len(got) != 2 {
t.Fatalf("Expected 2 results from Get, got %v.", len(got))
}
if got[0] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[0])
}
if got[1] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[1])
}
got = values.Get("baz")
if len(got) != 1 {
t.Fatalf("Expected 1 result from Get, got %v.", len(got))
}
if got[0] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[0])
}
values.Set("foo", "firstbar")
actual = values.Encode()
expected = "foo=firstbar&foo=bar&baz=bar"
if expected != actual {
t.Fatalf("Expected encoded value of %v but got %v.", expected, actual)
}
got = values.Get("foo")
if len(got) != 2 {
t.Fatalf("Expected 2 results from Get, got %v.", len(got))
}
if got[0] != "firstbar" {
t.Fatalf("Expected 'firstbar' result from Get, got %v.", got[0])
}
if got[1] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[1])
}
got = values.Get("baz")
if len(got) != 1 {
t.Fatalf("Expected 1 result from Get, got %v.", len(got))
}
if got[0] != "bar" {
t.Fatalf("Expected 'bar' result from Get, got %v.", got[0])
}
values.Set("new", "appended")
actual = values.Encode()
expected = "foo=firstbar&foo=bar&baz=bar&new=appended"
if expected != actual {
t.Fatalf("Expected encoded value of %v but got %v.", expected, actual)
}
urlValues := values.ToValues()
expectedURLValues := url.Values{
"baz": {"bar"},
"foo": {"firstbar", "bar"},
"new": {"appended"},
}
if !reflect.DeepEqual(urlValues, expectedURLValues) {
t.Fatalf("Expected body of %v but got %v.", expectedURLValues, urlValues)
}
got = values.Get("new")
if len(got) != 1 {
t.Fatalf("Expected 1 result from Get, got %v.", len(got))
}
if got[0] != "appended" {
t.Fatalf("Expected 'appended' result from Get, got %v.", got[0])
}
got = values.Get("boguskey")
if got != nil {
t.Fatalf("Expected nil, got %v", got)
}
}
func TestParamsWithExtras(t *testing.T) {
testCases := []struct {
InitialBody [][2]string
Extras [][2]string
ExpectedBody [][2]string
}{
{
InitialBody: [][2]string{{"foo", "bar"}},
Extras: [][2]string{},
ExpectedBody: [][2]string{{"foo", "bar"}},
},
{
InitialBody: [][2]string{{"foo", "bar"}},
Extras: [][2]string{{"foo", "baz"}, {"other", "thing"}},
ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}, {"other", "thing"}},
},
}
for _, testCase := range testCases {
p := stripe.Params{}
for _, extra := range testCase.Extras {
p.AddExtra(extra[0], extra[1])
}
body := valuesFromArray(testCase.InitialBody)
p.AppendTo(body)
expected := valuesFromArray(testCase.ExpectedBody)
if !reflect.DeepEqual(body, expected) {
t.Fatalf("Expected body of %v but got %v.", expected, body)
}
}
}
func TestCheckinListParamsExpansion(t *testing.T) {
testCases := []struct {
InitialBody [][2]string
Expand []string
ExpectedBody [][2]string
}{
{
InitialBody: [][2]string{{"foo", "bar"}},
Expand: []string{},
ExpectedBody: [][2]string{{"foo", "bar"}},
},
{
InitialBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}},
Expand: []string{"data", "data.foo"},
ExpectedBody: [][2]string{{"foo", "bar"}, {"foo", "baz"}, {"expand[]", "data"}, {"expand[]", "data.foo"}},
},
}
for _, testCase := range testCases {
p := stripe.ListParams{}
for _, exp := range testCase.Expand {
p.Expand(exp)
}
body := valuesFromArray(testCase.InitialBody)
p.AppendTo(body)
expected := valuesFromArray(testCase.ExpectedBody)
if !reflect.DeepEqual(body, expected) {
t.Fatalf("Expected body of %v but got %v.", expected, body)
}
}
}
func TestCheckinListParamsToParams(t *testing.T) {
listParams := &stripe.ListParams{StripeAccount: TestMerchantID}
params := listParams.ToParams()
if params.StripeAccount != TestMerchantID {
t.Fatalf("Expected StripeAccount of %v but got %v.",
TestMerchantID, params.StripeAccount)
}
}
func TestCheckinParamsSetAccount(t *testing.T) {
p := &stripe.Params{}
p.SetAccount(TestMerchantID)
if p.Account != TestMerchantID {
t.Fatalf("Expected Account of %v but got %v.", TestMerchantID, p.Account)
}
if p.StripeAccount != TestMerchantID {
t.Fatalf("Expected StripeAccount of %v but got %v.", TestMerchantID, p.StripeAccount)
}
}
func TestCheckinParamsSetStripeAccount(t *testing.T) {
p := &stripe.Params{}
p.SetStripeAccount(TestMerchantID)
if p.StripeAccount != TestMerchantID {
t.Fatalf("Expected Account of %v but got %v.", TestMerchantID, p.StripeAccount)
}
// Check that we don't set the deprecated parameter.
if p.Account != "" {
t.Fatalf("Expected empty Account but got %v.", TestMerchantID)
}
}
// Converts a collection of key/value tuples in a two dimensional slice/array
// into RequestValues form. The purpose of this is that it's much cleaner to
// initialize the array all at once on a single line.
func valuesFromArray(arr [][2]string) *stripe.RequestValues {
body := &stripe.RequestValues{}
for _, v := range arr {
body.Add(v[0], v[1])
}
return body
}