Example #1
0
func (m Muxer) Handle(method, path string, handler stack.Handler, middleware ...stack.Middleware) {
	s := stack.NewStack(handler).Use(middleware...)
	m.Router.Handle(path, s).Methods(method).Name(fmt.Sprintf("%s %s", method, path))
}
Example #2
0
type PanickingMiddleware struct {
	err error
}

func (m PanickingMiddleware) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) bool {
	if m.err != nil {
		panic(m.err)
	}
	return true
}

var _ = Describe("Stack", func() {
	It("assigns the Handler as the last object passed to the constructor", func() {
		handler := Handler{}
		s := stack.NewStack(handler).Use(Middleware{})
		Expect(s.Handler).To(Equal(handler))
	})

	It("assigns the remaining handlers as middleware", func() {
		middleware1 := Middleware{}
		middleware2 := Middleware{}
		s := stack.NewStack(Handler{}).Use(middleware1, middleware2)
		Expect(len(s.Middleware)).To(Equal(2))
		Expect(s.Middleware).To(ContainElement(middleware1))
		Expect(s.Middleware).To(ContainElement(middleware2))
	})

	It("executes the stack in order", func() {
		s := stack.NewStack(Handler{}).Use(Middleware{"first"}, Middleware{"second"})
		writer := httptest.NewRecorder()