util/http: add trie matching func

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-09-27 23:30:43 +03:00
parent dd29bf457e
commit d0f2bc8346
2 changed files with 243 additions and 0 deletions

42
util/http/trie_test.go Normal file
View File

@@ -0,0 +1,42 @@
package http
import (
"net/http"
"testing"
)
func TestTrieNoMatchMethod(t *testing.T) {
tr := NewTrie()
tr.Insert([]string{http.MethodPut}, "/v1/create/{id}", nil)
_, _, ok := tr.Search(http.MethodPost, "/v1/create")
if ok {
t.Fatalf("must be not found error")
}
}
type handler struct{}
func TestTrieMatchRegexp(t *testing.T) {
tr := NewTrie()
tr.Insert([]string{http.MethodPut}, "/v1/create/{category}/{id:[0-9]+}", &handler{})
_, params, ok := tr.Search(http.MethodPut, "/v1/create/test_cat/12345")
if !ok {
t.Fatalf("route not found")
} else if len(params) != 2 {
t.Fatalf("param matching error %v", params)
} else if params["category"] != "test_cat" {
t.Fatalf("param matching error %v", params)
}
}
func TestTrieMatchRegexpFail(t *testing.T) {
tr := NewTrie()
tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[a-z]+}", &handler{})
_, _, ok := tr.Search(http.MethodPut, "/v1/create/12345")
if ok {
t.Fatalf("route must not be not found")
}
}