| 25 | ) |
| 26 | |
| 27 | func TestChain(t *testing.T) { |
| 28 | a := assertions.New(t) |
| 29 | |
| 30 | var trace []string |
| 31 | middleware := []MiddlewareFunc{ |
| 32 | func(next http.Handler) http.Handler { |
| 33 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 34 | trace = append(trace, "outer begin") |
| 35 | next.ServeHTTP(w, r) |
| 36 | trace = append(trace, "outer end") |
| 37 | }) |
| 38 | }, |
| 39 | func(next http.Handler) http.Handler { |
| 40 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | trace = append(trace, "inner begin") |
| 42 | next.ServeHTTP(w, r) |
| 43 | trace = append(trace, "inner end") |
| 44 | }) |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | chain := Chain(middleware, http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 49 | trace = append(trace, "handler") |
| 50 | })) |
| 51 | chain.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil)) |
| 52 | |
| 53 | a.So(trace, should.Resemble, []string{"outer begin", "inner begin", "handler", "inner end", "outer end"}) |
| 54 | } |
| 55 | |
| 56 | func TestConditional(t *testing.T) { |
| 57 | a := assertions.New(t) |