-
Notifications
You must be signed in to change notification settings - Fork 2
/
request_client.go
172 lines (133 loc) · 4.08 KB
/
request_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
package grpcsteps
import (
"context"
"fmt"
"reflect"
"sync"
"time"
"github.com/swaggest/assertjson"
"go.nhat.io/grpcmock"
"go.nhat.io/grpcmock/invoker"
"go.nhat.io/grpcmock/must"
xreflect "go.nhat.io/grpcmock/reflect"
"go.nhat.io/grpcmock/service"
)
// ErrNoClientRequestInContext indicates that there is no client request in context.
const ErrNoClientRequestInContext err = "no client request in context"
type clientRequest interface {
Do() ([]byte, error)
}
type clientRequestInvoker struct {
invoker *invoker.Invoker
response []byte
responseRaw interface{}
responseErr error
once sync.Once
}
func (r *clientRequestInvoker) Do() ([]byte, error) {
r.once.Do(func() {
r.responseErr = r.invoker.Invoke(context.Background())
if r.responseErr != nil {
return
}
payload, err := assertjson.MarshalIndentCompact(r.responseRaw, "", " ", 80)
must.NotFail(err) // this should not happen
r.response = payload
})
return r.response, r.responseErr
}
func newClientRequestInvoker(svc *Service, payload interface{}) *clientRequestInvoker {
out := newServerOutput(svc.MethodType, svc.Output)
i := invoker.New(svc.Method, clientRequestInvokerOptions(svc, payload, out)...)
i.WithTimeout(time.Second)
return &clientRequestInvoker{
invoker: i,
responseRaw: out,
responseErr: nil,
}
}
func clientRequestInvokerOptions(svc *Service, payload interface{}, out interface{}) []invoker.Option {
opts := []invoker.Option{
invoker.WithAddress(svc.Address),
}
switch svc.MethodType {
case service.TypeBidirectionalStream:
opts = append(opts, invoker.WithBidirectionalStreamHandler(grpcmock.SendAndRecvAll(payload, out)))
case service.TypeClientStream:
opts = append(opts, invoker.WithInputStreamHandler(grpcmock.SendAll(payload)),
invoker.WithOutput(out),
)
case service.TypeServerStream:
opts = append(opts, invoker.WithInput(payload),
invoker.WithOutputStreamHandler(grpcmock.RecvAll(out)),
)
case service.TypeUnary:
fallthrough
default:
opts = append(opts, invoker.WithInput(payload),
invoker.WithOutput(out),
)
}
opts = append(opts, invoker.WithDialOptions(svc.DialOptions...))
return opts
}
type missingClientRequest struct{}
func (m missingClientRequest) Do() ([]byte, error) {
return nil, missingClientRequestPlannerErr()
}
func clientRequestFromContext(ctx context.Context) clientRequest {
r, ok := ctx.Value(requestCtxKey{}).(clientRequest)
if !ok {
return missingClientRequest{}
}
return r
}
func clientRequestToContext(ctx context.Context, r clientRequest) context.Context {
return context.WithValue(ctx, requestCtxKey{}, r)
}
type clientRequestPlanner struct {
request *clientRequestInvoker
}
func (c clientRequestPlanner) WithHeader(header string, value interface{}) error {
c.request.invoker.WithInvokeOption(grpcmock.WithHeader(header, value.(string)))
return nil
}
func (c clientRequestPlanner) WithTimeout(d time.Duration) error {
c.request.invoker.WithTimeout(d)
return nil
}
func newClientRequestPlanner(req *clientRequestInvoker) *clientRequestPlanner {
return &clientRequestPlanner{
request: req,
}
}
func newClientRequestPlannerContext(ctx context.Context, svc *Service, payload interface{}) context.Context {
r := newClientRequestInvoker(svc, payload)
ctx = requestPlannerToContext(ctx, newClientRequestPlanner(r))
return clientRequestToContext(ctx, r)
}
func newServerOutput(methodType service.Type, out interface{}) interface{} {
result := reflect.New(xreflect.UnwrapType(out))
if service.IsMethodServerStream(methodType) ||
service.IsMethodBidirectionalStream(methodType) {
value := reflect.MakeSlice(reflect.SliceOf(result.Type()), 0, 0)
result = reflect.New(value.Type())
result.Elem().Set(value)
}
return result.Interface()
}
func missingClientRequestPlannerErr() error {
//goland:noinspection GoErrorStringFormat
return fmt.Errorf(
"%w, did you forget to setup a gprc request in the scenario?\n\nFor example:\n%s",
ErrNoClientRequestInContext,
`
When I request a gRPC method "/grpctest.ItemService/GetItem" with payload:
"""
{
"id": 42
}
"""
`,
)
}