-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
grpc.go
150 lines (140 loc) · 4.39 KB
/
grpc.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
package testutil
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/k1LoW/grpcstub"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var Cacert = func() []byte {
b, err := os.ReadFile(filepath.Join(Testdata(), "cacert.pem"))
if err != nil {
panic(err)
}
return b
}()
var Cert = func() []byte {
b, err := os.ReadFile(filepath.Join(Testdata(), "cert.pem"))
if err != nil {
panic(err)
}
return b
}()
var Key = func() []byte {
b, err := os.ReadFile(filepath.Join(Testdata(), "key.pem"))
if err != nil {
panic(err)
}
return b
}()
func GRPCServer(t *testing.T, useTLS bool, disableReflection bool) *grpcstub.Server {
pf := filepath.Join(Testdata(), "grpctest.proto")
opts := []grpcstub.Option{
grpcstub.EnableHealthCheck(),
grpcstub.BufLock(filepath.Join(Testdata(), "buf.lock")),
}
if disableReflection {
opts = append(opts, grpcstub.DisableReflection())
}
if useTLS {
opts = append(opts, grpcstub.UseTLS(Cacert, Cert, Key))
}
ts := grpcstub.NewServer(t, pf, opts...)
t.Cleanup(func() {
ts.Close()
})
// slow responses
ts.Method("grpctest.GrpcTestService/Hello").Match(func(r *grpcstub.Request) bool {
h := r.Headers.Get("slow")
if len(h) > 0 {
time.Sleep(10 * time.Second)
return true
}
return false
}).Response(func() map[string]any {
return map[string]any{}
}())
ts.Method("grpctest.GrpcTestService/ListHello").Match(func(r *grpcstub.Request) bool {
h := r.Headers.Get("slow")
if len(h) > 0 {
time.Sleep(10 * time.Second)
return true
}
return false
}).Response(func() map[string]any {
return map[string]any{}
}())
ts.Method("grpctest.GrpcTestService/MultiHello").Match(func(r *grpcstub.Request) bool {
h := r.Headers.Get("slow")
if len(h) > 0 {
time.Sleep(10 * time.Second)
return true
}
return false
}).Response(func() map[string]any {
return map[string]any{}
}())
ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool {
h := r.Headers.Get("slow")
if len(h) > 0 {
time.Sleep(10 * time.Second)
return true
}
return false
}).Response(func() map[string]any {
return map[string]any{}
}())
// error responses
ts.Method("grpctest.GrpcTestService/Hello").Match(func(r *grpcstub.Request) bool {
h := r.Headers.Get("error")
return len(h) > 0
}).Status(status.New(codes.Canceled, "request canceled"))
// default responses
ts.Method("grpctest.GrpcTestService/Hello").
Header("hello", "header").Trailer("hello", "trailer").
ResponseString(`{"message":"hello", "num":32, "create_time":"2022-06-25T05:24:43.861872Z"}`)
ts.Method("grpctest.GrpcTestService/ListHello").
Header("listhello", "header").Trailer("listhello", "trailer").
ResponseString(`{"message":"hello", "num":33, "create_time":"2022-06-25T05:24:43.861872Z"}`).
ResponseString(`{"message":"hello", "num":34, "create_time":"2022-06-25T05:24:44.382783Z"}`)
ts.Method("grpctest.GrpcTestService/MultiHello").
Header("multihello", "header").Trailer("multihello", "trailer").
ResponseString(`{"message":"hello", "num":35, "create_time":"2022-06-25T05:24:45.382783Z"}`)
ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool {
n, ok := r.Message["name"]
if !ok {
return false
}
return n.(string) == "alice"
}).Header("hellochat", "header").Trailer("hellochat", "trailer").
ResponseString(`{"message":"hello", "num":34, "create_time":"2022-06-25T05:24:46.382783Z"}`)
ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool {
n, ok := r.Message["name"]
if !ok {
return false
}
return n.(string) == "bob"
}).Header("hellochat-second", "header").Trailer("hellochat-second", "trailer").
ResponseString(`{"message":"hello", "num":35, "create_time":"2022-06-25T05:24:47.382783Z"}`)
ts.Method("grpctest.GrpcTestService/HelloChat").Match(func(r *grpcstub.Request) bool {
n, ok := r.Message["name"]
if !ok {
return false
}
return n.(string) == "charlie"
}).Header("hellochat-third", "header").Trailer("hellochat-second", "trailer").
ResponseString(`{"message":"hello", "num":36, "create_time":"2022-06-25T05:24:48.382783Z"}`)
ts.Method("grpctest.GrpcTestService/HelloFields").Match(func(r *grpcstub.Request) bool { return true }).
Handler(func(r *grpcstub.Request) *grpcstub.Response {
return &grpcstub.Response{
Messages: []grpcstub.Message{
{
"field_bytes": r.Message["field_bytes"],
},
},
}
})
return ts
}