forked from grpc-ecosystem/grpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_json_test.go
333 lines (304 loc) · 10.2 KB
/
marshal_json_test.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package runtime_test
import (
"bytes"
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"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/testing/protocmp"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func TestJSONBuiltinMarshal(t *testing.T) {
var m runtime.JSONBuiltin
msg := &examplepb.SimpleMessage{
Id: "foo",
}
buf, err := m.Marshal(msg)
if err != nil {
t.Errorf("m.Marshal(%v) failed with %v; want success", msg, err)
}
got := new(examplepb.SimpleMessage)
if err := json.Unmarshal(buf, got); err != nil {
t.Errorf("json.Unmarshal(%q, got) failed with %v; want success", buf, err)
}
if diff := cmp.Diff(got, msg, protocmp.Transform()); diff != "" {
t.Error(diff)
}
}
func TestJSONBuiltinMarshalField(t *testing.T) {
var (
m runtime.JSONBuiltin
buf []byte
err error
)
for _, fixt := range builtinFieldFixtures {
if len(fixt.indent) == 0 {
buf, err = m.Marshal(fixt.data)
if err != nil {
t.Errorf("m.Marshal(%v) failed with %v; want success", fixt.data, err)
}
} else {
buf, err = m.MarshalIndent(fixt.data, "", fixt.indent)
if err != nil {
t.Errorf("m.MarshalIndent(%v, \"\", \"%s\") failed with %v; want success", fixt.data, fixt.indent, err)
}
}
if got, want := string(buf), fixt.json; got != want {
t.Errorf("got = %q; want %q; data = %#v", got, want, fixt.data)
}
}
}
func TestJSONBuiltinMarshalFieldKnownErrors(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinKnownErrors {
buf, err := m.Marshal(fixt.data)
if err != nil {
t.Errorf("m.Marshal(%v) failed with %v; want success", fixt.data, err)
}
if got, want := string(buf), fixt.json; got == want {
t.Errorf("surprisingly got = %q; as want %q; data = %#v", got, want, fixt.data)
}
}
}
func TestJSONBuiltinsnmarshal(t *testing.T) {
var (
m runtime.JSONBuiltin
got = new(examplepb.SimpleMessage)
data = []byte(`{"id": "foo"}`)
)
if err := m.Unmarshal(data, got); err != nil {
t.Errorf("m.Unmarshal(%q, got) failed with %v; want success", data, err)
}
want := &examplepb.SimpleMessage{
Id: "foo",
}
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
t.Error(diff)
}
}
func TestJSONBuiltinUnmarshalField(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinFieldFixtures {
dest := alloc(reflect.TypeOf(fixt.data))
if err := m.Unmarshal([]byte(fixt.json), dest.Interface()); err != nil {
t.Errorf("m.Unmarshal(%q, dest) failed with %v; want success", fixt.json, err)
}
got, want := dest.Elem().Interface(), fixt.data
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
t.Error(diff)
}
}
}
func alloc(t reflect.Type) reflect.Value {
if t == nil {
return reflect.ValueOf(new(interface{}))
}
return reflect.New(t)
}
func TestJSONBuiltinUnmarshalFieldKnownErrors(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinKnownErrors {
dest := reflect.New(reflect.TypeOf(fixt.data))
if err := m.Unmarshal([]byte(fixt.json), dest.Interface()); err == nil {
t.Errorf("m.Unmarshal(%q, dest) succeeded; want an error", fixt.json)
}
}
}
func TestJSONBuiltinEncoder(t *testing.T) {
var m runtime.JSONBuiltin
msg := &examplepb.SimpleMessage{
Id: "foo",
}
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if err := enc.Encode(msg); err != nil {
t.Errorf("enc.Encode(%v) failed with %v; want success", msg, err)
}
got := new(examplepb.SimpleMessage)
if err := json.Unmarshal(buf.Bytes(), got); err != nil {
t.Errorf("json.Unmarshal(%q, got) failed with %v; want success", buf.String(), err)
}
if diff := cmp.Diff(got, msg, protocmp.Transform()); diff != "" {
t.Error(diff)
}
}
func TestJSONBuiltinEncoderFields(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinFieldFixtures {
var buf bytes.Buffer
enc := m.NewEncoder(&buf)
if fixt.indent != "" {
if e, ok := enc.(*json.Encoder); ok {
e.SetIndent("", fixt.indent)
} else {
// By default, JSONBuiltin.NewEncoder returns *json.Encoder as runtime.Encoder.
// Otherwise it's better to fail the tests than skip fixtures with non empty indent
t.Errorf("enc is not *json.Encoder, unable to set indentation settings. " +
"This failure prevents testing the correctness of indentation in JSON output.")
}
}
if err := enc.Encode(fixt.data); err != nil {
t.Errorf("enc.Encode(%#v) failed with %v; want success", fixt.data, err)
}
if got, want := buf.String(), fixt.json+"\n"; got != want {
t.Errorf("got = %q; want %q; data = %#v", got, want, fixt.data)
}
}
}
func TestJSONBuiltinDecoder(t *testing.T) {
var (
m runtime.JSONBuiltin
got = new(examplepb.SimpleMessage)
data = `{"id": "foo"}`
)
r := strings.NewReader(data)
dec := m.NewDecoder(r)
if err := dec.Decode(got); err != nil {
t.Errorf("m.Unmarshal(got) failed with %v; want success", err)
}
want := &examplepb.SimpleMessage{
Id: "foo",
}
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
t.Errorf("got = %v; want = %v", got, want)
}
}
func TestJSONBuiltinDecoderFields(t *testing.T) {
var m runtime.JSONBuiltin
for _, fixt := range builtinFieldFixtures {
r := strings.NewReader(fixt.json)
dec := m.NewDecoder(r)
dest := alloc(reflect.TypeOf(fixt.data))
if err := dec.Decode(dest.Interface()); err != nil {
t.Errorf("dec.Decode(dest) failed with %v; want success; data = %q", err, fixt.json)
}
got, want := dest.Elem().Interface(), fixt.data
if diff := cmp.Diff(got, want, protocmp.Transform()); diff != "" {
t.Error(diff)
}
}
}
var (
defaultIndent = " "
builtinFieldFixtures = []struct {
data interface{}
indent string
json string
}{
{data: "", json: `""`},
{data: "", indent: defaultIndent, json: `""`},
{data: proto.String(""), json: `""`},
{data: proto.String(""), indent: defaultIndent, json: `""`},
{data: "foo", json: `"foo"`},
{data: "foo", indent: defaultIndent, json: `"foo"`},
{data: []byte("foo"), json: `"Zm9v"`},
{data: []byte("foo"), indent: defaultIndent, json: `"Zm9v"`},
{data: []byte{}, json: `""`},
{data: []byte{}, indent: defaultIndent, json: `""`},
{data: proto.String("foo"), json: `"foo"`},
{data: proto.String("foo"), indent: defaultIndent, json: `"foo"`},
{data: int32(-1), json: "-1"},
{data: int32(-1), indent: defaultIndent, json: "-1"},
{data: proto.Int32(-1), json: "-1"},
{data: proto.Int32(-1), indent: defaultIndent, json: "-1"},
{data: int64(-1), json: "-1"},
{data: int64(-1), indent: defaultIndent, json: "-1"},
{data: proto.Int64(-1), json: "-1"},
{data: proto.Int64(-1), indent: defaultIndent, json: "-1"},
{data: uint32(123), json: "123"},
{data: uint32(123), indent: defaultIndent, json: "123"},
{data: proto.Uint32(123), json: "123"},
{data: proto.Uint32(123), indent: defaultIndent, json: "123"},
{data: uint64(123), json: "123"},
{data: uint64(123), indent: defaultIndent, json: "123"},
{data: proto.Uint64(123), json: "123"},
{data: proto.Uint64(123), indent: defaultIndent, json: "123"},
{data: float32(-1.5), json: "-1.5"},
{data: float32(-1.5), indent: defaultIndent, json: "-1.5"},
{data: proto.Float32(-1.5), json: "-1.5"},
{data: proto.Float32(-1.5), indent: defaultIndent, json: "-1.5"},
{data: float64(-1.5), json: "-1.5"},
{data: float64(-1.5), indent: defaultIndent, json: "-1.5"},
{data: proto.Float64(-1.5), json: "-1.5"},
{data: proto.Float64(-1.5), indent: defaultIndent, json: "-1.5"},
{data: true, json: "true"},
{data: true, indent: defaultIndent, json: "true"},
{data: proto.Bool(true), json: "true"},
{data: proto.Bool(true), indent: defaultIndent, json: "true"},
{data: (*string)(nil), json: "null"},
{data: (*string)(nil), indent: defaultIndent, json: "null"},
{data: new(emptypb.Empty), json: "{}"},
{data: new(emptypb.Empty), indent: defaultIndent, json: "{}"},
{data: examplepb.NumericEnum_ONE, json: "1"},
{data: examplepb.NumericEnum_ONE, indent: defaultIndent, json: "1"},
{data: nil, json: "null"},
{data: nil, indent: defaultIndent, json: "null"},
{data: (*string)(nil), json: "null"},
{data: (*string)(nil), indent: defaultIndent, json: "null"},
{data: []interface{}{nil, "foo", -1.0, 1.234, true}, json: `[null,"foo",-1,1.234,true]`},
{data: []interface{}{nil, "foo", -1.0, 1.234, true}, indent: defaultIndent, json: "[\n null,\n \"foo\",\n -1,\n 1.234,\n true\n]"},
{
data: map[string]interface{}{"bar": nil, "baz": -1.0, "fiz": 1.234, "foo": true},
json: `{"bar":null,"baz":-1,"fiz":1.234,"foo":true}`,
},
{
data: map[string]interface{}{"bar": nil, "baz": -1.0, "fiz": 1.234, "foo": true},
indent: defaultIndent,
json: "{\n \"bar\": null,\n \"baz\": -1,\n \"fiz\": 1.234,\n \"foo\": true\n}",
},
{
data: (*examplepb.NumericEnum)(proto.Int32(int32(examplepb.NumericEnum_ONE))),
json: "1",
},
{
data: (*examplepb.NumericEnum)(proto.Int32(int32(examplepb.NumericEnum_ONE))),
indent: defaultIndent,
json: "1",
},
{data: map[string]int{"FOO": 0, "BAR": -1}, json: "{\"BAR\":-1,\"FOO\":0}"},
{data: map[string]int{"FOO": 0, "BAR": -1}, indent: defaultIndent, json: "{\n \"BAR\": -1,\n \"FOO\": 0\n}"},
{data: struct {
A string
B int
C map[string]int
}{A: "Go", B: 3, C: map[string]int{"FOO": 0, "BAR": -1}},
json: "{\"A\":\"Go\",\"B\":3,\"C\":{\"BAR\":-1,\"FOO\":0}}"},
{data: struct {
A string
B int
C map[string]int
}{A: "Go", B: 3, C: map[string]int{"FOO": 0, "BAR": -1}}, indent: defaultIndent,
json: "{\n \"A\": \"Go\",\n \"B\": 3,\n \"C\": {\n \"BAR\": -1,\n \"FOO\": 0\n }\n}"},
}
builtinKnownErrors = []struct {
data interface{}
json string
}{
{data: examplepb.NumericEnum_ONE, json: "ONE"},
{
data: (*examplepb.NumericEnum)(proto.Int32(int32(examplepb.NumericEnum_ONE))),
json: "ONE",
},
{
data: &examplepb.ABitOfEverything_OneofString{OneofString: "abc"},
json: `"abc"`,
},
{
data: ×tamppb.Timestamp{
Seconds: 1462875553,
Nanos: 123000000,
},
json: `"2016-05-10T10:19:13.123Z"`,
},
{
data: wrapperspb.Int32(123),
json: "123",
},
}
)