Пример #1
0
func TestBeforeAndAfterHandlers(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil)
	codecService := codecsservices.NewWebCodecService()
	handler := NewHttpHandler(codecService)

	// setup some test handlers
	handler1 := new(handlers_test.TestHandler)
	handler2 := new(handlers_test.TestHandler)
	handler3 := new(handlers_test.TestHandler)

	handler.MapBefore(func(c context.Context) error {
		_, err := handler1.Handle(c)
		return err
	})
	handler.Map(func(c context.Context) error {
		_, err := handler2.Handle(c)
		return err
	})
	handler.MapAfter(func(c context.Context) error {
		_, err := handler3.Handle(c)
		return err
	})

	handler1.On("Handle", mock.Anything).Return(false, nil)
	handler2.On("Handle", mock.Anything).Return(false, nil)
	handler3.On("Handle", mock.Anything).Return(false, nil)

	handler.ServeHTTP(responseWriter, testRequest)

	mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock)

	// make sure it's always the same context
	ctx1 := handler1.Calls[0].Arguments[0].(context.Context)
	ctx2 := handler2.Calls[0].Arguments[0].(context.Context)
	ctx3 := handler3.Calls[0].Arguments[0].(context.Context)

	assert.Equal(t, ctx1, ctx2, "Contexts should be the same")
	assert.Equal(t, ctx2, ctx3, "Contexts should be the same")

}