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

Support multiple metadata annotators #586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func AnnotateContext(ctx context.Context, mux *ServeMux, req *http.Request) (con
return ctx, nil
}
md := metadata.Pairs(pairs...)
if mux.metadataAnnotator != nil {
md = metadata.Join(md, mux.metadataAnnotator(ctx, req))
for _, mda := range mux.metadataAnnotators {
md = metadata.Join(md, mda(ctx, req))
}
return metadata.NewOutgoingContext(ctx, md), nil
}
Expand Down
20 changes: 20 additions & 0 deletions runtime/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,23 @@ func TestAnnotateContext_SupportsTimeouts(t *testing.T) {
}
}
}
func TestAnnotateContext_SupportsCustomAnnotators(t *testing.T) {
md1 := func(context.Context, *http.Request) metadata.MD { return metadata.New(map[string]string{"foo": "bar"}) }
md2 := func(context.Context, *http.Request) metadata.MD { return metadata.New(map[string]string{"baz": "qux"}) }
expected := metadata.New(map[string]string{"foo": "bar", "baz": "qux"})
request, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatalf(`http.NewRequest("GET", "http://example.com", nil failed with %v; want success`, err)
}
annotated, err := runtime.AnnotateContext(context.Background(), runtime.NewServeMux(runtime.WithMetadata(md1), runtime.WithMetadata(md2)), request)
if err != nil {
t.Errorf("runtime.AnnotateContext(ctx, %#v) failed with %v; want success", request, err)
return
}
actual, _ := metadata.FromOutgoingContext(annotated)
for key, e := range expected {
if a, ok := actual[key]; !ok || !reflect.DeepEqual(e, a) {
t.Errorf("metadata.MD[%s] = %v; want %v", key, a, e)
}
}
}
4 changes: 2 additions & 2 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ServeMux struct {
marshalers marshalerRegistry
incomingHeaderMatcher HeaderMatcherFunc
outgoingHeaderMatcher HeaderMatcherFunc
metadataAnnotator func(context.Context, *http.Request) metadata.MD
metadataAnnotators []func(context.Context, *http.Request) metadata.MD
protoErrorHandler ProtoErrorHandlerFunc
}

Expand Down Expand Up @@ -87,7 +87,7 @@ func WithOutgoingHeaderMatcher(fn HeaderMatcherFunc) ServeMuxOption {
// is reading token from cookie and adding it in gRPC context.
func WithMetadata(annotator func(context.Context, *http.Request) metadata.MD) ServeMuxOption {
return func(serveMux *ServeMux) {
serveMux.metadataAnnotator = annotator
serveMux.metadataAnnotators = append(serveMux.metadataAnnotators, annotator)
}
}

Expand Down