Skip to content

Commit

Permalink
[base] use 400 status for parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Dec 5, 2024
1 parent cf6ceae commit fc29c5e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
6 changes: 3 additions & 3 deletions internal/sms-gateway/handlers/base/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ type Handler struct {

func (h *Handler) BodyParserValidator(c *fiber.Ctx, out any) error {
if err := c.BodyParser(out); err != nil {
return fmt.Errorf("can't parse body: %w", err)
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Can't parse body: %s", err.Error()))
}

return h.ValidateStruct(out)
}

func (h *Handler) QueryParserValidator(c *fiber.Ctx, out any) error {
if err := c.QueryParser(out); err != nil {
return fmt.Errorf("can't parse query: %w", err)
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Can't parse query: %s", err.Error()))
}

return h.ValidateStruct(out)
}

func (h *Handler) ParamsParserValidator(c *fiber.Ctx, out any) error {
if err := c.ParamsParser(out); err != nil {
return fmt.Errorf("can't parse params: %w", err)
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Can't parse params: %s", err.Error()))
}

return h.ValidateStruct(out)
Expand Down
39 changes: 16 additions & 23 deletions internal/sms-gateway/handlers/base/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package base
package base_test

import (
"bytes"
Expand All @@ -8,53 +8,46 @@ import (
"net/http/httptest"
"testing"

"github.com/android-sms-gateway/server/internal/sms-gateway/handlers/base"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

type TestRequestBody struct {
type testRequestBody struct {
Name string `json:"name" validate:"required"`
Age int `json:"age" validate:"required"`
}

type TestRequestBodyNoValidate struct {
type testRequestBodyNoValidate struct {
Name string `json:"name" validate:"required"`
Age int `json:"age" validate:"required"`
}

func (t *TestRequestBody) Validate() error {
func (t *testRequestBody) Validate() error {
if t.Age < 18 {
return fmt.Errorf("must be at least 18 years old")
}
return nil
}

type TestQueryParams struct {
Page int `query:"page" validate:"required"`
}

type TestURLParams struct {
ID string `params:"id" validate:"required,uuid"`
}

func TestHandler_BodyParserValidator(t *testing.T) {
logger := zaptest.NewLogger(t)
validate := validator.New()

handler := &Handler{
handler := &base.Handler{
Logger: logger,
Validator: validate,
}

app := fiber.New()
app.Post("/test", func(c *fiber.Ctx) error {
var body TestRequestBody
var body testRequestBody
return handler.BodyParserValidator(c, &body)
})
app.Post("/test2", func(c *fiber.Ctx) error {
var body TestRequestBodyNoValidate
var body testRequestBodyNoValidate
return handler.BodyParserValidator(c, &body)
})

Expand All @@ -67,32 +60,32 @@ func TestHandler_BodyParserValidator(t *testing.T) {
{
description: "Valid request body",
path: "/test",
payload: &TestRequestBody{Name: "John Doe", Age: 25},
payload: &testRequestBody{Name: "John Doe", Age: 25},
expectedStatus: fiber.StatusOK,
},
{
description: "Invalid request body - missing name",
path: "/test",
payload: &TestRequestBody{Age: 25},
payload: &testRequestBody{Age: 25},
expectedStatus: fiber.StatusBadRequest,
},
{
description: "Invalid request body - age too low",
path: "/test",
payload: &TestRequestBody{Name: "John Doe", Age: 17},
payload: &testRequestBody{Name: "John Doe", Age: 17},
expectedStatus: fiber.StatusBadRequest,
},
{
description: "Valid request body - no validation",
path: "/test2",
payload: &TestRequestBodyNoValidate{Name: "John Doe", Age: 17},
payload: &testRequestBodyNoValidate{Name: "John Doe", Age: 17},
expectedStatus: fiber.StatusOK,
},
{
description: "No request body",
path: "/test",
payload: nil,
expectedStatus: fiber.StatusUnprocessableEntity,
expectedStatus: fiber.StatusBadRequest,
},
}

Expand Down Expand Up @@ -134,7 +127,7 @@ func TestHandler_QueryParserValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Handler{
h := &base.Handler{
Logger: tt.fields.Logger,
Validator: tt.fields.Validator,
}
Expand Down Expand Up @@ -164,7 +157,7 @@ func TestHandler_ParamsParserValidator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Handler{
h := &base.Handler{
Logger: tt.fields.Logger,
Validator: tt.fields.Validator,
}
Expand Down Expand Up @@ -193,7 +186,7 @@ func TestHandler_validateStruct(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := &Handler{
h := &base.Handler{
Logger: tt.fields.Logger,
Validator: tt.fields.Validator,
}
Expand Down

0 comments on commit fc29c5e

Please sign in to comment.