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

Support binary passed in GRPC metadata #3234

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions js/modules/k6/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,17 @@ 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)
}
strval = string(binval)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we link to https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md#storing-binary-data-in-metadata so that whoever comes after us knows why this is how it is done instead of keeping it in binary and giving it to the grpc library as binary

} 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