-
Notifications
You must be signed in to change notification settings - Fork 7
/
unmarshal_test.go
168 lines (133 loc) · 5.44 KB
/
unmarshal_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
package qs
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestUnmarshal(t *testing.T) {
hash, err := Unmarshal("")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{})
}
hash, err = Unmarshal("foo")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": nil})
}
hash, err = Unmarshal("foo=")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": ""})
}
hash, err = Unmarshal("foo=bar")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "bar"})
}
hash, err = Unmarshal("foo=\"bar\"")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "\"bar\""})
}
hash, err = Unmarshal("foo=bar&foo=quux")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "quux"})
}
hash, err = Unmarshal("foo&foo=")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": ""})
}
hash, err = Unmarshal("foo=1&bar=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "1", "bar": "2"})
}
hash, err = Unmarshal("&foo=1&&bar=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "1", "bar": "2"})
}
hash, err = Unmarshal("foo&bar=")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": nil, "bar": ""})
}
hash, err = Unmarshal("foo=bar&baz=")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "bar", "baz": ""})
}
hash, err = Unmarshal("my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"my weird field": `q1!2"'w$5&7/z8)?`})
}
hash, err = Unmarshal("a=b&pid%3D1234=1023")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"pid=1234": "1023", "a": "b"})
}
hash, err = Unmarshal("foo[]")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": []interface{}{nil}})
}
hash, err = Unmarshal("foo[]=")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": []interface{}{""}})
}
hash, err = Unmarshal("foo[]=bar")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": []interface{}{"bar"}})
}
hash, err = Unmarshal("foo[]=1&foo[]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": []interface{}{"1", "2"}})
}
hash, err = Unmarshal("foo=bar&baz[]=1&baz[]=2&baz[]=3")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": "bar", "baz": []interface{}{"1", "2", "3"}})
}
hash, err = Unmarshal("foo[]=bar&baz[]=1&baz[]=2&baz[]=3")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"foo": []interface{}{"bar"}, "baz": []interface{}{"1", "2", "3"}})
}
hash, err = Unmarshal("x[y][z]=1")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": map[string]interface{}{"z": "1"}}})
}
hash, err = Unmarshal("x[y][z][]=1")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": map[string]interface{}{"z": []interface{}{"1"}}}})
}
hash, err = Unmarshal("x[y][z]=1&x[y][z]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": map[string]interface{}{"z": "2"}}})
}
hash, err = Unmarshal("x[y][z][]=1&x[y][z][]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": map[string]interface{}{"z": []interface{}{"1", "2"}}}})
}
hash, err = Unmarshal("x[y][][z]=1")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": "1"}}}})
}
hash, err = Unmarshal("x[y][][z][]=1")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": []interface{}{"1"}}}}})
}
hash, err = Unmarshal("x[y][][z]=1&x[y][][w]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": "1", "w": "2"}}}})
}
hash, err = Unmarshal("x[y][][v][w]=1")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"v": map[string]interface{}{"w": "1"}}}}})
}
hash, err = Unmarshal("x[y][][z]=1&x[y][][v][w]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": "1", "v": map[string]interface{}{"w": "2"}}}}})
}
hash, err = Unmarshal("x[y][][z]=1&x[y][][z]=2")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": "1"}, map[string]interface{}{"z": "2"}}}})
}
hash, err = Unmarshal("x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3")
if assert.NoError(t, err) {
assert.Equal(t, hash, map[string]interface{}{"x": map[string]interface{}{"y": []interface{}{map[string]interface{}{"z": "1", "w": "a"}, map[string]interface{}{"z": "2", "w": "3"}}}})
}
hash, err = Unmarshal("x[y]=1&x[y]z=2")
assert.Error(t, err)
hash, err = Unmarshal("x[y]=1&x[]=1")
assert.Error(t, err)
hash, err = Unmarshal("x[y]=1&x[y][][w]=2")
assert.Error(t, err)
}