Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Support binary metadata
Browse files Browse the repository at this point in the history
Add support for binary metadata (postfixed with `-bin`).

This ports changes from the grafana/k6#3234
  • Loading branch information
olegbespalov committed Sep 4, 2023
1 parent 9da0f76 commit 1cea35a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
25 changes: 25 additions & 0 deletions grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ func TestClient(t *testing.T) {
}
`},
},
{
name: "RequestBinHeaders",
initString: codeBlock{
code: `
var client = new grpc.Client();
client.load([], "../grpc/testdata/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
17 changes: 14 additions & 3 deletions grpc/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,23 @@ func newCallParams(vu modules.VU, input goja.Value) (*callParams, error) {
return result, errors.New("metadata must be an object with key-value pairs")
}
for hk, kv := range rawHeaders {
strval, ok := kv.(string)
if !ok {
var val string

// The gRPC spec defines that Binary-valued keys end in -bin
// https://grpc.io/docs/what-is-grpc/core-concepts/#metadata
if strings.HasSuffix(hk, "-bin") {
var binVal []byte
if binVal, ok = kv.([]byte); !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
val = string(binVal)
} else if val, ok = kv.(string); !ok {
return result, fmt.Errorf("metadata %q value must be a string", hk)
}

result.Metadata.Append(hk, strval)
result.Metadata.Append(hk, val)
}
case "tags":
if err := common.ApplyCustomUserTags(rt, &result.TagsAndMeta, params.Get(k)); err != nil {
Expand Down

0 comments on commit 1cea35a

Please sign in to comment.