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