refactor: use testutil helpers in proxy tests
This commit is contained in:
parent
094976df2b
commit
8681472c4b
@ -17,6 +17,7 @@ import (
|
|||||||
"rua.plus/lolly/internal/config"
|
"rua.plus/lolly/internal/config"
|
||||||
"rua.plus/lolly/internal/loadbalance"
|
"rua.plus/lolly/internal/loadbalance"
|
||||||
"rua.plus/lolly/internal/proxy"
|
"rua.plus/lolly/internal/proxy"
|
||||||
|
"rua.plus/lolly/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestProxyCreation 测试代理创建
|
// TestProxyCreation 测试代理创建
|
||||||
|
|||||||
@ -42,22 +42,15 @@ func TestNewProxy(t *testing.T) {
|
|||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "正常创建",
|
name: "正常创建",
|
||||||
cfg: &config.ProxyConfig{
|
cfg: testutil.NewTestProxyConfig("/api"),
|
||||||
Path: "/api",
|
targets: testutil.NewTestTargets("http://localhost:8081", "http://localhost:8082"),
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second, Read: 30 * time.Second, Write: 30 * time.Second},
|
|
||||||
},
|
|
||||||
targets: []*loadbalance.Target{
|
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
{URL: "http://localhost:8082"},
|
|
||||||
},
|
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "nil配置",
|
name: "nil配置",
|
||||||
cfg: nil,
|
cfg: nil,
|
||||||
targets: []*loadbalance.Target{{URL: "http://localhost:8081"}},
|
targets: testutil.NewTestTargets("http://localhost:8081"),
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
errContains: "proxy config is nil",
|
errContains: "proxy config is nil",
|
||||||
},
|
},
|
||||||
@ -81,9 +74,7 @@ func TestNewProxy(t *testing.T) {
|
|||||||
Path: "/api",
|
Path: "/api",
|
||||||
LoadBalance: "",
|
LoadBalance: "",
|
||||||
},
|
},
|
||||||
targets: []*loadbalance.Target{
|
targets: testutil.NewTestTargets("http://localhost:8081"),
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
},
|
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -129,9 +120,7 @@ func TestNewProxy(t *testing.T) {
|
|||||||
Path: "/api",
|
Path: "/api",
|
||||||
LoadBalance: "invalid_algorithm",
|
LoadBalance: "invalid_algorithm",
|
||||||
},
|
},
|
||||||
targets: []*loadbalance.Target{
|
targets: testutil.NewTestTargets("http://localhost:8081"),
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
},
|
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
errContains: "unsupported load balance algorithm",
|
errContains: "unsupported load balance algorithm",
|
||||||
},
|
},
|
||||||
@ -170,17 +159,10 @@ func TestNewProxy(t *testing.T) {
|
|||||||
|
|
||||||
// TestServeHTTP_NoHealthyTargets 测试没有健康目标时返回502
|
// TestServeHTTP_NoHealthyTargets 测试没有健康目标时返回502
|
||||||
func TestServeHTTP_NoHealthyTargets(t *testing.T) {
|
func TestServeHTTP_NoHealthyTargets(t *testing.T) {
|
||||||
cfg := &config.ProxyConfig{
|
cfg := testutil.NewTestProxyConfig("/api")
|
||||||
Path: "/api",
|
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second, Read: 30 * time.Second, Write: 30 * time.Second},
|
|
||||||
}
|
|
||||||
|
|
||||||
// 所有目标都不健康
|
// 所有目标都不健康
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8081", "http://localhost:8082")
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
{URL: "http://localhost:8082"},
|
|
||||||
}
|
|
||||||
targets[0].Healthy.Store(false)
|
targets[0].Healthy.Store(false)
|
||||||
targets[1].Healthy.Store(false)
|
targets[1].Healthy.Store(false)
|
||||||
|
|
||||||
@ -222,15 +204,9 @@ func TestServeHTTP_RequestForwarding(t *testing.T) {
|
|||||||
// 等待服务器启动
|
// 等待服务器启动
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
|
||||||
cfg := &config.ProxyConfig{
|
cfg := testutil.NewTestProxyConfig("/api")
|
||||||
Path: "/api",
|
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second, Read: 30 * time.Second, Write: 30 * time.Second},
|
|
||||||
}
|
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8080")
|
||||||
{URL: "http://localhost:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -523,9 +499,7 @@ func TestModifyResponseHeaders(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8080")
|
||||||
{URL: "http://localhost:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -606,10 +580,7 @@ func TestUpdateTargets(t *testing.T) {
|
|||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
||||||
}
|
}
|
||||||
|
|
||||||
initialTargets := []*loadbalance.Target{
|
initialTargets := testutil.NewTestTargets("http://old1:8080", "http://old2:8080")
|
||||||
{URL: "http://old1:8080"},
|
|
||||||
{URL: "http://old2:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, initialTargets, nil, nil)
|
p, err := NewProxy(cfg, initialTargets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -617,11 +588,7 @@ func TestUpdateTargets(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新目标
|
// 更新目标
|
||||||
newTargets := []*loadbalance.Target{
|
newTargets := testutil.NewTestTargets("http://new1:8080", "http://new2:8080", "http://new3:8080")
|
||||||
{URL: "http://new1:8080"},
|
|
||||||
{URL: "http://new2:8080"},
|
|
||||||
{URL: "http://new3:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
err = p.UpdateTargets(newTargets)
|
err = p.UpdateTargets(newTargets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -655,10 +622,7 @@ func TestGetTargets(t *testing.T) {
|
|||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://backend1:8080", "http://backend2:8080")
|
||||||
{URL: "http://backend1:8080"},
|
|
||||||
{URL: "http://backend2:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -685,9 +649,7 @@ func TestGetConfig(t *testing.T) {
|
|||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8080")
|
||||||
{URL: "http://localhost:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -877,9 +839,7 @@ func TestHandleWebSocket(t *testing.T) {
|
|||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
Timeout: config.ProxyTimeout{Connect: 5 * time.Second},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8080")
|
||||||
{URL: "http://localhost:8080"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -889,7 +849,7 @@ func TestHandleWebSocket(t *testing.T) {
|
|||||||
// 由于 handleWebSocket 使用 Hijack,在测试环境中无法正常工作
|
// 由于 handleWebSocket 使用 Hijack,在测试环境中无法正常工作
|
||||||
// (需要一个真实的 HTTP 连接),因此我们仅验证函数存在且可调用
|
// (需要一个真实的 HTTP 连接),因此我们仅验证函数存在且可调用
|
||||||
// 实际功能通过集成测试验证
|
// 实际功能通过集成测试验证
|
||||||
target := &loadbalance.Target{URL: "http://localhost:8080"}
|
target := testutil.NewTestTarget("http://localhost:8080")
|
||||||
client := p.getClient(target.URL)
|
client := p.getClient(target.URL)
|
||||||
|
|
||||||
// 验证客户端和目标已正确配置
|
// 验证客户端和目标已正确配置
|
||||||
@ -905,15 +865,9 @@ func TestHandleWebSocket(t *testing.T) {
|
|||||||
// 注意:SetHealthChecker 是公开方法,但 healthChecker 是私有字段
|
// 注意:SetHealthChecker 是公开方法,但 healthChecker 是私有字段
|
||||||
// 此测试验证方法可以正常调用
|
// 此测试验证方法可以正常调用
|
||||||
func TestSetHealthChecker(t *testing.T) {
|
func TestSetHealthChecker(t *testing.T) {
|
||||||
cfg := &config.ProxyConfig{
|
cfg := testutil.NewTestProxyConfig("/api")
|
||||||
Path: "/api",
|
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second, Read: 30 * time.Second, Write: 30 * time.Second},
|
|
||||||
}
|
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8081")
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -942,16 +896,9 @@ func TestSetHealthChecker(t *testing.T) {
|
|||||||
|
|
||||||
// TestGetClient 测试客户端获取
|
// TestGetClient 测试客户端获取
|
||||||
func TestGetClient(t *testing.T) {
|
func TestGetClient(t *testing.T) {
|
||||||
cfg := &config.ProxyConfig{
|
cfg := testutil.NewTestProxyConfig("/api")
|
||||||
Path: "/api",
|
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{Connect: 5 * time.Second, Read: 30 * time.Second, Write: 30 * time.Second},
|
|
||||||
}
|
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestTargets("http://localhost:8081", "http://localhost:8082")
|
||||||
{URL: "http://localhost:8081"},
|
|
||||||
{URL: "http://localhost:8082"},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1012,10 +959,7 @@ func TestProxyCache(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestHealthyTargets("http://" + addr)
|
||||||
{URL: "http://" + addr},
|
|
||||||
}
|
|
||||||
targets[0].Healthy.Store(true)
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1065,10 +1009,7 @@ func TestServeHTTP_WithPassiveHealthCheck(t *testing.T) {
|
|||||||
Timeout: config.ProxyTimeout{Connect: 100 * time.Millisecond, Read: 100 * time.Millisecond, Write: 100 * time.Millisecond},
|
Timeout: config.ProxyTimeout{Connect: 100 * time.Millisecond, Read: 100 * time.Millisecond, Write: 100 * time.Millisecond},
|
||||||
}
|
}
|
||||||
|
|
||||||
targets := []*loadbalance.Target{
|
targets := testutil.NewTestHealthyTargets("http://127.0.0.1:59999") // 不存在的后端
|
||||||
{URL: "http://127.0.0.1:59999"}, // 不存在的后端
|
|
||||||
}
|
|
||||||
targets[0].Healthy.Store(true)
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1145,15 +1086,7 @@ func TestUpstreamVariablesCapture(t *testing.T) {
|
|||||||
}
|
}
|
||||||
targets[0].Healthy.Store(true)
|
targets[0].Healthy.Store(true)
|
||||||
|
|
||||||
cfg := &config.ProxyConfig{
|
cfg := testutil.NewTestProxyConfig("/")
|
||||||
Path: "/",
|
|
||||||
LoadBalance: "round_robin",
|
|
||||||
Timeout: config.ProxyTimeout{
|
|
||||||
Connect: 5 * time.Second,
|
|
||||||
Read: 30 * time.Second,
|
|
||||||
Write: 30 * time.Second,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
p, err := NewProxy(cfg, targets, nil, nil)
|
p, err := NewProxy(cfg, targets, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user