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

Support binary metadata #46

Merged
merged 1 commit into from
Sep 4, 2023
Merged
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
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