2018-01-26 18:43:28 +00:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-09-24 06:16:55 +00:00
|
|
|
"context"
|
2018-01-26 18:43:28 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
|
|
|
"github.com/coredns/coredns/plugin/test"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExample(t *testing.T) {
|
2018-02-25 08:52:52 +00:00
|
|
|
// Create a new Example Plugin. Use the test.ErrorHandler as the next plugin.
|
|
|
|
x := Example{Next: test.ErrorHandler()}
|
2018-01-26 18:43:28 +00:00
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Setup a new output buffer that is *not* standard output, so we can check if
|
|
|
|
// example is really being printed.
|
2018-01-26 18:43:28 +00:00
|
|
|
b := &bytes.Buffer{}
|
|
|
|
out = b
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
r := new(dns.Msg)
|
|
|
|
r.SetQuestion("example.org.", dns.TypeA)
|
2018-02-25 08:52:52 +00:00
|
|
|
// Create a new Recorder that captures the result, this isn't actually used in this test
|
|
|
|
// as it just serves as something that implements the dns.ResponseWriter interface.
|
2018-01-26 18:43:28 +00:00
|
|
|
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
|
|
|
|
2018-02-25 08:52:52 +00:00
|
|
|
// Call our plugin directly, and check the result.
|
|
|
|
x.ServeDNS(ctx, rec, r)
|
2019-08-15 08:18:00 +00:00
|
|
|
if a := b.String(); a != "example\n" {
|
|
|
|
t.Errorf("Failed to print '%s', got %s", example, a)
|
2018-01-26 18:43:28 +00:00
|
|
|
}
|
|
|
|
}
|