Skip to content

Commit

Permalink
Merge pull request #77 from gengo/feature/support-multivalue-metadata
Browse files Browse the repository at this point in the history
Support multivalue of metadata
  • Loading branch information
yugui committed Dec 24, 2015
2 parents 49bbcc0 + 31e9441 commit 714a8df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
21 changes: 12 additions & 9 deletions runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ will be the same context.
*/
func AnnotateContext(ctx context.Context, req *http.Request) context.Context {
var pairs []string
for key, val := range req.Header {
if strings.HasPrefix(key, metadataHeaderPrefix) {
pairs = append(pairs, key[len(metadataHeaderPrefix):], val[0])
}
if key == "Authorization" {
pairs = append(pairs, key, val[0])
for key, vals := range req.Header {
for _, val := range vals {
if key == "Authorization" {
pairs = append(pairs, key, val)
continue
}
if strings.HasPrefix(key, metadataHeaderPrefix) {
pairs = append(pairs, key[len(metadataHeaderPrefix):], val)
}
}
}

if len(pairs) != 0 {
ctx = metadata.NewContext(ctx, metadata.Pairs(pairs...))
if len(pairs) == 0 {
return ctx
}
return ctx
return metadata.NewContext(ctx, metadata.Pairs(pairs...))
}
19 changes: 14 additions & 5 deletions runtime/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,32 @@ import (
func TestAnnotateContext(t *testing.T) {
ctx := context.Background()

request, _ := http.NewRequest("GET", "http://localhost", nil)
request.Header = http.Header{}
request, err := http.NewRequest("GET", "http://localhost", nil)
if err != nil {
t.Fatalf("http.NewRequest(%q, %q, nil) failed with %v; want success", "GET", "http://localhost", err)
}
request.Header.Add("Some-Irrelevant-Header", "some value")
annotated := runtime.AnnotateContext(ctx, request)
if annotated != ctx {
t.Errorf("AnnotateContext(ctx, request) = %v; want %v", annotated, ctx)
}

request.Header.Add("Grpc-Metadata-FooBar", "Value1")
request.Header.Add("Grpc-Metadata-Foo-BAZ", "Value2")
request.Header.Add("Grpc-Metadata-foo-bAz", "Value3")
request.Header.Add("Authorization", "Token 1234567890")
annotated = runtime.AnnotateContext(ctx, request)
md, ok := metadata.FromContext(annotated)
if !ok || len(md) != 2 {
t.Errorf("Expected 2 metadata items in context; got %v", md)
if !ok || len(md) != 3 {
t.Errorf("Expected 3 metadata items in context; got %v", md)
}
if got, want := md["foobar"], []string{"Value1"}; !reflect.DeepEqual(got, want) {
t.Errorf(`md["foobar"] = %q; want %q`, got, want)
}
if got, want := md["foo-baz"], []string{"Value2"}; !reflect.DeepEqual(got, want) {
if got, want := md["foo-baz"], []string{"Value2", "Value3"}; !reflect.DeepEqual(got, want) {
t.Errorf(`md["foo-baz"] = %q want %q`, got, want)
}
if got, want := md["authorization"], []string{"Token 1234567890"}; !reflect.DeepEqual(got, want) {
t.Errorf(`md["authorization"] = %q want %q`, got, want)
}
}

0 comments on commit 714a8df

Please sign in to comment.