2019-11-16 21:48:24 +03:00
|
|
|
package wrapper
|
2016-04-15 18:45:59 +03:00
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-04-15 18:45:59 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-02-26 01:15:44 +03:00
|
|
|
"github.com/micro/go-micro/v2/auth"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
2016-04-15 18:45:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrapper(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
existing metadata.Metadata
|
|
|
|
headers metadata.Metadata
|
|
|
|
overwrite bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
existing: metadata.Metadata{},
|
|
|
|
headers: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "bar",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
overwrite: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
existing: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "bar",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
headers: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "baz",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
overwrite: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
|
|
|
c := &clientWrapper{
|
2020-02-26 01:15:44 +03:00
|
|
|
auth: func() auth.Auth { return nil },
|
2016-04-15 18:45:59 +03:00
|
|
|
headers: d.headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := metadata.NewContext(context.Background(), d.existing)
|
2016-11-02 20:21:53 +03:00
|
|
|
ctx = c.setHeaders(ctx)
|
2016-04-15 18:45:59 +03:00
|
|
|
md, _ := metadata.FromContext(ctx)
|
|
|
|
|
|
|
|
for k, v := range d.headers {
|
|
|
|
if d.overwrite && md[k] != v {
|
|
|
|
t.Fatalf("Expected %s=%s got %s=%s", k, v, k, md[k])
|
|
|
|
}
|
|
|
|
if !d.overwrite && md[k] != d.existing[k] {
|
|
|
|
t.Fatalf("Expected %s=%s got %s=%s", k, d.existing[k], k, md[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|