-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
230 lines (183 loc) · 7.36 KB
/
client.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
package grpcsteps
import (
"context"
"net"
"os"
"github.com/cucumber/godog"
xreflect "go.nhat.io/grpcmock/reflect"
"go.nhat.io/grpcmock/service"
"google.golang.org/grpc"
)
// Service contains needed information to form a GRPC request.
type Service struct {
service.Method
Address string
DialOptions []grpc.DialOption
}
// ServiceOption sets up a service.
type ServiceOption func(s *Service)
// Client is a grpc client for godog.
type Client struct {
services map[string]*Service
defaultSvcOptions []ServiceOption
}
// ClientOption sets up a client.
type ClientOption func(s *Client)
func (c *Client) registerService(id string, svc interface{}, opts ...ServiceOption) {
for _, method := range xreflect.FindServiceMethods(svc) {
svc := &Service{
Method: service.Method{
ServiceName: id,
MethodName: method.Name,
MethodType: service.ToType(method.IsClientStream, method.IsServerStream),
Input: method.Input,
Output: method.Output,
},
Address: ":9090",
}
// Apply default options.
for _, o := range c.defaultSvcOptions {
o(svc)
}
// Apply inline options.
for _, o := range opts {
o(svc)
}
c.services[svc.FullName()] = svc
}
}
// RegisterContext registers to godog scenario.
func (c *Client) RegisterContext(sc *godog.ScenarioContext) {
sc.Step(`^I request(?: a)? (?:gRPC|GRPC|grpc)(?: method)? "([^"]*)" with payload:?$`, c.iRequestWithPayloadFromDocString)
sc.Step(`^I request(?: a)? (?:gRPC|GRPC|grpc)(?: method)? "([^"]*)" with payload from file "([^"]+)"$`, c.iRequestWithPayloadFromFile)
sc.Step(`^I request(?: a)? (?:gRPC|GRPC|grpc)(?: method)? "([^"]*)" with payload from file:$`, c.iRequestWithPayloadFromFileDocString)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with payload:?$`, c.iShouldHaveResponseWithPayloadFromDocString)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with payload from file "([^"]+)"$`, c.iShouldHaveResponseWithPayloadFromFile)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with payload from file:?$`, c.iShouldHaveResponseWithPayloadFromFileDocString)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with code "([^"]*)"$`, c.iShouldHaveResponseWithCode)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with error (?:message )?"([^"]*)"$`, c.iShouldHaveResponseWithErrorMessage)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with code "([^"]*)" and error (?:message )?"([^"]*)"$`, c.iShouldHaveResponseWithCodeAndErrorMessage)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with error(?: message)?:$`, c.iShouldHaveResponseWithErrorMessageFromDocString)
sc.Step(`^I should have(?: a)? (?:gRPC|GRPC|grpc) response with code "([^"]*)" and error(?: message)?:$`, c.iShouldHaveResponseWithCodeAndErrorMessageFromDocString)
registerRequestPlanner(sc)
}
func (c *Client) iRequestWithPayload(ctx context.Context, method string, data string) (context.Context, error) {
svc, ok := c.services[method]
if !ok {
return ctx, ErrInvalidGRPCMethod
}
payload, err := toPayload(svc.MethodType, svc.Input, &data)
if err != nil {
return ctx, err
}
return newClientRequestPlannerContext(ctx, svc, payload), nil
}
func (c *Client) iRequestWithPayloadFromDocString(ctx context.Context, method string, payload *godog.DocString) (context.Context, error) {
return c.iRequestWithPayload(ctx, method, payload.Content)
}
func (c *Client) iRequestWithPayloadFromFile(ctx context.Context, method string, path string) (context.Context, error) {
payload, err := os.ReadFile(path) // nolint: gosec
if err != nil {
return ctx, err
}
return c.iRequestWithPayload(ctx, method, string(payload))
}
func (c *Client) iRequestWithPayloadFromFileDocString(ctx context.Context, method string, path *godog.DocString) (context.Context, error) {
return c.iRequestWithPayloadFromFile(ctx, method, path.Content)
}
func (c *Client) iShouldHaveResponseWithPayload(ctx context.Context, response string) error {
return assertServerResponsePayload(clientRequestFromContext(ctx), response)
}
func (c *Client) iShouldHaveResponseWithPayloadFromDocString(ctx context.Context, response *godog.DocString) error {
return c.iShouldHaveResponseWithPayload(ctx, response.Content)
}
func (c *Client) iShouldHaveResponseWithPayloadFromFile(ctx context.Context, path string) error {
payload, err := os.ReadFile(path) // nolint: gosec
if err != nil {
return err
}
return c.iShouldHaveResponseWithPayload(ctx, string(payload))
}
func (c *Client) iShouldHaveResponseWithPayloadFromFileDocString(ctx context.Context, path *godog.DocString) error {
return c.iShouldHaveResponseWithPayloadFromFile(ctx, path.Content)
}
func (c *Client) iShouldHaveResponseWithCode(ctx context.Context, codeValue string) error {
code, err := toStatusCode(codeValue)
if err != nil {
return err
}
return assertServerResponseErrorCode(clientRequestFromContext(ctx), code)
}
func (c *Client) iShouldHaveResponseWithErrorMessage(ctx context.Context, err string) error {
return assertServerResponseErrorMessage(clientRequestFromContext(ctx), err)
}
func (c *Client) iShouldHaveResponseWithCodeAndErrorMessage(ctx context.Context, codeValue, err string) error {
if err := c.iShouldHaveResponseWithCode(ctx, codeValue); err != nil {
return err
}
return c.iShouldHaveResponseWithErrorMessage(ctx, err)
}
func (c *Client) iShouldHaveResponseWithErrorMessageFromDocString(ctx context.Context, err *godog.DocString) error {
return c.iShouldHaveResponseWithErrorMessage(ctx, err.Content)
}
func (c *Client) iShouldHaveResponseWithCodeAndErrorMessageFromDocString(ctx context.Context, codeValue string, err *godog.DocString) error {
if err := c.iShouldHaveResponseWithCode(ctx, codeValue); err != nil {
return err
}
return c.iShouldHaveResponseWithErrorMessage(ctx, err.Content)
}
// NewClient initiates a new grpc server extension for testing.
func NewClient(opts ...ClientOption) *Client {
s := &Client{
services: make(map[string]*Service),
}
for _, o := range opts {
o(s)
}
return s
}
// RegisterServiceFromInstance registers a grpc server by its interface.
func RegisterServiceFromInstance(id string, svc interface{}, opts ...ServiceOption) ClientOption {
return func(c *Client) {
c.registerService(id, svc, opts...)
}
}
// RegisterService registers a grpc server by its interface.
func RegisterService(registerFunc interface{}, opts ...ServiceOption) ClientOption {
return func(c *Client) {
serviceDesc, svc := xreflect.ParseRegisterFunc(registerFunc)
c.registerService(serviceDesc.ServiceName, svc, opts...)
}
}
// WithDefaultServiceOptions set default service options.
func WithDefaultServiceOptions(opts ...ServiceOption) ClientOption {
return func(s *Client) {
s.defaultSvcOptions = append(s.defaultSvcOptions, opts...)
}
}
// AddrProvider provides a net address.
type AddrProvider interface {
Addr() net.Addr
}
// WithAddressProvider sets service address.
func WithAddressProvider(p AddrProvider) ServiceOption {
return WithAddr(p.Addr().String())
}
// WithAddr sets service address.
func WithAddr(addr string) ServiceOption {
return func(s *Service) {
s.Address = addr
}
}
// WithDialOption adds a dial option.
func WithDialOption(o grpc.DialOption) ServiceOption {
return func(s *Service) {
s.DialOptions = append(s.DialOptions, o)
}
}
// WithDialOptions sets dial options.
func WithDialOptions(opts ...grpc.DialOption) ServiceOption {
return func(s *Service) {
s.DialOptions = opts
}
}