Skip to content

Commit

Permalink
fix: [#515] bugs and add more tests (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Dec 14, 2024
1 parent dce7445 commit d453e9b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
8 changes: 6 additions & 2 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func (r *ContextRequest) InputArray(key string, defaultValue ...[]string) []stri
}

if value, exist := r.instance.GetQueryArray(key); exist {
if len(value) == 1 && value[0] == "" {
return []string{}
}

return value
}

Expand All @@ -322,8 +326,8 @@ func (r *ContextRequest) InputMap(key string, defaultValue ...map[string]string)
return cast.ToStringMapString(valueFromHttpBody)
}

if value, exist := r.instance.GetQueryMap(key); exist {
return value
if _, exist := r.instance.GetQuery(key); exist {
return r.instance.QueryMap(key)
}

if len(defaultValue) > 0 {
Expand Down
63 changes: 58 additions & 5 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func (s *ContextRequestSuite) TestInput_Route() {
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInput_Empty() {
func (s *ContextRequestSuite) TestInput_KeyInBodyIsEmpty() {
s.route.Post("/input/empty/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id1": ctx.Request().Input("id1", "a"),
Expand All @@ -669,6 +669,23 @@ func (s *ContextRequestSuite) TestInput_Empty() {
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInput_KeyInQueryIsEmpty() {
s.route.Post("/input/empty/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"id1": ctx.Request().Input("id1", "a"),
})
})

req, err := http.NewRequest("POST", "/input/empty/1?id1=", nil)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

s.Equal("{\"id1\":\"\"}", body)
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInput_Default() {
s.route.Post("/input/default/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
Expand Down Expand Up @@ -785,7 +802,7 @@ func (s *ContextRequestSuite) TestInputArray_Default() {
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInputArray_Empty() {
func (s *ContextRequestSuite) TestInputArray_KeyInBodyIsEmpty() {
s.route.Post("/input-array/empty/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"name": ctx.Request().InputArray("name", []string{"a", "b"}),
Expand All @@ -795,7 +812,24 @@ func (s *ContextRequestSuite) TestInputArray_Empty() {
payload := strings.NewReader(`{
"name": []
}`)
req, err := http.NewRequest("POST", "/input-array/empty/1?id=2", payload)
req, err := http.NewRequest("POST", "/input-array/empty/1", payload)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

s.Equal("{\"name\":[]}", body)
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInputArray_KeyInQueryIsEmpty() {
s.route.Post("/input-array/empty/{id}", func(ctx contractshttp.Context) contractshttp.Response {
return ctx.Response().Success().Json(contractshttp.Json{
"name": ctx.Request().InputArray("name", []string{"a", "b"}),
})
})

req, err := http.NewRequest("POST", "/input-array/empty/1?name=", nil)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -895,7 +929,7 @@ func (s *ContextRequestSuite) TestInputMap_Default() {
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInputMap_Empty() {
func (s *ContextRequestSuite) TestInputMap_KeyInBodyIsEmpty() {
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", map[string]string{
Expand All @@ -907,7 +941,26 @@ func (s *ContextRequestSuite) TestInputMap_Empty() {
payload := strings.NewReader(`{
"name": {}
}`)
req, err := http.NewRequest("POST", "/input-map/empty/1?id=2", payload)
req, err := http.NewRequest("POST", "/input-map/empty/1", payload)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

s.Equal("{\"name\":{}}", body)
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestInputMap_KeyInQueryIsEmpty() {
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", map[string]string{
"a": "b",
}),
})
})

req, err := http.NewRequest("POST", "/input-map/empty/1?name=", nil)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
Expand Down

0 comments on commit d453e9b

Please sign in to comment.