diff options
author | 2016-03-19 12:58:08 +0000 | |
---|---|---|
committer | 2016-03-19 13:37:20 +0000 | |
commit | ae5783b7c4fd168799cab639229b7ea6f1ccbbe6 (patch) | |
tree | 4bb1e69e4ba16d5acd3bf115fac4bc57c5e61de3 /middleware/errors/errors_test.go | |
parent | afc4b85d8654eb4cdff945bc8119af22f6dc4d18 (diff) | |
download | coredns-ae5783b7c4fd168799cab639229b7ea6f1ccbbe6.tar.gz coredns-ae5783b7c4fd168799cab639229b7ea6f1ccbbe6.tar.zst coredns-ae5783b7c4fd168799cab639229b7ea6f1ccbbe6.zip |
Errors directive testing and fixing
Drop a few tests and make it work and compile.
Also add the documentation: errors.md
Diffstat (limited to 'middleware/errors/errors_test.go')
-rw-r--r-- | middleware/errors/errors_test.go | 129 |
1 files changed, 35 insertions, 94 deletions
diff --git a/middleware/errors/errors_test.go b/middleware/errors/errors_test.go index f4b7aab45..77783bf6d 100644 --- a/middleware/errors/errors_test.go +++ b/middleware/errors/errors_test.go @@ -1,89 +1,53 @@ package errors -/* -func TestErrors(t *testing.T) { - // create a temporary page - path := filepath.Join(os.TempDir(), "errors_test.html") - f, err := os.Create(path) - if err != nil { - t.Fatal(err) - } - defer os.Remove(path) +import ( + "bytes" + "errors" + "fmt" + "log" + "strings" + "testing" - const content = "This is a error page" - _, err = f.WriteString(content) - if err != nil { - t.Fatal(err) - } - f.Close() + "golang.org/x/net/context" + + "github.com/miekg/coredns/middleware" + "github.com/miekg/dns" +) +func TestErrors(t *testing.T) { buf := bytes.Buffer{} - em := ErrorHandler{ - ErrorPages: map[int]string{ - http.StatusNotFound: path, - http.StatusForbidden: "not_exist_file", - }, - Log: log.New(&buf, "", 0), - } - _, notExistErr := os.Open("not_exist_file") + em := ErrorHandler{Log: log.New(&buf, "", 0)} testErr := errors.New("test error") tests := []struct { next middleware.Handler expectedCode int - expectedBody string expectedLog string expectedErr error }{ { - next: genErrorHandler(http.StatusOK, nil, "normal"), - expectedCode: http.StatusOK, - expectedBody: "normal", + next: genErrorHandler(dns.RcodeSuccess, nil), + expectedCode: dns.RcodeSuccess, expectedLog: "", expectedErr: nil, }, { - next: genErrorHandler(http.StatusMovedPermanently, testErr, ""), - expectedCode: http.StatusMovedPermanently, - expectedBody: "", - expectedLog: fmt.Sprintf("[ERROR %d %s] %v\n", http.StatusMovedPermanently, "/", testErr), + next: genErrorHandler(dns.RcodeNotAuth, testErr), + expectedCode: dns.RcodeNotAuth, + expectedLog: fmt.Sprintf("[ERROR %d %s] %v\n", dns.RcodeNotAuth, "example.org. A", testErr), expectedErr: testErr, }, - { - next: genErrorHandler(http.StatusBadRequest, nil, ""), - expectedCode: 0, - expectedBody: fmt.Sprintf("%d %s\n", http.StatusBadRequest, - http.StatusText(http.StatusBadRequest)), - expectedLog: "", - expectedErr: nil, - }, - { - next: genErrorHandler(http.StatusNotFound, nil, ""), - expectedCode: 0, - expectedBody: content, - expectedLog: "", - expectedErr: nil, - }, - { - next: genErrorHandler(http.StatusForbidden, nil, ""), - expectedCode: 0, - expectedBody: fmt.Sprintf("%d %s\n", http.StatusForbidden, - http.StatusText(http.StatusForbidden)), - expectedLog: fmt.Sprintf("[NOTICE %d /] could not load error page: %v\n", - http.StatusForbidden, notExistErr), - expectedErr: nil, - }, } - req, err := http.NewRequest("GET", "/", nil) - if err != nil { - t.Fatal(err) - } + ctx := context.TODO() + req := new(dns.Msg) + req.SetQuestion("example.org.", dns.TypeA) + for i, test := range tests { em.Next = test.next buf.Reset() - rec := httptest.NewRecorder() - code, err := em.ServeHTTP(rec, req) + rec := middleware.NewResponseRecorder(&middleware.TestResponseWriter{}) + code, err := em.ServeDNS(ctx, rec, req) if err != test.expectedErr { t.Errorf("Test %d: Expected error %v, but got %v", @@ -93,10 +57,6 @@ func TestErrors(t *testing.T) { t.Errorf("Test %d: Expected status code %d, but got %d", i, test.expectedCode, code) } - if body := rec.Body.String(); body != test.expectedBody { - t.Errorf("Test %d: Expected body %q, but got %q", - i, test.expectedBody, body) - } if log := buf.String(); !strings.Contains(log, test.expectedLog) { t.Errorf("Test %d: Expected log %q, but got %q", i, test.expectedLog, log) @@ -107,48 +67,29 @@ func TestErrors(t *testing.T) { func TestVisibleErrorWithPanic(t *testing.T) { const panicMsg = "I'm a panic" eh := ErrorHandler{ - ErrorPages: make(map[int]string), - Debug: true, - Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + Debug: true, + Next: middleware.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { panic(panicMsg) }), } - req, err := http.NewRequest("GET", "/", nil) - if err != nil { - t.Fatal(err) - } - rec := httptest.NewRecorder() + ctx := context.TODO() + req := new(dns.Msg) + req.SetQuestion("example.org.", dns.TypeA) - code, err := eh.ServeHTTP(rec, req) + rec := middleware.NewResponseRecorder(&middleware.TestResponseWriter{}) + code, err := eh.ServeDNS(ctx, rec, req) if code != 0 { t.Errorf("Expected error handler to return 0 (it should write to response), got status %d", code) } if err != nil { t.Errorf("Expected error handler to return nil error (it should panic!), but got '%v'", err) } - - body := rec.Body.String() - - if !strings.Contains(body, "[PANIC /] middleware/errors/errors_test.go") { - t.Errorf("Expected response body to contain error log line, but it didn't:\n%s", body) - } - if !strings.Contains(body, panicMsg) { - t.Errorf("Expected response body to contain panic message, but it didn't:\n%s", body) - } - if len(body) < 500 { - t.Errorf("Expected response body to contain stack trace, but it was too short: len=%d", len(body)) - } } -func genErrorHandler(status int, err error, body string) middleware.Handler { - return middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { - if len(body) > 0 { - w.Header().Set("Content-Length", strconv.Itoa(len(body))) - fmt.Fprint(w, body) - } - return status, err +func genErrorHandler(rcode int, err error) middleware.Handler { + return middleware.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { + return rcode, err }) } -*/ |