-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
helper.go
120 lines (100 loc) · 3.04 KB
/
helper.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package helpertest
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"github.com/0xERR0R/blocky/log"
"github.com/miekg/dns"
"github.com/onsi/gomega/types"
)
// TempFile creates temp file with passed data
func TempFile(data string) *os.File {
f, err := os.CreateTemp("", "prefix")
if err != nil {
log.Log().Fatal(err)
}
_, err = f.WriteString(data)
if err != nil {
log.Log().Fatal(err)
}
return f
}
// TestServer creates temp http server with passed data
func TestServer(data string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
_, err := rw.Write([]byte(data))
if err != nil {
log.Log().Fatal("can't write to buffer:", err)
}
}))
}
// DoGetRequest performs a GET request
func DoGetRequest(url string,
fn func(w http.ResponseWriter, r *http.Request)) (*httptest.ResponseRecorder, *bytes.Buffer) {
r, _ := http.NewRequest(http.MethodGet, url, nil)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fn)
handler.ServeHTTP(rr, r)
return rr, rr.Body
}
// BeDNSRecord returns new dns matcher
func BeDNSRecord(domain string, dnsType uint16, ttl uint32, answer string) types.GomegaMatcher {
return &dnsRecordMatcher{
domain: domain,
dnsType: dnsType,
TTL: ttl,
answer: answer,
}
}
type dnsRecordMatcher struct {
domain string
dnsType uint16
TTL uint32
answer string
}
func (matcher *dnsRecordMatcher) matchSingle(rr dns.RR) (success bool, err error) {
if (rr.Header().Name != matcher.domain) ||
(rr.Header().Rrtype != matcher.dnsType) ||
(matcher.TTL > 0 && (rr.Header().Ttl != matcher.TTL)) {
return false, nil
}
switch v := rr.(type) {
case *dns.A:
return v.A.String() == matcher.answer, nil
case *dns.AAAA:
return v.AAAA.String() == matcher.answer, nil
case *dns.PTR:
return v.Ptr == matcher.answer, nil
case *dns.MX:
return v.Mx == matcher.answer, nil
}
return false, nil
}
// Match checks the DNS record
func (matcher *dnsRecordMatcher) Match(actual interface{}) (success bool, err error) {
switch i := actual.(type) {
case *dns.Msg:
return matcher.Match(i.Answer)
case dns.RR:
return matcher.matchSingle(i)
case []dns.RR:
if len(i) == 1 {
return matcher.matchSingle(i[0])
}
return false, fmt.Errorf("DNSRecord matcher expects []dns.RR with len == 1")
default:
return false, fmt.Errorf("DNSRecord matcher expects an dns.RR or []dns.RR")
}
}
// FailureMessage generates a failure messge
func (matcher *dnsRecordMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%s\n to contain\n\t domain '%s', ttl '%d', type '%s', answer '%s'",
actual, matcher.domain, matcher.TTL, dns.TypeToString[matcher.dnsType], matcher.answer)
}
// NegatedFailureMessage creates negated message
func (matcher *dnsRecordMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("Expected\n\t%s\n not to contain\n\t domain '%s', ttl '%d', type '%s', answer '%s'",
actual, matcher.domain, matcher.TTL, dns.TypeToString[matcher.dnsType], matcher.answer)
}