forked from kazegusuri/grpcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_test.go
334 lines (302 loc) · 11.8 KB
/
call_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
334
package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/golang/protobuf/jsonpb"
pb "github.com/kazegusuri/grpcurl/internal/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleCall() {
cmd := NewRootCommand(strings.NewReader(`{"value": "hello"}`), os.Stdout)
cmd.Command().SetArgs([]string{"-k", "call", addr, "grpcurl.test.Echo.Echo"})
cmd.Command().Execute()
// Output:
// {"value":"hello","error_code":0}
}
func testCall(method, msg string) (*bytes.Buffer, error) {
buf := &bytes.Buffer{}
buf.Grow(1024)
cmd := NewRootCommand(strings.NewReader(msg), buf)
cmd.Command().SetArgs([]string{"-k", "call", "-v", addr, method})
return buf, cmd.Command().Execute()
}
type testResponse struct {
RequestMessage string
ResponseMessage string
RequestHeader map[string][]string
ResponseHeader map[string][]string
ResponseTrailer map[string][]string
}
func parseTestResponse(s string) *testResponse {
lines := strings.Split(s, "\n")
msgs := map[string][]string{}
marker := ""
for i := range lines {
switch lines[i] {
case requestMessageMarker:
marker = requestMessageMarker
case responseMessageMarker:
marker = responseMessageMarker
case responseHeaderMarker:
marker = responseHeaderMarker
case responseTrailerMarker:
marker = responseTrailerMarker
default:
msgs[marker] = append(msgs[marker], lines[i])
}
}
return &testResponse{
RequestMessage: strings.Join(msgs[requestMessageMarker], "\n"),
ResponseMessage: strings.Join(msgs[responseMessageMarker], "\n"),
}
}
func TestCallEcho(t *testing.T) {
buf, err := testCall("grpcurl.test.Echo.Echo", `{"value": "xxx"}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"value":"xxx","error_code":0}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestCallEchoEmpty(t *testing.T) {
buf, err := testCall("grpcurl.test.Echo.Echo", `{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"value":"","error_code":0}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestCallEchoUnknownField(t *testing.T) {
buf, err := testCall("grpcurl.test.Echo.Echo", `{"xxx": "vvvv"}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"value":"","error_code":0}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingSimple(t *testing.T) {
buf, err := testCall(
"grpcurl.test.Everything.Simple",
`{"string_value": "aaa", "bool_value": true}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"string_value":"aaa","bool_value":true}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingSimpleEmpty(t *testing.T) {
buf, err := testCall(
"grpcurl.test.Everything.Simple",
`{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"string_value":"","bool_value":false}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingNumber(t *testing.T) {
m := &jsonpb.Marshaler{OrigName: true}
msg, err := m.MarshalToString(&pb.NumberMessage{
FloatValue: 1.1,
DoubleValue: 2.2,
Int32Value: 3,
Int64Value: 4,
Uint32Value: 5,
Uint64Value: 6,
Sint32Value: 7,
Sint64Value: 8,
Fixed32Value: 9,
Fixed64Value: 10,
Sfixed32Value: 11,
Sfixed64Value: 12,
})
require.NoError(t, err)
t.Logf("request messge: %q", msg)
buf, err := testCall("grpcurl.test.Everything.Number", msg)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"float_value":1.1,"double_value":2.2,"int32_value":3,"int64_value":4,"uint32_value":5,"uint64_value":6,"sint32_value":7,"sint64_value":8,"fixed32_value":9,"fixed64_value":10,"sfixed32_value":11,"sfixed64_value":12}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingNumberEmpty(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Number", `{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"float_value":0,"double_value":0,"int32_value":0,"int64_value":0,"uint32_value":0,"uint64_value":0,"sint32_value":0,"sint64_value":0,"fixed32_value":0,"fixed64_value":0,"sfixed32_value":0,"sfixed64_value":0}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingEnum(t *testing.T) {
m := &jsonpb.Marshaler{OrigName: true}
msg, err := m.MarshalToString(&pb.EnumMessage{
NumericEnumValue: pb.NumericEnum_ONE,
RepeatedNumericEnumValues: []pb.NumericEnum{
pb.NumericEnum_ONE,
pb.NumericEnum_TWO,
},
AliasedEnumValue: pb.AliasedEnum_RUNNING,
NestedEnumValue: pb.EnumMessage_PENDING,
RepeatedNestedEnumValues: []pb.EnumMessage_Nested{
pb.EnumMessage_PENDING,
pb.EnumMessage_COMPLETED,
},
})
require.NoError(t, err)
t.Logf("request messge: %q", msg)
buf, err := testCall("grpcurl.test.Everything.Enum", msg)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"numeric_enum_value":"ONE","repeated_numeric_enum_values":["ONE","TWO"],"aliased_enum_value":"STARTED","nested_enum_value":"PENDING","repeated_nested_enum_values":["PENDING","COMPLETED"]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingEnumAsInt(t *testing.T) {
m := &jsonpb.Marshaler{EnumsAsInts: true, OrigName: true}
msg, err := m.MarshalToString(&pb.EnumMessage{
NumericEnumValue: pb.NumericEnum_ONE,
RepeatedNumericEnumValues: []pb.NumericEnum{
pb.NumericEnum_ONE,
pb.NumericEnum_TWO,
},
AliasedEnumValue: pb.AliasedEnum_RUNNING,
NestedEnumValue: pb.EnumMessage_PENDING,
RepeatedNestedEnumValues: []pb.EnumMessage_Nested{
pb.EnumMessage_PENDING,
pb.EnumMessage_COMPLETED,
},
})
require.NoError(t, err)
t.Logf("request messge: %q", msg)
buf, err := testCall("grpcurl.test.Everything.Enum", msg)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"numeric_enum_value":"ONE","repeated_numeric_enum_values":["ONE","TWO"],"aliased_enum_value":"STARTED","nested_enum_value":"PENDING","repeated_nested_enum_values":["PENDING","COMPLETED"]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingEnumEmpty(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Enum", `{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"numeric_enum_value":"ZERO","repeated_numeric_enum_values":[],"aliased_enum_value":"UNKNOWN","nested_enum_value":"UNKNOWN","repeated_nested_enum_values":[]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingOneof1(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Oneof", `{"int32_value": 100}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"int32_value":100,"string_value":"","repeated_oneof_values":[]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingOneof2(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Oneof", `{"string_value": "xxx"}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"int32_value":0,"string_value":"xxx","repeated_oneof_values":[]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingOneof3(t *testing.T) {
m := &jsonpb.Marshaler{EnumsAsInts: true, OrigName: true}
msg, err := m.MarshalToString(&pb.OneofMessage{
OneofValue: &pb.OneofMessage_Int32Value{Int32Value: 2000},
})
require.NoError(t, err)
t.Logf("request messge: %q", msg)
buf, err := testCall("grpcurl.test.Everything.Oneof", msg)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"int32_value":2000,"string_value":"","repeated_oneof_values":[]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingOneofEmpty(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Oneof", `{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"int32_value":0,"string_value":"","repeated_oneof_values":[]}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingMap(t *testing.T) {
t.Skip("todo: fix map message order issue")
m := &jsonpb.Marshaler{OrigName: true}
msg, err := m.MarshalToString(&pb.MapMessage{
MappedValue: map[string]string{
"foo": "foo1",
"bar": "bar1",
},
MappedEnumValue: map[string]pb.NumericEnum{
"one": pb.NumericEnum_ONE,
"two": pb.NumericEnum_TWO,
},
MappedNestedValue: map[string]*pb.NestedMessage{
"foo": &pb.NestedMessage{
NestedValue: &pb.NestedMessage_Nested{
Int32Value: 100,
StringValue: "xxx",
},
RepeatedNestedValues: []*pb.NestedMessage_Nested{
{
Int32Value: 200,
StringValue: "yyy",
},
{
Int32Value: 300,
StringValue: "zzz",
},
},
},
},
})
require.NoError(t, err)
t.Logf("request messge: %q", msg)
buf, err := testCall("grpcurl.test.Everything.Map", msg)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expectedJSON := `{"mapped_value":{"bar":"bar1","foo":"foo1"},"mapped_enum_value":{},"mapped_nested_value":{"foo":{"nested_value":{"int32_value":100,"string_value":"xxx"},"repeated_nested_values":[{"int32_value":200,"string_value":"yyy"},{"int32_value":300,"string_value":"zzz"}]}}}`
var expected, reqProto, respProto pb.MapMessage
err = jsonpb.UnmarshalString(expectedJSON, &expected)
require.NoError(t, err)
err = jsonpb.UnmarshalString(resp.RequestMessage, &reqProto)
require.NoError(t, err)
err = jsonpb.UnmarshalString(resp.ResponseMessage, &respProto)
require.NoError(t, err)
assert.Equal(t, expected, reqProto, "request message")
assert.Equal(t, expected, respProto, "response message")
}
func TestEverythingMapEmpty(t *testing.T) {
buf, err := testCall("grpcurl.test.Everything.Map", `{}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"mapped_value":{},"mapped_enum_value":{},"mapped_nested_value":{}}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEverythingGoogle(t *testing.T) {
buf, err := testCall(
"grpcurl.test.Everything.Google",
`{"info":{"request_id":"xxx","serving_data":"xyz"}}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"info":{"request_id":"xxx","serving_data":"xyz"}}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}
func TestEchoV2Echo(t *testing.T) {
// cross package message
buf, err := testCall(
"grpcurl.test.v2.Echo.Echo",
`{"value": "xxx"}`)
require.NoError(t, err)
resp := parseTestResponse(buf.String())
expected := `{"value":"xxx","error_code":0}`
assert.Equal(t, expected, resp.RequestMessage, "request message")
assert.Equal(t, expected, resp.ResponseMessage, "response message")
}