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: 1 addition & 1 deletion schema/googleapis
Submodule googleapis updated 154 files
18 changes: 10 additions & 8 deletions server/services/echo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,11 @@ func echoTrailers(ctx context.Context) {
return
}

values := md.Get("showcase-trailer")
for _, value := range values {
trailer := metadata.Pairs("showcase-trailer", value)
grpc.SetTrailer(ctx, trailer)
for k, v := range md {
for _, value := range v {
trailer := metadata.Pairs(k, value)
grpc.SetTrailer(ctx, trailer)
}
}
}

Expand All @@ -326,9 +327,10 @@ func echoStreamingTrailers(stream grpc.ServerStream) {
return
}

values := md.Get("showcase-trailer")
for _, value := range values {
trailer := metadata.Pairs("showcase-trailer", value)
stream.SetTrailer(trailer)
for k, v := range md {
for _, value := range v {
trailer := metadata.Pairs(k, value)
stream.SetTrailer(trailer)
}
}
}
9 changes: 7 additions & 2 deletions server/services/echo_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"io"
"reflect"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -116,13 +117,16 @@ func (m *mockUnaryStream) Send(resp *pb.EchoResponse) error { return nil }
func (m *mockUnaryStream) Context() context.Context { return nil }
func (m *mockUnaryStream) SetTrailer(md metadata.MD) {
m.trail = append(m.trail, md.Get("showcase-trailer")...)
m.trail = append(m.trail, md.Get("x-goog-api-version")...)
// Sort the trailer values as having a guaranteed order will help with array comparison
sort.Strings(m.trail)
}
func (m *mockUnaryStream) SetHeader(md metadata.MD) error {
m.head = append(m.head, md.Get("x-goog-request-params")...)
return nil
}
func (m *mockUnaryStream) verify(expectHeadersAndTrailers bool) {
if expectHeadersAndTrailers && (!reflect.DeepEqual([]string{"show", "case"}, m.trail) || !reflect.DeepEqual([]string{"showcaseHeader", "anotherHeader"}, m.head)) {
if expectHeadersAndTrailers && (!reflect.DeepEqual([]string{"apiVersion", "case", "show"}, m.trail) || !reflect.DeepEqual([]string{"showcaseHeader", "anotherHeader"}, m.head)) {
m.t.Errorf("Unary stream did not get all expected headers and trailers.\nGot these headers: %+v\nGot these trailers: %+v", m.head, m.trail)
}
}
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
}
Loading