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

fix #782 Cannot return HTTP header using "Grpc-Metadata-" prefix #784

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
2 changes: 2 additions & 0 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/textproto"
"strings"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -50,6 +51,7 @@ type HeaderMatcherFunc func(string) (string, bool)
// keys (as specified by the IANA) to gRPC context with grpcgateway- prefix. HTTP headers that start with
// 'Grpc-Metadata-' are mapped to gRPC metadata after removing prefix 'Grpc-Metadata-'.
func DefaultHeaderMatcher(key string) (string, bool) {
key = textproto.CanonicalMIMEHeaderKey(key)
if isPermanentHTTPHeader(key) {
return MetadataPrefix + key, true
} else if strings.HasPrefix(key, MetadataHeaderPrefix) {
Expand Down
40 changes: 40 additions & 0 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,43 @@ func TestMuxServeHTTP(t *testing.T) {
}
}
}

var defaultHeaderMatcherTests = []struct {
name string
in string
outValue string
outValid bool
}{
{
"permanent HTTP header should return prefixed",
"Accept",
"grpcgateway-Accept",
true,
},
{
"key prefixed with MetadataHeaderPrefix should return without the prefix",
"Grpc-Metadata-Custom-Header",
"Custom-Header",
true,
},
{
"non-permanent HTTP header key without prefix should not return",
"Custom-Header",
"",
false,
},
}

func TestDefaultHeaderMatcher(t *testing.T) {
for _, tt := range defaultHeaderMatcherTests {
t.Run(tt.name, func(t *testing.T) {
out, valid := runtime.DefaultHeaderMatcher(tt.in)
if out != tt.outValue {
t.Errorf("got %v, want %v", out, tt.outValue)
}
if valid != tt.outValid {
t.Errorf("got %v, want %v", valid, tt.outValid)
}
})
}
}