Debian Codegraph @

micro/service_internal_test.go

raw
1 // Copyright 2026 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package micro
15
16 import "testing"
17
18 func TestMatchEndpointSubject(t *testing.T) {
19 for _, test := range []struct {
20 name string
21 endpointSubject string
22 literalSubject string
23 match bool
24 }{
25 {"exact", "foo.bar", "foo.bar", true},
26 {"exact single token", "foo", "foo", true},
27 {"single wildcard", "foo.*", "foo.bar", true},
28 {"single wildcard mismatched literal", "foo.*", "foo.bar.baz", false},
29 {"full wildcard", "foo.>", "foo.bar.baz", true},
30 {"full wildcard one token", "foo.>", "foo.bar", true},
31 {"literal mismatch", "foo.bar", "foo.baz", false},
32 {"endpoint longer than subject", "foo.bar", "foo", false},
33 // A shorter literal endpoint must not match a longer subject.
34 {"prefix over-match single token", "foo", "foo.bar", false},
35 {"prefix over-match multi token", "foo.bar", "foo.bar.baz", false},
36 {"wildcard prefix over-match", "foo.*", "foo.bar.baz", false},
37 } {
38 t.Run(test.name, func(t *testing.T) {
39 if got := matchEndpointSubject(test.endpointSubject, test.literalSubject); got != test.match {
40 t.Errorf("matchEndpointSubject(%q, %q) = %v; want %v",
41 test.endpointSubject, test.literalSubject, got, test.match)
42 }
43 })
44 }
45 }