Skip to content

Commit

Permalink
Merge pull request #43 from acoshift/nil-result
Browse files Browse the repository at this point in the history
handle nil result
  • Loading branch information
acoshift committed Aug 3, 2018
2 parents 6ddc0a6 + f5f1ee7 commit 99d0a67
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

Expand Down
4 changes: 1 addition & 3 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 99d0a67

Please sign in to comment.