| 1 |
package dnshttp |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"codeberg.org/miekg/dns" |
| 8 |
"codeberg.org/miekg/dns/dnsutil" |
| 9 |
) |
| 10 |
|
| 11 |
func TestDOH(t *testing.T) { |
| 12 |
testcases := map[string]struct { |
| 13 |
method string |
| 14 |
url string |
| 15 |
}{ |
| 16 |
"POST request HTTPS": {method: http.MethodPost, url: "https://example.org:443"}, |
| 17 |
"POST request HTTP": {method: http.MethodPost, url: "http://example.org:443"}, |
| 18 |
"POST request no protocol": {method: http.MethodPost, url: "example.org:443"}, |
| 19 |
"GET request HTTPS": {method: http.MethodGet, url: "https://example.org:443"}, |
| 20 |
"GET request HTTP": {method: http.MethodGet, url: "http://example.org"}, |
| 21 |
"GET request no protocol": {method: http.MethodGet, url: "example.org:443"}, |
| 22 |
} |
| 23 |
|
| 24 |
MsgAcceptFunc = func(m *dns.Msg) dns.MsgAcceptAction { return dns.MsgAccept } |
| 25 |
|
| 26 |
for name, tc := range testcases { |
| 27 |
t.Run(name, func(t *testing.T) { |
| 28 |
m := new(dns.Msg) |
| 29 |
dnsutil.SetQuestion(m, "example.org.", dns.TypeDNSKEY) |
| 30 |
|
| 31 |
req, err := NewRequest(tc.method, tc.url, m) |
| 32 |
if err != nil { |
| 33 |
t.Fatalf("failure to make request: %s", err) |
| 34 |
} |
| 35 |
ct := req.Header.Get("Content-Type") |
| 36 |
if ct != "" && req.Method == http.MethodGet { |
| 37 |
t.Errorf("GET request should not have a Content-Type header") |
| 38 |
} |
| 39 |
if ct == "" && req.Method == http.MethodPost { |
| 40 |
t.Errorf("POST request should have a Content-Type header") |
| 41 |
} |
| 42 |
|
| 43 |
m1, err := Request(req) |
| 44 |
if err != nil { |
| 45 |
t.Fatalf("failure to get message from request: %s", err) |
| 46 |
} |
| 47 |
|
| 48 |
if x := m1.Question[0].Header().Name; x != "example.org." { |
| 49 |
t.Errorf("qname expected %s, got %s", "example.org.", x) |
| 50 |
} |
| 51 |
if x, ok := m1.Question[0].(*dns.DNSKEY); !ok { |
| 52 |
t.Errorf("qtype expected %T, got %T", &dns.DNSKEY{}, x) |
| 53 |
} |
| 54 |
}) |
| 55 |
} |
| 56 |
} |