Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable user headers in the reflect call in client.connect #3242

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
// Same way metadata is handled in invoke parameter parsing
for hk, kv := range rawHeaders {
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{})
Expand Down
14 changes: 14 additions & 0 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down