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 dnsserver
import (
"bytes"
"crypto/tls"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/miekg/dns"
)
var (
validPath = regexp.MustCompile("^/(dns-query|(?P<uuid>[0-9a-f]+))$")
validator = func(r *http.Request) bool { return validPath.MatchString(r.URL.Path) }
)
func testServerHTTPS(t *testing.T, path string, validator func(*http.Request) bool) *http.Response {
c := Config{
Zone: "example.com.",
Transport: "https",
TLSConfig: &tls.Config{},
ListenHosts: []string{"127.0.0.1"},
Port: "443",
HTTPRequestValidateFunc: validator,
}
s, err := NewServerHTTPS("127.0.0.1:443", []*Config{&c})
if err != nil {
t.Log(err)
t.Fatal("could not create HTTPS server")
}
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeDNSKEY)
buf, err := m.Pack()
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(buf))
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
return w.Result()
}
func TestCustomHTTPRequestValidator(t *testing.T) {
testCases := map[string]struct {
path string
expected int
validator func(*http.Request) bool
}{
"default": {"/dns-query", http.StatusOK, nil},
"custom validator": {"/b10cada", http.StatusOK, validator},
"no validator set": {"/adb10c", http.StatusNotFound, nil},
"invalid path with validator": {"/helloworld", http.StatusNotFound, validator},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
res := testServerHTTPS(t, tc.path, tc.validator)
if res.StatusCode != tc.expected {
t.Error("unexpected HTTP code", res.StatusCode)
}
})
}
}
|