func TestUnionFromWireInconsistencies(t *testing.T) { tests := []struct { desc string input wire.Value success *tu.Document failure string }{ { desc: "multiple recognized fields", input: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueBinary([]byte{1, 2, 3})}, {ID: 2, Value: wire.NewValueString("hello")}, }}), failure: "should have exactly one field: got 2 fields", }, { desc: "recognized and unrecognized fields", input: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueBinary([]byte{1, 2, 3})}, {ID: 3, Value: wire.NewValueString("hello")}, }}), success: &tu.Document{Pdf: []byte{1, 2, 3}}, }, { desc: "recognized field duplicates", input: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueBinary([]byte{1, 2, 3})}, {ID: 1, Value: wire.NewValueBinary([]byte{4, 5, 6})}, }}), success: &tu.Document{Pdf: []byte{4, 5, 6}}, }, { desc: "only unrecognized fields", input: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 2, Value: wire.NewValueI32(42)}, // also a type mismatch {ID: 3, Value: wire.NewValueString("hello")}, }}), failure: "should have exactly one field: got 0 fields", }, { desc: "no fields", input: wire.NewValueStruct(wire.Struct{}), failure: "should have exactly one field: got 0 fields", }, } for _, tt := range tests { var o tu.Document err := o.FromWire(tt.input) if tt.success != nil { if assert.NoError(t, err, tt.desc) { assert.Equal(t, tt.success, &o, tt.desc) } } else { if assert.Error(t, err, tt.desc) { assert.Contains(t, err.Error(), tt.failure, tt.desc) } } } }
func TestClient(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockClient := envelopetest.NewMockClient(mockCtrl) mockClient.EXPECT().Send("Foo:hello", wire.NewValueStruct(wire.Struct{})). Return(wire.NewValueStruct(wire.Struct{}), nil) client := NewClient("Foo", mockClient) _, err := client.Send("hello", wire.NewValueStruct(wire.Struct{})) assert.NoError(t, err) }
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) }() } }
func (v *TypePair) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Left == nil { return w, errors.New("field Left of TypePair is required") } w, err = v.Left.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ if v.Right == nil { return w, errors.New("field Right of TypePair is required") } w, err = v.Right.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Plugin_Goodbye_Result) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *SecondService_BlahBlah_Result) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *ThriftTest_TestMultiException_Args) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Arg0 != nil { w, err = wire.NewValueString(*(v.Arg0)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.Arg1 != nil { w, err = wire.NewValueString(*(v.Arg1)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *ThriftTest_TestVoid_Result) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *MapOfBinaryAndString) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.BinaryToString != nil { w, err = wire.NewValueMap(_Map_Binary_String_MapItemList(v.BinaryToString)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.StringToBinary != nil { w, err = wire.NewValueMap(_Map_String_Binary_MapItemList(v.StringToBinary)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *NonStandardServiceName_NonStandardFunctionName_Result) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Frame) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.TopLeft == nil { return w, errors.New("field TopLeft of Frame is required") } w, err = v.TopLeft.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ if v.Size == nil { return w, errors.New("field Size of Frame is required") } w, err = v.Size.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Edge) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.StartPoint == nil { return w, errors.New("field StartPoint of Edge is required") } w, err = v.StartPoint.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ if v.EndPoint == nil { return w, errors.New("field EndPoint of Edge is required") } w, err = v.EndPoint.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *PrimitiveRequiredStruct) ToWire() (wire.Value, error) { var ( fields [8]wire.Field i int = 0 w wire.Value err error ) w, err = wire.NewValueBool(v.BoolField), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ w, err = wire.NewValueI8(v.ByteField), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ w, err = wire.NewValueI16(v.Int16Field), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 3, Value: w} i++ w, err = wire.NewValueI32(v.Int32Field), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 4, Value: w} i++ w, err = wire.NewValueI64(v.Int64Field), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 5, Value: w} i++ w, err = wire.NewValueDouble(v.DoubleField), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 6, Value: w} i++ w, err = wire.NewValueString(v.StringField), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 7, Value: w} i++ if v.BinaryField == nil { return w, errors.New("field BinaryField of PrimitiveRequiredStruct is required") } w, err = wire.NewValueBinary(v.BinaryField), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 8, Value: w} i++ return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *HandshakeRequest) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *EmptyUnion) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *PrimitiveContainers) ToWire() (wire.Value, error) { var ( fields [3]wire.Field i int = 0 w wire.Value err error ) if v.A != nil { w, err = wire.NewValueList(_List_String_ValueList(v.A)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.B != nil { w, err = wire.NewValueSet(_Set_String_ValueList(v.B)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 3, Value: w} i++ } if v.C != nil { w, err = wire.NewValueMap(_Map_String_String_MapItemList(v.C)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 5, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *KeyValue_DeleteValue_Result) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.DoesNotExist != nil { w, err = v.DoesNotExist.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.InternalError != nil { w, err = v.InternalError.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } if i > 1 { return wire.Value{}, fmt.Errorf("KeyValue_DeleteValue_Result should have at most one field: got %v fields", i) } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *UnionCollision2) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.CollisionField != nil { w, err = wire.NewValueBool(*(v.CollisionField)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.CollisionField2 != nil { w, err = wire.NewValueString(*(v.CollisionField2)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } if i != 1 { return wire.Value{}, fmt.Errorf("UnionCollision2 should have exactly one field: got %v fields", i) } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *EnumContainers) ToWire() (wire.Value, error) { var ( fields [3]wire.Field i int = 0 w wire.Value err error ) if v.ListOfEnums != nil { w, err = wire.NewValueList(_List_EnumDefault_ValueList(v.ListOfEnums)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.SetOfEnums != nil { w, err = wire.NewValueSet(_Set_EnumWithValues_ValueList(v.SetOfEnums)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } if v.MapOfEnums != nil { w, err = wire.NewValueMap(_Map_EnumWithDuplicateValues_I32_MapItemList(v.MapOfEnums)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 3, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Document) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Pdf != nil { w, err = v.Pdf.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.PlainText != nil { w, err = wire.NewValueString(*(v.PlainText)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } if i != 1 { return wire.Value{}, fmt.Errorf("Document should have exactly one field: got %v fields", i) } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Transition) ToWire() (wire.Value, error) { var ( fields [3]wire.Field i int = 0 w wire.Value err error ) w, err = v.FromState.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ w, err = v.ToState.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ if v.Events != nil { w, err = v.Events.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 3, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Event) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.UUID == nil { return w, errors.New("field UUID of Event is required") } w, err = v.UUID.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ if v.Time != nil { w, err = v.Time.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Type) ToWire() (wire.Value, error) { var ( fields [6]wire.Field i int = 0 w wire.Value err error ) if v.SimpleType != nil { w, err = v.SimpleType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.SliceType != nil { w, err = v.SliceType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } if v.KeyValueSliceType != nil { w, err = v.KeyValueSliceType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 3, Value: w} i++ } if v.MapType != nil { w, err = v.MapType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 4, Value: w} i++ } if v.ReferenceType != nil { w, err = v.ReferenceType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 5, Value: w} i++ } if v.PointerType != nil { w, err = v.PointerType.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 6, Value: w} i++ } if i != 1 { return wire.Value{}, fmt.Errorf("Type should have exactly one field: got %v fields", i) } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *TApplicationException) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Message != nil { w, err = wire.NewValueString(*(v.Message)), error(nil) if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.Type != nil { w, err = v.Type.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *Cache_Clear_Args) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *KeyValue_SetValue_Args) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Key != nil { w, err = v.Key.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if v.Value != nil { w, err = v.Value.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 2, Value: w} i++ } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *KeyValue_SetValue_Result) ToWire() (wire.Value, error) { var ( fields [0]wire.Field i int = 0 ) return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func (v *KeyValue_GetValue_Result) ToWire() (wire.Value, error) { var ( fields [2]wire.Field i int = 0 w wire.Value err error ) if v.Success != nil { w, err = v.Success.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 0, Value: w} i++ } if v.DoesNotExist != nil { w, err = v.DoesNotExist.ToWire() if err != nil { return w, err } fields[i] = wire.Field{ID: 1, Value: w} i++ } if i != 1 { return wire.Value{}, fmt.Errorf("KeyValue_GetValue_Result should have exactly one field: got %v fields", i) } return wire.NewValueStruct(wire.Struct{Fields: fields[:i]}), nil }
func TestStructFromWireUnrecognizedField(t *testing.T) { tests := []struct { desc string give wire.Value want ts.ContactInfo wantError string }{ { desc: "unknown field", give: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueString("foo")}, {ID: 2, Value: wire.NewValueI32(42)}, }}), want: ts.ContactInfo{EmailAddress: "foo"}, }, { desc: "only unknown field", give: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 2, Value: wire.NewValueString("bar")}, }}), wantError: "field EmailAddress of ContactInfo is required", }, { desc: "wrong type for recognized field", give: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueI32(42)}, {ID: 1, Value: wire.NewValueString("foo")}, }}), want: ts.ContactInfo{EmailAddress: "foo"}, }, } for _, tt := range tests { var o ts.ContactInfo err := o.FromWire(tt.give) if tt.wantError != "" { if assert.Error(t, err, tt.desc) { assert.Contains(t, err.Error(), tt.wantError) } } else { if assert.NoError(t, err, tt.desc) { assert.Equal(t, tt.want, o) } } } }
func TestDisableEnveloperEncode(t *testing.T) { rand := rand.New(rand.NewSource(time.Now().Unix())) tests := []struct { value wire.Value want []byte }{ { wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}), []byte{0x00}, }, { wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueI32(42)}, }}), []byte{ 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2a, 0x00, }, }, } for _, tt := range tests { e := wire.Envelope{Value: tt.value, Type: wire.Call} generate(&e.Name, rand) generate(&e.SeqID, rand) var buffer bytes.Buffer proto := disableEnvelopingProtocol{protocol.Binary, wire.Reply} if !assert.NoError(t, proto.EncodeEnveloped(e, &buffer)) { continue } assert.Equal(t, tt.want, buffer.Bytes()) gotE, err := proto.DecodeEnveloped(bytes.NewReader(tt.want)) if !assert.NoError(t, err) { continue } assert.Equal(t, wire.Reply, gotE.Type) assert.True(t, wire.ValuesAreEqual(tt.value, gotE.Value)) } }