Ejemplo n.º 1
0
Archivo: hello.go Proyecto: jqk6/H2
// Hello provides a func to process an incoming 'hello' request.
// A handler requires a request parameter and will return either a response or an error
func Hello(req h2.Request) (proto.Message, h2.Error) {
	request := req.Data().(*protoHello.Request)

	// Error example
	if len(request.GetName()) == 0 {
		// We specify multi error types in the platform layer repository in the 'errors'
		// package. See: https://godoc.org/github.com/hailocab/platform-layer/errors
		return nil, errors.BadRequest(HelloEndpoint, "You didn't specify a name in the request")
	}

	// Return response with the message set to `Hello <name>`
	return &protoHello.Response{
		Message: proto.String(fmt.Sprintf("Hello %q", request.GetName())),
	}, nil
}
func TestFluentStubbingWithError(t *testing.T) {
	mock := NewMock()
	mock.
		On(mockFooService, mockHealthEndpoint).
		Fail(errors.BadRequest("code", "description"))

	req, _ := client.NewRequest(mockFooService, mockHealthEndpoint, NewDummy("ping"))
	rsp := &Dummy{}

	// Fail with given error
	err := mock.Caller()(req, rsp)
	assert.NotNil(t, err)
	assert.Equal(t, err.Code(), "code")
	assert.Equal(t, err.Description(), "description")
}
func (suite *multiClientSuite) TestAnyErrorsIgnoring() {
	cases := []struct {
		errs         map[string]errors.Error
		types, codes []string
		isErr        bool
	}{
		{
			errs:  map[string]errors.Error{},
			types: []string{},
			codes: []string{},
			isErr: false,
		},
		// allow nils
		{
			errs:  map[string]errors.Error{},
			types: nil,
			codes: nil,
			isErr: false,
		},
		// no errors = always false
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{},
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: nil,
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: nil,
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		// types
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: false,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadRequest("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: true,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
				"bar": errors.BadRequest("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: true,
		},
		// codes
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar"},
			isErr: false,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar.baz"},
			isErr: true,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
				"bar": errors.BadResponse("foo.bar.baz", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar"},
			isErr: true,
		},
	}

	cl := &defClient{
		errors: &errorsImpl{},
	}

	for _, tc := range cases {
		for uid, err := range tc.errs {
			cl.errors.set(uid, &client.Request{}, err, nil)
		}

		res := cl.AnyErrorsIgnoring(tc.types, tc.codes)
		suite.Assertions.Equal(tc.isErr, res, fmt.Sprintf("Wrong result for errors: %v - types: %v - codes: %v",
			tc.errs, tc.types, tc.codes))
	}
}