Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax ThenFunc signature to allow any HandlerFunc #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ func (c Chain) Then(h http.Handler) http.Handler {
// c.ThenFunc(fn)
//
// ThenFunc provides all the guarantees of Then.
func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler {
// This nil check cannot be removed due to the "nil is not nil" common mistake in Go.
// Required due to: https://stackoverflow.com/questions/33426977/how-to-golang-check-a-variable-is-nil
func (c Chain) ThenFunc(fn func(http.ResponseWriter, *http.Request)) http.Handler {
// We want to preserve the special behavior similar to Then(nil)
if fn == nil {
return c.Then(nil)
}
return c.Then(fn)
return c.Then(http.HandlerFunc(fn))
}

// Append extends a chain, adding the specified constructors
Expand Down
3 changes: 3 additions & 0 deletions chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func TestThenFuncTreatsNilAsDefaultServeMux(t *testing.T) {
if New().ThenFunc(nil) != http.DefaultServeMux {
t.Error("ThenFunc does not treat nil as DefaultServeMux")
}
if New().ThenFunc(http.HandlerFunc(nil)) != http.DefaultServeMux {
t.Error("ThenFunc does not treat nil as DefaultServeMux")
}
}

func TestThenFuncConstructsHandlerFunc(t *testing.T) {
Expand Down
Loading