diff options
author | David Anderson <[email protected]> | 2017-01-01 14:35:01 -0800 |
---|---|---|
committer | David Anderson <[email protected]> | 2017-01-01 14:35:01 -0800 |
commit | c41a68d73b757355dbd1f433fc4e2afe161c1f7b (patch) | |
tree | 4f776d4e165c515c391f458c21e6b85723fc5930 | |
parent | 6546db44e46c75d1ec05fbd47f1396c49705c34d (diff) |
Add tests for hostname matching, and make DNS matches match entire string.
-rw-r--r-- | config.go | 2 | ||||
-rw-r--r-- | config_test.go | 51 |
2 files changed, 52 insertions, 1 deletions
@@ -52,7 +52,7 @@ func dnsRegex(s string) (*regexp.Regexp, error) { b = append(b, regexp.QuoteMeta(f)) } } - return regexp.Compile(strings.Join(b, `\.`)) + return regexp.Compile(fmt.Sprintf("^%s$", strings.Join(b, `\.`))) } // Match returns the backend for hostname. diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..bcb9e5f --- /dev/null +++ b/config_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "bytes" + "testing" +) + +func TestConfig(t *testing.T) { + cases := []struct { + Config string + Tests map[string]string + }{ + { + Config: ` +# Comment +go.universe.tf 1.2.3.4 +*.universe.tf 2.3.4.5 +# Comment +google.* 3.4.5.6 +/gooo+gle\.com/ 4.5.6.7 +`, + Tests: map[string]string{ + "go.universe.tf": "1.2.3.4", + "foo.universe.tf": "2.3.4.5", + "bar.universe.tf": "2.3.4.5", + "google.com": "3.4.5.6", + "google.fr": "3.4.5.6", + "goooooooooogle.com": "4.5.6.7", + + "blah.com": "", + "google.com.br": "", + "foo.bar.universe.tf": "", + "goooooglexcom": "", + }, + }, + } + + for _, test := range cases { + var cfg Config + if err := cfg.Read(bytes.NewBufferString(test.Config)); err != nil { + t.Fatalf("Failed to read config (%s):\n%q", err, test.Config) + } + + for hostname, expected := range test.Tests { + actual := cfg.Match(hostname) + if expected != actual { + t.Errorf("cfg.Match(%q) is %q, want %q", hostname, actual, expected) + } + } + } +} |