From 48a91d05b5adc3cbb55da81596d7e3ddab1834dd Mon Sep 17 00:00:00 2001 From: Emile Vauge Date: Sat, 13 May 2017 19:36:37 +0200 Subject: [PATCH] Add Recover tests --- middlewares/recover.go | 3 ++- middlewares/recover_test.go | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 middlewares/recover_test.go diff --git a/middlewares/recover.go b/middlewares/recover.go index 3b8475d2a..fff7c2e0b 100644 --- a/middlewares/recover.go +++ b/middlewares/recover.go @@ -1,9 +1,10 @@ package middlewares import ( + "net/http" + "github.com/codegangsta/negroni" "github.com/containous/traefik/log" - "net/http" ) // RecoverHandler recovers from a panic in http handlers diff --git a/middlewares/recover_test.go b/middlewares/recover_test.go new file mode 100644 index 000000000..31cf098ca --- /dev/null +++ b/middlewares/recover_test.go @@ -0,0 +1,45 @@ +package middlewares + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/codegangsta/negroni" +) + +func TestRecoverHandler(t *testing.T) { + fn := func(w http.ResponseWriter, r *http.Request) { + panic("I love panicing!") + } + recoverHandler := RecoverHandler(http.HandlerFunc(fn)) + server := httptest.NewServer(recoverHandler) + defer server.Close() + + resp, err := http.Get(server.URL) + if err != nil { + t.Fatal(err) + } + if resp.StatusCode != http.StatusInternalServerError { + t.Fatalf("Received non-%d response: %d\n", http.StatusInternalServerError, resp.StatusCode) + } +} + +func TestNegroniRecoverHandler(t *testing.T) { + n := negroni.New() + n.Use(NegroniRecoverHandler()) + panicHandler := func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + panic("I love panicing!") + } + n.UseFunc(negroni.HandlerFunc(panicHandler)) + server := httptest.NewServer(n) + defer server.Close() + + resp, err := http.Get(server.URL) + if err != nil { + t.Fatal(err) + } + if resp.StatusCode != http.StatusInternalServerError { + t.Fatalf("Received non-%d response: %d\n", http.StatusInternalServerError, resp.StatusCode) + } +}