service/tests/gateway_test.go

535 lines
11 KiB
Go
Raw Permalink Normal View History

2024-10-18 17:54:37 +08:00
package service_test
import (
"fmt"
"net/http"
"testing"
"apigo.cc/gojs/service"
"github.com/ssgo/s"
"github.com/ssgo/u"
)
func TestRedirect(t *testing.T) {
type TestCase struct {
Url string
CheckToApp string
CheckToPath string
CheckOK bool
}
type TestSet struct {
Key string
Value string
Cases []TestCase
}
testSets := []TestSet{
{
Key: "https://abc.com:443/user/",
Value: "user",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com/user/",
Value: "user",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "abc.com/user/",
Value: "user",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "/user/",
Value: "user",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com:443/user/(.*)",
Value: "user/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com/user/(.*)",
Value: "user/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "abc.com/user/(.*)",
Value: "user/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "/user/(.*)",
Value: "user/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "user",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com:443/user/",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com/user/",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "abc.com/user/",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "/user/",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com:443/user/(.*)",
Value: "https://def.com/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com/user/(.*)",
Value: "https://def.com/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "abc.com/user/(.*)",
Value: "https://def.com/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "/user/(.*)",
Value: "https://def.com/$1",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/login",
CheckOK: true,
},
},
},
{
Key: "abc.com:443",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
},
},
{
Key: "abc.com",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
},
},
{
Key: "/",
Value: "https://def.com",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "https://def.com",
CheckToPath: "/user/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com:443",
Value: "/def/",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
},
},
{
Key: "abc.com:443",
Value: "/def/",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
},
},
{
Key: "https://abc.com",
Value: "/def/",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
},
},
{
Key: "abc.com",
Value: "/def/",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
},
},
{
Key: "/",
Value: "/def/",
Cases: []TestCase{
{
Url: "https://abc.com:443/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
{
Url: "https://abc.com/user/login",
CheckToApp: "",
CheckToPath: "/def/user/login",
CheckOK: true,
},
},
},
{
Key: "/aaa/111.txt",
Value: "/bbb/222/.txt",
Cases: []TestCase{
{
Url: "https://abc.com:443/aaa/111.txt",
CheckToApp: "",
CheckToPath: "/bbb/222/.txt",
CheckOK: true,
},
{
Url: "https://abc.com/aaa/111.txt",
CheckToApp: "",
CheckToPath: "/bbb/222/.txt",
CheckOK: true,
},
},
},
}
makeRequest := func(url string) *s.Request {
req, _ := http.NewRequest("GET", url, nil)
return &s.Request{Request: req}
}
// s.ServerLogger = log.NewLogger(log.Config{File: "/dev/null"})
for _, testSet := range testSets {
service.ClearRewritesAndProxies()
service.UpdateProxy(map[string]string{testSet.Key: testSet.Value})
for _, testCase := range testSet.Cases {
toApp, toPath, ok := service.MatchProxy(makeRequest(testCase.Url))
if toApp != testCase.CheckToApp || toPath != testCase.CheckToPath || ok != testCase.CheckOK {
c1 := u.TextGreen
c2 := u.TextGreen
c3 := u.TextGreen
if toApp != testCase.CheckToApp {
c1 = u.TextRed
}
if toPath != testCase.CheckToPath {
c2 = u.TextRed
}
if ok != testCase.CheckOK {
c3 = u.TextRed
}
fmt.Println(u.BRed("test failed"), testCase.Url, u.Green(testCase.CheckToApp), u.Color(toApp, c1, u.BgNone), u.Green(testCase.CheckToPath), u.Color(toPath, c2, u.BgNone), u.Green(testCase.CheckOK), u.Color(ok, c3, u.BgNone))
} else {
fmt.Println(u.BGreen("test success"), testCase.Url, u.Green(testCase.CheckToApp), u.BGreen(toApp), u.Green(testCase.CheckToPath), u.BGreen(toPath), u.Green(testCase.CheckOK), u.BGreen(ok))
}
}
}
service.ClearRewritesAndProxies()
}