-
Notifications
You must be signed in to change notification settings - Fork 10
/
example_test.go
46 lines (37 loc) · 886 Bytes
/
example_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
package mockdns_test
import (
"context"
"fmt"
"net"
"github.com/foxcpp/go-mockdns"
)
func ExampleResolver() {
// Use for code that supports custom resolver implementations.
r := mockdns.Resolver{
Zones: map[string]mockdns.Zone{
"example.org.": {
A: []string{"1.2.3.4"},
},
},
}
addrs, err := r.LookupHost(context.Background(), "example.org")
fmt.Println(addrs, err)
// Output:
// [1.2.3.4] <nil>
}
func ExampleServer_PatchNet() {
// Use for code that directly calls net.Lookup*.
srv, _ := mockdns.NewServer(map[string]mockdns.Zone{
"example.org.": {
A: []string{"1.2.3.4"},
},
}, false)
defer srv.Close()
srv.PatchNet(net.DefaultResolver)
// Important if net.DefaultResolver is modified.
defer mockdns.UnpatchNet(net.DefaultResolver)
addrs, err := net.LookupHost("example.org")
fmt.Println(addrs, err)
// Output:
// [1.2.3.4] <nil>
}