forked from joncalhoun/qson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqson_test.go
170 lines (157 loc) · 4.97 KB
/
qson_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
package qson
import (
"fmt"
"testing"
)
func ExampleUnmarshal() {
type Ex struct {
A string `json:"a"`
B struct {
C int `json:"c"`
} `json:"b"`
}
var ex Ex
if err := Unmarshal(&ex, "a=xyz&b[c]=456"); err != nil {
panic(err)
}
fmt.Printf("%+v\n", ex)
// Output: {A:xyz B:{C:456}}
}
type unmarshalT struct {
A string `json:"a"`
B unmarshalB `json:"b"`
}
type unmarshalB struct {
C int `json:"c"`
D string `json:"D"`
}
func TestUnmarshal(t *testing.T) {
query := "a=xyz&b[c]=456"
expected := unmarshalT{
A: "xyz",
B: unmarshalB{
C: 456,
},
}
var actual unmarshalT
err := Unmarshal(&actual, query)
if err != nil {
t.Error(err)
}
if expected != actual {
t.Errorf("Expected: %+v Actual: %+v", expected, actual)
}
}
func ExampleToJSON() {
b, err := ToJSON("a=xyz&b[c]=456")
if err != nil {
panic(err)
}
fmt.Printf(string(b))
// Output: {"a":"xyz","b":{"c":456}}
}
func TestToJSONNested(t *testing.T) {
query := "bar%5Bone%5D%5Btwo%5D=2&bar[one][red]=112"
expected := `{"bar":{"one":{"red":112,"two":2}}}`
actual, err := ToJSON(query)
if err != nil {
t.Error(err)
}
actualStr := string(actual)
if actualStr != expected {
t.Errorf("Expected: %s Actual: %s", expected, actualStr)
}
}
func TestToJSONPlain(t *testing.T) {
query := "cat=1&dog=2"
expected := `{"cat":1,"dog":2}`
actual, err := ToJSON(query)
if err != nil {
t.Error(err)
}
actualStr := string(actual)
if actualStr != expected {
t.Errorf("Expected: %s Actual: %s", expected, actualStr)
}
}
func TestToJSONSlice(t *testing.T) {
query := "cat[]=1&cat[]=34"
expected := `{"cat":[1,34]}`
actual, err := ToJSON(query)
if err != nil {
t.Error(err)
}
actualStr := string(actual)
if actualStr != expected {
t.Errorf("Expected: %s Actual: %s", expected, actualStr)
}
}
func TestToJSONBig(t *testing.T) {
query := "distinct_id=763_1495187301909_3495×tamp=1495187523&event=product_add_cart¶ms%5BproductRefId%5D=8284563078¶ms%5Bapps%5D%5B%5D=precommend¶ms%5Bapps%5D%5B%5D=bsales¶ms%5Bsource%5D=item¶ms%5Boptions%5D%5Bsegment%5D=cart_recommendation¶ms%5Boptions%5D%5Btype%5D=up_sell¶ms%5BtimeExpire%5D=1495187599642¶ms%5Brecommend_system_product_source%5D=item¶ms%5Bproduct_id%5D=8284563078¶ms%5Bvariant_id%5D=27661944134¶ms%5Bsku%5D=00483332%20(black)¶ms%5Bsources%5D%5B%5D=product_recommendation¶ms%5Bcart_token%5D=dc2c336a009edf2762128e65806dfb1d¶ms%5Bquantity%5D=1¶ms%5Bnew_popup_upsell_mobile%5D=false¶ms%5BclientDevice%5D=desktop¶ms%5BclientIsMobile%5D=false¶ms%5BclientIsSmallScreen%5D=false¶ms%5Bnew_popup_crossell_mobile%5D=false&api_key=14c5b7dacea9157029265b174491d340"
expected := `{"api_key":"14c5b7dacea9157029265b174491d340","distinct_id":"763_1495187301909_3495","event":"product_add_cart","params":{"apps":["precommend","bsales"],"cart_token":"dc2c336a009edf2762128e65806dfb1d","clientDevice":"desktop","clientIsMobile":false,"clientIsSmallScreen":false,"new_popup_crossell_mobile":false,"new_popup_upsell_mobile":false,"options":{"segment":"cart_recommendation","type":"up_sell"},"productRefId":8284563078,"product_id":8284563078,"quantity":1,"recommend_system_product_source":"item","sku":"00483332 (black)","source":"item","sources":["product_recommendation"],"timeExpire":1495187599642,"variant_id":27661944134},"timestamp":1495187523}`
actual, err := ToJSON(query)
if err != nil {
t.Error(err)
}
actualStr := string(actual)
if actualStr != expected {
t.Errorf("Expected: %s Actual: %s", expected, actualStr)
}
}
func TestToJSONDuplicateKey(t *testing.T) {
query := "cat=1&cat=2"
expected := `{"cat":2}`
actual, err := ToJSON(query)
if err != nil {
t.Error(err)
}
actualStr := string(actual)
if actualStr != expected {
t.Errorf("Expected: %s Actual: %s", expected, actualStr)
}
}
func TestSplitKeyAndValue(t *testing.T) {
param := "a[dog][=cat]=123"
eKey, eValue := "a[dog][=cat]", "123"
aKey, aValue, err := splitKeyAndValue(param)
if err != nil {
t.Error(err)
}
if eKey != aKey {
t.Errorf("Keys do not match. Expected: %s Actual: %s", eKey, aKey)
}
if eValue != aValue {
t.Errorf("Values do not match. Expected: %s Actual: %s", eValue, aValue)
}
}
func TestEncodedAmpersand(t *testing.T) {
query := "a=xyz&b[d]=ben%26jerry"
expected := unmarshalT{
A: "xyz",
B: unmarshalB{
D: "ben&jerry",
},
}
var actual unmarshalT
err := Unmarshal(&actual, query)
if err != nil {
t.Error(err)
}
if expected != actual {
t.Errorf("Expected: %+v Actual: %+v", expected, actual)
}
}
func TestEncodedAmpersand2(t *testing.T) {
query := "filter=parent%3Dflow12345%26request%3Dreq12345&meta.limit=20&meta.offset=0"
expected := map[string]interface{}{"filter": "parent=flow12345&request=req12345", "meta.limit": float64(20), "meta.offset": float64(0)}
actual := make(map[string]interface{})
err := Unmarshal(&actual, query)
if err != nil {
t.Error(err)
}
for k, v := range actual {
if nv, ok := expected[k]; !ok || nv != v {
t.Errorf("Expected: %+v Actual: %+v", expected, actual)
}
}
}