Skip to content

Commit

Permalink
Support multiple metadata annotators (grpc-ecosystem#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmacthedestroyer authored and achew22 committed Apr 15, 2018
1 parent bdfb07f commit ec7cd6e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
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

0 comments on commit ec7cd6e

Please sign in to comment.