本文整理汇总了Golang中github.com/stretchrcom/goweb/handlers/test.TestHandler.On方法的典型用法代码示例。如果您正苦于以下问题:Golang TestHandler.On方法的具体用法?Golang TestHandler.On怎么用?Golang TestHandler.On使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/stretchrcom/goweb/handlers/test.TestHandler
的用法示例。
在下文中一共展示了TestHandler.On方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestErrorHandlerGetsUsedOnError
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())
}
}
示例2: TestPipe_Handle
func TestPipe_Handle(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(false, nil)
handler2.On("WillHandle", ctx).Return(false, nil)
handler3.On("WillHandle", ctx).Return(true, nil)
handler3.On("Handle", ctx).Return(false, nil)
// call handle
p.Handle(ctx)
// assert expectations
mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock)
}
示例3: TestPrependPreHandler
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)
}
示例4: TestAppendPostHandler
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)
}
示例5: TestBeforeAndAfterHandlers
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")
}
示例6: TestServeHTTP
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")
}