forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
44 lines (36 loc) · 852 Bytes
/
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
package rdns
import (
"errors"
"github.com/miekg/dns"
)
func init() {
// Silence the logger while running tests
Log.SetLevel(0)
}
// TestResolver is a configurable resolver used for testing. It counts the
// number of queries, can be set to fail, and the resolve function can be
// defined externally.
type TestResolver struct {
ResolveFunc func(*dns.Msg, ClientInfo) (*dns.Msg, error)
hitCount int
shouldFail bool
}
func (r *TestResolver) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
r.hitCount++
if r.shouldFail {
return nil, errors.New("failed")
}
if r.ResolveFunc != nil {
return r.ResolveFunc(q, ci)
}
return q, nil
}
func (r *TestResolver) String() string {
return "TestResolver()"
}
func (r *TestResolver) HitCount() int {
return r.hitCount
}
func (r *TestResolver) SetFail(f bool) {
r.shouldFail = f
}