Exemple #1
0
// New creates a new multiplexer with default settings.
func New() *Mux {
	m := &Mux{Layer: plugin.New()}
	m.Middleware = middleware.New()
	handler := m.Handler()
	m.DefaultHandler = handler
	return m
}
func TestMiddlewarePlugin(t *testing.T) {
	calls := 0
	fn := func(ctx *context.Context, h context.Handler) {
		calls++
		h.Next(ctx)
	}
	plugin := plugin.New()
	plugin.SetHandler("request", fn)
	plugin.SetHandler("response", fn)
	plugin.SetHandler("error", fn)

	mw := New()
	mw.Use(plugin)

	ctx := context.New()
	mw.Run("request", ctx)
	mw.Run("error", ctx)
	mw.Run("response", ctx)

	if calls != 3 {
		t.Errorf("Invalid middleware calls: %d", calls)
	}
}