File: encoding_test.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (84 lines) | stat: -rw-r--r-- 2,492 bytes parent folder | download | duplicates (3)
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
package util

import (
	"testing"
	"time"
)

type TestString string

type SubStruct struct {
	A string
	B int
}

type MyStruct struct {
	SubStruct
}

type YourStruct struct {
	SubStruct SubStruct
}

func TestConvertToQueryValues2(t *testing.T) {

	result := ConvertToQueryValues(MyStruct{SubStruct: SubStruct{A: "A", B: 1}}).Encode()
	const expectedResult = "A=A&B=1"
	if result != expectedResult {
		// Sometimes result is not matched for the different orders
		t.Logf("Incorrect encoding: %s", result)
	}
	result2 := ConvertToQueryValues(YourStruct{SubStruct: SubStruct{A: "A2", B: 2}}).Encode()
	const expectedResult2 = "SubStruct.A=A2&SubStruct.B=2"
	if result2 != expectedResult2 {
		// Sometimes result is not matched for the different orders
		t.Logf("Incorrect encoding: %s", result2)
	}
}

type TestStruct struct {
	Format       string
	Version      string
	AccessKeyId  string
	Timestamp    time.Time
	Empty        string
	IntValue     int      `ArgName:"int-value"`
	BoolPtr      *bool    `ArgName:"bool-ptr"`
	IntPtr       *int     `ArgName:"int-ptr"`
	StringArray  []string `ArgName:"str-array"`
	StringArray2 []string `ArgName:"array" query:"list"`
	StructArray  []SubStruct
	SubStruct    SubStruct
	test         TestString
	tests        []TestString
	Tag          map[string]string
}

func TestConvertToQueryValues(t *testing.T) {
	boolValue := true
	request := TestStruct{
		Format:       "JSON",
		Version:      "1.0",
		Timestamp:    time.Date(2015, time.Month(5), 26, 1, 2, 3, 4, time.UTC),
		IntValue:     10,
		BoolPtr:      &boolValue,
		StringArray:  []string{"abc", "xyz"},
		StringArray2: []string{"abc", "xyz"},
		StructArray: []SubStruct{
			SubStruct{A: "a", B: 1},
			SubStruct{A: "x", B: 2},
		},
		SubStruct: SubStruct{A: "M", B: 0},
		test:      TestString("test"),
		tests:     []TestString{TestString("test1"), TestString("test2")},
		Tag:       map[string]string{"abc": "xyz", "123": "456"},
	}
	result := ConvertToQueryValues(&request).Encode()
	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"

	if result != expectedResult {
		// Sometimes result is not matched for the different orders
		t.Logf("Incorrect encoding: %s", result)
	}

}