Пример #1
0
func newHandler() *handler {
	h := &handler{}
	h.fn = context.NewHandler(func(c *context.Context) {
		h.called = true
	})
	return h
}
Пример #2
0
func TestNewErrorPlugin(t *testing.T) {
	called := false
	plugin := NewErrorPlugin(func(c *context.Context, h context.Handler) { h.Next(c) })
	plugin.Exec("error", context.New(), context.NewHandler(func(c *context.Context) { called = true }))
	if !called {
		t.Errorf("Handler not called")
	}
}
Пример #3
0
func TestPluginLayer(t *testing.T) {
	phase := ""
	fn := func(c *context.Context, h context.Handler) {
		phase = c.GetString("$phase")
		h.Next(c)
	}

	ctx := context.New()
	plugin := New()
	plugin.SetHandler("request", fn)
	plugin.SetHandler("response", fn)
	plugin.SetHandler("error", fn)

	calls := 0
	createHandler := func() context.Handler {
		return context.NewHandler(func(c *context.Context) { calls++ })
	}

	ctx.Set("$phase", "request")
	plugin.Exec("request", ctx, createHandler())
	if phase != "request" {
		t.Errorf("Invalid phase: %s", phase)
	}

	ctx.Set("$phase", "response")
	plugin.Exec("response", ctx, createHandler())
	if phase != "response" {
		t.Errorf("Invalid phase: %s", phase)
	}

	ctx.Set("$phase", "error")
	plugin.Exec("error", ctx, createHandler())
	if phase != "error" {
		t.Errorf("Invalid phase: %s", phase)
	}

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