39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package controller
|
|
|
|
import "testing"
|
|
|
|
// TestValidateInstallServerURL 验证 Agent 公网地址仅接受安全的 HTTP(S) origin。 // Logan.Li
|
|
func TestValidateInstallServerURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{name: "HTTP 域名", input: "http://aiops.example.com", want: "http://aiops.example.com"},
|
|
{name: "HTTPS 域名", input: "https://aiops.example.com/", want: "https://aiops.example.com"},
|
|
{name: "自定义端口", input: "https://aiops.example.com:8443", want: "https://aiops.example.com:8443"},
|
|
{name: "拒绝路径", input: "https://aiops.example.com/api", wantErr: true},
|
|
{name: "拒绝查询参数", input: "https://aiops.example.com?x=1", wantErr: true},
|
|
{name: "拒绝非 HTTP 协议", input: "file:///tmp/agent", wantErr: true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got, err := validateInstallServerURL(test.input)
|
|
if test.wantErr {
|
|
if err == nil {
|
|
t.Fatalf("期望校验失败,实际返回 %q", got)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("地址校验失败: %v", err)
|
|
}
|
|
if got != test.want {
|
|
t.Fatalf("期望 %q,实际 %q", test.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|