Skip to content

Commit ec6914c

Browse files
committed
add server test
1 parent a939f9c commit ec6914c

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

server_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package zeroconf
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestRegister(t *testing.T) {
10+
expected := ServiceEntry{
11+
ServiceRecord: ServiceRecord{
12+
Instance: "instance",
13+
Service: "service",
14+
Domain: "domain",
15+
},
16+
Port: 1337,
17+
Text: []string{"txtv=0", "lo=1", "la=2"},
18+
}
19+
server, err := Register(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.Text, nil)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
server.Shutdown()
24+
25+
hostname, err := os.Hostname()
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
expected.HostName = hostname + ".domain."
30+
31+
checkServiceEntry(t, *server.service, expected)
32+
}
33+
34+
func TestRegisterProxy(t *testing.T) {
35+
expected := ServiceEntry{
36+
ServiceRecord: ServiceRecord{
37+
Instance: "instance",
38+
Service: "service",
39+
Domain: "domain",
40+
},
41+
Port: 1337,
42+
HostName: "proxy.domain.",
43+
Text: []string{"txtv=0", "lo=1", "la=2"},
44+
}
45+
server, err := RegisterProxy(expected.Instance, expected.Service, expected.Domain, expected.Port, expected.HostName, nil, expected.Text, nil)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
server.Shutdown()
50+
51+
checkServiceEntry(t, *server.service, expected)
52+
}
53+
54+
func checkServiceEntry(t *testing.T, got, expected ServiceEntry) {
55+
t.Helper()
56+
if got.Instance != expected.Instance {
57+
t.Fatalf("Expected Instance is %s, but got %s", expected.Instance, got.Instance)
58+
}
59+
if got.Service != expected.Service {
60+
t.Fatalf("Expected Service is %s, but got %s", expected.Service, got.Service)
61+
}
62+
if got.Domain != expected.Domain {
63+
t.Fatalf("Expected Domain is %s, but got %s", expected.Domain, got.Domain)
64+
}
65+
if got.Port != expected.Port {
66+
t.Fatalf("Expected Port is %d, but got %d", expected.Port, got.Port)
67+
}
68+
if strings.Join(got.Text, " ") != strings.Join(expected.Text, " ") {
69+
t.Fatalf("Expected Text is %s, but got %s", expected.Text, got.Text)
70+
}
71+
if got.HostName != expected.HostName {
72+
t.Fatalf("Expected HostName is %s, but got %s", expected.HostName, got.HostName)
73+
}
74+
if len(got.AddrIPv4) == 0 && len(got.AddrIPv6) == 0 {
75+
t.Fatal("Unexpected empty IPV4 and IPV6")
76+
}
77+
}

service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestNoRegister(t *testing.T) {
7474
t.Fatalf("Expected create resolver success, but got %v", err)
7575
}
7676

77-
// before register, mdns resolve shuold not have any entry
77+
// before register, mdns resolve should not have any entry
7878
entries := make(chan *ServiceEntry)
7979
go func(results <-chan *ServiceEntry) {
8080
s := <-results

0 commit comments

Comments
 (0)