forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouting_test.go
More file actions
145 lines (134 loc) · 6.09 KB
/
Copy pathrouting_test.go
File metadata and controls
145 lines (134 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
*
* Copyright 2026 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package server
import (
"context"
"strings"
"sync/atomic"
"testing"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/transport"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func (s) TestMatchTypeForDomain(t *testing.T) {
tests := []struct {
d string
want domainMatchType
}{
{d: "", want: domainMatchTypeInvalid},
{d: "*", want: domainMatchTypeUniversal},
{d: "bar.*", want: domainMatchTypePrefix},
{d: "*.abc.com", want: domainMatchTypeSuffix},
{d: "foo.bar.com", want: domainMatchTypeExact},
{d: "foo.*.com", want: domainMatchTypeInvalid},
}
for _, tt := range tests {
if got := matchTypeForDomain(tt.d); got != tt.want {
t.Errorf("matchTypeForDomain(%q) = %v, want %v", tt.d, got, tt.want)
}
}
}
func (s) TestMatch(t *testing.T) {
tests := []struct {
name string
domain string
host string
wantTyp domainMatchType
wantMatched bool
}{
{name: "invalid-empty", domain: "", host: "", wantTyp: domainMatchTypeInvalid, wantMatched: false},
{name: "invalid", domain: "a.*.b", host: "", wantTyp: domainMatchTypeInvalid, wantMatched: false},
{name: "universal", domain: "*", host: "abc.com", wantTyp: domainMatchTypeUniversal, wantMatched: true},
{name: "prefix-match", domain: "abc.*", host: "abc.123", wantTyp: domainMatchTypePrefix, wantMatched: true},
{name: "prefix-no-match", domain: "abc.*", host: "abcd.123", wantTyp: domainMatchTypePrefix, wantMatched: false},
{name: "suffix-match", domain: "*.123", host: "abc.123", wantTyp: domainMatchTypeSuffix, wantMatched: true},
{name: "suffix-no-match", domain: "*.123", host: "abc.1234", wantTyp: domainMatchTypeSuffix, wantMatched: false},
{name: "exact-match", domain: "foo.bar", host: "foo.bar", wantTyp: domainMatchTypeExact, wantMatched: true},
{name: "exact-no-match", domain: "foo.bar.com", host: "foo.bar", wantTyp: domainMatchTypeExact, wantMatched: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotTyp, gotMatched := match(tt.domain, tt.host); gotTyp != tt.wantTyp || gotMatched != tt.wantMatched {
t.Errorf("match() = %v, %v, want %v, %v", gotTyp, gotMatched, tt.wantTyp, tt.wantMatched)
}
})
}
}
func (s) TestFindBestMatchingVirtualHost(t *testing.T) {
var (
oneExactMatch = virtualHostWithInterceptors{domains: []string{"foo.bar.com"}}
oneSuffixMatch = virtualHostWithInterceptors{domains: []string{"*.bar.com"}}
onePrefixMatch = virtualHostWithInterceptors{domains: []string{"foo.bar.*"}}
oneUniversalMatch = virtualHostWithInterceptors{domains: []string{"*"}}
longExactMatch = virtualHostWithInterceptors{domains: []string{"v2.foo.bar.com"}}
multipleMatch = virtualHostWithInterceptors{domains: []string{"pi.foo.bar.com", "314.*", "*.159"}}
vhs = []virtualHostWithInterceptors{oneExactMatch, oneSuffixMatch, onePrefixMatch, oneUniversalMatch, longExactMatch, multipleMatch}
)
tests := []struct {
name string
host string
vHosts []virtualHostWithInterceptors
want *virtualHostWithInterceptors
}{
{name: "exact-match", host: "foo.bar.com", vHosts: vhs, want: &oneExactMatch},
{name: "suffix-match", host: "123.bar.com", vHosts: vhs, want: &oneSuffixMatch},
{name: "prefix-match", host: "foo.bar.org", vHosts: vhs, want: &onePrefixMatch},
{name: "universal-match", host: "abc.123", vHosts: vhs, want: &oneUniversalMatch},
{name: "long-exact-match", host: "v2.foo.bar.com", vHosts: vhs, want: &longExactMatch},
// Matches suffix "*.bar.com" and exact "pi.foo.bar.com". Takes exact.
{name: "multiple-match-exact", host: "pi.foo.bar.com", vHosts: vhs, want: &multipleMatch},
// Matches suffix "*.159" and prefix "foo.bar.*". Takes suffix.
{name: "multiple-match-suffix", host: "foo.bar.159", vHosts: vhs, want: &multipleMatch},
// Matches suffix "*.bar.com" and prefix "314.*". Takes suffix.
{name: "multiple-match-prefix", host: "314.bar.com", vHosts: vhs, want: &oneSuffixMatch},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := findBestMatchingVirtualHostServer(tt.host, tt.vHosts); !cmp.Equal(got, tt.want, cmp.AllowUnexported(virtualHostWithInterceptors{}, routeWithInterceptors{})) {
t.Errorf("findBestMatchingVirtualHostServer() = %v, want %v", got, tt.want)
}
})
}
}
type testServerTransportStream struct {
method string
}
func (s *testServerTransportStream) Method() string { return s.method }
func (s *testServerTransportStream) SetHeader(metadata.MD) error { return nil }
func (s *testServerTransportStream) SendHeader(metadata.MD) error { return nil }
func (s *testServerTransportStream) SetTrailer(metadata.MD) error { return nil }
func (s) TestRouteAndProcess_MissingAuthority(t *testing.T) {
var ptr atomic.Pointer[usableRouteConfiguration]
ptr.Store(&usableRouteConfiguration{})
cw := &connWrapper{urc: &ptr}
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
ctx = transport.SetConnection(ctx, cw)
ctx = grpc.NewContextWithServerTransportStream(ctx, &testServerTransportStream{method: "/test.Service/Method"})
ctx = metadata.NewIncomingContext(ctx, metadata.MD{})
err := RouteAndProcess(ctx)
if status.Code(err) != codes.Internal {
t.Fatalf("RouteAndProcess() returned error code %v, want %v", status.Code(err), codes.Internal)
}
if !strings.Contains(err.Error(), "no :authority header present") {
t.Fatalf("RouteAndProcess() returned error message %q, want %q", err.Error(), "no :authority header present")
}
}