5 func TestAddOrigin(t *testing.T) {
6 var tests = []struct{ e1, e2, expected string }{
7 {"@", "example.com", "example.com"},
8 {"foo", "example.com", "foo.example.com"},
9 {"foo.", "example.com", "foo."},
10 {"@", "example.com.", "example.com."},
11 {"foo", "example.com.", "foo.example.com."},
12 {"foo.", "example.com.", "foo."},
13 {"example.com", ".", "example.com."},
14 {"example.com.", ".", "example.com."},
16 // In general origin should not be "" or "." but at least
17 // these tests verify we don't crash and will keep results
18 // from changing unexpectedly.
21 {"foobar", "", "foobar"},
22 {"foobar.", "", "foobar."},
25 {"foobar", ".", "foobar."},
26 {"foobar.", ".", "foobar."},
28 for _, test := range tests {
29 actual := AddOrigin(test.e1, test.e2)
30 if test.expected != actual {
31 t.Errorf("AddOrigin(%#v, %#v) expected %#v, got %#v\n", test.e1, test.e2, test.expected, actual)
36 func TestTrimDomainName(t *testing.T) {
38 // Try trimming "example.com" and "example.com." from typical use cases.
39 testsEx := []struct{ experiment, expected string }{
40 {"foo.example.com", "foo"},
41 {"foo.example.com.", "foo"},
42 {".foo.example.com", ".foo"},
43 {".foo.example.com.", ".foo"},
44 {"*.example.com", "*"},
46 {"example.com.", "@"},
49 {"serverfault.com.", "serverfault.com."},
50 {"serverfault.com", "serverfault.com"},
51 {".foo.ronco.com", ".foo.ronco.com"},
52 {".foo.ronco.com.", ".foo.ronco.com."},
54 for _, dom := range []string{"example.com", "example.com."} {
55 for i, test := range testsEx {
56 actual := TrimDomainName(test.experiment, dom)
57 if test.expected != actual {
58 t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.experiment, dom, test.expected, actual)
64 // These test shouldn't be needed but I was weary of off-by-one errors.
65 // In theory, these can't happen because there are no single-letter TLDs,
66 // but it is good to exercize the code this way.
67 tests := []struct{ experiment, expected string }{
70 {"a.b.c.d.e.f.", "a.b.c.d.e"},
71 {"b.c.d.e.f.", "b.c.d.e"},
72 {"c.d.e.f.", "c.d.e"},
76 {".a.b.c.d.e.f.", ".a.b.c.d.e"},
77 {".b.c.d.e.f.", ".b.c.d.e"},
78 {".c.d.e.f.", ".c.d.e"},
82 {"a.b.c.d.e.f", "a.b.c.d.e"},
83 {"a.b.c.d.e.", "a.b.c.d.e."},
84 {"a.b.c.d.e", "a.b.c.d.e"},
85 {"a.b.c.d.", "a.b.c.d."},
86 {"a.b.c.d", "a.b.c.d"},
93 {".a.b.c.d.e.f", ".a.b.c.d.e"},
94 {".a.b.c.d.e.", ".a.b.c.d.e."},
95 {".a.b.c.d.e", ".a.b.c.d.e"},
96 {".a.b.c.d.", ".a.b.c.d."},
97 {".a.b.c.d", ".a.b.c.d"},
98 {".a.b.c.", ".a.b.c."},
105 for _, dom := range []string{"f", "f."} {
106 for i, test := range tests {
107 actual := TrimDomainName(test.experiment, dom)
108 if test.expected != actual {
109 t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.experiment, dom, test.expected, actual)
114 // Test cases for bugs found in the wild.
115 // These test cases provide both origin, s, and the expected result.
116 // If you find a bug in the while, this is probably the easiest place
117 // to add it as a test case.
118 var testsWild = []struct{ e1, e2, expected string }{
119 {"mathoverflow.net.", ".", "mathoverflow.net"},
120 {"mathoverflow.net", ".", "mathoverflow.net"},
124 for i, test := range testsWild {
125 actual := TrimDomainName(test.e1, test.e2)
126 if test.expected != actual {
127 t.Errorf("%d TrimDomainName(%#v, %#v): expected %v, got %v\n", i, test.e1, test.e2, test.expected, actual)