mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
chore: fix test failure (#4031)
This commit is contained in:
parent
3ef59f6a71
commit
5dd9342703
@ -19,18 +19,21 @@ func TestParseForm(t *testing.T) {
|
||||
Name string `form:"name"`
|
||||
Age int `form:"age"`
|
||||
Percent float64 `form:"percent,optional"`
|
||||
Statuses []string `form:"statuses[],optional"`
|
||||
Statuses []string `form:"statuses,optional"`
|
||||
NoValue string `form:"noValue,optional"`
|
||||
}
|
||||
|
||||
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4&statuses[]=try&statuses[]=done", http.NoBody)
|
||||
r, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
"/a?name=hello&age=18&percent=3.4&statuses=try&statuses=done",
|
||||
http.NoBody)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, Parse(r, &v))
|
||||
assert.Equal(t, "hello", v.Name)
|
||||
assert.Equal(t, 18, v.Age)
|
||||
assert.Equal(t, 3.4, v.Percent)
|
||||
assert.Equal(t, 2, len(v.Statuses))
|
||||
assert.True(t, v.Statuses[0] == "try" || v.Statuses[1] == "try")
|
||||
assert.True(t, v.Statuses[0] == "done" || v.Statuses[1] == "done")
|
||||
assert.EqualValues(t, []string{"try", "done"}, v.Statuses)
|
||||
assert.Equal(t, 0, len(v.NoValue))
|
||||
}
|
||||
|
||||
func TestParseForm_Error(t *testing.T) {
|
||||
|
@ -21,10 +21,15 @@ func GetFormValues(r *http.Request) (map[string]any, error) {
|
||||
|
||||
params := make(map[string]any, len(r.Form))
|
||||
for name := range r.Form {
|
||||
if len(r.Form[name]) > 1 {
|
||||
switch len(r.Form[name]) {
|
||||
case 1:
|
||||
formValue := r.Form.Get(name)
|
||||
if len(formValue) > 0 {
|
||||
params[name] = formValue
|
||||
}
|
||||
default:
|
||||
// len(r.Form[name]) > 1, never be 0
|
||||
params[name] = r.Form[name]
|
||||
} else if len(r.Form[name]) == 1 {
|
||||
params[name] = r.Form.Get(name)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user