From a1999ff81c588e2668cdce63ef0df911022442a4 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 28 Sep 2021 01:02:28 +0300 Subject: [PATCH] util/http: trie add more tests Signed-off-by: Vasiliy Tolstov --- util/http/trie_test.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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") + } +}