mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-05-19 11:45:18 +08:00
实现gzip
This commit is contained in:
@@ -2,8 +2,6 @@ package serverconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"github.com/iwind/TeaGo/utils/string"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -18,21 +16,19 @@ func TestHTTPRewriteRule(t *testing.T) {
|
||||
a.IsNil(rule.Init())
|
||||
|
||||
{
|
||||
_, _, b := rule.Match("/hello/worl", func(source string) string {
|
||||
replace, _, b := rule.MatchRequest("/hello/worl", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsFalse(b)
|
||||
a.Log("proxy:", rule.TargetProxy())
|
||||
a.Log("url:", rule.TargetURL())
|
||||
a.Log("url:", replace)
|
||||
}
|
||||
|
||||
{
|
||||
_, _, b := rule.Match("/hello/world", func(source string) string {
|
||||
replace, _, b := rule.MatchRequest("/hello/world", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsTrue(b)
|
||||
a.Log("proxy:", rule.TargetProxy())
|
||||
a.Log("url:", rule.TargetURL())
|
||||
a.Log("url:", replace)
|
||||
}
|
||||
|
||||
{
|
||||
@@ -43,7 +39,7 @@ func TestHTTPRewriteRule(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u, _, b := r.Match("/hello", func(source string) string {
|
||||
u, _, b := r.MatchRequest("/hello", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.Log(b)
|
||||
@@ -63,7 +59,7 @@ func TestRewriteRule_NamedMatch(t *testing.T) {
|
||||
before := time.Now()
|
||||
count := 100
|
||||
for i := 0; i < count; i++ {
|
||||
s, _, b := r.Match("/hello/world/ni", func(source string) string {
|
||||
s, _, b := r.MatchRequest("/hello/world/ni", func(source string) string {
|
||||
return source
|
||||
})
|
||||
if i == 0 {
|
||||
@@ -77,53 +73,6 @@ func TestRewriteRule_NamedMatch(t *testing.T) {
|
||||
t.Log(float64(count) / (time.Since(before).Seconds()))
|
||||
}
|
||||
|
||||
func TestRewriteRule_NamedMatchConcurrent(t *testing.T) {
|
||||
r := &HTTPRewriteRule{}
|
||||
r.Replace = "http://127.0.0.1/${1}/${last}/${ni}"
|
||||
r.Pattern = "/(\\w+)/(?P<last>\\w+)/(?P<ni>\\w+)"
|
||||
err := r.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
threads := 1000
|
||||
count := 1000
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(threads * count)
|
||||
fails := 0
|
||||
var locker sync.Mutex
|
||||
for i := 0; i < threads; i++ {
|
||||
go func() {
|
||||
for j := 0; j < count; j++ {
|
||||
func() {
|
||||
defer wg.Done()
|
||||
|
||||
var randomString = stringutil.Rand(16)
|
||||
|
||||
replace, _, b := r.Match("/hello/world/"+randomString, func(source string) string {
|
||||
return source
|
||||
})
|
||||
if !b {
|
||||
locker.Lock()
|
||||
fails++
|
||||
locker.Unlock()
|
||||
} else if replace != "http://127.0.0.1/hello/world/"+randomString {
|
||||
locker.Lock()
|
||||
fails++
|
||||
locker.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if fails > 0 {
|
||||
t.Log("fail")
|
||||
} else {
|
||||
t.Log("success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewriteRule_CaseInsensitive(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
@@ -134,12 +83,12 @@ func TestRewriteRule_CaseInsensitive(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _, ok := r.Match("/index.php", func(source string) string {
|
||||
_, _, ok := r.MatchRequest("/index.php", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsTrue(ok)
|
||||
|
||||
_, _, ok = r.Match("/INDEX.php", func(source string) string {
|
||||
_, _, ok = r.MatchRequest("/INDEX.php", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsTrue(ok)
|
||||
@@ -155,7 +104,7 @@ func TestRewriteRule_Slashes(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replace, _, ok := r.Match("/index.php", func(source string) string {
|
||||
replace, _, ok := r.MatchRequest("/index.php", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsTrue(ok)
|
||||
@@ -167,14 +116,13 @@ func TestRewriteRuleProxy(t *testing.T) {
|
||||
|
||||
rule := &HTTPRewriteRule{
|
||||
Pattern: "/(hello)/(world)",
|
||||
Replace: "proxy://lb001/${1}/${2}",
|
||||
Replace: "/${1}/${2}",
|
||||
}
|
||||
a.IsNil(rule.Init())
|
||||
|
||||
replace, _, b := rule.Match("/hello/world", func(source string) string {
|
||||
replace, _, b := rule.MatchRequest("/hello/world", func(source string) string {
|
||||
return source
|
||||
})
|
||||
a.IsTrue(b)
|
||||
a.IsTrue(rule.TargetProxy() == "lb001")
|
||||
a.IsTrue(replace == "/hello/world")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user