Exemplo n.º 1
0
func TestHandlerErrors(t *testing.T) {
	tests := []struct {
		name      string
		expect    func(meta, kv *envelopetest.MockHandler)
		wantError error
	}{
		{
			name: "Meta:health",
			expect: func(meta, kv *envelopetest.MockHandler) {
				meta.EXPECT().Handle("health", gomock.Any()).
					Return(wire.NewValueStruct(wire.Struct{}), nil)
			},
		},
		{
			name: "KeyValue:setValue",
			expect: func(meta, kv *envelopetest.MockHandler) {
				kv.EXPECT().Handle("setValue", gomock.Any()).
					Return(wire.NewValueStruct(wire.Struct{}), nil)
			},
		},
		{
			name:      "keyValue:setValue",
			wantError: envelope.ErrUnknownMethod("keyValue:setValue"),
		},
		{
			name:      "setValue",
			wantError: envelope.ErrUnknownMethod("setValue"),
		},
	}

	for _, tt := range tests {
		func() {
			mockCtrl := gomock.NewController(t)
			defer mockCtrl.Finish()

			meta := envelopetest.NewMockHandler(mockCtrl)
			kv := envelopetest.NewMockHandler(mockCtrl)

			handler := NewHandler()
			handler.Put("Meta", meta)
			handler.Put("KeyValue", kv)

			if tt.expect != nil {
				tt.expect(meta, kv)
			}

			_, err := handler.Handle(tt.name, wire.NewValueStruct(wire.Struct{}))
			assert.Equal(t, tt.wantError, err)
		}()
	}
}
Exemplo n.º 2
0
// Handle handles the given request, dispatching to one of the
// registered services.
func (h Handler) Handle(name string, req wire.Value) (wire.Value, error) {
	parts := strings.SplitN(name, ":", 2)
	if len(parts) < 2 {
		return wire.Value{}, envelope.ErrUnknownMethod(name)
	}

	service, ok := h.services[parts[0]]
	if !ok {
		return wire.Value{}, envelope.ErrUnknownMethod(name)
	}

	name = parts[1]
	return service.Handle(name, req)
}
// Handle receives and handles a request for the ServiceGenerator service.
func (h ServiceGeneratorHandler) Handle(name string, reqValue wire.Value) (wire.Value, error) {
	switch name {

	case "generate":

		var args ServiceGenerator_Generate_Args
		if err := args.FromWire(reqValue); err != nil {
			return wire.Value{}, err
		}

		result, err := ServiceGenerator_Generate_Helper.WrapResponse(
			h.impl.Generate(args.Request),
		)
		if err != nil {
			return wire.Value{}, err
		}

		return result.ToWire()

	default:

		return wire.Value{}, envelope.ErrUnknownMethod(name)

	}
}
Exemplo n.º 4
0
// Handle receives and handles a request for the Plugin service.
func (h PluginHandler) Handle(name string, reqValue wire.Value) (wire.Value, error) {
	switch name {

	case "goodbye":

		var args Plugin_Goodbye_Args
		if err := args.FromWire(reqValue); err != nil {
			return wire.Value{}, err
		}

		result, err := Plugin_Goodbye_Helper.WrapResponse(
			h.impl.Goodbye(),
		)
		if err != nil {
			return wire.Value{}, err
		}

		return result.ToWire()

	case "handshake":

		var args Plugin_Handshake_Args
		if err := args.FromWire(reqValue); err != nil {
			return wire.Value{}, err
		}

		result, err := Plugin_Handshake_Helper.WrapResponse(
			h.impl.Handshake(args.Request),
		)
		if err != nil {
			return wire.Value{}, err
		}

		return result.ToWire()

	default:

		return wire.Value{}, envelope.ErrUnknownMethod(name)

	}
}