package weknora import "testing" func TestHTTPAllowedSharesOneJudgement(t *testing.T) { cases := []struct { name string url string policy HTTPPolicy want bool }{ {"https always", "https://weknora.example", HTTPPolicy{}, true}, {"loopback http", "http://127.0.0.1:8092", HTTPPolicy{}, true}, {"localhost http", "http://localhost:8092", HTTPPolicy{}, true}, {"private http refused by default", "http://172.29.151.11:18093", HTTPPolicy{}, false}, {"private http allowed when opted in", "http://172.29.151.11:18093", HTTPPolicy{AllowPrivateHTTP: true}, true}, {"rfc1918 /8", "http://10.1.2.3", HTTPPolicy{AllowPrivateHTTP: true}, true}, {"rfc1918 /16", "http://192.168.1.10", HTTPPolicy{AllowPrivateHTTP: true}, true}, {"link-local", "http://169.254.10.10", HTTPPolicy{AllowPrivateHTTP: true}, true}, {"public http still refused", "http://203.0.113.9", HTTPPolicy{AllowPrivateHTTP: true}, false}, {"public name still refused", "http://weknora.example", HTTPPolicy{AllowPrivateHTTP: true}, false}, {"docker single label opt-in", "http://weknora", HTTPPolicy{AllowDockerHTTP: true}, true}, {"docker single label off", "http://weknora", HTTPPolicy{}, false}, {"credentials refused", "http://user:pass@127.0.0.1:8092", HTTPPolicy{}, false}, {"empty host", "http://", HTTPPolicy{}, false}, {"other scheme", "ftp://127.0.0.1", HTTPPolicy{}, false}, } for _, testCase := range cases { if got := HTTPAllowed(testCase.url, testCase.policy); got != testCase.want { t.Fatalf("%s: HTTPAllowed(%q, %+v) = %v, want %v", testCase.name, testCase.url, testCase.policy, got, testCase.want) } } } func TestNewClientAcceptsPrivateHTTPOnlyWhenOptedIn(t *testing.T) { base := ClientConfig{BaseURL: "http://172.29.151.11:18093", APIKey: "k", Timeout: 1} if _, err := NewClient(base); err == nil { t.Fatal("private http must be refused without the opt-in") } base.AllowPrivateHTTP = true if _, err := NewClient(base); err != nil { t.Fatalf("private http must be accepted with the opt-in: %v", err) } }