1 package httpbinding
2
3 import (
4 "fmt"
5 "math/big"
6 "net/url"
7 "reflect"
8 "testing"
9 )
10
11 func TestQueryValue(t *testing.T) {
12 const queryKey = "someKey"
13
14 cases := map[string]struct {
15 values url.Values
16 args []interface{}
17 append bool
18 expected url.Values
19 }{
20 "set blob": {
21 values: url.Values{queryKey: []string{"foobar"}},
22 args: []interface{}{[]byte("baz")},
23 expected: map[string][]string{
24 queryKey: {"YmF6"},
25 },
26 },
27 "set bool": {
28 values: url.Values{queryKey: []string{"foobar"}},
29 args: []interface{}{true},
30 expected: map[string][]string{
31 queryKey: {"true"},
32 },
33 },
34 "set string": {
35 values: url.Values{queryKey: []string{"foobar"}},
36 args: []interface{}{"string value"},
37 expected: map[string][]string{
38 queryKey: {"string value"},
39 },
40 },
41 "set byte": {
42 values: url.Values{queryKey: []string{"foobar"}},
43 args: []interface{}{int8(127)},
44 expected: map[string][]string{
45 queryKey: {"127"},
46 },
47 },
48 "set short": {
49 values: url.Values{queryKey: []string{"foobar"}},
50 args: []interface{}{int16(32767)},
51 expected: map[string][]string{
52 queryKey: {"32767"},
53 },
54 },
55 "set integer": {
56 values: url.Values{queryKey: []string{"foobar"}},
57 args: []interface{}{int32(2147483647)},
58 expected: map[string][]string{
59 queryKey: {"2147483647"},
60 },
61 },
62 "set long": {
63 values: url.Values{queryKey: []string{"foobar"}},
64 args: []interface{}{int64(9223372036854775807)},
65 expected: map[string][]string{
66 queryKey: {"9223372036854775807"},
67 },
68 },
69 "set float": {
70 values: url.Values{queryKey: []string{"foobar"}},
71 args: []interface{}{float32(3.14159)},
72 expected: map[string][]string{
73 queryKey: {"3.14159"},
74 },
75 },
76 "set double": {
77 values: url.Values{queryKey: []string{"foobar"}},
78 args: []interface{}{float64(3.14159)},
79 expected: map[string][]string{
80 queryKey: {"3.14159"},
81 },
82 },
83 "set bigInteger": {
84 values: url.Values{queryKey: []string{"foobar"}},
85 args: []interface{}{new(big.Int).SetInt64(1)},
86 expected: map[string][]string{
87 queryKey: {"1"},
88 },
89 },
90 "set bigDecimal": {
91 values: url.Values{queryKey: []string{"foobar"}},
92 args: []interface{}{new(big.Float).SetFloat64(1024.10241024)},
93 expected: map[string][]string{
94 queryKey: {"1.02410241024e+03"},
95 },
96 },
97 "add blob": {
98 values: url.Values{queryKey: []string{"foobar"}},
99 args: []interface{}{[]byte("baz")},
100 append: true,
101 expected: map[string][]string{
102 queryKey: {"foobar", "YmF6"},
103 },
104 },
105 "add bool": {
106 values: url.Values{queryKey: []string{"foobar"}},
107 args: []interface{}{true},
108 append: true,
109 expected: map[string][]string{
110 queryKey: {"foobar", "true"},
111 },
112 },
113 "add string": {
114 values: url.Values{queryKey: []string{"foobar"}},
115 args: []interface{}{"string value"},
116 append: true,
117 expected: map[string][]string{
118 queryKey: {"foobar", "string value"},
119 },
120 },
121 "add byte": {
122 values: url.Values{queryKey: []string{"foobar"}},
123 args: []interface{}{int8(127)},
124 append: true,
125 expected: map[string][]string{
126 queryKey: {"foobar", "127"},
127 },
128 },
129 "add short": {
130 values: url.Values{queryKey: []string{"foobar"}},
131 args: []interface{}{int16(32767)},
132 append: true,
133 expected: map[string][]string{
134 queryKey: {"foobar", "32767"},
135 },
136 },
137 "add integer": {
138 values: url.Values{queryKey: []string{"foobar"}},
139 args: []interface{}{int32(2147483647)},
140 expected: map[string][]string{
141 queryKey: {"2147483647"},
142 },
143 },
144 "add long": {
145 values: url.Values{queryKey: []string{"foobar"}},
146 args: []interface{}{int64(9223372036854775807)},
147 append: true,
148 expected: map[string][]string{
149 queryKey: {"foobar", "9223372036854775807"},
150 },
151 },
152 "add float": {
153 values: url.Values{queryKey: []string{"foobar"}},
154 args: []interface{}{float32(3.14159)},
155 append: true,
156 expected: map[string][]string{
157 queryKey: {"foobar", "3.14159"},
158 },
159 },
160 "add double": {
161 values: url.Values{queryKey: []string{"foobar"}},
162 args: []interface{}{float64(3.14159)},
163 append: true,
164 expected: map[string][]string{
165 queryKey: {"foobar", "3.14159"},
166 },
167 },
168 "add bigInteger": {
169 values: url.Values{queryKey: []string{"foobar"}},
170 args: []interface{}{new(big.Int).SetInt64(1)},
171 append: true,
172 expected: map[string][]string{
173 queryKey: {"foobar", "1"},
174 },
175 },
176 "add bigDecimal": {
177 values: url.Values{queryKey: []string{"foobar"}},
178 args: []interface{}{new(big.Float).SetFloat64(1024.10241024)},
179 append: true,
180 expected: map[string][]string{
181 queryKey: {"foobar", "1.02410241024e+03"},
182 },
183 },
184 }
185
186 for name, tt := range cases {
187 t.Run(name, func(t *testing.T) {
188 if tt.values == nil {
189 tt.values = url.Values{}
190 }
191
192 qv := NewQueryValue(tt.values, queryKey, tt.append)
193
194 if err := setQueryValue(qv, tt.args); err != nil {
195 t.Fatalf("expected no error, got %v", err)
196 }
197
198 if e, a := tt.expected, qv.query; !reflect.DeepEqual(e, a) {
199 t.Errorf("expected %v, got %v", e, a)
200 }
201 })
202 }
203 }
204
205 func setQueryValue(qv QueryValue, args []interface{}) error {
206 value := args[0]
207
208 switch value.(type) {
209 case []byte:
210 return reflectCall(reflect.ValueOf(qv.Blob), args)
211 case bool:
212 return reflectCall(reflect.ValueOf(qv.Boolean), args)
213 case string:
214 return reflectCall(reflect.ValueOf(qv.String), args)
215 case int8:
216 return reflectCall(reflect.ValueOf(qv.Byte), args)
217 case int16:
218 return reflectCall(reflect.ValueOf(qv.Short), args)
219 case int32:
220 return reflectCall(reflect.ValueOf(qv.Integer), args)
221 case int64:
222 return reflectCall(reflect.ValueOf(qv.Long), args)
223 case float32:
224 return reflectCall(reflect.ValueOf(qv.Float), args)
225 case float64:
226 return reflectCall(reflect.ValueOf(qv.Double), args)
227 case *big.Int:
228 return reflectCall(reflect.ValueOf(qv.BigInteger), args)
229 case *big.Float:
230 return reflectCall(reflect.ValueOf(qv.BigDecimal), args)
231 default:
232 return fmt.Errorf("unhandled query value type")
233 }
234 }
235
View as plain text