From 46517cb7263208c098f5aec759261bdc25204a10 Mon Sep 17 00:00:00 2001 From: Bowen Date: Fri, 13 Dec 2024 23:25:12 +0800 Subject: [PATCH] add test --- context_request.go | 8 ++++++-- context_request_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/context_request.go b/context_request.go index 6d6437c..a201a20 100644 --- a/context_request.go +++ b/context_request.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "github.com/goravel/framework/support/str" "io" "net/http" "reflect" @@ -21,6 +20,7 @@ import ( contractsvalidate "github.com/goravel/framework/contracts/validation" "github.com/goravel/framework/filesystem" "github.com/goravel/framework/support/json" + "github.com/goravel/framework/support/str" "github.com/goravel/framework/validation" "github.com/spf13/cast" ) @@ -295,7 +295,11 @@ func (r *ContextRequest) Input(key string, defaultValue ...string) string { func (r *ContextRequest) InputArray(key string, defaultValue ...[]string) []string { if valueFromHttpBody := r.getValueFromHttpBody(key); valueFromHttpBody != nil { - return cast.ToStringSlice(valueFromHttpBody) + if value := cast.ToStringSlice(valueFromHttpBody); value == nil { + return []string{} + } else { + return value + } } if value, exist := r.instance.GetQueryArray(key); exist { diff --git a/context_request_test.go b/context_request_test.go index 33ab591..ce85848 100644 --- a/context_request_test.go +++ b/context_request_test.go @@ -898,12 +898,14 @@ func (s *ContextRequestSuite) TestInputMap_Default() { func (s *ContextRequestSuite) TestInputMap_Empty() { s.route.Post("/input-map/empty/{id}", func(ctx contractshttp.Context) contractshttp.Response { return ctx.Response().Success().Json(contractshttp.Json{ - "name": ctx.Request().InputMap("name"), + "name": ctx.Request().InputMap("name", map[string]string{ + "a": "b", + }), }) }) payload := strings.NewReader(`{ - "id": {"a": "3", "b": "4"} + "name": {} }`) req, err := http.NewRequest("POST", "/input-map/empty/1?id=2", payload) s.Require().Nil(err)