@@ -31,12 +31,7 @@ import (
3131// StubServer is a server that is easy to customize within individual test
3232// cases.
3333type StubServer struct {
34- // Guarantees we satisfy this interface; panics if unimplemented methods are called.
35- testpb.TestServiceServer
36-
37- EmptyCallF func (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error )
38- UnaryCallF func (ctx context.Context , in * testpb.SimpleRequest ) (* testpb.SimpleResponse , error )
39- FullDuplexCallF func (stream testpb.TestService_FullDuplexCallServer ) error
34+ testService testpb.TestServiceServer
4035
4136 s * grpc.Server
4237
@@ -47,19 +42,8 @@ type StubServer struct {
4742 cleanups []func () // Lambdas executed in Stop(); populated by Start().
4843}
4944
50- // EmptyCall is the handler for testpb.EmptyCall.
51- func (ss * StubServer ) EmptyCall (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error ) {
52- return ss .EmptyCallF (ctx , in )
53- }
54-
55- // UnaryCall is the handler for testpb.UnaryCall.
56- func (ss * StubServer ) UnaryCall (ctx context.Context , in * testpb.SimpleRequest ) (* testpb.SimpleResponse , error ) {
57- return ss .UnaryCallF (ctx , in )
58- }
59-
60- // FullDuplexCall is the handler for testpb.FullDuplexCall.
61- func (ss * StubServer ) FullDuplexCall (stream testpb.TestService_FullDuplexCallServer ) error {
62- return ss .FullDuplexCallF (stream )
45+ func New (testService testpb.TestServiceServer ) * StubServer {
46+ return & StubServer {testService : testService }
6347}
6448
6549// Start starts the server and creates a client connected to it.
@@ -79,7 +63,7 @@ func (ss *StubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption)
7963 ss .cleanups = append (ss .cleanups , func () { lis .Close () })
8064
8165 s := grpc .NewServer (sopts ... )
82- testpb .RegisterTestServiceServer (s , ss )
66+ testpb .RegisterTestServiceServer (s , ss . testService )
8367 go s .Serve (lis )
8468 ss .cleanups = append (ss .cleanups , s .Stop )
8569 ss .s = s
@@ -98,3 +82,23 @@ func (ss *StubServer) Stop() {
9882func (ss * StubServer ) Addr () string {
9983 return ss .Address
10084}
85+
86+ type dummyStubServer struct {
87+ testpb.UnimplementedTestServiceServer
88+ body []byte
89+ }
90+
91+ func (d dummyStubServer ) UnaryCall (context.Context , * testpb.SimpleRequest ) (* testpb.SimpleResponse , error ) {
92+ return & testpb.SimpleResponse {
93+ Payload : & testpb.Payload {
94+ Type : testpb .PayloadType_COMPRESSABLE ,
95+ Body : d .body ,
96+ },
97+ }, nil
98+ }
99+
100+ // NewDummyStubServer creates a simple test server that serves Unary calls with
101+ // responses with the given payload.
102+ func NewDummyStubServer (body []byte ) * StubServer {
103+ return New (dummyStubServer {body : body })
104+ }
0 commit comments