Skip to content

Commit

Permalink
Support binary passed in GRPC metadata (#3234)
Browse files Browse the repository at this point in the history
Support binary data for Metadata following the specification that requires a -bin suffix.
  • Loading branch information
sapphire-janrain authored and oleiade committed Sep 8, 2023
1 parent b7d200d commit 8ac324a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
14 changes: 12 additions & 2 deletions js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 8ac324a

Please sign in to comment.