2026-09-23 09:40:59 +08:00
|
|
|
package nginx
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-10-01 16:54:35 +08:00
|
|
|
"github.com/havline/havline/internal/config"
|
|
|
|
|
"github.com/havline/havline/internal/proxy"
|
2026-09-23 09:40:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGenerateRuleBlocksIncludesComments(t *testing.T) {
|
|
|
|
|
cfg := config.Config{DataDir: t.TempDir()}
|
|
|
|
|
rule := proxy.Rule{
|
|
|
|
|
ID: 3,
|
2026-10-01 16:54:35 +08:00
|
|
|
Name: "havline",
|
2026-09-23 09:40:59 +08:00
|
|
|
Upstream: "http://127.0.0.1:8080",
|
|
|
|
|
ListenPort: 8011,
|
|
|
|
|
ListenIPv4: true,
|
|
|
|
|
Hosts: []proxy.Host{{Hostname: "s.example.com"}},
|
|
|
|
|
HTTPSEnabled: false,
|
|
|
|
|
Enabled: true,
|
|
|
|
|
Security: proxy.SecurityConfig{
|
|
|
|
|
IPBlacklist: []string{"192.168.8.2"},
|
|
|
|
|
IPWhitelistMode: true,
|
|
|
|
|
IPWhitelist: []string{"192.168.8.1"},
|
|
|
|
|
RateLimit: &proxy.RateLimitConfig{Enabled: true, Rate: 10, Burst: 20},
|
|
|
|
|
ConnLimit: &proxy.ConnLimitConfig{Enabled: true, Max: 20},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
out, err := GenerateRuleBlocks(cfg, rule, nil, GenerateOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for _, want := range []string{
|
2026-10-01 16:54:35 +08:00
|
|
|
"# 规则:havline",
|
2026-09-23 09:40:59 +08:00
|
|
|
"# 上游:http://127.0.0.1:8080",
|
|
|
|
|
"# 限流 zone 在 http 块中自动生成",
|
|
|
|
|
"# IP 黑名单",
|
|
|
|
|
"# IP 白名单模式",
|
|
|
|
|
"# 请求限流:10 次/秒,突发 20",
|
|
|
|
|
"# 连接数限制:每 IP 最多 20 并发",
|
|
|
|
|
"# 反代 location /",
|
|
|
|
|
"# 反向代理与 WebSocket 头",
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(out, want) {
|
|
|
|
|
t.Fatalf("missing comment %q in:\n%s", want, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|