Skip to content

Commit

Permalink
DefaultHeaderMatcher: use textproto.CanonicalMIMEHeaderKey ok receive…
Browse files Browse the repository at this point in the history
…d key
  • Loading branch information
joelclouddistrict authored and johanbrandhorst committed Oct 19, 2018
1 parent 1771d34 commit f7cf649
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit f7cf649

Please sign in to comment.