MCPcopy
hub / github.com/grpc/grpc-go / TestChainUnaryServerInterceptor

Method TestChainUnaryServerInterceptor

test/server_test.go:171–286  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

169}
170
171func (s) TestChainUnaryServerInterceptor(t *testing.T) {
172 var (
173 firstIntKey = ctxKey("firstIntKey")
174 secondIntKey = ctxKey("secondIntKey")
175 )
176
177 firstInt := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
178 if ctx.Value(firstIntKey) != nil {
179 return nil, status.Errorf(codes.Internal, "first interceptor should not have %v in context", firstIntKey)
180 }
181 if ctx.Value(secondIntKey) != nil {
182 return nil, status.Errorf(codes.Internal, "first interceptor should not have %v in context", secondIntKey)
183 }
184
185 firstCtx := context.WithValue(ctx, firstIntKey, 0)
186 resp, err := handler(firstCtx, req)
187 if err != nil {
188 return nil, status.Errorf(codes.Internal, "failed to handle request at firstInt")
189 }
190
191 simpleResp, ok := resp.(*testpb.SimpleResponse)
192 if !ok {
193 return nil, status.Errorf(codes.Internal, "failed to get *testpb.SimpleResponse at firstInt")
194 }
195 return &testpb.SimpleResponse{
196 Payload: &testpb.Payload{
197 Type: simpleResp.GetPayload().GetType(),
198 Body: append(simpleResp.GetPayload().GetBody(), '1'),
199 },
200 }, nil
201 }
202
203 secondInt := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
204 if ctx.Value(firstIntKey) == nil {
205 return nil, status.Errorf(codes.Internal, "second interceptor should have %v in context", firstIntKey)
206 }
207 if ctx.Value(secondIntKey) != nil {
208 return nil, status.Errorf(codes.Internal, "second interceptor should not have %v in context", secondIntKey)
209 }
210
211 secondCtx := context.WithValue(ctx, secondIntKey, 1)
212 resp, err := handler(secondCtx, req)
213 if err != nil {
214 return nil, status.Errorf(codes.Internal, "failed to handle request at secondInt")
215 }
216
217 simpleResp, ok := resp.(*testpb.SimpleResponse)
218 if !ok {
219 return nil, status.Errorf(codes.Internal, "failed to get *testpb.SimpleResponse at secondInt")
220 }
221 return &testpb.SimpleResponse{
222 Payload: &testpb.Payload{
223 Type: simpleResp.GetPayload().GetType(),
224 Body: append(simpleResp.GetPayload().GetBody(), '2'),
225 },
226 }, nil
227 }
228

Callers

nothing calls this directly

Calls 15

StartMethod · 0.95
StopMethod · 0.95
ErrorfFunction · 0.92
ChainUnaryInterceptorFunction · 0.92
FromErrorFunction · 0.92
newPayloadFunction · 0.85
WithValueMethod · 0.80
CodeMethod · 0.80
ctxKeyTypeAlias · 0.70
FatalfMethod · 0.65
UnaryCallMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected