metadata: split context to incoming and outgoing

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-02-09 01:08:45 +03:00
parent 1de9911b73
commit 0e51a79bb6
9 changed files with 84 additions and 136 deletions

View File

@@ -10,10 +10,9 @@ import (
func FromRequest(r *http.Request) context.Context {
ctx := r.Context()
md, ok := metadata.FromContext(ctx)
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
// create needed map with specific len
md = make(metadata.Metadata, len(r.Header)+2)
md = metadata.New(len(r.Header) + 2)
}
for key, val := range r.Header {
md.Set(key, strings.Join(val, ","))
@@ -22,5 +21,5 @@ func FromRequest(r *http.Request) context.Context {
md["Host"] = r.Host
// pass http method
md["Method"] = r.Method
return metadata.NewContext(ctx, md)
return metadata.NewIncomingContext(ctx, md)
}

View File

@@ -28,7 +28,7 @@ func TestRequestToContext(t *testing.T) {
for _, d := range testData {
ctx := FromRequest(d.request)
md, ok := metadata.FromContext(ctx)
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
t.Fatalf("Expected metadata for request %+v", d.request)
}