github.com/grpc-ecosystem/grpc-gateway/v2@v2.19.1/runtime/mux_internal_test.go (about)

     1  package runtime
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  )
     7  
     8  func TestWithIncomingHeaderMatcher_matchedMalformedHeaders(t *testing.T) {
     9  	tests := []struct {
    10  		name    string
    11  		matcher HeaderMatcherFunc
    12  		want    []string
    13  	}{
    14  		{
    15  			"nil matcher returns nothing",
    16  			nil,
    17  			nil,
    18  		},
    19  		{
    20  			"default matcher returns nothing",
    21  			DefaultHeaderMatcher,
    22  			nil,
    23  		},
    24  		{
    25  			"passthrough matcher returns all malformed headers",
    26  			func(s string) (string, bool) {
    27  				return s, true
    28  			},
    29  			[]string{"connection"},
    30  		},
    31  	}
    32  
    33  	sliceEqual := func(a, b []string) bool {
    34  		if len(a) != len(b) {
    35  			return false
    36  		}
    37  		sort.Slice(a, func(i, j int) bool {
    38  			return a[i] < a[j]
    39  		})
    40  		sort.Slice(b, func(i, j int) bool {
    41  			return a[i] < a[j]
    42  		})
    43  		for idx := range a {
    44  			if a[idx] != b[idx] {
    45  				return false
    46  			}
    47  		}
    48  		return true
    49  	}
    50  
    51  	for _, tt := range tests {
    52  		out := tt.matcher.matchedMalformedHeaders()
    53  		if !sliceEqual(tt.want, out) {
    54  			t.Errorf("matchedMalformedHeaders not match; Want %v; got %v",
    55  				tt.want, out)
    56  		}
    57  	}
    58  }