util/http: trie add more tests

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-09-28 01:02:28 +03:00
parent d0f2bc8346
commit a1999ff81c

View File

@ -15,9 +15,8 @@ func TestTrieNoMatchMethod(t *testing.T) {
} }
} }
type handler struct{}
func TestTrieMatchRegexp(t *testing.T) { func TestTrieMatchRegexp(t *testing.T) {
type handler struct{}
tr := NewTrie() tr := NewTrie()
tr.Insert([]string{http.MethodPut}, "/v1/create/{category}/{id:[0-9]+}", &handler{}) 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) { func TestTrieMatchRegexpFail(t *testing.T) {
type handler struct{}
tr := NewTrie() tr := NewTrie()
tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[a-z]+}", &handler{}) 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") 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")
}
}