-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgrpc_unwrap_interceptors_test.go
78 lines (66 loc) · 1.72 KB
/
grpc_unwrap_interceptors_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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package herodot
import (
"context"
"net"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/ory/herodot/internal"
)
type testingGreeter struct {
internal.UnimplementedGreeterServer
shouldErr bool
}
func (g *testingGreeter) SayHello(context.Context, *internal.HelloRequest) (*internal.HelloReply, error) {
if g.shouldErr {
return nil, errors.WithStack(ErrInternalServerError)
}
return &internal.HelloReply{Message: "see, no error"}, nil
}
func TestGRPCInterceptors(t *testing.T) {
server := &testingGreeter{}
s := grpc.NewServer(grpc.UnaryInterceptor(UnaryErrorUnwrapInterceptor))
internal.RegisterGreeterServer(s, server)
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
serveErr := &errgroup.Group{}
serveErr.Go(func() error {
return s.Serve(l)
})
conn, err := grpc.Dial(l.Addr().String(), grpc.WithInsecure())
require.NoError(t, err)
c := internal.NewGreeterClient(conn)
for _, tc := range []struct {
name string
shouldErr bool
}{
{
name: "no error",
shouldErr: false,
},
{
name: "internal error",
shouldErr: true,
},
} {
t.Run("case="+tc.name, func(t *testing.T) {
server.shouldErr = tc.shouldErr
resp, err := c.SayHello(context.Background(), &internal.HelloRequest{})
if tc.shouldErr {
assert.Equal(t, codes.Internal, status.Code(err))
} else {
assert.NoError(t, err)
assert.Equal(t, "see, no error", resp.Message)
}
})
}
s.Stop()
require.NoError(t, serveErr.Wait())
}