1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
package runtime_test
import (
"bytes"
"testing"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime/internal/examplepb"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
var message = &examplepb.ABitOfEverything{
SingleNested: &examplepb.ABitOfEverything_Nested{},
RepeatedStringValue: nil,
MappedStringValue: nil,
MappedNestedValue: nil,
RepeatedEnumValue: nil,
TimestampValue: ×tamppb.Timestamp{},
Uuid: "6EC2446F-7E89-4127-B3E6-5C05E6BECBA7",
Nested: []*examplepb.ABitOfEverything_Nested{
{
Name: "foo",
Amount: 12345,
},
},
Uint64Value: 0xFFFFFFFFFFFFFFFF,
EnumValue: examplepb.NumericEnum_ONE,
OneofValue: &examplepb.ABitOfEverything_OneofString{
OneofString: "bar",
},
MapValue: map[string]examplepb.NumericEnum{
"a": examplepb.NumericEnum_ONE,
"b": examplepb.NumericEnum_ZERO,
},
}
func TestProtoMarshalUnmarshal(t *testing.T) {
marshaller := runtime.ProtoMarshaller{}
// Marshal
buffer, err := marshaller.Marshal(message)
if err != nil {
t.Fatalf("Marshalling returned error: %s", err.Error())
}
// Unmarshal
unmarshalled := &examplepb.ABitOfEverything{}
err = marshaller.Unmarshal(buffer, unmarshalled)
if err != nil {
t.Fatalf("Unmarshalling returned error: %s", err.Error())
}
if !proto.Equal(unmarshalled, message) {
t.Errorf(
"Unmarshalled didn't match original message: (original = %v) != (unmarshalled = %v)",
unmarshalled,
message,
)
}
}
func TestProtoEncoderDecodert(t *testing.T) {
marshaller := runtime.ProtoMarshaller{}
var buf bytes.Buffer
encoder := marshaller.NewEncoder(&buf)
decoder := marshaller.NewDecoder(&buf)
// Encode
err := encoder.Encode(message)
if err != nil {
t.Fatalf("Encoding returned error: %s", err.Error())
}
// Decode
unencoded := &examplepb.ABitOfEverything{}
err = decoder.Decode(unencoded)
if err != nil {
t.Fatalf("Unmarshalling returned error: %s", err.Error())
}
if !proto.Equal(unencoded, message) {
t.Errorf(
"Unencoded didn't match original message: (original = %v) != (unencoded = %v)",
unencoded,
message,
)
}
}
|