forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interceptor_test.go
81 lines (73 loc) · 2.64 KB
/
interceptor_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
package requestid
import (
"context"
"testing"
mock_transport "github.com/infobloxopen/atlas-app-toolkit/mocks/transport"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type testRequest struct{}
type testResponse struct{}
func TestUnaryServerInterceptorWithoutRequestId(t *testing.T) {
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
reqID, exists := FromContext(ctx)
if exists && reqID == "" {
t.Errorf("requestId must be generated by interceptor")
}
return &testResponse{}, nil
}
ctx := mock_transport.DummyContextWithServerTransportStream()
_, err := UnaryServerInterceptor()(ctx, testRequest{}, nil, handler)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestUnaryServerInterceptorWithDummyRequestId(t *testing.T) {
dummyRequestID := newRequestID()
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
reqID, exists := FromContext(ctx)
if !exists || reqID != dummyRequestID {
t.Errorf("expected requestID: %q, returned requestId: %q", dummyRequestID, reqID)
}
return &testResponse{}, nil
}
ctx := mock_transport.DummyContextWithServerTransportStream()
md := metadata.Pairs(DefaultRequestIDKey, dummyRequestID)
newCtx := metadata.NewIncomingContext(ctx, md)
_, err := UnaryServerInterceptor()(newCtx, testRequest{}, nil, handler)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestStreamServerInterceptorWithoutRequestId(t *testing.T) {
handler := func(srv interface{}, stream grpc.ServerStream) error {
reqID, exists := FromContext(stream.Context())
if exists && reqID == "" {
t.Errorf("requestId must be generated by interceptor")
}
return nil
}
ctx := mock_transport.DummyContextWithServerTransportStream()
ms := mock_transport.NewMockServerStream(ctx)
err := StreamServerInterceptor()(testRequest{}, ms, nil, handler)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestStreamServerInterceptorWithDummyRequestId(t *testing.T) {
dummyRequestID := newRequestID()
handler := func(srv interface{}, stream grpc.ServerStream) error {
reqID, exists := FromContext(stream.Context())
if !exists || reqID != dummyRequestID {
t.Errorf("expected requestID: %q, returned requestId: %q", dummyRequestID, reqID)
}
return nil
}
ctx := mock_transport.DummyContextWithServerTransportStream()
md := metadata.Pairs(DefaultRequestIDKey, dummyRequestID)
newCtx := metadata.NewIncomingContext(ctx, md)
streamInterceptor := StreamServerInterceptor()
if err := streamInterceptor(testRequest{}, mock_transport.NewMockServerStream(newCtx), nil, handler); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}