From c5b66903efc847695ebfa1dda35bdf691199989b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sun, 5 Nov 2023 18:45:22 +0800 Subject: [PATCH] chore: a int test fail with validator --- context_request_test.go | 35 +++++++++++++++++++++++++++++++++++ route_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/context_request_test.go b/context_request_test.go index fd817af..9d7cfc3 100644 --- a/context_request_test.go +++ b/context_request_test.go @@ -1176,6 +1176,41 @@ func TestRequest(t *testing.T) { expectCode: http.StatusOK, expectBodyJson: "{\"name\":\"Goravel1\"}", }, + { + name: "GET with int validator and validate request pass", + method: "GET", + url: "/validator/validate-request/success?id=1", + setup: func(method, url string) error { + fiber.Get("/validator/validate-request/success", func(ctx contractshttp.Context) contractshttp.Response { + mockValidation := &validationmocks.Validation{} + mockValidation.On("Rules").Return([]validation.Rule{}).Once() + ValidationFacade = mockValidation + + var createUser CreateUser2 + validateErrors, err := ctx.Request().ValidateRequest(&createUser) + if err != nil { + return ctx.Response().String(http.StatusBadRequest, "Validate error: "+err.Error()) + } + if validateErrors != nil { + return ctx.Response().String(http.StatusBadRequest, fmt.Sprintf("Validate fail: %+v", validateErrors.All())) + } + + return ctx.Response().Success().Json(contractshttp.Json{ + "id": createUser.ID, + }) + }) + + var err error + req, err = http.NewRequest(method, url, nil) + if err != nil { + return err + } + + return nil + }, + expectCode: http.StatusOK, + expectBodyJson: "{\"id\":1}", + }, { name: "GET with validator but validate request fail", method: "GET", diff --git a/route_test.go b/route_test.go index 6eee787..84bf498 100644 --- a/route_test.go +++ b/route_test.go @@ -447,6 +447,34 @@ func (r *CreateUser) PrepareForValidation(ctx contractshttp.Context, data valida return nil } +type CreateUser2 struct { + ID int `form:"id" json:"id"` +} + +func (r *CreateUser2) Authorize(ctx contractshttp.Context) error { + return nil +} + +func (r *CreateUser2) Rules(ctx contractshttp.Context) map[string]string { + return map[string]string{ + "id": "required|uint", + } +} + +func (r *CreateUser2) Messages(ctx contractshttp.Context) map[string]string { + return map[string]string{} +} + +func (r *CreateUser2) Attributes(ctx contractshttp.Context) map[string]string { + return map[string]string{} +} + +func (r *CreateUser2) PrepareForValidation(ctx contractshttp.Context, data validation.Data) error { + _ = data.Set("id", ctx.Request().QueryInt("id")) + + return nil +} + type Unauthorize struct { Name string `form:"name" json:"name"` }