Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
qydysky committed Oct 31, 2024
1 parent c3600b6 commit 7b52734
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
136 changes: 136 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 7b52734

Please sign in to comment.