example_test.go 969 B

1234567891011121314151617181920212223242526272829303132333435
  1. package example
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "github.com/coredns/coredns/plugin/pkg/dnstest"
  7. "github.com/coredns/coredns/plugin/test"
  8. "github.com/miekg/dns"
  9. )
  10. func TestExample(t *testing.T) {
  11. // Create a new Example Plugin. Use the test.ErrorHandler as the next plugin.
  12. x := Example{Next: test.ErrorHandler()}
  13. // Setup a new output buffer that is *not* standard output, so we can check if
  14. // example is really being printed.
  15. b := &bytes.Buffer{}
  16. out = b
  17. ctx := context.TODO()
  18. r := new(dns.Msg)
  19. r.SetQuestion("example.org.", dns.TypeA)
  20. // Create a new Recorder that captures the result, this isn't actually used in this test
  21. // as it just serves as something that implements the dns.ResponseWriter interface.
  22. rec := dnstest.NewRecorder(&test.ResponseWriter{})
  23. // Call our plugin directly, and check the result.
  24. x.ServeDNS(ctx, rec, r)
  25. if a := b.String(); a != "example\n" {
  26. t.Errorf("Failed to print '%s', got %s", example, a)
  27. }
  28. }