diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 932686c..e2af1da 100755 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,6 +41,7 @@ jobs: run: | cd main go get . + go test -count 1 -timeout 30s -v . CGO_ENABLED=0 go build -pgo=auto -v -buildmode=exe main.go - name: zip diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..7afa772 --- /dev/null +++ b/config_test.go @@ -0,0 +1,136 @@ +package front + +import ( + "context" + "net/http" + "testing" + "time" + + "github.com/qydysky/front/filiter" + plog "github.com/qydysky/part/log" + reqf "github.com/qydysky/part/reqf" +) + +var logger = plog.New(plog.Config{ + Stdout: true, + Prefix_string: map[string]struct{}{ + `T:`: plog.On, + `I:`: plog.On, + `W:`: plog.On, + `E:`: plog.On, + }, +}) + +func Test_Uri(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + conf := &Config{ + Addr: "127.0.0.1:19000", + Routes: []Route{ + { + Path: []string{"/"}, + PathAdd: true, + Setting: Setting{ + Filiter: filiter.Filiter{ + ReqUri: filiter.Uri{ + AccessRule: "!{go}", + Items: map[string]string{ + "go": "\\.go$", + }, + }, + }, + }, + Backs: []Back{ + { + Name: "1", + To: "./", + Weight: 1, + }, + }, + }, + }, + } + + go conf.Run(ctx, logger) + + time.Sleep(time.Second) + + r := reqf.New() + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/config_test.go", + }); e != nil { + if r.Response.StatusCode != http.StatusForbidden { + t.Fail() + } + } else { + t.Fail() + } + + conf.Routes[0].Setting.Filiter.ReqUri.AccessRule = "{go}" + + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/config_test.go", + }); e != nil { + t.Fail() + } +} + +func Test_Back(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + conf := &Config{ + Addr: "127.0.0.1:19000", + Routes: []Route{ + { + Path: []string{"/"}, + PathAdd: true, + Backs: []Back{}, + }, + }, + } + + go conf.Run(ctx, logger) + + time.Sleep(time.Second) + + r := reqf.New() + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/config_test.go", + }); e != nil { + if r.Response.StatusCode != http.StatusNotFound { + t.Fail() + } + } else { + t.Fail() + } + + conf.Routes[0].Backs = append(conf.Routes[0].Backs, + Back{ + Name: "1", + To: "./", + Weight: 1, + }, + ) + conf.SwapSign(ctx, logger) + + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/config_test.go", + }); e != nil { + t.Fail() + } + + conf.Routes[0].Backs = conf.Routes[0].Backs[:0] + conf.SwapSign(ctx, logger) + + if e := r.Reqf(reqf.Rval{ + Url: "http://127.0.0.1:19000/config_test.go", + }); e != nil { + if r.Response.StatusCode != http.StatusNotFound { + t.Fail() + } + } else { + t.Fail() + } +} diff --git a/go.mod b/go.mod index b880658..87160d2 100755 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/go-ole/go-ole v1.3.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/qydysky/brotli v0.0.0-20240828134800-e9913a6e7ed9 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/numcpus v0.8.0 // indirect diff --git a/go.sum b/go.sum index b15cda2..889b80a 100755 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZ github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY= github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=