From f4a8f066201f145c24715f9846b05dd357925361 Mon Sep 17 00:00:00 2001 From: Bowen Date: Sun, 8 Sep 2024 11:32:26 +0800 Subject: [PATCH] optimize test cases --- goravel | Bin 0 -> 20480 bytes tests/feature/route_test.go | 132 ++++++++++++++++++------------------ 2 files changed, 66 insertions(+), 66 deletions(-) create mode 100644 goravel diff --git a/goravel b/goravel new file mode 100644 index 0000000000000000000000000000000000000000..c467ea18ad3bcae53343c00f74eeb20ff8027abc GIT binary patch literal 20480 zcmeI)J#X4T7zgk>3;`lhSCt}nd@==;1Ucp{FqJm0L@*>FAe9L@@sU~r(-?!=u~olK z$9|#C-P^fm8cHCE+M!eUpM3ay&%M|`dpTh^KeOCGP=DkP?SOLZDdU{IAYzOqWRA%^ zPl_x==Ld4nZj)HkMnQIDS(vVa5u2tWV=5P$##AOHafKmY>&UEnmj zq3!PSZeU*w#Q1t37kj<2xSj~_a`iCws%2K&Cbg?CEt6K-ragNo_GHh@r!6I}X&V2W zoQ*jd3x7QOh%WV?RZ)7A$aTo|0&yjLI&IY&)mE44W|yj+_DQWN`!vjEo0=ywcPwj9 zb_l1UsqJ6Bwf(eFC}il!d|mBWZQ9vcYG_aG!1n*C>7_61KsY_Q$aQ2PaOD)Y=bXF? zs~?U#Vjx!I^Ro)ido$CE#I?*e|Hxg>5$~r`#T|LQiRVh}p?l@qfjk~7H%Ogk?W|)` zt$Ad=q2fThHlmTEMZ?F?Pd(2e}QUf$PBWEho#Q85bHa;aD><#zSMimv}HUXR4tRybPt ztBB>s`GZQnn9UpI!(#cq!S2TE{Ncg$O5q?|KFk?L;oiaS#PR0uX=z Z1Rwwb2tWV=5LhPx`ToDgXMfP3`~vrw^fCYd literal 0 HcmV?d00001 diff --git a/tests/feature/route_test.go b/tests/feature/route_test.go index 7b4ae28..f407fd4 100644 --- a/tests/feature/route_test.go +++ b/tests/feature/route_test.go @@ -17,7 +17,7 @@ import ( type RouteTestSuite struct { suite.Suite tests.TestCase - http *resty.Request + http *resty.Client } func TestRouteTestSuite(t *testing.T) { @@ -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"), }) } @@ -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") @@ -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()) @@ -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)) @@ -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()) @@ -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()) @@ -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()) - }) - } -}