-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_server.go
98 lines (75 loc) · 2.45 KB
/
request_server.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
package grpcsteps
import (
"context"
"fmt"
"time"
"google.golang.org/grpc/codes"
)
// ErrNoServiceRequestInContext indicates that there is no service request in context.
const ErrNoServiceRequestInContext err = "no service request in context"
type serverRequestPlanner interface {
requestPlanner
Return(payload string) error
ReturnError(code codes.Code, message string) error
}
type serverRequestReflectorPlanner struct {
expected expectation
}
func (s *serverRequestReflectorPlanner) WithHeader(header string, value interface{}) error {
s.expected.WithHeader(header, value)
return nil
}
func (s *serverRequestReflectorPlanner) WithTimeout(time.Duration) error {
return fmt.Errorf("grpc service request does not have timeout") // nolint: goerr113
}
func (s *serverRequestReflectorPlanner) Return(payload string) error { // nolint: unparam
s.expected.Return(payload)
return nil
}
func (s *serverRequestReflectorPlanner) ReturnError(code codes.Code, message string) error { // nolint: unparam
s.expected.ReturnError(code, message)
return nil
}
func newServerRequestPlanner(expected expectation) *serverRequestReflectorPlanner {
return &serverRequestReflectorPlanner{
expected: expected,
}
}
func serverRequestPlannerFromContext(ctx context.Context) serverRequestPlanner {
r, ok := ctx.Value(requestPlannerCtxKey{}).(serverRequestPlanner)
if !ok {
return missingServerRequestPlanner{}
}
return r
}
func newServerRequestPlannerContext(ctx context.Context, expected expectation) context.Context {
return requestPlannerToContext(ctx, newServerRequestPlanner(expected))
}
type missingServerRequestPlanner struct{}
func (missingServerRequestPlanner) WithHeader(string, interface{}) error {
return missingServerRequestPlannerErr()
}
func (missingServerRequestPlanner) WithTimeout(time.Duration) error {
return missingServerRequestPlannerErr()
}
func (missingServerRequestPlanner) Return(string) error {
return missingServerRequestPlannerErr()
}
func (missingServerRequestPlanner) ReturnError(codes.Code, string) error {
return missingServerRequestPlannerErr()
}
func missingServerRequestPlannerErr() error {
//goland:noinspection GoErrorStringFormat
return fmt.Errorf(
"%w, did you forget to setup a gprc request in the scenario?\n\nFor example:\n%s",
ErrNoServiceRequestInContext,
`
When "item-service" receives a grpc request "/grpctest.ItemService/GetItem" with payload:
"""
{
"id": 42
}
"""
`,
)
}