Skip to content

Commit

Permalink
optimize test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Sep 8, 2024
1 parent 8a8ff4c commit f4a8f06
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 66 deletions.
Binary file added goravel
Binary file not shown.
132 changes: 66 additions & 66 deletions tests/feature/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type RouteTestSuite struct {
suite.Suite
tests.TestCase
http *resty.Request
http *resty.Client
}

func TestRouteTestSuite(t *testing.T) {
Expand All @@ -26,7 +26,7 @@ func TestRouteTestSuite(t *testing.T) {
SetBaseURL(fmt.Sprintf("http://%s:%s",
facades.Config().GetString("APP_HOST"),
facades.Config().GetString("APP_PORT"))).
SetHeader("Content-Type", "application/json").R(),
SetHeader("Content-Type", "application/json"),
})
}

Expand All @@ -39,13 +39,69 @@ func (s *RouteTestSuite) SetupTest() {
func (s *RouteTestSuite) TearDownTest() {
}

func (s *RouteTestSuite) TestLang() {
tests := []struct {
name string
lang string
expectResponse string
}{
{
name: "use default lang",
expectResponse: "{\"current_locale\":\"en\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel Framework\"}",
},
{
name: "lang is cn",
lang: "cn",
expectResponse: "{\"current_locale\":\"cn\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel 框架\"}",
},
}

for _, test := range tests {
s.Run(test.name, func() {
resp, err := s.http.R().Get(fmt.Sprintf("/lang?lang=%s", test.lang))

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal(test.expectResponse, resp.String())
})
}
}

func (s *RouteTestSuite) TestThrottle() {
tests := []struct {
name string
expectStatusCode int
}{
{
name: "no throttle",
expectStatusCode: 200,
},
{
name: "throttle",
expectStatusCode: 429,
},
}

for _, test := range tests {
s.Run(test.name, func() {
var resp *resty.Response
var err error
for i := 0; i < 5; i++ {
resp, err = s.http.R().Get("/jwt/login")
s.Require().NoError(err)
}
s.Equal(test.expectStatusCode, resp.StatusCode())
})
}
}

func (s *RouteTestSuite) TestUsers() {
// Add a user
var createdUser struct {
User models.User
}

resp, err := s.http.SetResult(&createdUser).SetBody(map[string]string{
resp, err := s.http.R().SetResult(&createdUser).SetBody(map[string]string{
"name": "Goravel",
"avatar": "https://goravel.dev/avatar.png",
}).Post("users")
Expand All @@ -60,7 +116,7 @@ func (s *RouteTestSuite) TestUsers() {
var users struct {
Users []models.User
}
resp, err = s.http.SetResult(&users).Get("users")
resp, err = s.http.R().SetResult(&users).Get("users")

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
Expand All @@ -74,7 +130,7 @@ func (s *RouteTestSuite) TestUsers() {
User models.User
}

resp, err = s.http.SetResult(&updatedUser).SetBody(map[string]string{
resp, err = s.http.R().SetResult(&updatedUser).SetBody(map[string]string{
"name": "Framework",
}).Put(fmt.Sprintf("users/%d", createdUser.User.ID))

Expand All @@ -88,7 +144,7 @@ func (s *RouteTestSuite) TestUsers() {
var user struct {
User models.User
}
resp, err = s.http.SetResult(&user).Get(fmt.Sprintf("users/%d", createdUser.User.ID))
resp, err = s.http.R().SetResult(&user).Get(fmt.Sprintf("users/%d", createdUser.User.ID))

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
Expand All @@ -97,55 +153,27 @@ func (s *RouteTestSuite) TestUsers() {
s.Equal("https://goravel.dev/avatar.png", user.User.Avatar)

// Delete the User
resp, err = s.http.Delete(fmt.Sprintf("users/%d", createdUser.User.ID))
resp, err = s.http.R().Delete(fmt.Sprintf("users/%d", createdUser.User.ID))

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"rows_affected\":1}", resp.String())

// Get Users
resp, err = s.http.Get("users")
resp, err = s.http.R().Get("users")

s.Require().NoError(err)
s.Require().Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"users\":[]}", resp.String())
}

func (s *RouteTestSuite) TestLang() {
tests := []struct {
name string
lang string
expectResponse string
}{
{
name: "use default lang",
expectResponse: "{\"current_locale\":\"en\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel Framework\"}",
},
{
name: "lang is cn",
lang: "cn",
expectResponse: "{\"current_locale\":\"cn\",\"fallback\":\"Goravel 是一个基于 Go 语言的 Web 开发框架\",\"name\":\"Goravel 框架\"}",
},
}

for _, test := range tests {
s.Run(test.name, func() {
resp, err := s.http.Get(fmt.Sprintf("/lang?lang=%s", test.lang))

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal(test.expectResponse, resp.String())
})
}
}

func (s *RouteTestSuite) TestValidationJson() {
payload := strings.NewReader(`{
"name": "Goravel",
"date": "2024-07-08 18:33:32"
}`)

resp, err := s.http.SetBody(payload).Post("/validation/json")
resp, err := s.http.R().SetBody(payload).Post("/validation/json")

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
Expand All @@ -160,37 +188,9 @@ func (s *RouteTestSuite) TestValidationRequest() {
"scores": [1, 2]
}`)

resp, err := s.http.SetBody(payload).Post("/validation/request")
resp, err := s.http.R().SetBody(payload).Post("/validation/request")

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\",\"scores\":[1,2],\"tags\":[\"tag1\",\"tag2\"]}", resp.String())
}

func (s *RouteTestSuite) TestThrottle() {
tests := []struct {
name string
expectStatusCode int
}{
{
name: "no throttle",
expectStatusCode: 200,
},
{
name: "throttle",
expectStatusCode: 429,
},
}

for _, test := range tests {
s.Run(test.name, func() {
var resp *resty.Response
var err error
for i := 0; i < 5; i++ {
resp, err = s.http.Get("/jwt/login")
s.Require().NoError(err)
}
s.Equal(test.expectStatusCode, resp.StatusCode())
})
}
}

0 comments on commit f4a8f06

Please sign in to comment.