tests: add SortAndCheck helper (#926)

There was quite some code duplication in a lot of tests to check if
an answer was considered Ok. Created a test.SortAndCheck helper function
that takes care of this.
This commit is contained in:
Miek Gieben 2017-08-16 15:30:58 +01:00 committed by GitHub
parent 65b56248f0
commit 7f46df6d27
24 changed files with 58 additions and 520 deletions

View file

@ -1,6 +1,7 @@
package test
import (
"sort"
"testing"
"github.com/miekg/dns"
@ -263,6 +264,33 @@ func Section(t *testing.T, tc Case, sec sect, rr []dns.RR) bool {
return true
}
// SortAndCheck sorts resp and the checks the header and three sections against the testcase in tc.
func SortAndCheck(t *testing.T, resp *dns.Msg, tc Case) {
sort.Sort(RRSet(resp.Answer))
sort.Sort(RRSet(resp.Ns))
sort.Sort(RRSet(resp.Extra))
if !Header(t, tc, resp) {
t.Logf("%v\n", resp)
return
}
if !Section(t, tc, Answer, resp.Answer) {
t.Logf("%v\n", resp)
return
}
if !Section(t, tc, Ns, resp.Ns) {
t.Logf("%v\n", resp)
return
}
if !Section(t, tc, Extra, resp.Extra) {
t.Logf("%v\n", resp)
return
}
return
}
// ErrorHandler returns a Handler that returns ServerFailure error when called.
func ErrorHandler() Handler {
return HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {