Repository: zeromicro/go-zero
Pull request #4031 (2024-03-30 04:34:40)

chore: fix test failure

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/4031?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.13%. Comparing base [(`2629636`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/2629636f648bd829f5643c63b5359292be565eac?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) to head [(`3cc8d5e`)](https://app.codecov.io/gh/zeromicro/go-zero/pull/4031?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro).
> Report is 37 commits behind head on master.

<details><summary>Additional details and impacted files</summary>



| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/4031?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [rest/httpx/util.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/4031?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-cmVzdC9odHRweC91dGlsLmdv) | `100.00% <100.00%> (ø)` | |

... and [252 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/4031/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
diff --git a/rest/httpx/util.go b/rest/httpx/util.go
index 9301522c..48d76592 100644
--- a/rest/httpx/util.go
+++ b/rest/httpx/util.go
@@ -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)
 		}
 	}
 


Test patch:
diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go
index 3c4d8d16..157b1dbb 100644
--- a/rest/httpx/requests_test.go
+++ b/rest/httpx/requests_test.go
@@ -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) {