go-micro.dev/v5@v5.12.0/util/mdns/dns_sd_test.go (about)

     1  package mdns
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/miekg/dns"
     8  )
     9  
    10  type mockMDNSService struct{}
    11  
    12  func (s *mockMDNSService) Records(q dns.Question) []dns.RR {
    13  	return []dns.RR{
    14  		&dns.PTR{
    15  			Hdr: dns.RR_Header{
    16  				Name:   "fakerecord",
    17  				Rrtype: dns.TypePTR,
    18  				Class:  dns.ClassINET,
    19  				Ttl:    42,
    20  			},
    21  			Ptr: "fake.local.",
    22  		},
    23  	}
    24  }
    25  
    26  func (s *mockMDNSService) Announcement() []dns.RR {
    27  	return []dns.RR{
    28  		&dns.PTR{
    29  			Hdr: dns.RR_Header{
    30  				Name:   "fakeannounce",
    31  				Rrtype: dns.TypePTR,
    32  				Class:  dns.ClassINET,
    33  				Ttl:    42,
    34  			},
    35  			Ptr: "fake.local.",
    36  		},
    37  	}
    38  }
    39  
    40  func TestDNSSDServiceRecords(t *testing.T) {
    41  	s := &DNSSDService{
    42  		MDNSService: &MDNSService{
    43  			serviceAddr: "_foobar._tcp.local.",
    44  			Domain:      "local",
    45  		},
    46  	}
    47  	q := dns.Question{
    48  		Name:   "_services._dns-sd._udp.local.",
    49  		Qtype:  dns.TypePTR,
    50  		Qclass: dns.ClassINET,
    51  	}
    52  	recs := s.Records(q)
    53  	if got, want := len(recs), 1; got != want {
    54  		t.Fatalf("s.Records(%v) returned %v records, want %v", q, got, want)
    55  	}
    56  
    57  	want := dns.RR(&dns.PTR{
    58  		Hdr: dns.RR_Header{
    59  			Name:   "_services._dns-sd._udp.local.",
    60  			Rrtype: dns.TypePTR,
    61  			Class:  dns.ClassINET,
    62  			Ttl:    defaultTTL,
    63  		},
    64  		Ptr: "_foobar._tcp.local.",
    65  	})
    66  	if got := recs[0]; !reflect.DeepEqual(got, want) {
    67  		t.Errorf("s.Records()[0] = %v, want %v", got, want)
    68  	}
    69  }