From f7c74fb243b2e85f3bc75f4e394e2ba8cad4c25d Mon Sep 17 00:00:00 2001 From: oliverpool Date: Sun, 24 Apr 2022 22:13:10 +0200 Subject: [PATCH] add server test --- server_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ service_test.go | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 server_test.go diff --git a/server_test.go b/server_test.go new file mode 100644 index 00000000..6020030b --- /dev/null +++ b/server_test.go @@ -0,0 +1,77 @@ +package zeroconf + +import ( + "os" + "strings" + "testing" +) + +func TestRegister(t *testing.T) { + expected := ServiceEntry{ + ServiceRecord: ServiceRecord{ + Instance: "instance", + Service: "service", + Domain: "domain", + }, + Port: 1337, + Text: []string{"txtv=0", "lo=1", "la=2"}, + } + server, err := Register(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.Text, nil) + if err != nil { + t.Fatal(err) + } + server.Shutdown() + + hostname, err := os.Hostname() + if err != nil { + t.Fatal(err) + } + expected.HostName = hostname + ".domain." + + checkServiceEntry(t, *server.service, expected) +} + +func TestRegisterProxy(t *testing.T) { + expected := ServiceEntry{ + ServiceRecord: ServiceRecord{ + Instance: "instance", + Service: "service", + Domain: "domain", + }, + Port: 1337, + HostName: "proxy.domain.", + Text: []string{"txtv=0", "lo=1", "la=2"}, + } + server, err := RegisterProxy(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.HostName, nil, expected.Text, nil) + if err != nil { + t.Fatal(err) + } + server.Shutdown() + + checkServiceEntry(t, *server.service, expected) +} + +func checkServiceEntry(t *testing.T, got, expected ServiceEntry) { + t.Helper() + if got.Instance != expected.Instance { + t.Fatalf("Expected Instance is %s, but got %s", expected.Instance, got.Instance) + } + if got.Service != expected.Service { + t.Fatalf("Expected Service is %s, but got %s", expected.Service, got.Service) + } + if got.Domain != expected.Domain { + t.Fatalf("Expected Domain is %s, but got %s", expected.Domain, got.Domain) + } + if got.Port != expected.Port { + t.Fatalf("Expected Port is %d, but got %d", expected.Port, got.Port) + } + if strings.Join(got.Text, " ") != strings.Join(expected.Text, " ") { + t.Fatalf("Expected Text is %s, but got %s", expected.Text, got.Text) + } + if got.HostName != expected.HostName { + t.Fatalf("Expected HostName is %s, but got %s", expected.HostName, got.HostName) + } + if len(got.AddrIPv4) == 0 && len(got.AddrIPv6) == 0 { + t.Fatal("Unexpected empty IPV4 and IPV6") + } +} diff --git a/service_test.go b/service_test.go index 2c5a23ed..0107d092 100644 --- a/service_test.go +++ b/service_test.go @@ -74,7 +74,7 @@ func TestNoRegister(t *testing.T) { t.Fatalf("Expected create resolver success, but got %v", err) } - // before register, mdns resolve shuold not have any entry + // before register, mdns resolve should not have any entry entries := make(chan *ServiceEntry) go func(results <-chan *ServiceEntry) { s := <-results