Skip to content

Commit

Permalink
fix: ignore empty form values in http request
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jan 12, 2025
1 parent 024f285 commit e59038d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 47 additions & 2 deletions rest/httpx/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ func TestParseForm(t *testing.T) {
assert.Nil(t, Parse(r, &v))
assert.Equal(t, 0, len(v.NoValue))
})

t.Run("slice with one value on array format", func(t *testing.T) {
var v struct {
Names string `form:"names"`
}

r, err := http.NewRequest(
http.MethodGet,
"/a?names=1,2,3",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.Equal(t, "1,2,3", v.Names)
}
})
}

func TestParseFormArray(t *testing.T) {
Expand Down Expand Up @@ -114,7 +129,7 @@ func TestParseFormArray(t *testing.T) {
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []string{""}, v.Name)
assert.Empty(t, v.Name)
}
})

Expand All @@ -129,7 +144,7 @@ func TestParseFormArray(t *testing.T) {
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []string{"", "1"}, v.Name)
assert.ElementsMatch(t, []string{"1"}, v.Name)
}
})

Expand Down Expand Up @@ -192,6 +207,36 @@ func TestParseFormArray(t *testing.T) {
assert.ElementsMatch(t, []string{"1", "2", "3"}, v.Names)
}
})

t.Run("slice with one value on integer array format", func(t *testing.T) {
var v struct {
Numbers []int `form:"numbers,optional"`
}

r, err := http.NewRequest(
http.MethodGet,
"/a?numbers=",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.Empty(t, v.Numbers)
}
})

t.Run("slice with one value on integer array format", func(t *testing.T) {
var v struct {
Numbers []int `form:"numbers,optional"`
}

r, err := http.NewRequest(
http.MethodGet,
"/a?numbers=&numbers=2",
http.NoBody)
assert.NoError(t, err)
if assert.NoError(t, Parse(r, &v)) {
assert.ElementsMatch(t, []int{2}, v.Numbers)
}
})
}

func TestParseForm_Error(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions rest/httpx/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func GetFormValues(r *http.Request) (map[string]any, error) {
for name, values := range r.Form {
filtered := make([]string, 0, len(values))
for _, v := range values {
if len(v) == 0 {
continue
}

if n < maxFormParamCount {
filtered = append(filtered, v)
n++
Expand Down

0 comments on commit e59038d

Please sign in to comment.