From 482620961818293e78caeeef13c797877eb3c70f Mon Sep 17 00:00:00 2001 From: hawry Date: Fri, 12 Jan 2018 16:28:57 +0100 Subject: [PATCH] add error handler to middlewares --- .gitignore | 1 + README.md | 4 ++++ error_handler.go | 44 ++++-------------------------------------- error_handler_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ fp_handler.go | 1 - 5 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 error_handler_test.go delete mode 100644 fp_handler.go diff --git a/.gitignore b/.gitignore index e69de29..fa92975 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +*.out \ No newline at end of file diff --git a/README.md b/README.md index 862ed31..bf9f210 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,7 @@ ## Usage Checkout the GoDoc page for the documentation: https://godoc.org/github.com/Hawry/middlewares + +## Testing +To see the test coverage and which lines that are being tested, run: +`go test -coverprofile=cp.out && go tool cover -html=cp.out` \ No newline at end of file diff --git a/error_handler.go b/error_handler.go index c9cb3bf..1838368 100644 --- a/error_handler.go +++ b/error_handler.go @@ -2,7 +2,6 @@ package middlewares import ( "html/template" - "log" "net/http" ) @@ -12,40 +11,7 @@ type errorHandler struct { body []byte } -const errBody = ` - - - - - {{.StatusCode}} - {{.StatusText}} - - - - -
-

{{.StatusCode}}

-

- {{.StatusMessage}} -

-
- - - -` +const errBody = `{{.StatusCode}} - {{.StatusText}}

{{.StatusCode}}

{{.StatusMessage}}

` var errTemplate *template.Template @@ -53,13 +19,12 @@ func init() { errTemplate = template.Must(template.New("err").Parse(errBody)) } -//ErrorHandler will inject a html response to any error status code +//ErrorHandler will inject a html response to any error status code (400/500 range) func ErrorHandler(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { eh := &errorHandler{ResponseWriter: w, statusCode: http.StatusOK} next.ServeHTTP(eh, r) if eh.statusCode >= 400 && eh.statusCode <= 509 { - // do something dval := map[string]interface{}{ "StatusCode": eh.statusCode, "StatusText": http.StatusText(eh.statusCode), @@ -70,16 +35,15 @@ func ErrorHandler(next http.Handler) http.Handler { }) } -// WriteHeader shadows ResponseWriter.Write +// WriteHeader shadows ResponseWriter.Write. If the code is in the 400 or 500 range, the error handler will be used to display the corresponding error page func (e *errorHandler) WriteHeader(code int) { e.statusCode = code e.ResponseWriter.WriteHeader(code) } -//Write shadows http.ResponseWriter.Write +//Write shadows http.ResponseWriter.Write and writes the body as the text message in the error page func (e *errorHandler) Write(b []byte) (n int, err error) { if e.statusCode >= 400 && e.statusCode <= 509 { - log.Printf("err handler write called") e.body = append(e.body, b...) // use any written text as the error message } else { e.ResponseWriter.Write(b) diff --git a/error_handler_test.go b/error_handler_test.go new file mode 100644 index 0000000..e5e305e --- /dev/null +++ b/error_handler_test.go @@ -0,0 +1,45 @@ +package middlewares + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +var tNotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("the page could not be found")) +}) + +var tFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("the page could be found")) +}) + +func TestErrorBody(t *testing.T) { + req, err := http.NewRequest("GET", "/notfound", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler := ErrorHandler(tNotFound) + handler.ServeHTTP(rr, req) + assert.Contains(t, rr.Body.String(), "the page could not be found") + assert.Equal(t, http.StatusNotFound, rr.Code) +} + +func TestPassthrough(t *testing.T) { + req, err := http.NewRequest("GET", "/found", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler := ErrorHandler(tFound) + handler.ServeHTTP(rr, req) + + assert.Contains(t, rr.Body.String(), "the page could be found") + assert.Equal(t, http.StatusOK, rr.Code) +} diff --git a/fp_handler.go b/fp_handler.go deleted file mode 100644 index ea553b7..0000000 --- a/fp_handler.go +++ /dev/null @@ -1 +0,0 @@ -package middlewares