Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to echo request headers in trailing metadata #1501

Merged
merged 12 commits into from
Apr 15, 2024
2 changes: 2 additions & 0 deletions cmd/gapic-showcase/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func init() {

EchoCmd.Flags().StringVar(&echoInputOtherRequestId, "other_request_id", "", "To facilitate testing of...")

EchoCmd.Flags().StringSliceVar(&EchoInput.HttpRequestHeaderToEcho, "http_request_header_to_echo", []string{}, "Http request headers to echo via...")

EchoCmd.Flags().StringVar(&EchoInputResponse, "response", "", "Choices: content, error")

EchoCmd.Flags().StringVar(&EchoFromFile, "from_file", "", "Absolute path to JSON file containing request payload")
Expand Down
10 changes: 10 additions & 0 deletions schema/google/showcase/v1beta1/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ message EchoRequest {
optional string other_request_id = 8 [
(google.api.field_info).format = UUID4
];

// Http request headers to echo via EchoResponse.http_request_header_value
repeated string http_request_header_to_echo = 9;
}

// The response message for the Echo methods.
Expand All @@ -237,6 +240,13 @@ message EchoResponse {

// The other request ID specified or autopopulated in the request.
string other_request_id = 4;


message RepeatedValues {
repeated string header_values = 1;
}
// The HTTP header to echo as specified in the request
map<string, RepeatedValues> http_request_header_value = 5;
}

// The request message used for the EchoErrorDetails method.
Expand Down
2 changes: 1 addition & 1 deletion schema/googleapis
Submodule googleapis updated 154 files
2 changes: 1 addition & 1 deletion server/genproto/compliance.pb.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert all of the changes in the server/genproto directory, the headers will just be replaced when things are regenerated by CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

746 changes: 425 additions & 321 deletions server/genproto/echo.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/genproto/identity.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/genproto/messaging.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/genproto/sequence.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/genproto/testing.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions server/services/echo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ type echoServerImpl struct {

func (s *echoServerImpl) Echo(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
err := status.ErrorProto(in.GetError())
if err != nil {
md, ok := metadata.FromIncomingContext(ctx)
if err != nil || !ok {
return nil, err
}
echoHeaders(ctx)
echoTrailers(ctx)
return &pb.EchoResponse{Content: in.GetContent(), Severity: in.GetSeverity(), RequestId: in.GetRequestId(), OtherRequestId: in.GetOtherRequestId()}, nil
requestHeaders := make(map[string]*pb.EchoResponse_RepeatedValues)
headersToTrack := in.GetHttpRequestHeaderToEcho()
for k, v := range md {
if strContains(headersToTrack, k) {
requestHeaders[k] = &pb.EchoResponse_RepeatedValues{HeaderValues: v}
}
}

return &pb.EchoResponse{Content: in.GetContent(), Severity: in.GetSeverity(), RequestId: in.GetRequestId(), OtherRequestId: in.GetOtherRequestId(), HttpRequestHeaderValue: requestHeaders}, nil
}

func (s *echoServerImpl) EchoErrorDetails(ctx context.Context, in *pb.EchoErrorDetailsRequest) (*pb.EchoErrorDetailsResponse, error) {
Expand Down Expand Up @@ -141,6 +150,8 @@ func (s *echoServerImpl) Chat(stream pb.Echo_ChatServer) error {
for {
req, err := stream.Recv()
if err == io.EOF {
// Echo headers and trailers when the stream ends
echoStreamingHeaders(stream)
echoStreamingTrailers(stream)
return nil
}
Expand All @@ -152,7 +163,6 @@ func (s *echoServerImpl) Chat(stream pb.Echo_ChatServer) error {
if s != nil {
return s
}
echoStreamingHeaders(stream)
stream.Send(&pb.EchoResponse{Content: req.GetContent()})
}
}
Expand Down
13 changes: 9 additions & 4 deletions server/services/echo_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ func TestEcho_success(t *testing.T) {
server := NewEchoServer()
for _, val := range table {
in := &pb.EchoRequest{
Response: &pb.EchoRequest_Content{Content: val},
Severity: pb.Severity_CRITICAL,
Response: &pb.EchoRequest_Content{Content: val},
Severity: pb.Severity_CRITICAL,
HttpRequestHeaderToEcho: []string{"x-goog-api-version"},
}
mockStream := &mockUnaryStream{t: t}
ctx := appendTestOutgoingMetadata(context.Background(), &mockSTS{t: t, stream: mockStream})
out, err := server.Echo(ctx, in)
if !reflect.DeepEqual(out.HttpRequestHeaderValue["x-goog-api-version"].HeaderValues[0], "apiVersion") {
t.Errorf("Did not find apiVersion in response")
}
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -153,6 +157,7 @@ func (m *mockExpandStream) SetTrailer(md metadata.MD) {

func (m *mockExpandStream) SetHeader(md metadata.MD) error {
m.head = append(m.head, md.Get("x-goog-request-params")...)
m.head = append(m.head, md.Get("x-goog-api-version")...)
return nil
}

Expand Down Expand Up @@ -367,7 +372,7 @@ func (m *mockChatStream) SetTrailer(md metadata.MD) {
}

func (m *mockChatStream) verify(expectHeadersAndTrailers bool) {
if expectHeadersAndTrailers && !reflect.DeepEqual([]string{"show", "case"}, m.trail) && !reflect.DeepEqual([]string{"showcaseHeader", "anotherHeader"}, m.head) {
if expectHeadersAndTrailers && (!reflect.DeepEqual([]string{"show", "case"}, m.trail) || !reflect.DeepEqual([]string{"showcaseHeader", "anotherHeader"}, m.head)) {
m.t.Errorf("Chat did not get all expected trailers.\nGot these headers: %+v\nGot these trailers: %+v", m.head, m.trail)
}
}
Expand Down Expand Up @@ -898,6 +903,6 @@ func TestBlockError(t *testing.T) {

func appendTestOutgoingMetadata(ctx context.Context, stream grpc.ServerTransportStream) context.Context {
ctx = grpc.NewContextWithServerTransportStream(ctx, stream)
ctx = metadata.NewIncomingContext(ctx, metadata.Pairs("showcase-trailer", "show", "showcase-trailer", "case", "trailer", "trail", "x-goog-request-params", "showcaseHeader", "x-goog-request-params", "anotherHeader", "header", "head"))
ctx = metadata.NewIncomingContext(ctx, metadata.Pairs("showcase-trailer", "show", "showcase-trailer", "case", "trailer", "trail", "x-goog-request-params", "showcaseHeader", "x-goog-request-params", "anotherHeader", "header", "head", "x-goog-api-version", "apiVersion"))
return ctx
}
Loading