| 1 | package dns |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestDotAsCatchAllWildcard(t *testing.T) { |
| 6 | mux := NewServeMux() |
| 7 | mux.Handle(".", HandlerFunc(HelloServer)) |
| 8 | mux.Handle("example.com.", HandlerFunc(AnotherHelloServer)) |
| 9 | |
| 10 | handler := mux.match("www.miek.nl.", TypeTXT) |
| 11 | if handler == nil { |
| 12 | t.Error("wildcard match failed") |
| 13 | } |
| 14 | |
| 15 | handler = mux.match("www.example.com.", TypeTXT) |
| 16 | if handler == nil { |
| 17 | t.Error("example.com match failed") |
| 18 | } |
| 19 | |
| 20 | handler = mux.match("a.www.example.com.", TypeTXT) |
| 21 | if handler == nil { |
| 22 | t.Error("a.www.example.com match failed") |
| 23 | } |
| 24 | |
| 25 | handler = mux.match("boe.", TypeTXT) |
| 26 | if handler == nil { |
| 27 | t.Error("boe. match failed") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestCaseFolding(t *testing.T) { |
| 32 | mux := NewServeMux() |
| 33 | mux.Handle("_udp.example.com.", HandlerFunc(HelloServer)) |
| 34 | |
| 35 | handler := mux.match("_dns._udp.example.com.", TypeSRV) |
| 36 | if handler == nil { |
| 37 | t.Error("case sensitive characters folded") |
| 38 | } |
| 39 | |
| 40 | handler = mux.match("_DNS._UDP.EXAMPLE.COM.", TypeSRV) |
| 41 | if handler == nil { |
| 42 | t.Error("case insensitive characters not folded") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestRootServer(t *testing.T) { |
| 47 | mux := NewServeMux() |
| 48 | mux.Handle(".", HandlerFunc(HelloServer)) |
| 49 | |
| 50 | handler := mux.match(".", TypeNS) |
| 51 | if handler == nil { |
| 52 | t.Error("root match failed") |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func BenchmarkMuxMatch(b *testing.B) { |
| 57 | mux := NewServeMux() |
| 58 | mux.Handle("_udp.example.com.", HandlerFunc(HelloServer)) |
| 59 | |
| 60 | bench := func(q string) func(*testing.B) { |
| 61 | return func(b *testing.B) { |
| 62 | for n := 0; n < b.N; n++ { |
| 63 | handler := mux.match(q, TypeSRV) |
| 64 | if handler == nil { |
| 65 | b.Fatal("couldn't find match") |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | b.Run("lowercase", bench("_dns._udp.example.com.")) |
| 71 | b.Run("uppercase", bench("_DNS._UDP.EXAMPLE.COM.")) |
| 72 | } |