From f5f1ee72e7a8b3c8553f2f71d9dbf9fe7e47166f Mon Sep 17 00:00:00 2001 From: acoshift Date: Fri, 3 Aug 2018 09:55:48 +0700 Subject: [PATCH] handle nil result --- handler.go | 6 +++++- result.go | 4 +--- result_test.go | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/handler.go b/handler.go index df3a63e..317a757 100644 --- a/handler.go +++ b/handler.go @@ -8,7 +8,11 @@ import ( func Wrap(h Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := NewContext(w, r) - h(ctx).ServeHTTP(ctx.w, ctx.r) + + result := h(ctx) + if result != nil { + result.ServeHTTP(ctx.w, ctx.r) + } }) } diff --git a/result.go b/result.go index ea8ead4..95607b9 100644 --- a/result.go +++ b/result.go @@ -104,9 +104,7 @@ func (ctx *Context) Error(error string) Result { // Nothing does nothing func (ctx *Context) Nothing() Result { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // do nothing - }) + return nil } // NotFound calls http.NotFound diff --git a/result_test.go b/result_test.go index 183c9e5..4de7c02 100644 --- a/result_test.go +++ b/result_test.go @@ -124,6 +124,21 @@ func TestResult(t *testing.T) { assert.Equal(t, "hello, hime", w.Body.String()) }) + t.Run("Nil", func(t *testing.T) { + t.Parallel() + + app := hime.New(). + Handler(hime.H(func(ctx *hime.Context) hime.Result { + return nil + })) + + assert.NotPanics(t, func() { + w := invokeHandler(app, "GET", "/", nil) + assert.Equal(t, http.StatusOK, w.Result().StatusCode) + assert.Empty(t, w.Body.String()) + }) + }) + t.Run("Nothing", func(t *testing.T) { t.Parallel()