forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor_test.go
52 lines (45 loc) · 1.51 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
package requestid
import (
"context"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/transport"
)
type testRequest struct{}
type testResponse struct{}
func DummyContextWithServerTransportStream() context.Context {
expectedStream := &transport.Stream{}
return grpc.NewContextWithServerTransportStream(context.Background(), expectedStream)
}
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 := 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 := 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)
}
}