Skip to content

Commit

Permalink
chore: a int test fail with validator
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 5, 2023
1 parent c3c3624 commit c5b6690
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
35 changes: 35 additions & 0 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit c5b6690

Please sign in to comment.