From aea09c9e9affca6f327dc31c015bebae8df65079 Mon Sep 17 00:00:00 2001 From: Sapphire Becker <41972890+sapphire-janrain@users.noreply.github.com> Date: Fri, 25 Aug 2023 01:21:11 -0700 Subject: [PATCH] Support binary passed in GRPC metadata (#3234) Support binary data for Metadata following the specification that requires a -bin suffix. --- js/modules/k6/grpc/client.go | 14 ++++++++++++-- js/modules/k6/grpc/client_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/js/modules/k6/grpc/client.go b/js/modules/k6/grpc/client.go index acde94093b4..2ffb81a72f2 100644 --- a/js/modules/k6/grpc/client.go +++ b/js/modules/k6/grpc/client.go @@ -459,8 +459,18 @@ func (c *Client) parseInvokeParams(paramsVal goja.Value) (*invokeParams, error) } for hk, kv := range rawHeaders { // TODO(rogchap): Should we manage a string slice? - strval, ok := kv.(string) - if !ok { + // The spec defines that Binary-valued keys end in -bin + // https://grpc.io/docs/what-is-grpc/core-concepts/#metadata + var strval string + if strings.HasSuffix(hk, "-bin") { + var binval []byte + binval, ok = kv.([]byte) + if !ok { + return result, fmt.Errorf("metadata %q value must be binary", hk) + } + // https://github.com/grpc/grpc-go/blob/v1.57.0/Documentation/grpc-metadata.md#storing-binary-data-in-metadata + strval = string(binval) + } else if strval, ok = kv.(string); !ok { return result, fmt.Errorf("metadata %q value must be a string", hk) } result.Metadata[hk] = strval diff --git a/js/modules/k6/grpc/client_test.go b/js/modules/k6/grpc/client_test.go index a0506f88c1e..a10fa263a03 100644 --- a/js/modules/k6/grpc/client_test.go +++ b/js/modules/k6/grpc/client_test.go @@ -485,6 +485,31 @@ func TestClient(t *testing.T) { } `}, }, + { + name: "RequestBinHeaders", + initString: codeBlock{ + code: ` + var client = new grpc.Client(); + client.load([], "../../../../lib/testutils/httpmultibin/grpc_testing/test.proto");`, + }, + setup: func(tb *httpmultibin.HTTPMultiBin) { + tb.GRPCStub.EmptyCallFunc = func(ctx context.Context, _ *grpc_testing.Empty) (*grpc_testing.Empty, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok || len(md["x-load-tester-bin"]) == 0 || md["x-load-tester-bin"][0] != string([]byte{2, 200}) { + return nil, status.Error(codes.FailedPrecondition, "") + } + + return &grpc_testing.Empty{}, nil + } + }, + vuString: codeBlock{code: ` + client.connect("GRPCBIN_ADDR"); + var resp = client.invoke("grpc.testing.TestService/EmptyCall", {}, { metadata: { "X-Load-Tester-bin": new Uint8Array([2, 200]) } }) + if (resp.status !== grpc.StatusOK) { + throw new Error("failed to send correct headers in the request") + } + `}, + }, { name: "ResponseMessage", initString: codeBlock{