Skip to content

Commit

Permalink
optimize test
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Sep 3, 2024
1 parent b578b94 commit ad7fbe5
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 91 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ English | [中文](./README_zh.md)

## About Goravel

Goravel is a web application framework with complete functions and good scalability. As a starting scaffolding to help
Gopher quickly build their own applications.
Goravel is a web application framework with complete functions and good scalability. As a starting scaffolding to help Gopher quickly build their own applications.

The framework style is consistent with [Laravel](https://github.com/laravel/laravel), let Phper don't need to learn a
new framework, but also happy to play around Golang! Tribute Laravel!
The framework style is consistent with [Laravel](https://github.com/laravel/laravel), let Phper don't need to learn a new framework, but also happy to play around Golang! Tribute Laravel!

Welcome to star, PR and issues!

Expand Down
4 changes: 2 additions & 2 deletions app/http/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package http

import (
"github.com/goravel/framework/contracts/http"
frameworkmiddleware "github.com/goravel/framework/http/middleware"
httpmiddleware "github.com/goravel/framework/http/middleware"
"github.com/goravel/framework/session/middleware"
)

Expand All @@ -13,7 +13,7 @@ type Kernel struct {
// These middleware are run during every request to your application.
func (kernel Kernel) Middleware() []http.Middleware {
return []http.Middleware{
frameworkmiddleware.Throttle("global"),
httpmiddleware.Throttle("global"),
middleware.StartSession(),
}
}
1 change: 0 additions & 1 deletion app/providers/route_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func (receiver *RouteServiceProvider) Boot(app foundation.Application) {

// Add routes
routes.Web()
routes.Api()
}

func (receiver *RouteServiceProvider) configureRateLimiting() {
Expand Down
34 changes: 0 additions & 34 deletions routes/api.go

This file was deleted.

24 changes: 24 additions & 0 deletions routes/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/facades"
httpmiddleware "github.com/goravel/framework/http/middleware"
"github.com/spf13/cast"

"goravel/app/http/controllers"
Expand All @@ -17,6 +18,29 @@ func Web() {
})
})

// DB
dbController := controllers.NewDBController()
facades.Route().Get("/db", dbController.Index)

// Websocket
websocketController := controllers.NewWebsocketController()
facades.Route().Get("/ws", websocketController.Server)

// Validation
validationController := controllers.NewValidationController()
facades.Route().Post("/validation/json", validationController.Json)
facades.Route().Post("/validation/request", validationController.Request)
facades.Route().Post("/validation/form", validationController.Form)

// JWT
jwtController := controllers.NewJwtController()
facades.Route().Middleware(httpmiddleware.Throttle("login")).Get("/jwt/login", jwtController.Login)
facades.Route().Middleware(middleware.Jwt()).Get("/jwt", jwtController.Index)

// Swagger
swaggerController := controllers.NewSwaggerController()
facades.Route().Get("/swagger/*any", swaggerController.Index)

// Single Page Application
// 1. Add your single page application to `resources/views/*`
// 2. Add route to `/route/web.go`, needs to contain your home page and static routes
Expand Down
16 changes: 7 additions & 9 deletions tests/controllers/lang_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package controllers

import (
"fmt"
"io"
"net/http"
"testing"

"github.com/stretchr/testify/suite"

"goravel/tests"
"goravel/tests/utils"
)

/*
Expand All @@ -34,6 +34,8 @@ func (s *LangControllerTestSuite) TearDownTest() {
}

func (s *LangControllerTestSuite) TestIndex() {
client := utils.Http()

tests := []struct {
name string
lang string
Expand All @@ -52,15 +54,11 @@ func (s *LangControllerTestSuite) TestIndex() {

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

defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
resp, err := client.R().Get(fmt.Sprintf("/lang?lang=%s", test.lang))

s.Require().NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
s.Equal(test.expectResponse, string(body))
s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal(test.expectResponse, resp.String())
})
}
}
28 changes: 13 additions & 15 deletions tests/controllers/validation_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package controllers

import (
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/suite"

"goravel/tests"
"goravel/tests/utils"
)

/*
Expand Down Expand Up @@ -42,13 +42,12 @@ func (s *ValidationControllerTestSuite) TestJson() {
"name": "Goravel",
"date": "2024-07-08 18:33:32"
}`)
resp, err := http.Post(route("/validation/json"), "application/json", payload)
s.Require().NoError(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
s.Require().NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
s.Equal(string(body), "{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\"}")

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

s.NoError(err)
s.Equal(http.StatusOK, resp.StatusCode())
s.Equal("{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\"}", resp.String())
}

func (s *ValidationControllerTestSuite) TestRequest() {
Expand All @@ -58,11 +57,10 @@ func (s *ValidationControllerTestSuite) TestRequest() {
"tags": ["tag1", "tag2"],
"scores": [1, 2]
}`)
resp, err := http.Post(route("/validation/request"), "application/json", payload)
s.Require().NoError(err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
s.Require().NoError(err)
s.Equal(http.StatusOK, resp.StatusCode)
s.Equal(string(body), "{\"date\":\"2024-07-08 18:33:32\",\"name\":\"Goravel\",\"scores\":[1,2],\"tags\":[\"tag1\",\"tag2\"]}")

resp, err := utils.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())
}
6 changes: 0 additions & 6 deletions tests/feature/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ func TestMain(m *testing.M) {
panic(err)
}

go func() {
if err := facades.Route().Run(); err != nil {
facades.Log().Errorf("Route run error: %v", err)
}
}()

exit := m.Run()

file.Remove("storage")
Expand Down
9 changes: 2 additions & 7 deletions tests/feature/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"net/http"
"testing"

"github.com/go-resty/resty/v2"
"github.com/goravel/framework/facades"
"github.com/stretchr/testify/suite"

"goravel/app/models"
"goravel/tests"
"goravel/tests/utils"
)

type RouteTestSuite struct {
Expand All @@ -32,11 +31,7 @@ func (s *RouteTestSuite) TearDownTest() {
}

func (s *RouteTestSuite) TestUsers() {
client := resty.New().
SetBaseURL(fmt.Sprintf("http://%s:%s",
facades.Config().GetString("APP_HOST"),
facades.Config().GetString("APP_PORT"))).
SetHeader("Content-Type", "application/json")
client := utils.Http()

// Add a user
var createdUser struct {
Expand Down
25 changes: 16 additions & 9 deletions tests/middlewares/main_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package middlewares

import (
"fmt"
"os"
"testing"

"github.com/goravel/framework/facades"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/str"
)

func TestMain(m *testing.M) {
m.Run()
database, err := facades.Testing().Docker().Database()
if err != nil {
panic(err)
}

if err := database.Build(); err != nil {
panic(err)
}

exit := m.Run()

file.Remove("storage")
}

func route(path string) string {
return fmt.Sprintf("http://%s:%s/%s",
facades.Config().GetString("APP_HOST"),
facades.Config().GetString("APP_PORT"),
str.Of(path).LTrim("/").String())
if err := database.Clear(); err != nil {
panic(err)
}

os.Exit(exit)
}
11 changes: 7 additions & 4 deletions tests/middlewares/throttle_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package middlewares

import (
"net/http"
"testing"

"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/suite"

"goravel/tests"
"goravel/tests/utils"
)

type ThrottleTestSuite struct {
Expand All @@ -27,6 +28,8 @@ func (s *ThrottleTestSuite) TearDownTest() {
}

func (s *ThrottleTestSuite) TestThrottle() {
client := utils.Http()

tests := []struct {
name string
expectStatusCode int
Expand All @@ -43,13 +46,13 @@ func (s *ThrottleTestSuite) TestThrottle() {

for _, test := range tests {
s.Run(test.name, func() {
var resp *http.Response
var resp *resty.Response
var err error
for i := 0; i < 5; i++ {
resp, err = http.Get(route("/jwt/login"))
resp, err = client.R().Get("/jwt/login")
s.Require().NoError(err)
}
s.Equal(test.expectStatusCode, resp.StatusCode)
s.Equal(test.expectStatusCode, resp.StatusCode())
})
}
}
16 changes: 16 additions & 0 deletions tests/utils/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"fmt"

"github.com/go-resty/resty/v2"
"github.com/goravel/framework/facades"
)

func Http() *resty.Client {
return resty.New().
SetBaseURL(fmt.Sprintf("http://%s:%s",
facades.Config().GetString("APP_HOST"),
facades.Config().GetString("APP_PORT"))).
SetHeader("Content-Type", "application/json")
}

0 comments on commit ad7fbe5

Please sign in to comment.