-
Notifications
You must be signed in to change notification settings - Fork 507
/
grpcurl_test.go
871 lines (774 loc) · 28.7 KB
/
grpcurl_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
package grpcurl_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/golang/protobuf/jsonpb" //lint:ignore SA1019 we have to import this because it appears in exported API
"github.com/golang/protobuf/proto" //lint:ignore SA1019 we have to import this because it appears in exported API
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/grpcreflect"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
. "github.com/fullstorydev/grpcurl"
grpcurl_testing "github.com/fullstorydev/grpcurl/internal/testing"
jsonpbtest "github.com/fullstorydev/grpcurl/internal/testing/jsonpb_test_proto"
)
var (
sourceProtoset DescriptorSource
sourceProtoFiles DescriptorSource
ccNoReflect *grpc.ClientConn
sourceReflect DescriptorSource
ccReflect *grpc.ClientConn
descSources []descSourceCase
)
type descSourceCase struct {
name string
source DescriptorSource
includeRefl bool
}
// NB: These tests intentionally use the deprecated InvokeRpc since that
// calls the other (non-deprecated InvokeRPC). That allows the tests to
// easily exercise both functions.
func TestMain(m *testing.M) {
var err error
sourceProtoset, err = DescriptorSourceFromProtoSets("internal/testing/test.protoset")
if err != nil {
panic(err)
}
sourceProtoFiles, err = DescriptorSourceFromProtoFiles([]string{"internal/testing"}, "test.proto")
if err != nil {
panic(err)
}
// Create a server that includes the reflection service
svrReflect := grpc.NewServer()
grpcurl_testing.RegisterTestServiceServer(svrReflect, grpcurl_testing.TestServer{})
reflection.Register(svrReflect)
var portReflect int
if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil {
panic(err)
} else {
portReflect = l.Addr().(*net.TCPAddr).Port
go svrReflect.Serve(l)
}
defer svrReflect.Stop()
// And a corresponding client
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if ccReflect, err = grpc.DialContext(ctx, fmt.Sprintf("127.0.0.1:%d", portReflect),
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()); err != nil {
panic(err)
}
defer ccReflect.Close()
refClient := grpcreflect.NewClientAuto(context.Background(), ccReflect)
defer refClient.Reset()
sourceReflect = DescriptorSourceFromServer(context.Background(), refClient)
// Also create a server that does *not* include the reflection service
svrProtoset := grpc.NewServer()
grpcurl_testing.RegisterTestServiceServer(svrProtoset, grpcurl_testing.TestServer{})
var portProtoset int
if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil {
panic(err)
} else {
portProtoset = l.Addr().(*net.TCPAddr).Port
go svrProtoset.Serve(l)
}
defer svrProtoset.Stop()
// And a corresponding client
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if ccNoReflect, err = grpc.DialContext(ctx, fmt.Sprintf("127.0.0.1:%d", portProtoset),
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()); err != nil {
panic(err)
}
defer ccNoReflect.Close()
descSources = []descSourceCase{
{"protoset", sourceProtoset, false},
{"proto", sourceProtoFiles, false},
{"reflect", sourceReflect, true},
}
os.Exit(m.Run())
}
func TestServerDoesNotSupportReflection(t *testing.T) {
refClient := grpcreflect.NewClientAuto(context.Background(), ccNoReflect)
defer refClient.Reset()
refSource := DescriptorSourceFromServer(context.Background(), refClient)
_, err := ListServices(refSource)
if err != ErrReflectionNotSupported {
t.Errorf("ListServices should have returned ErrReflectionNotSupported; instead got %v", err)
}
_, err = ListMethods(refSource, "SomeService")
if err != ErrReflectionNotSupported {
t.Errorf("ListMethods should have returned ErrReflectionNotSupported; instead got %v", err)
}
err = InvokeRpc(context.Background(), refSource, ccNoReflect, "FooService/Method", nil, nil, nil)
// InvokeRpc wraps the error, so we just verify the returned error includes the right message
if err == nil || !strings.Contains(err.Error(), ErrReflectionNotSupported.Error()) {
t.Errorf("InvokeRpc should have returned ErrReflectionNotSupported; instead got %v", err)
}
}
func TestProtosetWithImports(t *testing.T) {
sourceProtoset, err := DescriptorSourceFromProtoSets("internal/testing/example.protoset")
if err != nil {
t.Fatalf("failed to load protoset: %v", err)
}
// really shallow check of the loaded descriptors
if sd, err := sourceProtoset.FindSymbol("TestService"); err != nil {
t.Errorf("failed to find TestService in protoset: %v", err)
} else if sd == nil {
t.Errorf("FindSymbol returned nil for TestService")
} else if _, ok := sd.(*desc.ServiceDescriptor); !ok {
t.Errorf("FindSymbol returned wrong kind of descriptor for TestService: %T", sd)
}
if md, err := sourceProtoset.FindSymbol("TestRequest"); err != nil {
t.Errorf("failed to find TestRequest in protoset: %v", err)
} else if md == nil {
t.Errorf("FindSymbol returned nil for TestRequest")
} else if _, ok := md.(*desc.MessageDescriptor); !ok {
t.Errorf("FindSymbol returned wrong kind of descriptor for TestRequest: %T", md)
}
}
func TestListServices(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestListServices(t, ds.source, ds.includeRefl)
})
}
}
func doTestListServices(t *testing.T, source DescriptorSource, includeReflection bool) {
names, err := ListServices(source)
if err != nil {
t.Fatalf("failed to list services: %v", err)
}
var expected []string
if includeReflection {
// when using server reflection, we see the TestService as well as the ServerReflection service
expected = []string{"grpc.reflection.v1.ServerReflection", "grpc.reflection.v1alpha.ServerReflection", "testing.TestService"}
} else {
// without reflection, we see all services defined in the same test.proto file, which is the
// TestService as well as UnimplementedService
expected = []string{"testing.TestService", "testing.UnimplementedService"}
}
if !reflect.DeepEqual(expected, names) {
t.Errorf("ListServices returned wrong results: wanted %v, got %v", expected, names)
}
}
func TestListMethods(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestListMethods(t, ds.source, ds.includeRefl)
})
}
}
func doTestListMethods(t *testing.T, source DescriptorSource, includeReflection bool) {
names, err := ListMethods(source, "testing.TestService")
if err != nil {
t.Fatalf("failed to list methods for TestService: %v", err)
}
expected := []string{
"testing.TestService.EmptyCall",
"testing.TestService.FullDuplexCall",
"testing.TestService.HalfDuplexCall",
"testing.TestService.StreamingInputCall",
"testing.TestService.StreamingOutputCall",
"testing.TestService.UnaryCall",
}
if !reflect.DeepEqual(expected, names) {
t.Errorf("ListMethods returned wrong results: wanted %v, got %v", expected, names)
}
if includeReflection {
// when using server reflection, we see the TestService as well as the ServerReflection service
names, err = ListMethods(source, "grpc.reflection.v1.ServerReflection")
if err != nil {
t.Fatalf("failed to list methods for ServerReflection: %v", err)
}
expected = []string{"grpc.reflection.v1.ServerReflection.ServerReflectionInfo"}
} else {
// without reflection, we see all services defined in the same test.proto file, which is the
// TestService as well as UnimplementedService
names, err = ListMethods(source, "testing.UnimplementedService")
if err != nil {
t.Fatalf("failed to list methods for ServerReflection: %v", err)
}
expected = []string{"testing.UnimplementedService.UnimplementedCall"}
}
if !reflect.DeepEqual(expected, names) {
t.Errorf("ListMethods returned wrong results: wanted %v, got %v", expected, names)
}
// force an error
_, err = ListMethods(source, "FooService")
if err != nil && !strings.Contains(err.Error(), "Symbol not found: FooService") {
t.Errorf("ListMethods should have returned 'not found' error but instead returned %v", err)
}
}
func TestGetAllFiles(t *testing.T) {
expectedFiles := []string{"test.proto"}
expectedFilesWithReflection := []string{
"grpc/reflection/v1/reflection.proto", "grpc/reflection/v1alpha/reflection.proto", "test.proto",
}
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
files, err := GetAllFiles(ds.source)
if err != nil {
t.Fatalf("failed to get all files: %v", err)
}
names := fileNames(files)
match := false
var expected []string
if ds.includeRefl {
expected = expectedFilesWithReflection
} else {
expected = expectedFiles
}
match = reflect.DeepEqual(expected, names)
if !match {
t.Errorf("GetAllFiles returned wrong results: wanted %v, got %v", expected, names)
}
})
}
// try cases with more complicated set of files
otherSourceProtoset, err := DescriptorSourceFromProtoSets("internal/testing/test.protoset", "internal/testing/example.protoset")
if err != nil {
t.Fatal(err.Error())
}
otherSourceProtoFiles, err := DescriptorSourceFromProtoFiles([]string{"internal/testing"}, "test.proto", "example.proto")
if err != nil {
t.Fatal(err.Error())
}
otherDescSources := []descSourceCase{
{"protoset[b]", otherSourceProtoset, false},
{"proto[b]", otherSourceProtoFiles, false},
}
expectedFiles = []string{
"example.proto",
"example2.proto",
"google/protobuf/any.proto",
"google/protobuf/descriptor.proto",
"google/protobuf/empty.proto",
"google/protobuf/timestamp.proto",
"test.proto",
}
for _, ds := range otherDescSources {
t.Run(ds.name, func(t *testing.T) {
files, err := GetAllFiles(ds.source)
if err != nil {
t.Fatalf("failed to get all files: %v", err)
}
names := fileNames(files)
if !reflect.DeepEqual(expectedFiles, names) {
t.Errorf("GetAllFiles returned wrong results: wanted %v, got %v", expectedFiles, names)
}
})
}
}
func TestExpandHeaders(t *testing.T) {
inHeaders := []string{"key1: ${value}", "key2: bar", "key3: ${woo", "key4: woo}", "key5: ${TEST}",
"key6: ${TEST_VAR}", "${TEST}: ${TEST_VAR}", "key8: ${EMPTY}"}
os.Setenv("value", "value")
os.Setenv("TEST", "value5")
os.Setenv("TEST_VAR", "value6")
os.Setenv("EMPTY", "")
expectedHeaders := map[string]bool{"key1: value": true, "key2: bar": true, "key3: ${woo": true, "key4: woo}": true,
"key5: value5": true, "key6: value6": true, "value5: value6": true, "key8: ": true}
outHeaders, err := ExpandHeaders(inHeaders)
if err != nil {
t.Errorf("The ExpandHeaders function generated an unexpected error %s", err)
}
for _, expandedHeader := range outHeaders {
if _, ok := expectedHeaders[expandedHeader]; !ok {
t.Errorf("The ExpandHeaders function has returned an unexpected header. Received unexpected header %s", expandedHeader)
}
}
badHeaders := []string{"key: ${DNE}"}
_, err = ExpandHeaders(badHeaders)
if err == nil {
t.Errorf("The ExpandHeaders function should return an error for missing environment variables %q", badHeaders)
}
}
func fileNames(files []*desc.FileDescriptor) []string {
names := make([]string, len(files))
for i, f := range files {
names[i] = f.GetName()
}
return names
}
const expectKnownType = `{
"dur": "0s",
"ts": "1970-01-01T00:00:00Z",
"dbl": 0,
"flt": 0,
"i64": "0",
"u64": "0",
"i32": 0,
"u32": 0,
"bool": false,
"str": "",
"bytes": null,
"st": {"google.protobuf.Struct": "supports arbitrary JSON objects"},
"an": {"@type": "type.googleapis.com/google.protobuf.Empty", "value": {}},
"lv": [{"google.protobuf.ListValue": "is an array of arbitrary JSON values"}],
"val": {"google.protobuf.Value": "supports arbitrary JSON"}
}`
func TestMakeTemplateKnownTypes(t *testing.T) {
descriptor, err := desc.LoadMessageDescriptorForMessage((*jsonpbtest.KnownTypes)(nil))
if err != nil {
t.Fatalf("failed to load descriptor: %v", err)
}
message := MakeTemplate(descriptor)
jsm := jsonpb.Marshaler{EmitDefaults: true}
out, err := jsm.MarshalToString(message)
if err != nil {
t.Fatalf("failed to marshal to JSON: %v", err)
}
// make sure template JSON matches expected
var actual, expected interface{}
if err := json.Unmarshal([]byte(out), &actual); err != nil {
t.Fatalf("failed to parse actual JSON: %v", err)
}
if err := json.Unmarshal([]byte(expectKnownType), &expected); err != nil {
t.Fatalf("failed to parse expected JSON: %v", err)
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("template message is not as expected; want:\n%s\ngot:\n%s", expectKnownType, out)
}
}
func TestDescribe(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestDescribe(t, ds.source)
})
}
}
func doTestDescribe(t *testing.T, source DescriptorSource) {
sym := "testing.TestService.EmptyCall"
dsc, err := source.FindSymbol(sym)
if err != nil {
t.Fatalf("failed to get descriptor for %q: %v", sym, err)
}
if _, ok := dsc.(*desc.MethodDescriptor); !ok {
t.Fatalf("descriptor for %q was a %T (expecting a MethodDescriptor)", sym, dsc)
}
txt := proto.MarshalTextString(dsc.AsProto())
expected :=
`name: "EmptyCall"
input_type: ".testing.Empty"
output_type: ".testing.Empty"
`
if expected != txt {
t.Errorf("descriptor mismatch: expected %s, got %s", expected, txt)
}
sym = "testing.StreamingOutputCallResponse"
dsc, err = source.FindSymbol(sym)
if err != nil {
t.Fatalf("failed to get descriptor for %q: %v", sym, err)
}
if _, ok := dsc.(*desc.MessageDescriptor); !ok {
t.Fatalf("descriptor for %q was a %T (expecting a MessageDescriptor)", sym, dsc)
}
txt = proto.MarshalTextString(dsc.AsProto())
expected =
`name: "StreamingOutputCallResponse"
field: <
name: "payload"
number: 1
label: LABEL_OPTIONAL
type: TYPE_MESSAGE
type_name: ".testing.Payload"
json_name: "payload"
>
`
if expected != txt {
t.Errorf("descriptor mismatch: expected %s, got %s", expected, txt)
}
_, err = source.FindSymbol("FooService")
if err != nil && !strings.Contains(err.Error(), "Symbol not found: FooService") {
t.Errorf("FindSymbol should have returned 'not found' error but instead returned %v", err)
}
}
const (
// type == COMPRESSABLE, but that is default (since it has
// numeric value == 0) and thus doesn't actually get included
// on the wire
payload1 = `{
"payload": {
"body": "SXQncyBCdXNpbmVzcyBUaW1l"
}
}`
payload2 = `{
"payload": {
"type": "RANDOM",
"body": "Rm91eCBkdSBGYUZh"
}
}`
payload3 = `{
"payload": {
"type": "UNCOMPRESSABLE",
"body": "SGlwaG9wb3BvdGFtdXMgdnMuIFJoeW1lbm9jZXJvcw=="
}
}`
)
func getCC(includeRefl bool) *grpc.ClientConn {
if includeRefl {
return ccReflect
} else {
return ccNoReflect
}
}
func TestUnary(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestUnary(t, getCC(ds.includeRefl), ds.source)
})
}
}
func doTestUnary(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) {
// Success
h := &handler{reqMessages: []string{payload1}}
err := InvokeRpc(context.Background(), source, cc, "testing.TestService/UnaryCall", makeHeaders(codes.OK), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
if h.check(t, "testing.TestService.UnaryCall", codes.OK, 1, 1) {
if h.respMessages[0] != payload1 {
t.Errorf("unexpected response from RPC: expecting %s; got %s", payload1, h.respMessages[0])
}
}
// Failure
h = &handler{reqMessages: []string{payload1}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/UnaryCall", makeHeaders(codes.NotFound), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.UnaryCall", codes.NotFound, 1, 0)
}
func TestClientStream(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestClientStream(t, getCC(ds.includeRefl), ds.source)
})
}
}
func doTestClientStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) {
// Success
h := &handler{reqMessages: []string{payload1, payload2, payload3}}
err := InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingInputCall", makeHeaders(codes.OK), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
if h.check(t, "testing.TestService.StreamingInputCall", codes.OK, 3, 1) {
expected :=
`{
"aggregatedPayloadSize": 61
}`
if h.respMessages[0] != expected {
t.Errorf("unexpected response from RPC: expecting %s; got %s", expected, h.respMessages[0])
}
}
// Fail fast (server rejects as soon as possible)
h = &handler{reqMessages: []string{payload1, payload2, payload3}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingInputCall", makeHeaders(codes.InvalidArgument), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.StreamingInputCall", codes.InvalidArgument, -3, 0)
// Fail late (server waits until stream is complete to reject)
h = &handler{reqMessages: []string{payload1, payload2, payload3}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingInputCall", makeHeaders(codes.Internal, true), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.StreamingInputCall", codes.Internal, 3, 0)
}
func TestServerStream(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestServerStream(t, getCC(ds.includeRefl), ds.source)
})
}
}
func doTestServerStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) {
req := &grpcurl_testing.StreamingOutputCallRequest{
ResponseType: grpcurl_testing.PayloadType_COMPRESSABLE,
ResponseParameters: []*grpcurl_testing.ResponseParameters{
{Size: 10}, {Size: 20}, {Size: 30}, {Size: 40}, {Size: 50},
},
}
payload, err := (&jsonpb.Marshaler{}).MarshalToString(req)
if err != nil {
t.Fatalf("failed to construct request: %v", err)
}
// Success
h := &handler{reqMessages: []string{payload}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingOutputCall", makeHeaders(codes.OK), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
if h.check(t, "testing.TestService.StreamingOutputCall", codes.OK, 1, 5) {
resp := &grpcurl_testing.StreamingOutputCallResponse{}
for i, msg := range h.respMessages {
if err := jsonpb.UnmarshalString(msg, resp); err != nil {
t.Errorf("failed to parse response %d: %v", i+1, err)
}
if resp.Payload.GetType() != grpcurl_testing.PayloadType_COMPRESSABLE {
t.Errorf("response %d has wrong payload type; expecting %v, got %v", i, grpcurl_testing.PayloadType_COMPRESSABLE, resp.Payload.Type)
}
if len(resp.Payload.Body) != (i+1)*10 {
t.Errorf("response %d has wrong payload size; expecting %d, got %d", i, (i+1)*10, len(resp.Payload.Body))
}
resp.Reset()
}
}
// Fail fast (server rejects as soon as possible)
h = &handler{reqMessages: []string{payload}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingOutputCall", makeHeaders(codes.Aborted), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.StreamingOutputCall", codes.Aborted, 1, 0)
// Fail late (server waits until stream is complete to reject)
h = &handler{reqMessages: []string{payload}}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/StreamingOutputCall", makeHeaders(codes.AlreadyExists, true), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.StreamingOutputCall", codes.AlreadyExists, 1, 5)
}
func TestHalfDuplexStream(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestHalfDuplexStream(t, getCC(ds.includeRefl), ds.source)
})
}
}
func doTestHalfDuplexStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) {
reqs := []string{payload1, payload2, payload3}
// Success
h := &handler{reqMessages: reqs}
err := InvokeRpc(context.Background(), source, cc, "testing.TestService/HalfDuplexCall", makeHeaders(codes.OK), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
if h.check(t, "testing.TestService.HalfDuplexCall", codes.OK, 3, 3) {
for i, resp := range h.respMessages {
if resp != reqs[i] {
t.Errorf("unexpected response %d from RPC:\nexpecting %q\ngot %q", i, reqs[i], resp)
}
}
}
// Fail fast (server rejects as soon as possible)
h = &handler{reqMessages: reqs}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/HalfDuplexCall", makeHeaders(codes.Canceled), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.HalfDuplexCall", codes.Canceled, -3, 0)
// Fail late (server waits until stream is complete to reject)
h = &handler{reqMessages: reqs}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/HalfDuplexCall", makeHeaders(codes.DataLoss, true), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.HalfDuplexCall", codes.DataLoss, 3, 3)
}
func TestFullDuplexStream(t *testing.T) {
for _, ds := range descSources {
t.Run(ds.name, func(t *testing.T) {
doTestFullDuplexStream(t, getCC(ds.includeRefl), ds.source)
})
}
}
func doTestFullDuplexStream(t *testing.T, cc *grpc.ClientConn, source DescriptorSource) {
reqs := make([]string, 3)
req := &grpcurl_testing.StreamingOutputCallRequest{
ResponseType: grpcurl_testing.PayloadType_RANDOM,
}
for i := range reqs {
req.ResponseParameters = append(req.ResponseParameters, &grpcurl_testing.ResponseParameters{Size: int32((i + 1) * 10)})
payload, err := (&jsonpb.Marshaler{}).MarshalToString(req)
if err != nil {
t.Fatalf("failed to construct request %d: %v", i, err)
}
reqs[i] = payload
}
// Success
h := &handler{reqMessages: reqs}
err := InvokeRpc(context.Background(), source, cc, "testing.TestService/FullDuplexCall", makeHeaders(codes.OK), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
if h.check(t, "testing.TestService.FullDuplexCall", codes.OK, 3, 6) {
resp := &grpcurl_testing.StreamingOutputCallResponse{}
i := 0
for j := 1; j < 3; j++ {
// three requests
for k := 0; k < j; k++ {
// 1 response for first request, 2 for second, etc
msg := h.respMessages[i]
if err := jsonpb.UnmarshalString(msg, resp); err != nil {
t.Errorf("failed to parse response %d: %v", i+1, err)
}
if resp.Payload.GetType() != grpcurl_testing.PayloadType_RANDOM {
t.Errorf("response %d has wrong payload type; expecting %v, got %v", i, grpcurl_testing.PayloadType_RANDOM, resp.Payload.Type)
}
if len(resp.Payload.Body) != (k+1)*10 {
t.Errorf("response %d has wrong payload size; expecting %d, got %d", i, (k+1)*10, len(resp.Payload.Body))
}
resp.Reset()
i++
}
}
}
// Fail fast (server rejects as soon as possible)
h = &handler{reqMessages: reqs}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/FullDuplexCall", makeHeaders(codes.PermissionDenied), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.FullDuplexCall", codes.PermissionDenied, -3, 0)
// Fail late (server waits until stream is complete to reject)
h = &handler{reqMessages: reqs}
err = InvokeRpc(context.Background(), source, cc, "testing.TestService/FullDuplexCall", makeHeaders(codes.ResourceExhausted, true), h, h.getRequestData)
if err != nil {
t.Fatalf("unexpected error during RPC: %v", err)
}
h.check(t, "testing.TestService.FullDuplexCall", codes.ResourceExhausted, 3, 6)
}
type handler struct {
method *desc.MethodDescriptor
methodCount int
reqHeaders metadata.MD
reqHeadersCount int
reqMessages []string
reqMessagesCount int
respHeaders metadata.MD
respHeadersCount int
respMessages []string
respTrailers metadata.MD
respStatus *status.Status
respTrailersCount int
}
func (h *handler) getRequestData() ([]byte, error) {
// we don't use a mutex, though this method will be called from different goroutine
// than other methods for bidi calls, because this method does not share any state
// with the other methods.
h.reqMessagesCount++
if h.reqMessagesCount > len(h.reqMessages) {
return nil, io.EOF
}
if h.reqMessagesCount > 1 {
// insert delay between messages in request stream
time.Sleep(time.Millisecond * 50)
}
return []byte(h.reqMessages[h.reqMessagesCount-1]), nil
}
func (h *handler) OnResolveMethod(md *desc.MethodDescriptor) {
h.methodCount++
h.method = md
}
func (h *handler) OnSendHeaders(md metadata.MD) {
h.reqHeadersCount++
h.reqHeaders = md
}
func (h *handler) OnReceiveHeaders(md metadata.MD) {
h.respHeadersCount++
h.respHeaders = md
}
func (h *handler) OnReceiveResponse(msg proto.Message) {
jsm := jsonpb.Marshaler{Indent: " "}
respStr, err := jsm.MarshalToString(msg)
if err != nil {
panic(fmt.Errorf("failed to generate JSON form of response message: %v", err))
}
h.respMessages = append(h.respMessages, respStr)
}
func (h *handler) OnReceiveTrailers(stat *status.Status, md metadata.MD) {
h.respTrailersCount++
h.respTrailers = md
h.respStatus = stat
}
func (h *handler) check(t *testing.T, expectedMethod string, expectedCode codes.Code, expectedRequestQueries, expectedResponses int) bool {
// verify a few things were only ever called once
if h.methodCount != 1 {
t.Errorf("expected grpcurl to invoke OnResolveMethod once; was %d", h.methodCount)
}
if h.reqHeadersCount != 1 {
t.Errorf("expected grpcurl to invoke OnSendHeaders once; was %d", h.reqHeadersCount)
}
if h.reqHeadersCount != 1 {
t.Errorf("expected grpcurl to invoke OnSendHeaders once; was %d", h.reqHeadersCount)
}
if h.respHeadersCount != 1 {
t.Errorf("expected grpcurl to invoke OnReceiveHeaders once; was %d", h.respHeadersCount)
}
if h.respTrailersCount != 1 {
t.Errorf("expected grpcurl to invoke OnReceiveTrailers once; was %d", h.respTrailersCount)
}
// check other stuff against given expectations
if h.method.GetFullyQualifiedName() != expectedMethod {
t.Errorf("wrong method: expecting %v, got %v", expectedMethod, h.method.GetFullyQualifiedName())
}
if h.respStatus.Code() != expectedCode {
t.Errorf("wrong code: expecting %v, got %v", expectedCode, h.respStatus.Code())
}
if expectedRequestQueries < 0 {
// negative expectation means "negate and expect up to that number; could be fewer"
if h.reqMessagesCount > -expectedRequestQueries+1 {
// the + 1 is because there will be an extra query that returns EOF
t.Errorf("wrong number of messages queried: expecting no more than %v, got %v", -expectedRequestQueries, h.reqMessagesCount-1)
}
} else {
if h.reqMessagesCount != expectedRequestQueries+1 {
// the + 1 is because there will be an extra query that returns EOF
t.Errorf("wrong number of messages queried: expecting %v, got %v", expectedRequestQueries, h.reqMessagesCount-1)
}
}
if len(h.respMessages) != expectedResponses {
t.Errorf("wrong number of messages received: expecting %v, got %v", expectedResponses, len(h.respMessages))
}
// also check headers and trailers came through as expected
v := h.respHeaders["some-fake-header-1"]
if len(v) != 1 || v[0] != "val1" {
t.Errorf("wrong request header for %q: %v", "some-fake-header-1", v)
}
v = h.respHeaders["some-fake-header-2"]
if len(v) != 1 || v[0] != "val2" {
t.Errorf("wrong request header for %q: %v", "some-fake-header-2", v)
}
v = h.respTrailers["some-fake-trailer-1"]
if len(v) != 1 || v[0] != "valA" {
t.Errorf("wrong request header for %q: %v", "some-fake-trailer-1", v)
}
v = h.respTrailers["some-fake-trailer-2"]
if len(v) != 1 || v[0] != "valB" {
t.Errorf("wrong request header for %q: %v", "some-fake-trailer-2", v)
}
return len(h.respMessages) == expectedResponses
}
func makeHeaders(code codes.Code, failLate ...bool) []string {
if len(failLate) > 1 {
panic("incorrect use of makeContext; should be at most one failLate flag")
}
hdrs := append(make([]string, 0, 5),
fmt.Sprintf("%s: %s", grpcurl_testing.MetadataReplyHeaders, "some-fake-header-1: val1"),
fmt.Sprintf("%s: %s", grpcurl_testing.MetadataReplyHeaders, "some-fake-header-2: val2"),
fmt.Sprintf("%s: %s", grpcurl_testing.MetadataReplyTrailers, "some-fake-trailer-1: valA"),
fmt.Sprintf("%s: %s", grpcurl_testing.MetadataReplyTrailers, "some-fake-trailer-2: valB"))
if code != codes.OK {
if len(failLate) > 0 && failLate[0] {
hdrs = append(hdrs, fmt.Sprintf("%s: %d", grpcurl_testing.MetadataFailLate, code))
} else {
hdrs = append(hdrs, fmt.Sprintf("%s: %d", grpcurl_testing.MetadataFailEarly, code))
}
}
return hdrs
}