Skip to content

Commit

Permalink
add server test
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverpool committed Apr 24, 2022
1 parent a939f9c commit ec6914c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 77 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec6914c

Please sign in to comment.