2019-05-31 01:52:10 +03:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2019-05-31 01:52:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestToContext(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
request *http.Request
|
|
|
|
expect metadata.Metadata
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&http.Request{
|
|
|
|
Header: http.Header{
|
2019-12-31 16:53:48 +03:00
|
|
|
"Foo1": []string{"bar"},
|
|
|
|
"Foo2": []string{"bar", "baz"},
|
2019-05-31 01:52:10 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
metadata.Metadata{
|
2019-12-31 16:53:48 +03:00
|
|
|
"Foo1": "bar",
|
|
|
|
"Foo2": "bar,baz",
|
2019-05-31 01:52:10 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
|
|
|
ctx := FromRequest(d.request)
|
2021-02-09 01:08:45 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2019-05-31 01:52:10 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected metadata for request %+v", d.request)
|
|
|
|
}
|
|
|
|
for k, v := range d.expect {
|
|
|
|
if val := md[k]; val != v {
|
|
|
|
t.Fatalf("Expected %s for key %s for expected md %+v, got md %+v", v, k, d.expect, md)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|