19
19
dnsResult * dns.Msg
20
20
)
21
21
22
- func TestTXTRecords (t * testing.T ) {
23
- testDomains := make ([]string , 3 )
24
- testDomains [0 ] = "onerecord.com"
25
- testDomains [1 ] = "multistringrecord.com"
26
- testDomains [2 ] = "multiplerecords.com"
27
-
28
- expectedResults := make ([]string , 3 )
29
- expectedResults [0 ] = `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"`
30
- expectedResults [1 ] = `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"`
31
- expectedResults [2 ] = `multiplerecords.com.\s+5\s+IN\s+TXT\s+"record 1"\nmultiplerecords.com.\s*5\s*IN\s*TXT\s*"record 2"`
22
+ func TestDNSRecords (t * testing.T ) {
32
23
33
24
srv , _ := mockdns .NewServerWithLogger (map [string ]mockdns.Zone {
34
25
"onerecord.com." : {
@@ -52,33 +43,42 @@ func TestTXTRecords(t *testing.T) {
52
43
}
53
44
srv .PatchNet (net .DefaultResolver )
54
45
defer mockdns .UnpatchNet (net .DefaultResolver )
46
+ w := new (TestResponseWriter )
47
+ options := HandlerOptions {
48
+ IPv6 : true ,
49
+ StaticHosts : map [string ]string {
50
+ "MY.Host" : "host.lima.internal" ,
51
+ },
52
+ }
55
53
54
+ h , err := NewHandler (options )
55
+ assert .NilError (t , err )
56
+
57
+ regexMatch := func (value string , pattern string ) cmp.Comparison {
58
+ return func () cmp.Result {
59
+ re := regexp .MustCompile (pattern )
60
+ if re .MatchString (value ) {
61
+ return cmp .ResultSuccess
62
+ }
63
+ return cmp .ResultFailure (
64
+ fmt .Sprintf ("%q did not match pattern %q" , value , pattern ))
65
+ }
66
+ }
56
67
t .Run ("test TXT records" , func (t * testing.T ) {
57
- w := new (TestResponseWriter )
58
- options := HandlerOptions {
59
- IPv6 : true ,
60
- StaticHosts : map [string ]string {
61
- "MY.Host" : "host.lima.internal" ,
62
- },
68
+ tests := []struct {
69
+ testDomain string
70
+ expectedTXTRecord string
71
+ }{
72
+ {testDomain : "onerecord.com" , expectedTXTRecord : `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"` },
73
+ {testDomain : "multistringrecord.com" , expectedTXTRecord : `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"` },
74
+ {testDomain : "multiplerecords.com" , expectedTXTRecord : `multiplerecords.com.\s+5\s+IN\s+TXT\s+"record 1"\nmultiplerecords.com.\s*5\s*IN\s*TXT\s*"record 2"` },
63
75
}
64
- h , err := NewHandler (options )
65
- if err == nil {
66
- for i := 0 ; i < len (testDomains ); i ++ {
67
- req := new (dns.Msg )
68
- req .SetQuestion (dns .Fqdn (testDomains [i ]), dns .TypeTXT )
69
- h .ServeDNS (w , req )
70
- regexMatch := func (value string , pattern string ) cmp.Comparison {
71
- return func () cmp.Result {
72
- re := regexp .MustCompile (pattern )
73
- if re .MatchString (value ) {
74
- return cmp .ResultSuccess
75
- }
76
- return cmp .ResultFailure (
77
- fmt .Sprintf ("%q did not match pattern %q" , value , pattern ))
78
- }
79
- }
80
- assert .Assert (t , regexMatch (dnsResult .String (), expectedResults [i ]))
81
- }
76
+
77
+ for _ , tc := range tests {
78
+ req := new (dns.Msg )
79
+ req .SetQuestion (dns .Fqdn (tc .testDomain ), dns .TypeTXT )
80
+ h .ServeDNS (w , req )
81
+ assert .Assert (t , regexMatch (dnsResult .String (), tc .expectedTXTRecord ))
82
82
}
83
83
})
84
84
}
0 commit comments