Exemple #1
0
func TestSet_Find(t *testing.T) {
	var set suffix.Set

	if set.Has("test.com") {
		t.Error("test.com belongs to empty set")
	}

	set.Add("redtube.com")
	set.Add("bbc.co.uk")
	set.Add("a.b.c.d")
	set.Add(".arpa..")

	cases := []struct {
		name   string
		suffix string
	}{
		{"redtube.com", "redtube.com"},
		{"gang.redtube.com", "redtube.com"},
		{"bang.gang.redtube.com", "redtube.com"},

		{"a.b.c.d", "a.b.c.d"},
		{".a.b.c.d", "a.b.c.d"},

		{"arpa", "arpa"},
		{"in.arpa", "arpa"},

		{"a.a.c.d", ""},
		{"pinktube.com", ""},
		{"edtube.com", ""},
		{"com", ""},
		{"bbc", ""},
		{"co.uk", ""},
		{"bbc.co", ""},
	}

	for _, c := range cases {
		suffix := set.Match(c.name)
		if suffix != c.suffix {
			t.Errorf("%s expected suffix %q, got %q", c.name, c.suffix, suffix)
		}
		belongs := (suffix != "")
		if b := set.Matches(c.name); b != belongs {
			t.Error(c.name, b, " != ", belongs)
		}
	}
}
Exemple #2
0
func ExampleSet_ReadFrom_iana() {
	r, err := http.Get("http://data.iana.org/TLD/tlds-alpha-by-domain.txt")
	if err != nil {
		log.Fatal(err)
	}

	var tlds suffix.Set
	_, err = tlds.ReadFrom(r.Body)
	if err != nil {
		log.Fatal(err)
	}
	r.Body.Close()

	fmt.Println(tlds.Match("MERRY.CHRISTMAS"))

	// Output:
	// CHRISTMAS
}