-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_test.go
162 lines (145 loc) · 4.25 KB
/
grpc_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
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
package logger
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
pb "github.com/trinhdaiphuc/logger/proto/hello"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"net"
"strings"
"testing"
)
func TestGrpcInterceptor(t *testing.T) {
tests := []struct {
name string
reqName string
res *pb.HelloResponse
logLevel string
requestUri string
config ConfigGrpc
errCode codes.Code
errMsg string
}{
{
name: "invalid request with empty name",
reqName: "",
res: nil,
requestUri: "/hello.HelloService/Hello",
config: DefaultConfigGrpc,
logLevel: "error",
errCode: codes.InvalidArgument,
errMsg: fmt.Sprint("empty name"),
},
{
name: "valid request with non negative amount",
reqName: "world",
res: &pb.HelloResponse{Message: "Hello world"},
logLevel: "info",
requestUri: "/hello.HelloService/Hello",
config: ConfigGrpc{
SkipperGrpc: func(ctx context.Context, info *grpc.UnaryServerInfo) bool {
fmt.Println("method", info.FullMethod)
if strings.HasSuffix(info.FullMethod, "/Hello") {
return true
}
return false
},
},
errCode: codes.OK,
errMsg: "",
},
{
name: "valid request with non negative amount",
reqName: "world",
res: &pb.HelloResponse{Message: "Hello world"},
logLevel: "info",
requestUri: "/hello.HelloService/Hello",
config: ConfigGrpc{
BeforeFuncGrpc: func(ctx context.Context, info *grpc.UnaryServerInfo) {
context.WithValue(ctx, "Method", info.FullMethod)
},
},
errCode: codes.OK,
errMsg: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
New(WithFormatter(&JSONFormatter{}), WithOutput(buf))
ctx := context.Background()
conn, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithContextDialer(dialer(tt.config)))
if err != nil {
panic(err)
}
defer conn.Close()
client := pb.NewHelloServiceClient(conn)
request := &pb.HelloRequest{Name: tt.reqName}
response, err := client.Hello(ctx, request)
t.Logf("Out %v, err %v", response, err)
t.Logf("Log output %v", buf.String())
var data map[string]interface{}
if len(buf.String()) > 0 {
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
t.Error("unexpected error", err)
}
_, ok := data["STEP_1"]
uri, uriOk := data[URIField]
level, levelOk := data[FieldKeyLevel]
// TEST
assert.True(t, ok, `cannot found expected "STEP_1" field: %v`, data)
assert.True(t, uriOk, `cannot found expected "%v" field: %v`, URIField, data)
assert.Equal(t, tt.requestUri, uri)
assert.True(t, levelOk, `cannot found expected "%v" field: %v`, FieldKeyLevel, data)
assert.Equal(t, tt.logLevel, level)
}
if response != nil {
if response.GetMessage() != tt.res.GetMessage() {
t.Error("response: expected", tt.res.GetMessage(), "received", response.GetMessage())
}
}
if err != nil {
if er, ok := status.FromError(err); ok {
if er.Code() != tt.errCode {
t.Error("error code: expected", codes.InvalidArgument, "received", er.Code())
}
if er.Message() != tt.errMsg {
t.Error("error message: expected", tt.errMsg, "received", er.Message())
}
}
}
})
}
}
type HelloService struct{}
var _ pb.HelloServiceServer = (*HelloService)(nil)
func (h HelloService) Hello(ctx context.Context, request *pb.HelloRequest) (*pb.HelloResponse, error) {
logger := GetLogger(ctx)
logger.AddLog("request %v", ToJsonString(request))
if len(request.Name) == 0 {
return nil, status.Error(codes.InvalidArgument, "empty name")
}
return &pb.HelloResponse{Message: "Hello " + request.Name}, nil
}
func dialer(config ConfigGrpc) func(context.Context, string) (net.Conn, error) {
listener := bufconn.Listen(1024 * 1024)
server := grpc.NewServer(
grpc.UnaryInterceptor(
GrpcInterceptor(config),
),
)
pb.RegisterHelloServiceServer(server, &HelloService{})
go func() {
if err := server.Serve(listener); err != nil {
panic(err)
}
}()
return func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}