Skip to content

Commit

Permalink
feat: Add ability to echo request headers in response
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Apr 15, 2024
1 parent add2dc2 commit f15a0bb
Show file tree
Hide file tree
Showing 11 changed files with 463 additions and 332 deletions.
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{}, "Echo http request header as trailing metadata")

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
];

// Echo http request header as trailing metadata
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

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.

14 changes: 12 additions & 2 deletions server/services/echo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io"
"slices"

Check failure on line 21 in server/services/echo_service.go

View workflow job for this annotation

GitHub Actions / probes

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.2/x64/src/slices)

Check failure on line 21 in server/services/echo_service.go

View workflow job for this annotation

GitHub Actions / tests

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.2/x64/src/slices)
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -46,12 +47,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
request_headers := make(map[string]*pb.EchoResponse_RepeatedValues)
headers_to_track := in.GetHttpRequestHeaderToEcho()
for k, v := range md {
if slices.Contains(headers_to_track, k) {
request_headers[k] = &pb.EchoResponse_RepeatedValues{HeaderValues: v}
}
}

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

func (s *echoServerImpl) EchoErrorDetails(ctx context.Context, in *pb.EchoErrorDetailsRequest) (*pb.EchoErrorDetailsResponse, error) {
Expand Down
11 changes: 8 additions & 3 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 @@ -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
}

0 comments on commit f15a0bb

Please sign in to comment.