2021-09-16 19:55:51 +00:00
|
|
|
package grpc
|
2021-01-28 14:58:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-15 15:28:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2021-01-28 14:58:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2021-01-28 14:58:32 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2021-06-15 15:28:49 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-01-28 14:58:32 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
type customErrorData struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:58:32 +00:00
|
|
|
func TestAppendHeaders(t *testing.T) {
|
2021-06-15 15:28:49 +00:00
|
|
|
t.Run("one_header", func(t *testing.T) {
|
2021-01-28 14:58:32 +00:00
|
|
|
ctx := AppendHeaders(context.Background(), []string{"first=value1"})
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.Equal(t, true, ok, "Failed to read context metadata")
|
2021-06-02 05:44:34 +00:00
|
|
|
require.Equal(t, 1, md.Len(), "MetadataV0 contains wrong number of values")
|
2021-01-28 14:58:32 +00:00
|
|
|
assert.Equal(t, "value1", md.Get("first")[0])
|
|
|
|
})
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
t.Run("multiple_headers", func(t *testing.T) {
|
2021-01-28 14:58:32 +00:00
|
|
|
ctx := AppendHeaders(context.Background(), []string{"first=value1", "second=value2"})
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.Equal(t, true, ok, "Failed to read context metadata")
|
2021-06-02 05:44:34 +00:00
|
|
|
require.Equal(t, 2, md.Len(), "MetadataV0 contains wrong number of values")
|
2021-01-28 14:58:32 +00:00
|
|
|
assert.Equal(t, "value1", md.Get("first")[0])
|
|
|
|
assert.Equal(t, "value2", md.Get("second")[0])
|
|
|
|
})
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
t.Run("one_empty_header", func(t *testing.T) {
|
2021-01-28 14:58:32 +00:00
|
|
|
ctx := AppendHeaders(context.Background(), []string{"first=value1", ""})
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.Equal(t, true, ok, "Failed to read context metadata")
|
2021-06-02 05:44:34 +00:00
|
|
|
require.Equal(t, 1, md.Len(), "MetadataV0 contains wrong number of values")
|
2021-01-28 14:58:32 +00:00
|
|
|
assert.Equal(t, "value1", md.Get("first")[0])
|
|
|
|
})
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
t.Run("incorrect_header", func(t *testing.T) {
|
2021-01-28 14:58:32 +00:00
|
|
|
logHook := logTest.NewGlobal()
|
|
|
|
ctx := AppendHeaders(context.Background(), []string{"first=value1", "second"})
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.Equal(t, true, ok, "Failed to read context metadata")
|
2021-06-02 05:44:34 +00:00
|
|
|
require.Equal(t, 1, md.Len(), "MetadataV0 contains wrong number of values")
|
2021-01-28 14:58:32 +00:00
|
|
|
assert.Equal(t, "value1", md.Get("first")[0])
|
|
|
|
assert.LogsContain(t, logHook, "Skipping second")
|
|
|
|
})
|
|
|
|
|
2021-06-15 15:28:49 +00:00
|
|
|
t.Run("header_value_with_equal_sign", func(t *testing.T) {
|
2021-01-28 14:58:32 +00:00
|
|
|
ctx := AppendHeaders(context.Background(), []string{"first=value=1"})
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
require.Equal(t, true, ok, "Failed to read context metadata")
|
2021-06-02 05:44:34 +00:00
|
|
|
require.Equal(t, 1, md.Len(), "MetadataV0 contains wrong number of values")
|
2021-01-28 14:58:32 +00:00
|
|
|
assert.Equal(t, "value=1", md.Get("first")[0])
|
|
|
|
})
|
|
|
|
}
|
2021-06-15 15:28:49 +00:00
|
|
|
|
|
|
|
func TestAppendCustomErrorHeader(t *testing.T) {
|
|
|
|
stream := &runtime.ServerTransportStream{}
|
|
|
|
ctx := grpc.NewContextWithServerTransportStream(context.Background(), stream)
|
|
|
|
data := &customErrorData{Message: "foo"}
|
|
|
|
require.NoError(t, AppendCustomErrorHeader(ctx, data))
|
|
|
|
// The stream used in test setup sets the metadata key in lowercase.
|
|
|
|
value, ok := stream.Header()[strings.ToLower(CustomErrorMetadataKey)]
|
|
|
|
require.Equal(t, true, ok, "Failed to retrieve custom error metadata value")
|
|
|
|
expected, err := json.Marshal(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, string(expected), value[0])
|
|
|
|
|
|
|
|
}
|