-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolver_test.go
66 lines (59 loc) · 1.64 KB
/
resolver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ara_test
import (
"context"
"net/http/httptrace"
"testing"
"github.com/cevatbarisyilmaz/ara"
)
func TestNewCustomResolver(t *testing.T) {
resolver := ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}})
addrs, err := resolver.LookupHost(context.Background(), "example.com")
if err != nil {
t.Error(err)
} else if addrs == nil {
t.Error("addresses are nil")
} else if len(addrs) == 0 {
t.Error("no addresses")
} else if len(addrs) > 1 {
t.Error("too many addresses")
} else if addrs[0] != "127.0.0.1" {
t.Error("wrong address")
}
addrs, err = resolver.LookupHost(context.Background(), "google.com")
if err != nil {
t.Error(err)
} else if addrs == nil {
t.Error("addresses are nil")
} else if len(addrs) == 0 {
t.Error("no addresses")
}
}
func TestClientTrace(t *testing.T) {
resolver := ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}})
var gotDNSStart, gotDNSDone bool
ctx := context.Background()
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
DNSStart: func(info httptrace.DNSStartInfo) {
if info.Host != "example.com" {
t.Error("wrong address in ClientTrace.DNSStart")
}
gotDNSStart = true
},
DNSDone: func(info httptrace.DNSDoneInfo) {
if info.Err != nil {
t.Error("non-nil error in ClientTrace.DNSDone")
}
if len(info.Addrs) != 1 || info.Addrs[0].String() != "127.0.0.1" {
t.Error("wrong IP in ClientTrace.DNSDone")
}
gotDNSDone = true
},
})
resolver.LookupHost(ctx, "example.com")
if !gotDNSStart {
t.Error("ClientTrace.DNSStart not called")
}
if !gotDNSDone {
t.Error("ClientTrace.DNSDone not called")
}
}