-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption_test.go
119 lines (98 loc) · 2.55 KB
/
option_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
package gmicro
import (
"context"
"net/http"
"syscall"
"testing"
"time"
gRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
)
func TestStaticDir(t *testing.T) {
s := NewService(WithStaticDir("/a/b/c"))
assert.Equal(t, "/a/b/c", s.staticDir)
}
func TestAnnotator(t *testing.T) {
s := NewService(
WithAnnotator(func(ctx context.Context, req *http.Request) metadata.MD {
md := metadata.New(nil)
md.Set("key", "value")
return md
}),
)
assert.Len(t, s.annotators, 1)
}
func TestErrorHandler(t *testing.T) {
s := NewService(WithErrorHandler(nil))
assert.Nil(t, s.errorHandler)
}
func TestHTTPHandler(t *testing.T) {
s := NewService(WithHTTPHandler(nil))
assert.Nil(t, s.httpHandler)
}
func TestUnaryInterceptor(t *testing.T) {
s := NewService(
WithUnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
return nil, nil
}),
)
assert.Len(t, s.unaryInterceptors, 3)
}
func TestStreamInterceptor(t *testing.T) {
s := NewService(
WithStreamInterceptor(func(srv interface{}, stream grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return nil
}),
)
assert.Len(t, s.streamInterceptors, 3)
}
func TestInterruptSignal(t *testing.T) {
s := NewService(
WithInterruptSignal(syscall.SIGKILL),
)
assert.Len(t, s.interruptSignals, 7)
}
func TestGRPCServerOption(t *testing.T) {
s := NewService(
WithGRPCServerOption(grpc.ConnectionTimeout(10 * time.Second)),
)
assert.Len(t, s.gRPCServerOptions, 3)
}
func TestGRPCDialOption(t *testing.T) {
s := NewService(
WithGRPCDialOption(grpc.WithBlock()),
)
assert.Len(t, s.gRPCDialOptions, 1)
}
func TestWithHTTPServer(t *testing.T) {
s := NewService(WithHTTPServer(&http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}))
assert.NotNil(t, s.HTTPServer)
assert.Equal(t, 5*time.Second, s.HTTPServer.ReadTimeout)
}
func TestMuxOption(t *testing.T) {
s := NewService(
WithMuxOption(gRuntime.WithMarshalerOption(
gRuntime.MIMEWildcard, &gRuntime.HTTPBodyMarshaler{
Marshaler: &gRuntime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
},
)),
)
assert.Len(t, s.muxOptions, 3)
}