Skip to content

Commit

Permalink
tests(helpers): add EchoHeaders grpc rpc
Browse files Browse the repository at this point in the history
This will improve testability and reliability of some gRPC tests.
  • Loading branch information
gszr committed Dec 23, 2024
1 parent 8f3de57 commit bbde4b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spec/fixtures/grpc/proto/targetservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ message EchoMsg {
string nullable = 2;
}

message Header {
string key = 1;
string value = 2;
}

message Headers {
repeated Header headers = 1;
}

message Void {}
20 changes: 20 additions & 0 deletions spec/fixtures/grpc/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"log"
"net"
"time"
"strings"

pb "grpc/targetservice"

"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
Expand Down Expand Up @@ -53,6 +57,22 @@ func (s *server) Echo(ctx context.Context, in *pb.EchoMsg) (*pb.EchoMsg, error)
return in, nil
}

func (s *server) EchoHeaders(ctx context.Context, in *pb.Void) (*pb.Headers, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.DataLoss, "UnaryEcho: failed to get metadata")
}
headers := &pb.Headers{}
for k, v := range md {
header := &pb.Header{
Key: k,
Value: strings.Join(v, ", "),
}
headers.Headers = append(headers.Headers, header)
}
return headers, nil
}

func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
Expand Down

0 comments on commit bbde4b0

Please sign in to comment.