[frontend-gray] 重构业务逻辑,对于微前端和多版本支持更加友好 (#2011)

This commit is contained in:
mamba
2025-04-11 10:35:18 +08:00
committed by GitHub
parent 2c3188dad7
commit d1700009e8
10 changed files with 504 additions and 495 deletions

View File

@@ -3,12 +3,14 @@ package util
import (
"testing"
"github.com/bmatcuk/doublestar/v4"
"github.com/alibaba/higress/plugins/wasm-go/extensions/frontend-gray/config"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)
func TestExtractCookieValueByKey(t *testing.T) {
func TestGetCookieValue(t *testing.T) {
var tests = []struct {
cookie, cookieKey, output string
}{
@@ -21,7 +23,7 @@ func TestExtractCookieValueByKey(t *testing.T) {
for _, test := range tests {
testName := test.cookie
t.Run(testName, func(t *testing.T) {
output := ExtractCookieValueByKey(test.cookie, test.cookieKey)
output := GetCookieValue(test.cookie, test.cookieKey)
assert.Equal(t, test.output, output)
})
}
@@ -106,7 +108,7 @@ func TestPrefixFileRewrite(t *testing.T) {
}
}
func TestIsPageRequest(t *testing.T) {
func TestCheckIsHtmlRequest(t *testing.T) {
var tests = []struct {
p string
output bool
@@ -121,30 +123,11 @@ func TestIsPageRequest(t *testing.T) {
for _, test := range tests {
testPath := test.p
t.Run(testPath, func(t *testing.T) {
output := IsPageRequest(testPath)
output := CheckIsHtmlRequest(testPath)
assert.Equal(t, test.output, output)
})
}
}
func TestFilterGrayWeight(t *testing.T) {
var tests = []struct {
name string
input string
}{
{"demo", `{"grayKey":"userId","rules":[{"name":"inner-user","grayKeyValue":["00000001","00000005"]},{"name":"beta-user","grayKeyValue":["noah","00000003"],"grayTagKey":"level","grayTagValue":["level3","level5"]}],"rewrite":{"host":"frontend-gray-cn-shanghai.oss-cn-shanghai-internal.aliyuncs.com","notFoundUri":"/mfe/app1/dev/404.html","indexRouting":{"/app1":"/mfe/app1/{version}/index.html","/":"/mfe/app1/{version}/index.html"},"fileRouting":{"/":"/mfe/app1/{version}","/app1":"/mfe/app1/{version}"}},"baseDeployment":{"version":"dev"},"grayDeployments":[{"name":"beta-user","version":"0.0.1","backendVersion":"beta","enabled":true,"weight":50}]}`},
}
for _, test := range tests {
testName := test.name
t.Run(testName, func(t *testing.T) {
grayConfig := &config.GrayConfig{}
config.JsonToGrayConfig(gjson.Parse(test.input), grayConfig)
result := FilterGrayWeight(grayConfig, "base", "1.0.1", "192.168.1.1")
t.Logf("result-----: %v", result)
})
}
}
func TestReplaceHtml(t *testing.T) {
var tests = []struct {
name string
@@ -158,8 +141,26 @@ func TestReplaceHtml(t *testing.T) {
t.Run(testName, func(t *testing.T) {
grayConfig := &config.GrayConfig{}
config.JsonToGrayConfig(gjson.Parse(test.input), grayConfig)
result := InjectContent(grayConfig.Html, grayConfig.Injection)
result := InjectContent(grayConfig.Html, grayConfig.Injection, "")
t.Logf("result-----: %v", result)
})
}
}
func TestIsIndexRequest(t *testing.T) {
var tests = []struct {
name string
input string
output bool
}{
{"/api/user.json", "/api/**", true},
{"/api/blade-auth/oauth/captcha", "/api/**", true},
}
for _, test := range tests {
testName := test.name
t.Run(testName, func(t *testing.T) {
matchResult, _ := doublestar.Match(test.input, testName)
assert.Equal(t, test.output, matchResult)
})
}
}