Refactoring. 100% test coverage. Updated README.md
diff --git a/README.md b/README.md index 88d2134..44da570 100644 --- a/README.md +++ b/README.md
@@ -1,6 +1,8 @@ # Alice +[](http://godoc.org/github.com/justinas/alice) [](https://travis-ci.org/justinas/alice) +[](http://gocover.io/github.com/justinas/alice) Alice provides a convenient way to chain your HTTP middleware functions and the app handler.
diff --git a/chain.go b/chain.go index 8adf0c0..7bb0e6f 100644 --- a/chain.go +++ b/chain.go
@@ -43,18 +43,15 @@ // // Then() treats nil as http.DefaultServeMux. func (c Chain) Then(h http.Handler) http.Handler { - var final http.Handler - if h != nil { - final = h - } else { - final = http.DefaultServeMux + if h == nil { + h = http.DefaultServeMux } for i := len(c.constructors) - 1; i >= 0; i-- { - final = c.constructors[i](final) + h = c.constructors[i](h) } - return final + return h } // ThenFunc works identically to Then, but takes @@ -86,8 +83,7 @@ copy(newCons, c.constructors) copy(newCons[len(c.constructors):], constructors) - newChain := New(newCons...) - return newChain + return New(newCons...) } // Extend extends a chain by adding the specified chain
diff --git a/chain_test.go b/chain_test.go index 49c0470..97e0ff1 100644 --- a/chain_test.go +++ b/chain_test.go
@@ -69,6 +69,16 @@ assert.Equal(t, chained, http.DefaultServeMux) } +func TestThenFuncConstructsHandlerFunc(t *testing.T) { + fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + }) + chained := New().ThenFunc(fn) + rec := httptest.NewRecorder() + chained.ServeHTTP(rec, (*http.Request)(nil)) + assert.Equal(t, 200, rec.Code) +} + func TestThenOrdersHandlersRight(t *testing.T) { t1 := tagMiddleware("t1\n") t2 := tagMiddleware("t2\n")