2019-11-16 20:48:24 +02:00
|
|
|
package wrapper
|
2016-04-15 17:45:59 +02:00
|
|
|
|
|
|
|
import (
|
2018-03-03 13:53:52 +02:00
|
|
|
"context"
|
2020-05-24 21:26:37 +02:00
|
|
|
"reflect"
|
2016-04-15 17:45:59 +02:00
|
|
|
"testing"
|
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
"go-micro.dev/v5/auth"
|
|
|
|
"go-micro.dev/v5/client"
|
|
|
|
"go-micro.dev/v5/metadata"
|
|
|
|
"go-micro.dev/v5/server"
|
2016-04-15 17:45:59 +02: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 15:45:49 +02:00
|
|
|
"Foo": "bar",
|
2016-04-15 17:45:59 +02:00
|
|
|
},
|
|
|
|
overwrite: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
existing: metadata.Metadata{
|
2019-12-31 15:45:49 +02:00
|
|
|
"Foo": "bar",
|
2016-04-15 17:45:59 +02:00
|
|
|
},
|
|
|
|
headers: metadata.Metadata{
|
2019-12-31 15:45:49 +02:00
|
|
|
"Foo": "baz",
|
2016-04-15 17:45:59 +02:00
|
|
|
},
|
|
|
|
overwrite: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
2020-04-29 16:37:02 +02:00
|
|
|
c := &fromServiceWrapper{
|
2016-04-15 17:45:59 +02:00
|
|
|
headers: d.headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := metadata.NewContext(context.Background(), d.existing)
|
2016-11-02 19:21:53 +02:00
|
|
|
ctx = c.setHeaders(ctx)
|
2016-04-15 17:45:59 +02: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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 11:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type testAuth struct {
|
|
|
|
verifyCount int
|
|
|
|
inspectCount int
|
|
|
|
namespace string
|
|
|
|
inspectAccount *auth.Account
|
|
|
|
verifyError error
|
|
|
|
|
|
|
|
auth.Auth
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *testAuth) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error {
|
|
|
|
a.verifyCount = a.verifyCount + 1
|
|
|
|
return a.verifyError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *testAuth) Inspect(token string) (*auth.Account, error) {
|
|
|
|
a.inspectCount = a.inspectCount + 1
|
|
|
|
return a.inspectAccount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *testAuth) Options() auth.Options {
|
|
|
|
return auth.Options{Namespace: a.namespace}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testRequest struct {
|
|
|
|
service string
|
|
|
|
endpoint string
|
|
|
|
|
|
|
|
server.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r testRequest) Service() string {
|
|
|
|
return r.service
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r testRequest) Endpoint() string {
|
|
|
|
return r.endpoint
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:05:23 +02:00
|
|
|
type testClient struct {
|
|
|
|
callCount int
|
|
|
|
callRsp interface{}
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
c.callCount++
|
2020-05-24 21:26:37 +02:00
|
|
|
|
|
|
|
if c.callRsp != nil {
|
|
|
|
val := reflect.ValueOf(rsp).Elem()
|
|
|
|
val.Set(reflect.ValueOf(c.callRsp).Elem())
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:05:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 21:26:37 +02:00
|
|
|
type testRsp struct {
|
|
|
|
value string
|
2020-05-24 19:05:23 +02:00
|
|
|
}
|