chore: simplify http query array parsing (#4637)

This commit is contained in:
Kevin Wan
2025-02-09 01:00:52 +08:00
committed by GitHub
parent 507ff96546
commit f747585518
5 changed files with 38 additions and 116 deletions

View File

@@ -160,7 +160,7 @@ func TestParseFormArray(t *testing.T) {
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []string{"1", "2", "3"}, v.Names)
assert.ElementsMatch(t, []string{"1,2,3"}, v.Names)
}
})
@@ -189,9 +189,7 @@ func TestParseFormArray(t *testing.T) {
"/a?numbers=1,2,3",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []int{1, 2, 3}, v.Numbers)
}
assert.Error(t, Parse(r, &v))
})
t.Run("slice with one value on array format brackets", func(t *testing.T) {
@@ -268,9 +266,10 @@ func TestParseFormArray(t *testing.T) {
assert.ElementsMatch(t, []float64{2}, v.Numbers)
}
})
t.Run("slice with one value on disable array of comma split format", func(t *testing.T) {
t.Run("slice with one value", func(t *testing.T) {
var v struct {
Codes []string `form:"codes,arrayComma=false"`
Codes []string `form:"codes"`
}
r, err := http.NewRequest(
http.MethodGet,
@@ -281,7 +280,8 @@ func TestParseFormArray(t *testing.T) {
assert.ElementsMatch(t, []string{"aaa,bbb,ccc"}, v.Codes)
}
})
t.Run("slice with multiple value on disable array of comma split format", func(t *testing.T) {
t.Run("slice with multiple values", func(t *testing.T) {
var v struct {
Codes []string `form:"codes,arrayComma=false"`
}
@@ -295,34 +295,6 @@ func TestParseFormArray(t *testing.T) {
assert.ElementsMatch(t, []string{"aaa,bbb,ccc", "ccc,ddd,eee"}, v.Codes)
}
})
t.Run("slice with multiple value on enable array of comma split format", func(t *testing.T) {
var v struct {
Codes []string `form:"codes,arrayComma=true"`
}
r, err := http.NewRequest(
http.MethodGet,
"/a?codes=aaa,bbb,ccc&codes=ccc,ddd,eee",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []string{"aaa,bbb,ccc", "ccc,ddd,eee"}, v.Codes)
}
})
t.Run("slice with one value on enable array of comma split format", func(t *testing.T) {
var v struct {
Codes []string `form:"codes,arrayComma=true"`
}
r, err := http.NewRequest(
http.MethodGet,
"/a?codes=aaa,bbb,ccc",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []string{"aaa", "bbb", "ccc"}, v.Codes)
}
})
}
func TestParseForm_Error(t *testing.T) {