From be6640a55e8e6484f5f8eaa119c99c3e1a010f57 Mon Sep 17 00:00:00 2001 From: Rao Li Date: Tue, 1 Aug 2023 11:54:29 -0700 Subject: [PATCH 1/3] Add parameter to "client.connect" so headers can be attached to the reflect command that comes after client.connect --- js/modules/k6/grpc/client.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/js/modules/k6/grpc/client.go b/js/modules/k6/grpc/client.go index acde94093b4..4b1bfe67dfe 100644 --- a/js/modules/k6/grpc/client.go +++ b/js/modules/k6/grpc/client.go @@ -260,6 +260,13 @@ func (c *Client) Connect(addr string, params map[string]interface{}) (bool, erro if !p.UseReflectionProtocol { return true, nil } + + md := metadata.New(nil) + for param, strval := range p.ReflectMetadata { + md.Append(param, strval) + } + ctx = metadata.NewOutgoingContext(ctx, md) + fdset, err := c.conn.Reflect(ctx) if err != nil { return false, err @@ -490,6 +497,7 @@ type connectParams struct { MaxReceiveSize int64 MaxSendSize int64 TLS map[string]interface{} + ReflectMetadata map[string]string } func (c *Client) parseConnectParams(raw map[string]interface{}) (connectParams, error) { @@ -538,6 +546,20 @@ func (c *Client) parseConnectParams(raw map[string]interface{}) (connectParams, if params.MaxSendSize < 0 { return params, fmt.Errorf("invalid maxSendSize value: '%#v, it needs to be a positive integer", v) } + case "reflectMetadata": + params.ReflectMetadata = make(map[string]string) + rawHeaders, ok := v.(map[string]interface{}) + if !ok { + return params, errors.New("metadata must be an object with key-value pairs") + } + for hk, kv := range rawHeaders { + // TODO(rogchap): Should we manage a string slice? + strval, ok := kv.(string) + if !ok { + return params, fmt.Errorf("metadata %q value must be a string", hk) + } + params.ReflectMetadata[hk] = strval + } case "tls": var ok bool params.TLS, ok = v.(map[string]interface{}) From e2b89f65a032da75a252958878a70d717fc9a7e7 Mon Sep 17 00:00:00 2001 From: Rao Li Date: Tue, 1 Aug 2023 12:49:53 -0700 Subject: [PATCH 2/3] test --- js/modules/k6/grpc/client_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/js/modules/k6/grpc/client_test.go b/js/modules/k6/grpc/client_test.go index a0506f88c1e..a16b583fa67 100644 --- a/js/modules/k6/grpc/client_test.go +++ b/js/modules/k6/grpc/client_test.go @@ -654,6 +654,20 @@ func TestClient(t *testing.T) { err: `invalid reflect value`, }, }, + { + name: "ReflectMetadata", + setup: func(tb *httpmultibin.HTTPMultiBin) { + reflection.Register(tb.ServerGRPC) + }, + initString: codeBlock{ + code: `var client = new grpc.Client();`, + }, + vuString: codeBlock{ + code: ` + client.connect("GRPCBIN_ADDR", {reflect: true, reflectMetadata: { "X-Load-Tester": "k6" } }) + `, + }, + }, { name: "ReflectInvokeNoExist", setup: func(tb *httpmultibin.HTTPMultiBin) { From da66ac8e18eab4310d02df047b611deedfe6af10 Mon Sep 17 00:00:00 2001 From: Rao Li Date: Tue, 1 Aug 2023 15:16:56 -0700 Subject: [PATCH 3/3] modify comment --- js/modules/k6/grpc/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/modules/k6/grpc/client.go b/js/modules/k6/grpc/client.go index 4b1bfe67dfe..8fb2048a33c 100644 --- a/js/modules/k6/grpc/client.go +++ b/js/modules/k6/grpc/client.go @@ -552,8 +552,8 @@ func (c *Client) parseConnectParams(raw map[string]interface{}) (connectParams, if !ok { return params, errors.New("metadata must be an object with key-value pairs") } + // Same way metadata is handled in invoke parameter parsing for hk, kv := range rawHeaders { - // TODO(rogchap): Should we manage a string slice? strval, ok := kv.(string) if !ok { return params, fmt.Errorf("metadata %q value must be a string", hk)