diff --git a/util/http/trie_test.go b/util/http/trie_test.go index 94c805e8..4f301681 100644 --- a/util/http/trie_test.go +++ b/util/http/trie_test.go @@ -15,9 +15,8 @@ func TestTrieNoMatchMethod(t *testing.T) { } } -type handler struct{} - func TestTrieMatchRegexp(t *testing.T) { + type handler struct{} tr := NewTrie() tr.Insert([]string{http.MethodPut}, "/v1/create/{category}/{id:[0-9]+}", &handler{}) @@ -32,6 +31,7 @@ func TestTrieMatchRegexp(t *testing.T) { } func TestTrieMatchRegexpFail(t *testing.T) { + type handler struct{} tr := NewTrie() tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[a-z]+}", &handler{}) @@ -40,3 +40,22 @@ func TestTrieMatchRegexpFail(t *testing.T) { t.Fatalf("route must not be not found") } } + +func TestTrieMatchLongest(t *testing.T) { + type handler struct { + name string + } + tr := NewTrie() + tr.Insert([]string{http.MethodPut}, "/v1/create", &handler{name: "first"}) + tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[0-9]+}", &handler{name: "second"}) + if h, _, ok := tr.Search(http.MethodPut, "/v1/create/12345"); !ok { + t.Fatalf("route must be found") + } else if h.(*handler).name != "second" { + t.Fatalf("invalid handler found: %s != %s", h.(*handler).name, "second") + } + if h, _, ok := tr.Search(http.MethodPut, "/v1/create"); !ok { + t.Fatalf("route must be found") + } else if h.(*handler).name != "first" { + t.Fatalf("invalid handler found: %s != %s", h.(*handler).name, "first") + } +}