func TestErrorHandlerGetsUsedOnError(t *testing.T) { responseWriter := new(http_test.TestResponseWriter) testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil) codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) errorHandler := new(handlers_test.TestHandler) handler.SetErrorHandler(errorHandler) errorHandler.On("Handle", mock.Anything).Return(false, nil) // make a handler throw an error var theError error = errors.New("Test error") handler.Map(func(c context.Context) error { return theError }) handler.ServeHTTP(responseWriter, testRequest) if mock.AssertExpectationsForObjects(t, errorHandler.Mock) { // get the first context ctx := errorHandler.Calls[0].Arguments[0].(context.Context) // make sure the error data field was set assert.Equal(t, theError.Error(), ctx.Data().Get("error").(HandlerError).Error(), "the error should be set in the data with the 'error' key") assert.Equal(t, responseWriter, ctx.HttpResponseWriter()) assert.Equal(t, testRequest, ctx.HttpRequest()) } }
func TestAppendPostHandler(t *testing.T) { handler1 := new(handlers_test.TestHandler) codecService := new(codecservices.WebCodecService) h := NewHttpHandler(codecService) handler1.On("WillHandle", mock.Anything).Return(true, nil) handler1.On("Handle", mock.Anything).Return(false, nil) h.AppendPostHandler(handler1) h.Handlers.Handle(nil) assert.Equal(t, 1, len(h.PostHandlersPipe())) mock.AssertExpectationsForObjects(t, handler1.Mock) }
func TestPipe_Handle_Stopping(t *testing.T) { ctx := context_test.MakeTestContext() handler1 := new(handlers_test.TestHandler) handler2 := new(handlers_test.TestHandler) handler3 := new(handlers_test.TestHandler) // add the handlers to the pipe p := Pipe{handler1, handler2, handler3} // setup expectations handler1.On("WillHandle", ctx).Return(true, nil) handler1.On("Handle", ctx).Return(true, nil) // call handle p.Handle(ctx) // assert expectations mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock) }
func TestBeforeAndAfterHandlers(t *testing.T) { responseWriter := new(http_test.TestResponseWriter) testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil) codecService := new(codecservices.WebCodecService) 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") }
func TestPrependPreHandler(t *testing.T) { handler1 := new(handlers_test.TestHandler) handler2 := new(handlers_test.TestHandler) codecService := new(codecservices.WebCodecService) h := NewHttpHandler(codecService) handler1.TestData().Set("id", 1) handler2.TestData().Set("id", 2) handler1.On("WillHandle", mock.Anything).Return(true, nil) handler1.On("Handle", mock.Anything).Return(false, nil) handler2.On("WillHandle", mock.Anything).Return(true, nil) handler2.On("Handle", mock.Anything).Return(false, nil) h.PrependPreHandler(handler1) h.PrependPreHandler(handler2) h.Handlers.Handle(nil) assert.Equal(t, 2, len(h.PreHandlersPipe())) assert.Equal(t, 2, h.PreHandlersPipe()[0].(*handlers_test.TestHandler).TestData().Get("id")) assert.Equal(t, 1, h.PreHandlersPipe()[1].(*handlers_test.TestHandler).TestData().Get("id")) mock.AssertExpectationsForObjects(t, handler1.Mock) }
func TestServeHTTP(t *testing.T) { responseWriter := new(http_test.TestResponseWriter) testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil) codecService := new(codecservices.WebCodecService) handler := NewHttpHandler(codecService) // setup some test handlers handler1 := new(handlers_test.TestHandler) handler2 := new(handlers_test.TestHandler) handler3 := new(handlers_test.TestHandler) handler.Handlers = append(handler.Handlers, handler1) handler.Handlers = append(handler.Handlers, handler2) handler.Handlers = append(handler.Handlers, handler3) handler1.On("WillHandle", mock.Anything).Return(true, nil) handler1.On("Handle", mock.Anything).Return(false, nil) handler2.On("WillHandle", mock.Anything).Return(true, nil) handler2.On("Handle", mock.Anything).Return(false, nil) handler3.On("WillHandle", mock.Anything).Return(true, nil) handler3.On("Handle", mock.Anything).Return(false, nil) handler.ServeHTTP(responseWriter, testRequest) mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock) // get the first context ctx := handler1.Calls[0].Arguments[0].(context.Context) assert.Equal(t, responseWriter, ctx.HttpResponseWriter()) assert.Equal(t, testRequest, ctx.HttpRequest()) // 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") }