-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_test.go
115 lines (109 loc) · 2.79 KB
/
dynamic_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
package grpcstub
import (
"context"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/k1LoW/grpcstub/testdata/hello"
"github.com/k1LoW/grpcstub/testdata/routeguide"
)
func TestResponseDynamic(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/route_guide.proto")
t.Cleanup(func() {
ts.Close()
})
ts.Method("GetFeature").ResponseDynamic()
want := 5
responses := []*routeguide.Feature{}
for i := 0; i < want; i++ {
client := routeguide.NewRouteGuideClient(ts.Conn())
res, err := client.GetFeature(ctx, &routeguide.Point{
Latitude: 10,
Longitude: 13,
})
if err != nil {
t.Fatal(err)
}
responses = append(responses, res)
}
{
got := len(ts.Requests())
if got != want {
t.Errorf("got %v\nwant %v", got, want)
}
}
rc := len(responses)
for i := 0; i < rc; i++ {
a := responses[i%rc]
b := responses[(i+1)%rc]
opts := []cmp.Option{
cmpopts.IgnoreFields(routeguide.Feature{}, "state", "sizeCache", "unknownFields"),
cmpopts.IgnoreFields(routeguide.Point{}, "state", "sizeCache", "unknownFields"),
}
if diff := cmp.Diff(a, b, opts...); diff == "" {
t.Errorf("got same responses: %#v", a)
}
}
}
func TestResponseDynamicRepeated(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/hello.proto")
t.Cleanup(func() {
ts.Close()
})
ts.Method("Hello").ResponseDynamic()
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Fatal(err)
}
if len(res.Hellos) == 0 {
t.Error("invalid repeated field value")
}
}
func TestResponseDynamicGenerated(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/hello.proto")
t.Cleanup(func() {
ts.Close()
})
want := time.Now()
opts := []GeneratorOption{
Generator("*_time", func(req *Request) any {
return want
}),
}
ts.Method("Hello").ResponseDynamic(opts...)
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Fatal(err)
}
if res.CreateTime.AsTime().UnixNano() != want.UnixNano() {
t.Errorf("got %v\nwant %v", res.CreateTime.AsTime().UnixNano(), want.UnixNano())
}
}
func TestResponseDynamicServer(t *testing.T) {
ctx := context.Background()
ts := NewServer(t, "testdata/hello.proto")
t.Cleanup(func() {
ts.Close()
})
want := time.Now()
opts := []GeneratorOption{
Generator("*_time", func(req *Request) any {
return want
}),
}
ts.ResponseDynamic(opts...)
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Fatal(err)
}
if res.CreateTime.AsTime().UnixNano() != want.UnixNano() {
t.Errorf("got %v\nwant %v", res.CreateTime.AsTime().UnixNano(), want.UnixNano())
}
}