package router import ( "testing" "git.veillax.com/Veillax/spinner/internal/config" ) func TestMatch(t *testing.T) { cfg, err := config.Parse([]byte(`{"servers":[ {"name":"a","domains":["a.example.com","*.deep.example.com"],"addresses":["127.0.0.1:1"]}, {"name":"b","domains":["*.example.com"],"addresses":["127.0.0.1:2"]}, {"name":"c","domains":["*.x.deep.example.com"],"addresses":["127.0.0.1:3"]} ]}`)) if err != nil { t.Fatal(err) } r := New(cfg) cases := map[string]string{ "a.example.com": "a", "A.Example.COM.": "a", "a.example.com\x00FML\x00": "a", "q.deep.example.com": "a", "other.example.com": "b", "s.x.deep.example.com": "c", "example.com": "", "evil.com": "", "notexample.com": "", "": "", } for host, want := range cases { got := "" if s := r.Match(host); s != nil { got = s.Name } if got != want { t.Errorf("Match(%q) = %q, want %q", host, got, want) } } } func TestConfigValidation(t *testing.T) { bad := []string{ `{"servers":[{"name":"","domains":["a"],"addresses":["h:1"]}]}`, `{"servers":[{"name":"a","domains":[],"addresses":["h:1"]}]}`, `{"servers":[{"name":"a","domains":["a"],"addresses":["nope"]}]}`, `{"servers":[{"name":"a","domains":["a"],"addresses":["h:1"]},{"name":"a","domains":["b"],"addresses":["h:1"]}]}`, `{"unknown_field":1}`, } for _, b := range bad { if _, err := config.Parse([]byte(b)); err == nil { t.Errorf("expected error for %s", b) } } }