Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
upgraded Alice
  • Loading branch information
nbari committed Feb 11, 2016
2 parents a0e0833 + 9927897 commit 224b7c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
16 changes: 16 additions & 0 deletions middleware/Makefile
@@ -0,0 +1,16 @@
.PHONY: all test build cover

GO ?= go

all: build test

build:
${GO} build

test:
${GO} test -v

cover:
${GO} test -cover && \
${GO} test -coverprofile=coverage.out && \
${GO} tool cover -html=coverage.out
11 changes: 4 additions & 7 deletions middleware/middleware.go
Expand Up @@ -61,18 +61,15 @@ func New(constructors ...Constructor) Chain {
// m1(m2(m3(h)))
// 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
Expand Down
10 changes: 10 additions & 0 deletions middleware/middleware_test.go
Expand Up @@ -69,6 +69,16 @@ func TestThenFuncTreatsNilAsDefaultServeMux(t *testing.T) {
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")
Expand Down

0 comments on commit 224b7c5

Please sign in to comment.