Debian Codegraph @

util/encoding_test.go

raw symbols SCIP
1 package util
2
3 import (
4 "testing"
5 "time"
6 )
7
8 type TestString string
9
10 type SubStruct struct {
11 A string
12 B int
13 }
14
15 type MyStruct struct {
16 SubStruct
17 }
18
19 type YourStruct struct {
20 SubStruct SubStruct
21 }
22
23 func TestConvertToQueryValues2(t *testing.T) {
24
25 result := ConvertToQueryValues(MyStruct{SubStruct: SubStruct{A: "A", B: 1}}).Encode()
26 const expectedResult = "A=A&B=1"
27 if result != expectedResult {
28 // Sometimes result is not matched for the different orders
29 t.Logf("Incorrect encoding: %s", result)
30 }
31 result2 := ConvertToQueryValues(YourStruct{SubStruct: SubStruct{A: "A2", B: 2}}).Encode()
32 const expectedResult2 = "SubStruct.A=A2&SubStruct.B=2"
33 if result2 != expectedResult2 {
34 // Sometimes result is not matched for the different orders
35 t.Logf("Incorrect encoding: %s", result2)
36 }
37 }
38
39 type TestStruct struct {
40 Format string
41 Version string
42 AccessKeyId string
43 Timestamp time.Time
44 Empty string
45 IntValue int `ArgName:"int-value"`
46 BoolPtr *bool `ArgName:"bool-ptr"`
47 IntPtr *int `ArgName:"int-ptr"`
48 StringArray []string `ArgName:"str-array"`
49 StringArray2 []string `ArgName:"array" query:"list"`
50 StructArray []SubStruct
51 SubStruct SubStruct
52 test TestString
53 tests []TestString
54 Tag map[string]string
55 }
56
57 func TestConvertToQueryValues(t *testing.T) {
58 boolValue := true
59 request := TestStruct{
60 Format: "JSON",
61 Version: "1.0",
62 Timestamp: time.Date(2015, time.Month(5), 26, 1, 2, 3, 4, time.UTC),
63 IntValue: 10,
64 BoolPtr: &boolValue,
65 StringArray: []string{"abc", "xyz"},
66 StringArray2: []string{"abc", "xyz"},
67 StructArray: []SubStruct{
68 SubStruct{A: "a", B: 1},
69 SubStruct{A: "x", B: 2},
70 },
71 SubStruct: SubStruct{A: "M", B: 0},
72 test: TestString("test"),
73 tests: []TestString{TestString("test1"), TestString("test2")},
74 Tag: map[string]string{"abc": "xyz", "123": "456"},
75 }
76 result := ConvertToQueryValues(&request).Encode()
77 const expectedResult = "Format=JSON&StructArray.1.A=a&StructArray.1.B=1&StructArray.2.A=x&StructArray.2.B=2&SubStruct.A=M&Tag.1.Key=abc&Tag.1.Value=xyz&Tag.2.Key=123&Tag.2.Value=456&Timestamp=2015-05-26T01%3A02%3A03Z&Version=1.0&array.1=abc&array.2=xyz&bool-ptr=true&int-value=10&str-array=%5B%22abc%22%2C%22xyz%22%5D&test=test&tests=%5B%22test1%22%2C%22test2%22%5D"
78
79 if result != expectedResult {
80 // Sometimes result is not matched for the different orders
81 t.Logf("Incorrect encoding: %s", result)
82 }
83
84 }