コード例 #1
0
ファイル: http_handler_test.go プロジェクト: MG-RAST/Shock
func TestPrependPreHandler(t *testing.T) {

	handler1 := new(handlers_test.TestHandler)
	handler2 := new(handlers_test.TestHandler)
	codecService := codecsservices.NewWebCodecService()
	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").Data())
	assert.Equal(t, 1, h.PreHandlersPipe()[1].(*handlers_test.TestHandler).TestData().Get("id").Data())

	mock.AssertExpectationsForObjects(t, handler1.Mock)

}
コード例 #2
0
ファイル: pipe_test.go プロジェクト: MG-RAST/Shock
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
0
func TestMarshalWithCodec_WithFacade(t *testing.T) {

	// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	var bytesToReturn []byte = []byte("Hello World")
	testObjectWithFacade := new(test.TestObjectWithFacade)
	object := objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testObjectWithFacade.On("PublicData", args).Return(object, nil)
	testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)

	bytes, err := service.MarshalWithCodec(testCodec, testObjectWithFacade, args)

	if assert.Nil(t, err) {
		assert.Equal(t, string(bytesToReturn), string(bytes))
	}

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock, testObjectWithFacade.Mock)

}
コード例 #4
0
func TestMarshalWithCodec_WithError(t *testing.T) {

	// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	object := objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testCodec.On("Marshal", object, args).Return(nil, assert.AnError)

	_, err := service.MarshalWithCodec(testCodec, object, args)

	assert.Equal(t, assert.AnError, err, "The error should get returned")

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock)

}
コード例 #5
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicData_WithArray(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o1 := new(test.TestObjectWithFacade)
	o2 := new(test.TestObjectWithFacade)

	arr := []interface{}{o, o1, o2}

	o.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "1"}), nil)
	o1.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "2"}), nil)
	o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "3"}), nil)

	public, err := PublicData(arr, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Equal(t, reflect.Slice, reflect.TypeOf(public).Kind(), "Result should be array not %v", reflect.TypeOf(public))
	}

	mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)

	publicArray := public.([]interface{})
	if assert.Equal(t, 3, len(publicArray)) {
		assert.Equal(t, publicArray[0].(objx.Map).Get("theName").Str(), "1", "o")
		assert.Equal(t, publicArray[1].(objx.Map).Get("theName").Str(), "2", "o1")
		assert.Equal(t, publicArray[2].(objx.Map).Get("theName").Str(), "3", "o2")
	}

}
コード例 #6
0
func TestMarshalWithCodec(t *testing.T) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	var bytesToReturn []byte = []byte("Hello World")
	var object objx.Map = objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)

	bytes, err := service.MarshalWithCodec(testCodec, object, args)

	if assert.Nil(t, err) {
		assert.Equal(t, string(bytesToReturn), string(bytes))
	}

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock)

}
コード例 #7
0
ファイル: http_handler_test.go プロジェクト: MG-RAST/Shock
func TestErrorHandlerGetsUsedOnError(t *testing.T) {

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

	codecService := codecsservices.NewWebCodecService()
	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").Data().(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())

	}

}
コード例 #8
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicData_WithError(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o.Mock.On("PublicData", map[string]interface{}{}).Return(nil, assert.AnError)

	_, err := PublicData(o, map[string]interface{}{})

	assert.Equal(t, assert.AnError, err)
	mock.AssertExpectationsForObjects(t, o.Mock)

}
コード例 #9
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicDataMap_ReturningNil(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o.Mock.On("PublicData", map[string]interface{}{}).Return(nil, nil)

	public, err := PublicDataMap(o, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Nil(t, public)
	}

	mock.AssertExpectationsForObjects(t, o.Mock)

}
コード例 #10
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicDataMap_WithMSI(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o.Mock.On("PublicData", map[string]interface{}{}).Return(map[string]interface{}{"theName": "Mat"}, nil)

	public, err := PublicDataMap(o, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Equal(t, public.Get("theName").Str(), "Mat")
	}

	mock.AssertExpectationsForObjects(t, o.Mock)

}
コード例 #11
0
ファイル: http_handler_test.go プロジェクト: MG-RAST/Shock
func TestAppendPostHandler(t *testing.T) {

	handler1 := new(handlers_test.TestHandler)
	codecService := codecsservices.NewWebCodecService()
	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)

}
コード例 #12
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicData_WithRecursion(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o1 := new(test.TestObjectWithFacade)
	o2 := new(test.TestObjectWithFacade)

	o.Mock.On("PublicData", map[string]interface{}{}).Return(o1, nil)
	o1.Mock.On("PublicData", map[string]interface{}{}).Return(o2, nil)
	o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil)

	public, err := PublicData(o, map[string]interface{}{})

	if assert.Nil(t, err) {
		assert.Equal(t, public.(objx.Map).Get("theName").Str(), "Mat")
	}

	mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)

}
コード例 #13
0
ファイル: http_handler_test.go プロジェクト: MG-RAST/Shock
func TestServeHTTP(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.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")

}
コード例 #14
0
ファイル: mapping_test.go プロジェクト: MG-RAST/Shock
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")

}
コード例 #15
0
ファイル: facade_test.go プロジェクト: MG-RAST/Shock
func TestPublicData_WithRecursion_WithObjects(t *testing.T) {

	o := new(test.TestObjectWithFacade)
	o1 := new(test.TestObjectWithFacade)
	o2 := new(test.TestObjectWithFacade)

	args := map[string]interface{}{constants.OptionKeyClientCallback: "~d"}

	o.Mock.On("PublicData", args).Return(o1, nil)
	o1.Mock.On("PublicData", args).Return(o2, nil)
	o2.Mock.On("PublicData", args).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil)

	public, err := PublicData(o, args)

	if assert.Nil(t, err) {
		assert.Equal(t, public.(objx.Map).Get("theName").Str(), "Mat")
	}

	mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)

}
コード例 #16
0
func TestUnmarshalWithCodec_WithError(t *testing.T) {

	// func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// some test objects
	object := struct{}{}
	data := []byte("Some bytes")

	// setup expectations
	testCodec.On("Unmarshal", data, object).Return(assert.AnError)

	// call the target method
	err := service.UnmarshalWithCodec(testCodec, data, object)

	assert.Equal(t, assert.AnError, err)
	mock.AssertExpectationsForObjects(t, testCodec.Mock)

}
コード例 #17
0
ファイル: http_handler_test.go プロジェクト: MG-RAST/Shock
func TestDataGetsCopiedToEachContext(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	handler1 := new(handlers_test.TestHandler)
	h := NewHttpHandler(codecService)

	h.Data.Set("name", "Mat")

	handler1.On("WillHandle", mock.Anything).Return(true, nil)
	handler1.On("Handle", mock.Anything).Return(false, nil)

	h.AppendHandler(handler1)
	req, _ := http.NewRequest("GET", "something", nil)
	h.ServeHTTP(nil, req)

	assert.Equal(t, 1, len(h.HandlersPipe()))

	mock.AssertExpectationsForObjects(t, handler1.Mock)
	ctx := handler1.Calls[0].Arguments[0].(context.Context)

	assert.Equal(t, ctx.Data().Get("name").Str(), "Mat")

}